<template>
  <div class="p-2">
    <div>
      <el-card shadow="hover" class="mb-[10px]">
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px" size="default" class="ry_form">
          <el-form-item label="提现单号" prop="code">
            <el-input v-model.trim="queryParams.code" placeholder="请输入会员id" clearable />
          </el-form-item>
          <el-form-item label="昵称" prop="nickname">
            <el-input v-model.trim="queryParams.nickname" placeholder="请输入昵称" clearable />
          </el-form-item>
          <el-form-item label="提现方式" prop="method">
            <el-select v-model="queryParams.method">
              <el-option v-for="item in userWithdrawMethodMap" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="提现类型" prop="type">
            <el-select v-model="queryParams.type">
              <el-option v-for="item in userWithdrawTypeMap" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="审核状态" prop="auditStatus">
            <el-select v-model="queryParams.auditStatus">
              <el-option v-for="item in userWithdrawAuditMap" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="提现状态" prop="status">
            <el-select v-model="queryParams.status">
              <el-option v-for="item in userWithdrawStatusMap" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="提现人手机号" prop="phone">
            <el-input v-model.trim="queryParams.phone" placeholder="请输入手机号码" clearable />
          </el-form-item>
          <el-form-item class="flex_one tr">
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            <!--        <el-button :icon="showMoreCondition ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"  @click="showMoreCondition = !showMoreCondition">{{showMoreCondition ? '收起条件' : '展开条件'}}</el-button>-->
          </el-form-item>
        </el-form>
      </el-card>
      <el-card shadow="hover">
        <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" width="120" align="center">
            <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" align="center" width="100">
            <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="getList"
        />
      </el-card>
    </div>
    <!-- 提现审核-->
    <el-dialog title="提现审核" v-model="auditFormOpen" width="500px" append-to-body>
      <el-row>
        <el-col
          :span="4"
          :style="{
            display: 'flex',
            justifyContent: 'center',
            alignItems: 'center'
          }"
          >审核结果</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/ums/member';

export default {
  name: 'UmsWithdrawManage',
  data() {
    return {
      // 用户提现记录
      userWithdrawInfo: {
        total: 0,
        data: [],
        record: {}
      },
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        nickname: null,
        phone: null,
        code: '',
        method: undefined,
        type: undefined,
        auditStatus: undefined,
        status: undefined
      },
      userWithdrawInfoModalVisible: false,
      userWithdrawInfoTableLoading: false,
      withdrawDataParams: {
        current: 1,
        size: 10
      },
      auditStatus: null,
      auditReason: null,
      auditFormOpen: false,
      currentRecord: {},
      userWithdrawMethodMap: [],
      userWithdrawTypeMap: [],
      userWithdrawStatusMap: [],
      userWithdrawAuditMap: []
    };
  },
  created() {
    this.getList();
    this.getWithdrawEmu();
  },
  methods: {
    // 表单重置
    reset() {
      this.form = {
        id: null,
        nickName: null,
        phone: null,
        code: '',
        method: undefined,
        type: undefined,
        auditStatus: undefined,
        status: undefined
      };
      this.resetForm('form');
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm');
      this.handleQuery();
    },

    showAuditModal(record) {
      this.currentRecord = record;
      this.auditFormOpen = true;
    },
    resetAuditForm() {
      this.currentRecord = {};
      this.auditFormOpen = false;
      this.auditStatus = null;
      this.auditReason = null;
    },
    // 提交审核
    confirmSubmitAudit() {
      const { id, member = {} } = this.currentRecord;
      const { id: memberId } = member;
      if (!this.auditStatus) {
        return this.$modal.msgWarning(`请选择审核结果!`);
      }
      const params = {
        memberId: memberId,
        id: id,
        auditStatus: this.auditStatus,
        auditReason: this.auditReason
      };
      setWithdrawStatus(params)
        .then(() => {
          this.$modal.msgSuccess('审核成功');
          this.getList();
          this.resetAuditForm();
        })
        .catch(() => {
          this.$modal.msgError('审核失败');
        });
    },

    /** 查询会员信息列表 */
    getList() {
      this.userWithdrawInfoTableLoading = true;
      const { current: pageNum, size: pageSize } = this.queryParams;
      const query = { ...this.queryParams, pageNum: undefined, pageSize: undefined };
      const pageReq = { current: pageNum - 1, size: pageSize };
      getUserWithdrawList(query, pageReq)
        .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;
    }
  }
};
</script>
