<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :bodyStyle="bodyStyle"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    :maskClosable="false"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="服务费" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'serviceFee', validatorRules.serviceFee]" :precision="2" placeholder="请输入服务费" style="width: 100%" />
        </a-form-item>
        <template v-if="model.subcontractType != null">
          <a-form-item label="外包服务费" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input-number v-decorator="[ 'subServiceFee', validatorRules.subServiceFee]" :precision="2" placeholder="请输入外包服务费" style="width: 100%" />
          </a-form-item>
        </template>
      </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 moment from "moment"
  
  export default {
    name: "HwPaymentEditServiceFeeModal",
    components: { 
      JDate,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:600,
        visible: false,
        model: {},
        bodyStyle: {
          padding: '40px',
          'overflow-y': 'auto'
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        isThroughPlatform:1,//是否通过平台（0：否 1：是）
        confirmLoading: false,
        validatorRules:{
          serviceFee:{rules: [{ required: true, message: '请输入服务费!' }]},
          subServiceFee:{rules: [{ required: true, message: '请输入外包服务费!' },
              {validator: this.validateSubServiceFee},{validator: this.validateSub}
              ]},
        },
        url: {
          edit:"/saasmanager/hwPlatformCompanyAccountDetail/editServiceFee",
        }
     
      }
    },
    created () {
    },
    methods: {
      moment,
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.model.paymentId = this.model.id;
        this.model.id = null;
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'serviceFee','subServiceFee'));
        });
        if (record.subcontractType != null) {
          this.isThroughPlatform = 1;
        } else {
          this.isThroughPlatform = 0;
        }
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            httpurl+=this.url.edit;
            method = 'post';
            let formData = Object.assign(this.model, values);
            formData.isThroughPlatform = this.isThroughPlatform;
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$error({
                  title: '错误提示',
                  content: res.message,
                  okText: '确定',
                  centered:true
                })
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'serviceFee','subServiceFee'))
      },
      //验证外包服务费小于服务费
      validateSubServiceFee(rule, value, callback){
        if(!value){
          callback()
        }else{
          let serviceFee = this.form.getFieldValue('serviceFee')
          if(value < serviceFee){
            callback()
          }else{
            callback("外包服务费需小于服务费")
          }
        }
      },
      //校验正数字
      validateSub(rule, value, callback){
        if(!value){
          callback()
        }else{
          if(new RegExp(/^((0{1}\.\d{1,2})|([1-9]\d*\.{1}\d{1,2})|([1-9]+\d*)|0)$/).test(value)){
            callback()
          }else{
            callback("请输入正确的数字，最多俩位小数")
          }
        }
      },
    }
  }
</script>
