<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="toolId">
<!--              <a-input v-model="model.toolId" placeholder="请输入工具"  ></a-input>-->
              <j-dict-select-tag type="list" v-model="model.toolId" dictCode="v_bu_tool,name,id" placeholder="请选择工具" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="工具编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="code">
              <a-input v-model="model.code" placeholder="请输入工具编码"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="品牌" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="brand">
              <a-input v-model="model.brand" placeholder="请输入品牌"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="型号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="model">
              <a-input v-model="model.model" placeholder="请输入型号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="所属工具柜" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cabinet">
              <j-dict-select-tag type="list" v-model="model.cabinet" dictCode="bu_cabinet,name,id" placeholder="请选择所属工具柜" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="供应商" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="vendor">
              <a-input v-model="model.vendor" placeholder="请输入供应商"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="采购日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="purchaseDate">
              <j-date placeholder="请选择采购日期" v-model="model.purchaseDate"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="num">
              <a-input-number v-model="model.num" placeholder="请输入数量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="MAC地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="mac">
              <a-input v-model="model.mac" placeholder="请输入MAC地址"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="RFID标签" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rfid">
              <a-input v-model="model.rfid" placeholder="请输入RFID标签编号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="是否在虚拟库" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="inVirtual">
              <a-select :options="options" v-model="model.inVirtual" placeholder="请选择是否在虚拟库" :defaultValue="0"></a-select>
            </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: 'ToolSheetForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        options:[
          { value: '1', label: '是'},
          { value: '0', label: '否'}
        ],
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          code: [
            { required: true, message: '请输入工具类型!'},
          ],
           type: [
              { required: true, message: '请输入工具类型!'},
           ],
           brand: [
              { required: true, message: '请输入品牌!'},
           ],
           model: [
              { required: true, message: '请输入型号!'},
           ],
           vendor: [
              { required: false, message: '请输入供应商!'},
           ],
           purchaseDate: [
              { required: true, message: '请输入采购日期!'},
           ],
           toolId: [
              { required: true, message: '请输入工具!'},
           ],
          num: [
            { required: true, message: '请输入数量!'},
          ],
        },
        url: {
          add: "/toolSheet/toolSheet/add",
          edit: "/toolSheet/toolSheet/edit",
          queryById: "/toolSheet/toolSheet/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>