<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="wuplx">
              <j-dict-select-tag type="list" v-model="model.wuplx" dictCode="goods_category" placeholder="请选择物品类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="this.model.wuplx==1">
            <a-form-model-item label="面料" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="mlname">
              <j-popup
                v-model="model.mlname"
                field="mlname"
                org-fields="name,id,fabric_number"
                dest-fields="mlname,mlid,mlbh"
                code="findml"
                :multi="true"
                @input="popupCallback"
                />
            </a-form-model-item>
          </a-col>
          <a-col :span="24"  v-if="this.model.wuplx==2">
            <a-form-model-item label="辅料" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="flname">
              <j-popup
                v-model="model.flname"
                field="flname"
                org-fields="id,contents"
                dest-fields="flid,flname"
                code="ckfindfl"
                :multi="true"
                @input="popupCallback"
                />
            </a-form-model-item>
          </a-col>
          <a-col :span="24"  v-if="this.model.wuplx==3">
            <a-form-model-item label="设备" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sbname">
              <j-popup
                v-model="model.sbname"
                field="sbname"
                org-fields="id,code,name"
                dest-fields="sbid,sbbh,sbname"
                code="ckfindsb"
                :multi="true"
                @input="popupCallback"
                />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="this.model.wuplx==4">
            <a-form-model-item label="工具" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="gjname">
              <j-popup
                v-model="model.gjname"
                field="gjname"
                org-fields="id,name,nums"
                dest-fields="gjid,gjbh,gjname"
                code="kcfindgj"
                :multi="true"
                @input="popupCallback"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24"  v-if="this.model.wuplx==5">
            <a-form-model-item label="商品" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="spname">
              <j-popup
                v-model="model.spname"
                field="spname"
                org-fields="id,goods_code,goods_name"
                dest-fields="spid,spbh,spname"
                code="kcfindsp"
                :multi="true"
                @input="popupCallback"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="库存剩余" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="kcsy">
              <a-input-number v-model="model.kcsy" placeholder="请输入库存剩余" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <!--<a-col :span="24">
            <a-form-model-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="temp1">
              <a-input v-model="model.temp1" placeholder="请输入名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="temp2">
              <a-input v-model="model.temp2" 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: 'KucunForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
          wuplx : ""
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/kucun/kucun/add",
          edit: "/kucun/kucun/edit",
          queryById: "/kucun/kucun/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>