<template>
  <a-modal
    v-model="opened"
    :title="title"
    centered
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    :width="560"
    :footer="null"
    @cancel="cancel"
  >
    <a-spin :spinning="spinning" tip="Loading...">
      <a-form-model
        id="applyWithdrawal-form"
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol"
      >
        <a-form-model-item label="申请提现金额" prop="amount">
          <a-input-number
            v-model="form.amount"
            style="width: 73%;"
            :precision="2"
            :min="0"
            :max="activeAmount-0.5"
            id="applyWithdrawal-amount"
            placeholder="请输入正数(最多两位小数)" />
          <a-button type="primary" class="button-primary" @click="handleAmount" id="applyWithdrawal-getNum-btn">全部</a-button>
        </a-form-model-item>
        <a-form-model-item label="实际扣除金额">
          <span>{{ toThousands(serviceCharge,2)||'--' }}元 </span><span style="color: #999;">(申请提现金额+提现手续费)</span>
        </a-form-model-item>
        <a-form-model-item label="提现账户">
          <span style="display: block;" v-if="pageInfo">{{ pageInfo.merchantBankAccountEntity?pageInfo.merchantBankAccountEntity.bankAccount:'' }}{{ ' ( '+ pageInfo.dealerName +' ) ' }}</span>
          <span v-if="pageInfo">{{ pageInfo.merchantBankAccountEntity?pageInfo.merchantBankAccountEntity.bankCard:"" }}</span>
        </a-form-model-item>
        <a-form-model-item :wrapper-col="{ span: 12, offset: 6 }" style="text-align: center;">
          <a-button type="primary" :loading="confirmLoading" @click="handleSubmit" id="applyWithdrawal-btn-submit">确定</a-button>
          <a-button @click="cancel" style="margin-left: 15px" id="applyWithdrawal-btn-back">取消</a-button>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { saveMerchantCashOut } from '@/api/merchantNew'
export default {
  mixins: [commonMixin],
  props: {
    isOpenModal: {
      type: Boolean,
      default: false
    },
    pageInfo: {
      type: Object,
      default: () => {
        return null
      }
    }
  },
  computed: {
    serviceCharge () {
      return !this.form.amount ? 0 : this.form.amount + 0.5
    }
  },
  data () {
    return {
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 15 }
      },
      form: {
        amount: '' // 金额
      },
      activeAmount: null,
      opened: this.isOpenModal, // 提现弹框
      title: '申请提现',
      confirmLoading: false, // 确认按钮loading
      spinning: false,
      rules: { amount: [{ required: true, message: '请输入提现金额', trigger: 'blur' }] }
    }
  },
  methods: {
    handleAmount () {
      this.form.amount = this.activeAmount - 0.5
    },
    // 保存
    handleSubmit () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const form = JSON.parse(JSON.stringify(_this.form))
          if (form.amount <= 0) {
            _this.$message.warning('提现金额不能小于等于0')
            return
          }
          form.serviceCharge = this.serviceCharge
          form.merchantType = 'DEALER'
          form.merchantSn = _this.pageInfo ? _this.pageInfo.merchantSn : ''
          _this.spinning = true
          saveMerchantCashOut(form).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              setTimeout(() => {
                _this.cancel()
                _this.$emit('refresh')
                _this.spinning = false
              }, 1000)
            } else {
              _this.spinning = false
            }
          })
        } else {
          return false
        }
      })
    },
    // 取消
    cancel () {
      this.form.amount = ''
      this.$refs.ruleForm.resetFields()
      this.$emit('close')
    }
  },
  watch: {
    isOpenModal (nVal, oVal) {
      this.opened = nVal
    },
    opened (nVal, oVal) {
      if (!nVal) {
        this.form.amount = ''
        this.$emit('close')
      } else {
        this.activeAmount = this.pageInfo && (this.pageInfo.activeAmount || this.pageInfo.activeAmount == 0) ? this.pageInfo.activeAmount : null
      }
    }
  }
}
</script>

<style>
</style>
