<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"
            :min="0.1"
            style="width: 100%;"
            :precision="2"
            id="applyWithdrawal"
            placeholder="请输入正数(最多两位小数)"
            allowClear />
        </a-form-model-item>
        <a-form-model-item label="实际扣除金额">
          <span>{{ (serviceCharge||serviceCharge==0)?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, merchantCashOutDetail } from '@/api/merchant'
export default {
  mixins: [commonMixin],
  props: {
    isOpenModal: {
      type: Boolean,
      default: false
    },
    currentData: {
      type: Object,
      default: function () {
        return {}
      }
    }
  },
  computed: {
    serviceCharge () {
      return this.form.amount ? this.form.amount + 0.5 : 0
    }
  },
  data () {
    return {
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 15 }
      },
      form: {
        amount: ''
      },
      pageInfo: null,
      opened: this.isOpenModal,
      title: '申请提现',
      confirmLoading: false,
      spinning: false,
      rules: { amount: [{ required: true, message: '请输入提现金额', trigger: 'blur' }] }
    }
  },
  methods: {
    getPageInfo () {
      this.spinning = true
      merchantCashOutDetail({}).then(res => {
        console.log(res, '===========')
        if (res.status == 200) {
          this.pageInfo = res.data
        } else {
          this.pageInfo = null
        }
        this.spinning = false
      })
    },
    // 保存
    handleSubmit () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const form = JSON.parse(JSON.stringify(_this.form))
          form.serviceCharge = this.serviceCharge
          form.merchantType = 'DEALER'
          form.merchantSn = _this.pageInfo ? _this.pageInfo.merchantSn : ''
          // form.shelfSn = _this.nowData && _this.nowData.shelfSn
          _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 {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 取消
    cancel () {
      this.form.amount = ''
      this.$refs.ruleForm.resetFields()
      this.$emit('close')
    }
  },
  watch: {
    isOpenModal (nVal, oVal) {
      this.opened = nVal
      // this.getPageInfo()
    },
    opened (nVal, oVal) {
      if (nVal) {
        this.getPageInfo()
      } else {
        this.form.amount = ''
        this.$emit('close')
      }
    }
  }
}
</script>

<style>
</style>
