<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="样品名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sampleName">
              <a-input v-model="model.sampleName" placeholder="请输入样品名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="样品规格" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sampleSpecifications">
              <a-input v-model="model.sampleSpecifications" placeholder="请输入样品规格"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="生产厂家" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sampleManufacturer">
              <a-input v-model="model.sampleManufacturer" placeholder="请输入生产厂家"  ></a-input>
            </a-form-model-item>
          </a-col>


          <a-col :span="24">
            <a-form-model-item label="样品分类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sampleClassifications">
              <j-tree-select
                ref="treeSelect"
                placeholder="请选择样品分类"
                v-model="model.sampleClassifications"
                dict="itdm_sample_classification,name,id"
                pidField="pid"
                pidValue="0"

              >
              </j-tree-select>
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="样品单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sampleUnit">
              <a-input v-model="model.sampleUnit" placeholder="请输入样品单位"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="样品状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sampleStatus">
              <j-search-select-tag v-model="model.sampleStatus" dict="sample_status	"  />
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="样品存放位置" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="samplePosition">
              <a-input v-model="model.samplePosition" placeholder="请输入样品存放位置"  ></a-input>
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="model.remark" placeholder="请输入备注"  ></a-input>
            </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: 'ItdmSampleInventoryForm',
    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: {
           sampleName: [
              { required: true, message: '请输入样品名称!'},
           ],
           sampleSpecifications: [
              { required: true, message: '请输入样品规格!'},
           ],
           sampleNum: [
              { required: true, message: '请输入样品数量!'},
           ],
           sampleManufacturer: [
              { required: true, message: '请输入生产厂家!'},
           ],
           sampleClassifications: [
              { required: true, message: '请输入样品分类!'},
           ],
           sampleUnit: [
              { required: true, message: '请输入样品单位!'},
           ],
           sampleStatus: [
              { required: true, message: '请输入样品状态（是否损坏）!'},
           ],
           samplePosition: [
              { required: true, message: '请输入样品存放位置!'},
           ],
        },
        url: {
          add: "/inventory/itdmSampleInventory/add",
          edit: "/inventory/itdmSampleInventory/edit",
          queryById: "/inventory/itdmSampleInventory/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;
            })
          }

        })
      },
    }
  }
</script>