<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="用户编码" prop="loginName">
        <el-input
          v-model="queryParams.loginName"
          placeholder="请输入用户编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="integralList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="用户编码" align="center" prop="loginName" width="200px"/>
      <el-table-column label="用户名称" align="center" prop="nickName" width="200px"/>
      <el-table-column label="手机号" align="center" prop="phonenumber" width="200px"/>
      <el-table-column label="当前余额" align="center" prop="rechargeIntegral"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改积分管理对话框 -->

  </div>
</template>

<script>
import { doReturnPoints, integralGroupList, importGroupList } from '@/api/integral/integral'
import user from '@/store/modules/user'

export default {
  name: 'Refund',
  data() {
    return {
      exportLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 积分管理表格数据
      integralList: [],
      submitArray: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        loginName: null,
        nickName: null
      },
      // 表单参数
      form: {
        nickName: ''
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询积分管理列表 */
    getList() {
      this.queryParams.organizationCode = user.state.organ
      this.loading = true
      integralGroupList(this.queryParams).then(response => {
        this.integralList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        rechargeEquipmentCode: null,
        rechargeEquipmentName: null,
        loginName: null,
        nickName: null,
        cardNumber: null,
        cardName: null,
        rechargeTime: null,
        rechargeIntegral: null,
        isDelete: null,
        organizationCode: null,
        rechargeId: null,
        updateTime: null
      }
      this.resetForm('form')
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$modal.confirm('是否确认导出所有用户余额数据？').then(() => {
        this.exportLoading = true
        return importGroupList(queryParams)
      }).then(response => {
        this.$download.name(response.msg)
        this.exportLoading = false
      }).catch(() => {
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.submitArray = selection
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    handleReturnPoints(row) {
      const submitArray = this.submitArray
      const loginName = this.uniqueLoginName(this.submitArray)
      this.$modal.confirm('是否确认退还积分管理用户编码为"' + loginName + '"的数据项？').then(function() {
        return doReturnPoints(submitArray)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('退还成功')
      }).catch(() => {

      })
    },

    typeFormat(row) {
      if (row.moneyType === 1) {
        return '押金'
      } else if (row.moneyType !== 1) {
        return '余额'
      } else {
        return ''
      }
    },

    uniqueLoginName(arr) {
      const res = new Map()
      return arr.filter(item => !res.has(arr.loginName) && res.set(arr.loginName, 1)).map(e => e.loginName)
    },

    orgFormatter(val) {
      let name = ''
      user.state.organDicts.forEach(item => {
        if (item.deptCode == val) {
          name = item.deptName
        }
      })
      return name
    }
  }
}
</script>
