<template>
  <a-modal
    centered
    class="cashOutModal-modal"
    :footer="null"
    :maskClosable="false"
    title="申请提现"
    v-model="isShow"
    @cancle="isShow=false"
    :width="800">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 表单 -->
      <div>
        <a-form-model
          id="cashOutModal-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
              id="cashOutModal-amount"
              v-model="form.amount"
              :precision="2"
              :min="0.01"
              :max="maxAmount"
              :placeholder="'请输入'+ maxAmount +'以内的金额'"
              style="width: 100%;" />
          </a-form-model-item>
          <a-form-model-item label="实际扣除金额" prop="brandName">
            <strong>{{ deductionAmount }}</strong>元（申请提现金额+提现手续费）
          </a-form-model-item>
          <a-form-model-item label="提现账户" prop="brandName">
            <div>{{ nowData&&nowData.bankName || '--' }}（{{ nowData&&nowData.accountName || '--' }}）</div>
            <div>{{ nowData&&nowData.accountNo || '--' }}</div>
          </a-form-model-item>
        </a-form-model>
        <div class="btn-cont">
          <a-button type="primary" id="product-brand-edit-modal-save" @click="handleSave">提交</a-button>
          <a-button id="product-brand-edit-modal-back" @click="isShow = false" style="margin-left: 15px;">取消</a-button>
        </div>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { satelliteWHWithdrawalApply } from '@/api/satelliteWH'
export default {
  name: 'ProductBrandEditModal',
  mixins: [commonMixin],
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    nowData: {
      type: Object,
      default: null
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 16 }
      },
      form: {
        amount: ''
      },
      rules: {
        amount: [
          { required: true, message: '请输入申请提现金额', trigger: 'blur' }
        ]
      },
      spinning: false
    }
  },
  computed: {
    maxAmount () {
      const balance = this.nowData && this.nowData.balance ? this.nowData.balance : 0
      const serviceCharge = this.nowData && this.nowData.serviceCharge ? this.nowData.serviceCharge : 0
      if ((Number(balance) * 100 - Number(serviceCharge) * 100) <= 0) {
        return 0
      } else {
        return (Number(balance) * 100 - Number(serviceCharge) * 100) / 100
      }
    },
    deductionAmount () {
      const amount = this.form.amount || 0
      const serviceCharge = this.nowData && this.nowData.serviceCharge ? this.nowData.serviceCharge : 0
      if (amount == 0) {
        return 0
      } else {
        return (Number(amount) * 100 + Number(serviceCharge) * 100) / 100
      }
    }
  },
  methods: {
    //  提现
    handleSave () {
      const _this = this
      _this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const params = {
            amount: this.form.amount,
            bankName: this.nowData.bankName,
            bankAccount: this.nowData.accountName,
            bankAccountNo: this.nowData.accountNo
          }
          _this.spinning = true
          satelliteWHWithdrawalApply(params).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$emit('ok')
              _this.isShow = false
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        } else {
          return false
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.$refs.ruleForm.resetFields()
      }
    }
  }
}
</script>

<style lang="less">
  .cashOutModal-modal{
    .ant-modal-body {
      padding: 40px 40px 24px;
    }
    .btn-cont {
      text-align: center;
      margin: 35px 0 10px;
    }
  }
</style>
