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

        <a-form-item label="所属需求" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'requirementId', validatorRules.requirementId]" placeholder="请输入所属需求" style="width: 100%"/>
        </a-form-item>
          
        <a-form-item label="接单人" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'userId', validatorRules.userId]" placeholder="请输入接单人" style="width: 100%"/>
        </a-form-item>
          
        <a-form-item label="亮点" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['brightPoints']" rows="4" placeholder="请输入亮点"/>
        </a-form-item>
          
        <a-form-item label="报名时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择报名时间" v-decorator="[ 'enrollTime', validatorRules.enrollTime]" :trigger-change="true" style="width: 100%"/>
        </a-form-item>
          
        <a-form-item label="报名操作者id（小程序，APP报名此字段可为空，后台报名记录报名操作者）" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'enrollBy', validatorRules.enrollBy]" placeholder="请输入报名操作者id（小程序，APP报名此字段可为空，后台报名记录报名操作者）"></a-input>
        </a-form-item>
          
        
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import JDate from '@/components/jeecg/JDate'  
  
  export default {
    name: "HwEnrollmentModal",
    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:{
        requirementId:{rules: [{ required: true, message: '请输入所属需求!' }]},
        userId:{rules: [{ required: true, message: '请输入接单人!' }]},
        brightPoints:{},
        enrollTime:{rules: [{ required: true, message: '请输入报名时间!' }]},
        enrollBy:{},
        },
        url: {
          add: "/requiremanager/hwEnrollment/add",
          edit: "/requiremanager/hwEnrollment/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,'requirementId','userId','brightPoints','enrollTime','enrollBy'))
        })
      },
      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,'requirementId','userId','brightPoints','enrollTime','enrollBy'))
      }
      
    }
  }
</script>