<template>
  <a-modal
    :title="title"
    :width="width"
    :bodyStyle="bodyStyle"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    :maskClosable="false"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label=" " :colon="false" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <div>
            <div>
              账户余额： ￥{{balance}}
            </div>
            <div>
              本次支付： ￥{{model.totalPayment}}
            </div>
          </div>
        </a-form-item>
        <a-form-item label="复核结果" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['status',validatorRules.status]" :trigger-change="true"
                             dictCode="payment_review" placeholder="请选择复核结果" style="width: 100%" @change="checkStatus"/>
        </a-form-item>

        <a-form-item v-if="is_show" label="拒绝原因" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea
            v-decorator="[ 'refuseReason', validatorRules.refuseReason]"
            placeholder="请输入拒绝原因"
            rows="4"
          />
        </a-form-item>
        <HlgPaymentRechargeModal ref="RechargeModel"></HlgPaymentRechargeModal>
        <HlgPaymentErrorModal ref="errorModal" @ok="handleReviewAgain"></HlgPaymentErrorModal>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import JDate from '@/components/jeecg/JDate'
  import HlgPaymentRechargeModal from './HlgPaymentRechargeModal'
  import HlgPaymentErrorModal from './HlgPaymentErrorModal'

  export default {
    name: 'HlgPaymentReviewModal',
    components: {
      JDate,
      HlgPaymentRechargeModal,
      HlgPaymentErrorModal
    },
    data() {
      return {
        form: this.$form.createForm(this),
        title: '操作',
        width: 800,
        bodyStyle: {
          paddingTop: '20px',
          paddingBottom:'50px',
          maxHeight: (window.innerHeight * 0.47) + 'px',
          'overflow-y': 'auto'
        },
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        is_show: false,
        balance: 0.00,
        confirmLoading: false,
        validatorRules: {
          status: { rules: [{ required: true, message: '请选择复核结果!' }] },
          refuseReason: { rules: [{ required: true, message: '请输入拒绝原因!' }] },
        },
        url: {
          payment: '/hlgpayment/hlgPayment/payment',
          nextPayment: '/hlgpayment/hlgPayment/nextPayment'
        }

      }
    },
    created() {
    },
    methods: {
      add() {
        this.edit({})
      },
      edit(record) {
        this.form.resetFields()
        this.model = Object.assign({}, record)
        this.is_show = false;
        this.balance = record.isThroughPlatform ? record.hlgCpAccount.balance : record.hlgAccount.balance
        this.visible = true
        console.log(this.model)
      },
      close() {
        this.visible = false
      },
      //复核
      handleOk() {
        const that = this
        //余额是否足够
        const isEnough = this.balance >= this.model.totalPayment
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            if (isEnough || values.status == 2) {
              //开始复核
              that.confirmLoading = true;
              let httpurl = this.url.payment;
              let method = 'put';
              let formData = Object.assign(this.model, values);
              console.log("表单提交数据",formData)
              httpAction(httpurl,formData,method).then((res)=>{
                if(res.success){
                  that.$success({
                    title: '提示',
                    content: res.message,
                    okText: '确定',
                    centered:true,
                    onOk: () => {

                    }
                  })
                  that.$emit('ok');
                  that.close();
                }else{
                  if(res.message === '超出第一级税率'){
                    that.$refs.errorModal.edit(res.result,formData)
                    that.$refs.errorModal.title = '提示'
                    that.$refs.errorModal.disableSubmit = false
                    that.$refs.errorModal.desc = '本月累计收款超出第一级税率'
                  }
                  if(res.message === '当月累计金额找不到配置的税率'){
                    that.$refs.errorModal.edit(res.result,formData)
                    that.$refs.errorModal.title = '提示'
                    that.$refs.errorModal.disableSubmit = true
                    that.$refs.errorModal.desc = '找不到税率区间'
                  }
                  if(res.message === '余额不足，请充值'){
                    this.$error({
                      title: '失败',
                      content: '余额不足，请充值',
                      okText: '我要充值',
                      centered: true,
                      onOk: () => {
                        this.$refs.RechargeModel.edit(this.model)
                        this.$refs.RechargeModel.title = '我要充值'
                        this.$refs.RechargeModel.subcontractorName = this.model.subcontractorName
                        this.$refs.RechargeModel.disableSubmit = false
                      }
                    })
                  }
                  that.$message.error(res.message);
                }
              }).finally(() => {
                that.confirmLoading = false;
              })
            } else {
              this.$error({
                title: '失败',
                content: '余额不足，请充值',
                okText: '我要充值',
                centered: true,
                onOk: () => {
                  this.$refs.RechargeModel.edit(this.model)
                  this.$refs.RechargeModel.title = '我要充值'
                  this.$refs.RechargeModel.subcontractorName = this.model.subcontractorName
                  this.$refs.RechargeModel.disableSubmit = false
                }
              })

            }
          }

        })
      },
      handleCancel() {
        this.close()
      },
      handleChange(value) {
        console.log(value)
      },
      checkStatus(value){
        this.is_show = value === '2'
      },
      //继续付款
      handleReviewAgain() {
        // do something
        const that = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            //开始复核
            that.confirmLoading = true;
            let httpurl = this.url.nextPayment;
            let method = 'put';
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$success({
                  title: '提示',
                  content: res.message,
                  okText: '确定',
                  centered:true,
                  onOk: () => {

                  }
                })
                that.$emit('ok');
                that.close();
              }else{
                that.$error({
                  title: '错误提示',
                  content: res.message,
                  okText: '确定',
                  centered:true
                })
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
        })
      }
    }
  }
</script>
