<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="手机" prop="phoneNumber">
        <el-input
            v-model="queryParams.phoneNumber"
            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"
            v-hasPermi="['dk:dkCustomer:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dkCustomerList" @selection-change="handleSelectionChange">
      <el-table-column label="手机" align="center" prop="phoneNumber"/>
      <el-table-column label="用户名称" align="center" prop="nickName"/>
      <el-table-column label="真实名称" align="center" prop="realName"/>
      <el-table-column label="是否实名" align="center" prop="realNameAuth">
        <template v-slot="scope">
          <zl-dict-tag :options="yesOrNoList" :value="scope.row.realNameAuth"/>
        </template>
      </el-table-column>
      <el-table-column label="是否贷款" align="center" prop="loansFlag">
        <template v-slot="scope">
          <zl-dict-tag :options="yesOrNoList" :value="scope.row.loansFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="是否提现" align="center" prop="withdrawFlag">
        <template v-slot="scope">
          <zl-dict-tag :options="yesOrNoList" :value="scope.row.withdrawFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="余额" align="center" prop="account" />
      <el-table-column label="贷款金额" align="center" prop="borrowAccount" />
      <el-table-column label="允许提现" align="center" prop="allowWithdrawFlag">
        <template v-slot="scope">
          <el-switch
              v-model="scope.row.allowWithdrawFlag"
              :active-value="true"
              :inactive-value="false"
              @change="handleAllowWithdrawFlagChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="允许输入签名" align="center" prop="allowSignature">
        <template v-slot="scope">
          <el-switch
              v-model="scope.row.allowSignature"
              :active-value="true"
              :inactive-value="false"
              @change="handleAllowSignatureChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template v-slot="scope">
          <el-switch
              v-model="scope.row.status"
              :active-value="0"
              :inactive-value="1"
              @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="最后登陆IP" align="center" prop="lastLoginIp"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdateAccount(scope.row)"
          >修改余额
          </el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-document"
              @click="handleInfo(scope.row)"
          >详情
          </el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-key"
              @click="handleUpdatePwd(scope.row)"
          >重置密码
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />
    <info-dialog v-if="infoVisible" ref="info"/>
    <update-amount-dialog v-if="updateAmountVisible" ref="updateAmount" @refreshDataList="getList" />
  </div>
</template>

<script>
import {
  listDkCustomer,
  updateDkCustomer,
  exportDkCustomer, resetCustomerPwd
} from '@/api/dk/dkCustomer'
import { yesOrNoList, stopStatusList } from '@/constant/module/statusMap'
import InfoDialog from './info'
import UpdateAmountDialog from './update-amount'
import commonUtil from '@/mixins/common-util'
import { updateAllowSignature } from '@/api/dk/dkCustomerInfo'

export default {
  name: 'DkCustomer',
  mixins: [commonUtil],
  components: {
    InfoDialog,UpdateAmountDialog
  },
  data() {
    return {
      yesOrNoList, stopStatusList,
      infoVisible: false,
      updateAmountVisible: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户表格数据
      dkCustomerList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        phoneNumber: undefined,
        nickName: undefined
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleInfo(row) {
      this.infoVisible = true
      this.$nextTick(() => {
        this.$refs.info.init(row.id)
      })
    },
    handleUpdateAccount(row) {
      this.updateAmountVisible = true
      this.$nextTick(() => {
        this.$refs.updateAmount.init(row.id)
      })
    },
    handleUpdatePwd(row) {
      this.$prompt('请输入"' + row.nickName + '"的新密码', "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        inputPattern: /^.{5,20}$/,
        inputErrorMessage: "用户密码长度必须介于 5 和 20 之间",
      }).then(({ value }) => {
        resetCustomerPwd(row.id, value).then(() => {
          this.msgSuccess("修改成功，新密码是：" + value);
        });
      }).catch(() => {});
    },
    handleStatusChange(row) {
      let text = row.status === 0 ? '恢复' : '封禁'
      this.$confirm('确认要' + text + '[' + row.nickName + ']客户吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return updateDkCustomer({ id: row.id, status: row.status })
      }).then(() => {
        this.msgSuccess(text + '成功')
      }).catch(function() {
        row.status = row.status === 0 ? 1 : 0
      })
    },
    handleAllowWithdrawFlagChange(row){
      let text = row.allowWithdrawFlag === false ? '封禁' : '恢复'
      this.$confirm('确认要' + text + '[' + row.nickName + ']客户的提现功能吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return updateDkCustomer({ id: row.id, allowWithdrawFlag: row.allowWithdrawFlag })
      }).then(() => {
        this.msgSuccess(text + '成功')
      }).catch(function() {
        row.allowWithdrawFlag = row.allowWithdrawFlag !== false
      })
    },
    handleAllowSignatureChange(row){
      let text = row.allowSignature === false ? '封禁' : '恢复'
      this.$confirm('确认要' + text + '[' + row.nickName + ']客户的签名功能吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return updateAllowSignature({ customerId: row.id, allowSignature: row.allowSignature })
      }).then(() => {
        this.msgSuccess(text + '成功')
      }).catch(function() {
        row.allowSignature = row.allowSignature !== false
      })
    },
    /** 查询客户列表 */
    getList() {
      this.loading = true
      listDkCustomer(this.queryParams).then(response => {
        this.dkCustomerList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有客户数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.exportLoading = true
        return exportDkCustomer(queryParams)
      }).then(response => {
        this.download(response.msg)
        this.exportLoading = false
      }).catch(() => {
      })
    }
  }
}
</script>
