<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['name', validatorRules.name]" placeholder="请输入名称"></a-input>
        </a-form-item>
        <a-form-item label="法人代表" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['legalRepresentative']" placeholder="请输入法人代表"></a-input>
        </a-form-item>
        <a-form-item label="联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['contactPhone']" placeholder="请输入联系电话"></a-input>
        </a-form-item>
        <a-form-item label="前置机IP" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['paymentAddress']" placeholder="请输入前置机IP"></a-input>
        </a-form-item>
        <a-form-item label="前置机端口" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['paymentPort']" placeholder="请输入前置机端口"></a-input>
        </a-form-item>
        <a-form-item label="一网通id" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['appId']" placeholder="请输入一网通id"></a-input>
        </a-form-item>
        <a-form-item label="业务模式" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['businessMode']" placeholder="请输入业务模式"></a-input>
        </a-form-item>
        <a-form-item label="交易代码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['trsType']" placeholder="请输入交易代码"></a-input>
        </a-form-item>
        <a-form-item label="代发协议" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['payCnv']" placeholder="请输入代发协议"></a-input>
        </a-form-item>
        <a-form-item label="创建时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择创建时间" v-decorator="['createTime', validatorRules.createTime]" :trigger-change="true" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="创建者" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['createBy', validatorRules.createBy]" placeholder="请输入创建者"></a-input>
        </a-form-item>
        <a-form-item label="更新时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择更新时间" v-decorator="['updateTime']" :trigger-change="true" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="更新者" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['updateBy']" placeholder="请输入更新者"></a-input>
        </a-form-item>
        <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['remark']" rows="4" placeholder="请输入备注"/>
        </a-form-item>

      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'  


  export default {
    name: "HlwSubcontractorModal",
    components: { 
      JDate,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          name: {
            rules: [
              { required: true, message: '请输入名称!'},
            ]
          },
          createTime: {
            rules: [
              { required: true, message: '请输入创建时间!'},
            ]
          },
          createBy: {
            rules: [
              { required: true, message: '请输入创建者!'},
            ]
          },
        },
        url: {
          add: "/hlwsubcontractor/hlwSubcontractor/add",
          edit: "/hlwsubcontractor/hlwSubcontractor/edit",
        }
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'name','legalRepresentative','contactPhone','paymentAddress','paymentPort','appId','businessMode','trsType','payCnv','createTime','createBy','updateTime','updateBy','remark'))
        })
      },
      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 = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'name','legalRepresentative','contactPhone','paymentAddress','paymentPort','appId','businessMode','trsType','payCnv','createTime','createBy','updateTime','updateBy','remark'))
      },

      
    }
  }
</script>