<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="工单id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="woId">
              <j-popup
                v-model="model.woId"
                field="woId"
                org-fields="shiyan_type,shiyan_data"
                dest-fields="shiyanType,shiyanData"
                code="itdm_gongdan_master"
                :multi="true"
                @input="popupCallback"
                />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="试验类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shiyanType">
              <j-dict-select-tag type="list" v-model="model.shiyanType" dictCode="shiyan_type" placeholder="请选择试验类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="试验时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shiyanTime">
              <j-date placeholder="请选择试验时间" v-model="model.shiyanTime"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="试验数据" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shiyanData">
              <a-input v-model="model.shiyanData" placeholder="请输入试验数据"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="试验图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="extraPic">
              <j-image-upload isMultiple  v-model="model.extraPic" ></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" rows="4" placeholder="请输入备注" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'ItdmGongdanShujuForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           woId: [
              { required: true, message: '请输入工单id!'},
           ],
           shiyanType: [
              { required: true, message: '请输入试验类型!'},
           ],
           shiyanTime: [
              { required: true, message: '请输入试验时间!'},
           ],
           shiyanData: [
              { required: true, message: '请输入试验数据!'},
           ],
        },
        url: {
          add: "/Itdmgongdanshuju/itdmGongdanShuju/add",
          edit: "/Itdmgongdanshuju/itdmGongdanShuju/edit",
          queryById: "/Itdmgongdanshuju/itdmGongdanShuju/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
      popupCallback(value,row){
         this.model = Object.assign(this.model, row);
      },
    }
  }
</script>