<template>
  <div>
    <el-card>
      <el-table v-loading="userWithdrawInfoTableLoading" :data="userWithdrawInfo.data" border empty-text="暂无数据">
        <el-table-column label="申请人" prop="createBy" align="center">
          <template v-slot="scope">
            {{ scope.row.member ? scope.row.member.nickname : '' }}
          </template>
        </el-table-column>
        <el-table-column label="申请时间" prop="createTime" align="center" />
        <el-table-column label="提现码" prop="code"  align="center"/>
        <el-table-column label="提现金额" prop="money" align="center">
          <template v-slot="scope"> ￥ {{ scope.row.money || '0.00' }} </template>
        </el-table-column>
        <el-table-column label="手续费" prop="fee" align="center">
          <template v-slot="scope"> ￥ {{ scope.row.fee || '0.00' }} </template>
        </el-table-column>

        <el-table-column label="实际到账金额" prop="actualMoney" align="center" width="120">
          <template v-slot="scope"> ￥ {{ scope.row.actualMoney || '0.00' }} </template>
        </el-table-column>

        <el-table-column label="提现方式" prop="method" align="center">
          <template v-slot="scope">
            {{ getEmulistLabelById(scope.row.method, userWithdrawMethodMap) }}
          </template>
        </el-table-column>
        <el-table-column label="提现类型" prop="type" align="center">
          <template v-slot="scope">
            {{ getEmulistLabelById(scope.row.type, userWithdrawTypeMap) }}
          </template>
        </el-table-column>
        <el-table-column label="提现状态" prop="status" align="center">
          <template v-slot="scope">
            {{ getEmulistLabelById(scope.row.status, userWithdrawStatusMap) }}
          </template>
        </el-table-column>
        <el-table-column label="审批状态" prop="auditStatus" align="center">
          <template v-slot="scope">
            {{ getEmulistLabelById(scope.row.auditStatus, userWithdrawAuditMap) }}
            <el-tooltip v-if="scope.row.auditStatus == '2'" :content="scope.row.auditStatus == '2' ? scope.row.auditReason : null" placement="top">
              <el-icon>
                <question-filled />
              </el-icon>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column label="审核人" prop="auditBy" width="100" align="center" />
        <el-table-column label="审核时间" prop="auditTime" width="100" align="center" />
        <el-table-column label="操作" fixed="right" width="80" align="center">
          <template v-slot="scope">
            <el-button v-if="scope.row.auditStatus == '0'" text type="primary" @click="showAuditModal(scope.row)">审批</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-if="userWithdrawInfo.total"
        :total="userWithdrawInfo.total"
        v-model:page="withdrawDataParams.current"
        v-model:limit="withdrawDataParams.size"
        @pagination="
          () => {
            showWithdrawInfoList(userWithdrawInfo.record);
          }
        "
      />
    </el-card>
    <!-- 提现审核-->
    <el-dialog title="提现审核" v-model="auditFormOpen" width="500px" append-to-body>
      <el-row>
        <el-col :span="4">审核结果</el-col>
        <el-col :span="20">
          <el-select v-model="auditStatus">
            <el-option v-show="item.value != '0'" v-for="item in userWithdrawAuditMap" :value="item.value" :label="item.label" />
          </el-select>
        </el-col>
      </el-row>
      <el-row v-if="auditStatus == '2'" class="mt2">
        <el-col :span="4">拒绝原因</el-col>
        <el-col :span="20">
          <el-input v-model="auditReason" type="textarea" :rows="3"></el-input>
        </el-col>
      </el-row>
      <el-row class="mt6">
        <el-col :span="12"></el-col>
        <el-col :span="12">
          <el-button type="default" @click="resetAuditForm">取消</el-button>
          <el-button type="primary" @click="confirmSubmitAudit">确认</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getUserWithdrawList, setWithdrawStatus } from '@/api/trade/index';

export default {
  name: 'UmsMember',
  data() {
    return {
      show: false,
      // 用户提现记录
      userWithdrawInfo: {
        total: 0,
        data: [],
        record: {}
      },
      userWithdrawInfoModalVisible: false,
      userWithdrawInfoTableLoading: false,
      withdrawDataParams: {
        current: 1,
        size: 10
      },
      auditStatus: null,
      auditReason: null,
      auditFormOpen: false,
      currentRecord: {},
      userWithdrawMethodMap: [],
      userWithdrawTypeMap: [],
      userWithdrawStatusMap: [],
      userWithdrawAuditMap: []
    };
  },
  created() {
    this.pageInit();
    this.getWithdrawEmu();
  },
  methods: {
    pageInit() {
      const { id } = this.$route.query;
      this.showWithdrawInfoList(id);
    },

    showAuditModal(record) {
      this.currentRecord = record;
      this.auditFormOpen = true;
    },
    resetAuditForm() {
      this.currentRecord = {};
      this.auditFormOpen = false;
      this.auditStatus = null;
      this.auditReason = null;
    },
    // 提交审核
    confirmSubmitAudit() {
      const { id } = this.$route.query;
      if (!this.auditStatus) {
        return this.$modal.msgWarning(`请选择审核结果!`);
      }
      const params = {
        memberId: id,
        id: this.currentRecord.id,
        auditStatus: this.auditStatus,
        auditReason: this.auditReason
      };
      setWithdrawStatus(params)
        .then(() => {
          this.$modal.msgSuccess('审核成功');
          this.pageInit();
          this.resetAuditForm();
        })
        .catch(() => {
          this.$modal.msgError('审核失败');
        });
    },
    // 展示提现记录
    showWithdrawInfoList(id) {
      const reqParams = {
        ...this.withdrawDataParams,
        memberId: id
      };
      this.userWithdrawInfoTableLoading = true;
      getUserWithdrawList(reqParams)
        .then((response) => {
          const { records = [], total = 0 } = response.data || {};
          this.userWithdrawInfo = {
            total: total,
            data: records || []
          };
          this.userWithdrawInfoTableLoading = false;
        })
        .catch(() => {
          this.userWithdrawInfoTableLoading = false;
        });
    },
    async getWithdrawEmu() {
      const userWithdrawMethodMap = await this.getDictionaryByKey('ums_withdraw_method');
      const userWithdrawTypeMap = await this.getDictionaryByKey('ums_withdraw_type');
      const userWithdrawStatusMap = await this.getDictionaryByKey('ums_withdraw_status');
      const userWithdrawAuditMap = await this.getDictionaryByKey('sys_common_audit');

      this.userWithdrawMethodMap = userWithdrawMethodMap;
      this.userWithdrawTypeMap = userWithdrawTypeMap;
      this.userWithdrawStatusMap = userWithdrawStatusMap;
      this.userWithdrawAuditMap = userWithdrawAuditMap;
    }

    /*
    showStatistics(memberId) {
      this.$router.push({
        path: '/videoManage/detail',
        query: {
          memberId: memberId
        }
      });
    }*/
  }
};
</script>
