<template>
  <a-modal
    :title="title"
    :visible="visible"
    :width="width"
    :bodyStyle="bodyStyle"
    :confirmLoading="confirmLoading"
    okText="确定充值"
    @ok="handleOk"
    @cancel="handleCancel"
    :maskClosable="false"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="充值金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'totalAmount', validatorRules.totalAmount,{initialValue:0}]" :precision="2"
                          :min="0"
                          style="width: 100%"
                          placeholder="请输入充值金额"></a-input-number>
        </a-form-item>

        <a-form-item label="服务费率" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <span style="margin-left: 15px">{{serviceRate}}%</span>

          <a-button type="link" @click="handleEditServiceRateModal" v-has="'accountDetail:recharge:edit'">编辑</a-button>
        </a-form-item>
        <a-form-item label=" " :colon="false" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-row type="flex" align="middle">
            <div style="color: #1890ff">服务费{{service_charge}}元，实际到账{{real_money}}元</div>
          </a-row>

        </a-form-item>

      </a-form>
    </a-spin>
    <HlgAccountEditServiceRateModal ref="editServiceRateModal" @ok="setServiceRate"></HlgAccountEditServiceRateModal>
  </a-modal>
</template>

<script>
  import { Arithmetic } from '@/mixins/Arithmetic'
  import { httpAction, postAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import JDate from '@/components/jeecg/JDate'
  import HlgAccountEditServiceRateModal from './HlgAccountEditServiceRateModal'

  export default {
    name: 'HlgAccountRechargeModal',
    mixins: [Arithmetic],
    components: {
      JDate,
      HlgAccountEditServiceRateModal
    },
    data() {
      return {
        title: '',
        bodyStyle: {
          paddingTop: '20px',
          paddingBottom: '30px',
          // height: (window.innerHeight * 0.62) + 'px',
          'overflow-y': 'auto'
        },
        width: 800,
        serviceRate: 0,
        service_charge: '0.00',
        real_money: '0.00',
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        form: this.$form.createForm(this, {
          onValuesChange: (props, fields) => {
            if(fields.totalAmount){
              this.real_money = this.fix1(this.accDiv(fields.totalAmount || 0, this.accDiv(this.numAdd(this.serviceRate,100),100)))
              this.service_charge = this.fix(this.accSub(fields.totalAmount || 0, this.real_money))
            }
          }
        }),
        disableSubmit: false,
        validatorRules: {
          totalAmount: {
            rules: [{ required: true, message: '请输入充值金额！' }, {
              validator: this.checkMoney
            }]
          }
        },
        confirmLoading: false,
        companyId: 0,
        url: {
          add: '/hlgaccount/hlgAccountDetail/recharge'
        }

      }
    },
    mounted() {

    },
    watch: {
      serviceRate(newValue) {
        const money = this.form.getFieldValue('totalAmount')
        if (money) {
          this.real_money = this.fix1(this.accDiv(money || 0, this.accDiv(this.numAdd(newValue,100),100)))
          this.service_charge = this.fix(this.accSub(money || 0, this.real_money))
        }
      }
    },
    methods: {
      add() {
        this.edit({})
      },
      edit(record) {
        console.log(record)
        this.model = Object.assign({}, record)
        this.companyId = record.companyId
        this.visible = true
        this.serviceRate = record.serviceRate
        this.subcontractorId = record.subcontractorId
        this.accountId = record.id
      },
      close() {
        this.form.setFieldsValue({
          totalAmount: ''
        })
        this.service_charge = '0.00'
        this.real_money = '0.00'
        this.visible = false
      },
      handleOk() {
        this.form.validateFields((err, value) => {
          if (!err) {
            this.confirmLoading = true
            const { accountId, serviceRate } = this
            postAction(this.url.add, {
              totalAmount: value.totalAmount,
              accountId,
              serviceRate
            }).then(data => {
              if (data.success) {
                this.$message.success('充值成功')
                this.$emit('ok')
                this.close()
              } else {
                this.$error({
                  title: '充值失败',
                  content: data.message,
                  okText: '确定',
                  centered: true
                })
              }
              if (data.code === 510) {
                this.$message.warning(data.message)
              }
              this.confirmLoading = false
            })
          }
        })
      },
      handleCancel() {
        this.close()
      },

      checkMoney(rule, value, callback) {
        const _number = Number(value)
        if (value === 0) {
          callback('充值金额不能为0')
        }
        if (isNaN(_number)) {
          callback('格式错误！')
        }
        callback()
      },
      handleEditServiceRateModal() {
        const { subcontractorId, companyId, serviceRate } = this
        this.$refs.editServiceRateModal.edit({ subcontractorId, companyId, serviceRate })
        this.$refs.editServiceRateModal.title = '编辑'
      },
      setServiceRate(data) {
        this.serviceRate = data
      }


    }
  }
</script>