<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 v-decorator="[ 'refundAmount', validatorRules.refundAmount,{initialValue:0,trigger:['change']}]"
                   :precision="2"
                   style="width: 100%"
                   enterButton="Search"
                   placeholder="请输入退款总金额">
            <a-button slot="suffix" type="link" @click="inputAll">全部</a-button>
          </a-input>

        </a-form-item>

        <a-form-item label="服务费率" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <span style="margin-left: 15px">{{serviceRate}}%</span>
        </a-form-item>
        <a-form-item label="可见性" :labelCol="labelCol" :wrapperCol="wrapperCol" :required="true">
          <j-dict-select-tag v-decorator="['isDisplay', {initialValue:'0'}]" :type="'radio'" @change="editDisplay"
                             :triggerChange="true" dictCode="is_display"/>
        </a-form-item>

        <a-form-item label=" " :colon="false" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <div style="color: #1890ff">将扣除账户余额{{real_money}}元，服务费{{service_charge}}元</div>
        </a-form-item>

      </a-form>
    </a-spin>
    <!--    <div class="drawer-bootom-button" v-show="!disableSubmit">-->
    <!--      <a-button class="ant-btn" @click="handleCancel">取消</a-button>-->
    <!--      <a-button class="ant-btn ant-btn-primary" style="margin-left: 8px" @click="handleOk">确定</a-button>-->
    <!--    </div>-->
  </a-modal>
</template>

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

  export default {
    name: 'HlgAccountRefundModal',
    mixins: [JeecgListMixin, Arithmetic],
    components: {
      JDate

    },
    data() {
      return {
        title: '退款',
        bodyStyle: {
          paddingTop: '30px',
          paddingBottom: '20px',
          // height: (window.innerHeight * 0.62) + 'px',
          'overflow-y': 'auto'
        },
        width: 800,
        modelStyle: {
          width: '50%',
          style: { top: '20px' },
          fullScreen: false
        },
        serviceRate: 0,
        service_charge: '0.00',
        real_money: '0.00',
        isDisplay: 0,
        changeIndex: 0,
        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) => {
            this.service_charge = this.fix(this.accMul(fields.refundAmount || 0, this.accDiv(this.serviceRate, 100)))
            this.real_money = this.fix(this.accSub(fields.refundAmount || 0, this.service_charge || 0))
          }
        }),
        disableSubmit: false,
        validatorRules: {
          refundAmount: {
            rules: [{ required: true, message: '请输入充值金额！' }, {
              validator: this.checkMoney
            }]
          },
          isDisplay: { rules: [{ required: true, message: '请选择可见性！' }] }
        },
        confirmLoading: false,
        companyId: 0,
        url: {
          add: '/hlgaccount/hlgAccount/add',
          refund: '/hlgaccount/hlgAccountDetail/refund'
        }

      }
    },
    mounted() {

    },
    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.canRefund = this.accSub(this.accSub(this.model.totalAmount, this.model.totalRefundAmount), this.model.totalRefundServiceFee)
      },
      loadData(arg) {
        // if (!this.url.list) {
        //   this.$message.error('请设置url.list属性!')
        //   return
        // }
        // //加载数据 若传入参数1则加载第一页的内容
        // if (arg === 1) {
        //   this.ipagination.current = 1
        // }
        // var params = this.getQueryParams()//查询条件
        // params.companyId = this.companyId
        // this.loading = true
        // getAction(this.url.list, params).then((res) => {
        //   if (res.success) {
        //     this.dataSource = res.result.records
        //     this.ipagination.total = res.result.total
        //   }
        //   if (res.code === 510) {
        //     this.$message.warning(res.message)
        //   }
        //   this.loading = false
        // })
      },
      close() {
        this.form.setFieldsValue({
          refundAmount: ''
        })
        this.$emit('ok')
        this.visible = false
      },
      handleOk() {
        this.form.validateFields((err, values) => {
          if (!err) {
            this.confirmLoading = true
            this.loading = true
            let formData = Object.assign(this.model, values)
            putAction(this.url.refund, formData).then(data => {
              this.confirmLoading = false
              if (data.success) {
                this.$message.info('退款成功')
                this.close()
              } else {
                this.$error({
                  title: '退款失败',
                  content: data.message,
                  okText: '确定',
                  centered: true
                })
              }
              if (data.code === 510) {
                this.$message.warning(res.message)
              }
              this.loading = false
            })
          }
        })
      },
      handleCancel() {
        this.close()
      },

      checkMoney(rule, value, callback) {

        const _number = Number(value)
        if (_number <= 0 && value !== '') {
          callback('金额不可小于等于0！')
        }
        if (isNaN(_number)) {
          callback('格式错误！')
        }

        if (_number > this.canRefund) {
          callback('输入金额大于可退款金额！')
        }

        callback()
      },
      handleEditServiceRateModal() {
        const { subcontractorId, companyId, serviceRate } = this
        this.$refs.editServiceRateModal.edit({ subcontractorId, companyId, serviceRate })
        this.$refs.editServiceRateModal.title = '编辑'
      },
      setServiceRate(data) {
        this.serviceRate = data
      },
      editDisplay(e) {
        if (e != null && e != '') {
          this.form.setFieldsValue({ isDisplay: e })
        }
        var isDisplay = this.form.getFieldValue('isDisplay')
        this.model.isDisplay = isDisplay
      },
      inputAll() {
        this.form.setFieldsValue({
          refundAmount: this.canRefund
        })
      }
    }
  }
</script>