<template>
  <el-dialog
    :visible.sync="visible"
    v-bind="modalOptions"
    @close="cardData = {}"
  >
    <div>
      <el-descriptions title="" :column="2" border>
        <el-descriptions-item label="店铺名称：">
          {{ formData.shopName }}
        </el-descriptions-item>
        <el-descriptions-item label="店铺编码：">
          {{ formData.shopCode }}
        </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.collectionName }}
        </el-descriptions-item>
        <el-descriptions-item label="提现金额：">
          {{ formData.withdrawalMoney }}
        </el-descriptions-item>
        <el-descriptions-item label="处理编号：">
          {{ formData.handleSn }}
        </el-descriptions-item>
        <el-descriptions-item label="申请时间：">
          {{ formData.applyTime }}
        </el-descriptions-item>
        <el-descriptions-item v-if="formData.handleTime" label="处理时间：">
          {{ formData.handleTime }}
        </el-descriptions-item>
        <el-descriptions-item label="审核状态">
          <div>
            <span v-if="formData.state === 0">待处理</span>
            <span v-else-if="formData.state === 1">已处理</span>
            <span v-else-if="formData.state === 2">通过</span>
            <span v-else-if="formData.state === 3">拒绝</span>
            <span v-else-if="formData.state === 4">待确认</span>
            <span v-else>--</span>
          </div>
        </el-descriptions-item>
        <el-descriptions-item label="备注：">
          {{ formData.cause || '--' }}
        </el-descriptions-item>
        <el-descriptions-item label="惠市宝订单的分账状态：">
          <div>
            <span v-if="formData.summaryHsbSplitState === -1">无需分账</span>
            <span v-else-if="formData.summaryHsbSplitState === 0">未分账</span>
            <span v-else-if="formData.summaryHsbSplitState === 1">部分分账</span>
            <span v-else-if="formData.summaryHsbSplitState === 2">已分账</span>
            <span v-else>--</span>
          </div>
        </el-descriptions-item>
        <el-descriptions-item label="惠市宝已分账金额：">
          {{ formData.summaryHsbSplitedAmount }}
        </el-descriptions-item>
        <el-descriptions-item label="惠市宝剩余分账金额：">
          {{ formData.summaryHsbSplitRemainAmount }}
        </el-descriptions-item>
        <el-descriptions-item label="非惠市宝订单的相关提现金额：">
          {{ formData.summaryNotHsbAmount }}
        </el-descriptions-item>
        <el-descriptions-item label="已认证身份姓名">
          {{ cardData.name || "未认证" }}
        </el-descriptions-item>
        <el-descriptions-item label="已认证身份证">
          {{ cardData.idCard || "未认证" }}
        </el-descriptions-item>
        <el-descriptions-item label="已认证电话号码">
          {{ cardData.phone || "未认证"}}
        </el-descriptions-item>
      </el-descriptions>
    </div>
    <div style="color: red;font-size: 16px;text-align: center; margin-top:20px;">* 请确认您已转账成功，再点击确认。说明：分账金额代表已打款，T+1到账</div>
    <el-input v-show="formData.state == 3" v-model="formData.cause" type="textarea" autosize placeholder="请输入拒绝打款理由">
    </el-input>
    <div v-show="formData.state == 2" style="margin-top: 30px;">
      <el-radio v-model="formData.type" label="1" border>通联</el-radio>
      <el-radio v-model="formData.type" label="2" border>线下</el-radio>
    </div>

    <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 === 2" type="primary" @click="handleConfirm">确认打款</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { withdrawalGetById, withdrawalHandle,getWithdrawalAccount } from '@/api/withdrawal'

export default {
  name: 'WithdrawalProcessing',
  components: {
  },
  data() {
    return {
      modalOptions: {
        closeOnClickModal: false,
        width: '820px',
        title: '提现处理'
      },
      visible: false,
      formData: {
        withdrawalId: '',
        shopName: '',
        shopCode: '',
        bankName: '',
        bankCard: '',
        collectionName: '',
        withdrawalMoney: '',
        handleSn: '',
        applyTime: '',
        handleTime: '',
        state: '',
        type: '', // 支付方式选择
        cause: '', // 拒绝理由
        summaryHsbSplitState: '',
        summaryHsbSplitedAmount: '',
        summaryHsbSplitRemainAmount: '',
        summaryNotHsbAmount: ''
      },
      formRules: {
      },
      //  身份证信息
      cardData:{}
    }
  },
  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)
        this.getWithdrawalInfo(params.shopId)
      } else {
        this.$refs.formData && this.$refs.formData.resetFields()
      }
    },
    async getInfo(id) {
      const loading = this.$loading({ text: '加载中' })
      try {
        const res = await withdrawalGetById({ withdrawalId: id })
        this.formData = Object.assign(this.$options.data().formData, res.data, {
          withdrawalId: res.data.withdrawalId || '',
          shopName: res.data.shopName || '',
          shopCode: res.data.shopCode || '',
          bankName: res.data.bankName || '',
          bankCard: res.data.bankCard || '',
          collectionName: res.data.collectionName || '',
          withdrawalMoney: res.data.withdrawalMoney || 0,
          handleSn: res.data.handleSn || '',
          applyTime: res.data.applyTime || '',
          handleTime: res.data.handleTime || '',
          state: res.data.state || 0,
          type: res.data.type || '',
          cause: res.data.cause || '',
          summaryHsbSplitState: res.data.summaryHsbSplitState,
          summaryHsbSplitedAmount: res.data.summaryHsbSplitedAmount,
          summaryHsbSplitRemainAmount: res.data.summaryHsbSplitRemainAmount,
          summaryNotHsbAmount: res.data.summaryNotHsbAmount
        })
        this.$nextTick(() => {
          this.$refs.formData && this.$refs.formData.validate()
        })
      } finally {
        loading.close()
      }
    },
        // 单独用来请求提现的身份证以及手机号姓名等
        async getWithdrawalInfo(id){
      let {data} = await getWithdrawalAccount({ shopIds: id })
      this.cardData = data[0]
    },
    //  拒绝打款
    async handleRefuse() {
      if (this.formData.state == 3) {
        if (!this.formData.cause) return this.$message.error('拒绝理由不能为空')
        const res = await withdrawalHandle({
          withdrawalId: this.formData.withdrawalId,
          state: '3',
          audit: 2,
          cause: this.formData.cause
        })
        if (res.code == '') {
          this.$message.success('成功拒绝打款！！！')
        }
        this.visible = false
        this.$emit('success')
      } else {
        this.formData.state = 3
        this.$message.warning('请填写理由并确定拒绝打款！！！')
      }
    },
    // 确认审核
    handleExamine() {
      this.$confirm('确定审核通过吗？')
        .then(async () => {
          await withdrawalHandle({
            withdrawalId: this.formData.withdrawalId,
            state: '0',
            audit: 1,
            type: ''
          })
          this.$message.success('操作成功！请再次确认！')
          this.getInfo(this.formData.withdrawalId)
          this.$emit('success')
        })
        .catch(() => {})
    },
    // 确认打款
    handleConfirm() {
      this.$confirm('确定打款吗？')
        .then(async () => {
          if (!this.formData.type) return this.$message.error('请选择打款方式')
          await withdrawalHandle({
            withdrawalId: this.formData.withdrawalId,
            state: '2',
            audit: 1,
            type: this.formData.type
          })
          this.$message.success('打款成功！！！')
          this.visible = false
          this.$emit('success')
        })
        .catch(() => {})
    }
  }
}
</script>

