<template>
  <div class="custom_page">
    <div class="content">
      <!-- 顶部搜索 -->
      <div class="toolbar">
        <!-- 顶部搜索 -->
        <el-form :inline="true" :model="formInline">
          <el-form-item label="用户名称">
            <el-input v-model="formInline.name" maxlength="20" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="手机号">
            <el-input v-model="formInline.phone" maxlength="11" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="提现状态">
            <el-select v-model="formInline.state" placeholder="请选择">
              <el-option label="全部" :value="null" />
              <el-option label="待审核" value="0" />
              <el-option label="打款中" value="1" />
              <el-option label="拒绝" value="2" />
              <el-option label="打款成功" value="3" />
              <el-option label="打款失败" value="4" />
            </el-select>
          </el-form-item>
          <el-form-item label-width="0">
            <el-button type="primary" plain @click="search">查询</el-button>
            <el-button plain @click="clear">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <!--  表格 -->
      <div class="content_table">
        <div class="table">
          <el-table :data="tableData" border :header-cell-style="{ background: '#EEF3FF', color: '#333333' }"
            style="width: 100%">
            >
            <el-table-column prop="orderSn" label="流水号"></el-table-column>
            <el-table-column prop="name" label="用户名称" />
            <el-table-column prop="phone" label="手机号码" />
            <el-table-column prop="withdrawalMoney" label="提现金额" />
            <el-table-column prop="cost" label="手续费"></el-table-column>
            <el-table-column label="处理状态">
              <template slot-scope="scope">
                <span v-if="scope.row.state == 0">审核中</span>
                <span v-if="scope.row.state == 1">打款中</span>
                <span v-if="scope.row.state == 2">拒绝</span>
                <span v-if="scope.row.state == 3">打款成功</span>
                <span v-if="scope.row.state == 4">打款失败</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" show-overflow-tooltip>
              <template slot-scope="scope">
                <div class="btnList">
                  <el-button v-if="scope.row.state !== 0" type="text" @click="seeMore(scope.row)">查看</el-button>
                  <el-button v-else type="text" @click="del(scope.row)">处理</el-button>
                </div>
              </template>
            </el-table-column>
          </el-table>
          <div class="fenye">
            <el-pagination :current-page="currentPage" :page-sizes="[10, 20, 50, 100]" :page-size="10"
              layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
              @current-change="handleCurrentChange" />
          </div>
        </div>
      </div>
    </div>
    <!-- *****************弹框开始***************** -->
    <!-- 详情弹框 -->
    <el-dialog :visible.sync="dioObj.show" :title="dioObj.title" width="40%" center :close-on-click-modal="false" @close="closeDialog">
      <div class="box">
        <div class="dioBox">
          <div v-for="(item, index) in infoList" :key="index" class="inner">{{ item.name }}:{{ item.value }}</div>
        </div>
        <div v-if="dioObj.type === 2" class="botTitle">*请确认您已转账成功，再点击确认</div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button v-if="dioObj.type === 2" type="primary" @click="agreeEn(1)">{{  confirmInfo }}</el-button>
        <el-button v-if="dioObj.type === 2" type="danger" @click="agreeEn(2)">拒绝打款</el-button>
        <el-button v-if="dioObj.type === 1" @click="closeDialog">关 闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// import { getShopId } from '@/utils/auth'
import { hidden } from '@/utils/index'
import {
  applicationGetAll,
  applicationGetById,
  applicationHandle
} from '@/api/application'
export default {
  data() {
    return {
      formInline: {
        name: '', // 用户名称
        phone: '', // 手机号码
        state: null, // 提现状态 空-全部 0-待审核 1-通过 2-拒绝
        page: 1,
        pageSize: 10
      },
      total: 1,
      tableData: [],
      infoList: [
        { name: '手机号码', value: '', fields: 'phone' },
        { name: '银行名称', value: '', fields: 'bankName' },
        { name: '银行卡号', value: '', fields: 'bankCard' },
        { name: '收款人姓名', value: '', fields: 'name' },
        { name: '提现金额', value: '', fields: 'withdrawalMoney' },
        { name: "实际到账", value: '', fields: "actualReceipt" },
        { name: '申请时间', value: '', fields: 'applyTime' },
        { name: '处理时间', value: '', fields: 'handleTime' }
      ],
      currentPage: 1,
      multipleSelection: [],
      dioObj: {},
      //  拒绝打款理由
      rejectReason: "",
      //  控制确认信息和确认打款
      confirmInfo:"确认信息"
    }
  },
  created() {
    this.getAll(this.formInline)
  },
  methods: {
    handleSizeChange(val) {
      this.formInline.pageSize = val
      this.getAll(this.formInline)
    },
    handleCurrentChange(val) {
      this.formInline.page = val
      this.getAll(this.formInline)
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 查询
    search() {
      this.total = 1
      this.formInline.page = 1
      this.getAll(this.formInline)
    },
    // 清除
    clear() {
      this.formInline = {
        name: '', // 用户名称
        phone: '', // 手机号码
        state: null, // 提现状态 空-全部 0-待审核 1-通过 2-拒绝
        page: 1,
        pageSize: 10
      }
      this.getAll(this.formInline)
    },
    getTopList(o) {
      this.infoList.map((item) => {
        item.value = o[item.fields]
      })
    },
    // 查看
    seeMore(row) {
      this.dioObj = {
        title: '查看',
        show: true,
        arr: row,
        type: 1
      }
      this.getDetails(row.withdrawalId)
    },
    // 处理
    del(row) {
      this.dioObj = {
        title: '处理',
        show: true,
        arr: row,
        type: 2
      }
      this.getDetails(row.withdrawalId)
    },
    // 确认
    async agreeEn(index) {
      if (index === 1 && this.confirmInfo == "确认信息") {
        const res = await applicationHandle({
          withdrawalId: this.dioObj.arr.withdrawalId,
          state: 1
        });
        this.$message.success('确认信息成功，请确认打款')
        this.confirmInfo = "确认打款";
      }else if(index === 1 && this.confirmInfo == "确认打款"){
        const res = await applicationHandle({
          withdrawalId: this.dioObj.arr.withdrawalId,
          state: 3
        });
        if (res.code === '') {
          this.$message.success('确认打款成功')
          this.dioObj.show = false
          this.getAll(this.formInline)
        }
      } else if (index === 2) {
        if (this.rejectReason == "") {
          this.$prompt('请输入拒绝打款理由', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            inputPattern: /^\s*[\S]+\s*$/,
            inputErrorMessage: '输入的拒绝打款理由不能为空！！！'
          }).then(({ value }) => {
            this.rejectReason = value
            // console.log(value);
            this.infoList.push({ name: "拒绝打款理由", value: value, fields: 'rejectReason' })
          }).catch(() => {
            this.rejectReason = ""
          });
          return false
        }
        const res = await applicationHandle({
          withdrawalId: this.dioObj.arr.withdrawalId,
          state: 2,
          rejectReason: this.rejectReason
        })
        if (res.code === '') {
          this.$message.success('成功拒绝打款')
          this.dioObj.show = false
          this.getAll(this.formInline)
        }
      }
    },
    // 查询详情
    async getDetails(withdrawalId) {
      const res = await applicationGetById({ withdrawalId })
      if (res.code === '') {
        this.details = res.data
        this.getTopList(res.data)
      }
      //  判断是不是有拒绝打款理由
      if (res.data.rejectReason && res.data.state == 2) {
        this.infoList.push({ name: "拒绝打款理由", value: res.data.rejectReason, fields: 'rejectReason' })
      }
    },
    // 初始化查询所有数据
    async getAll(formInline) {
      const res = await applicationGetAll(formInline)
      this.tableData = res.data.list
      this.tableData.forEach((item) => {
        item.phone = hidden(item.phone, 3, 4)
      })
      this.total = res.data.total
    },
    //  关闭弹窗的回调
    closeDialog() {
      this.dioObj.show = false
      this.rejectReason = ""
      this.infoList = this.infoList.filter(item => item.fields != 'rejectReason');
    }
  }
}
</script>

<style lang='scss' scoped>
@import url("../../../styles/elDialog.scss");

.custom_page {
  padding: 20px;
}

.box {
  .dioBox {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    flex-wrap: wrap;

    .inner {
      width: 50%;
      padding: 20px;
    }
  }

  .botTitle {
    color: red;
    text-align: center;
  }
}
</style>

