<template>
  <el-dialog
    :visible.sync="visible"
    v-bind="modalOptions"
  >
    <div>
      <el-descriptions title="" :column="2" border>
        <el-descriptions-item label="手机号码：">
          {{ formData.phone }}
        </el-descriptions-item>
        <el-descriptions-item label="银行名称：">
          {{ formData.bankName }}
        </el-descriptions-item>
        <el-descriptions-item label="银行卡号：">
          {{ formData.bankCard }}
        </el-descriptions-item>
        <el-descriptions-item label="收款人姓名：">
          {{ formData.name }}
        </el-descriptions-item>
        <el-descriptions-item label="提现金额：">
          {{ formData.withdrawalMoney }}
        </el-descriptions-item>
        <el-descriptions-item label="实际到账：">
          {{ formData.actualReceipt }}
        </el-descriptions-item>
        <el-descriptions-item label="申请时间：">
          {{ formData.applyTime }}
        </el-descriptions-item>
        <el-descriptions-item label="处理时间：">
          {{ formData.handleTime }}
        </el-descriptions-item>
      </el-descriptions>
    </div>
    <el-input v-show="formData.state == 2" v-model="formData.rejectReason" type="textarea" autosize placeholder="请输入拒绝打款理由">
    </el-input>

    <template #footer>
      <span>
        <el-button @click="visible = false">取 消</el-button>
        <el-button v-if="formData.state === 3" type="danger" @click="handleRefuse">确认拒绝打款</el-button>
        <el-button v-else type="danger" @click="handleRefuse">拒绝打款</el-button>
        <el-button v-if="formData.state === 0" type="primary" @click="handleExamine">确认用户信息</el-button>
        <el-button v-else-if="formData.state === 1" type="primary" @click="handleConfirm">确认打款</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { applicationGetById, applicationHandle } from '@/api/application'

export default {
  name: 'WithdrawalProcessing',
  components: {
  },
  data() {
    return {
      modalOptions: {
        closeOnClickModal: false,
        width: '820px',
        title: '提现处理'
      },
      visible: false,
      formData: {
        actualReceipt: '',
        applyTime: '',
        bankCard: '',
        bankName: '',
        batchId: '',
        buyerUserId: '',
        cost: '',
        handleTime: '',
        isConfirm: '',
        name: '',
        orderSn: '',
        phone: '',
        rejectReason: '',
        state: '',
        withdrawalId: '',
        withdrawalMoney: ''
      }
    }
  },
  methods: {
    handleClose() {
      this.visible = false
    },
    async initList() {
    },
    handleOpen(params = {}) {
      this.modalOptions.title = '提现处理'
      this.formData = Object.assign(this.$options.data().formData, params)
      this.visible = true
      this.initList()
      if (params.withdrawalId) {
        this.getInfo(params.withdrawalId)
      } else {
        this.$refs.formData && this.$refs.formData.resetFields()
      }
    },
    async getInfo(id) {
      const loading = this.$loading({ text: '加载中' })
      try {
        const res = await applicationGetById({ withdrawalId: id })
        this.formData = Object.assign(this.$options.data().formData, res.data, {
          withdrawalId: res.data.withdrawalId || '',
          actualReceipt: res.data.actualReceipt || '',
          applyTime: res.data.applyTime || '',
          bankCard: res.data.bankCard || '',
          bankName: res.data.bankName || '',
          batchId: res.data.batchId || '',
          buyerUserId: res.data.buyerUserId || '',
          cost: res.data.cost,
          handleTime: res.data.handleTime || '',
          isConfirm: res.data.isConfirm,
          name: res.data.name || '',
          orderSn: res.data.orderSn || '',
          phone: res.data.phone || '',
          rejectReason: res.data.rejectReason || '',
          state: res.data.state,
          withdrawalMoney: res.data.withdrawalMoney
        })
        this.$nextTick(() => {
          this.$refs.formData && this.$refs.formData.validate()
        })
      } finally {
        loading.close()
      }
    },
    //  拒绝打款
    async handleRefuse() {
      if (this.formData.state == 2) {
        if (!this.formData.rejectReason) return this.$message.error('拒绝理由不能为空')
        const res = await applicationHandle({
          withdrawalId: this.formData.withdrawalId,
          state: '2',
          rejectReason: this.formData.rejectReason
        })
        if (res.code == '') {
          this.$message.success('成功拒绝打款！！！')
        }
        this.visible = false
        this.$emit('success')
      } else {
        this.formData.state = 2
        this.$message.warning('请填写理由并确定拒绝打款！！！')
      }
    },
    // 确认审核
    handleExamine() {
      this.$confirm('确定审核通过吗？')
        .then(async () => {
          await applicationHandle({
            withdrawalId: this.formData.withdrawalId,
            state: '1'
          })
          this.$message.success('操作成功！请再次确认！')
          this.getInfo(this.formData.withdrawalId)
          this.$emit('success')
        })
        .catch(() => {})
    },
    // 确认打款
    handleConfirm() {
      this.$confirm('确定打款吗？')
        .then(async () => {
          await applicationHandle({
            withdrawalId: this.formData.withdrawalId,
            withdrawalType: 1, // 1佣金
            state: '3'
          })
          this.$message.success('打款成功！！！')
          this.visible = false
          this.$emit('success')
        })
        .catch(() => {})
    }
  }
}
</script>

