<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">
          {{model.companyName}}
        </a-form-item>
        <a-form-item label="收款方" :labelCol="labelCol" :wrapperCol="wrapperCol">
          {{model.subcontractorName}}
        </a-form-item>
        <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>
    </a-spin>
  </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'


  export default {
    name: 'HlwCompanyAccountRechargeModal',
    mixins: [Arithmetic],
    components: {
      JDate
    },
    data() {
      return {
        title: '',
        bodyStyle: {
          paddingTop: '20px',
          paddingBottom: '30px',
          // height: (window.innerHeight * 0.62) + 'px',
          'overflow-y': 'auto'
        },
        width: 1000,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        form: this.$form.createForm(this),
        disableSubmit: false,
        validatorRules: {
          totalAmount: {
            rules: [{ required: true, message: '请输入充值金额！' }, {
              validator: this.checkMoney
            }]
          }
        },
        confirmLoading: false,
        companyAccountId: 0,
        currentTab: 0,
        url: {
          add: '/wisdom/hlwCompanyAccountDetail/recharge'
        }

      }
    },
    mounted() {

    },
    methods: {
      add() {
        this.edit({})
      },
      edit(record,companyName) {
        this.model = Object.assign({}, record)
        this.visible = true
        this.model.companyName = companyName;
        this.companyAccountId = record.id
      },
      close() {
        this.form.setFieldsValue({
          totalAmount: ''
        })
        this.visible = false
      },

      handleOk() {
        this.form.validateFields((err, value) => {
          if (!err) {
            this.confirmLoading = true
            const { companyAccountId } = this
            postAction(this.url.add, {
              amount: value.totalAmount,
              companyAccountId
            }).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()
      },
    }
  }
</script>
<style lang="less" scoped>
</style>