<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="manufacturer">
              <j-select-depart v-model="model.orgCode"   />
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="设备名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入设备名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设备图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="img">
              <j-image-upload isMultiple  v-model="model.img" ></j-image-upload>
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="设备类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="typeid">-->
<!--              <j-dict-select-tag type="list" v-model="model.typeid" dictCode="zy_devicetype,name,id" pidField="pid"-->
<!--                                 pidValue="" placeholder="请选择设备类型" />-->

<!--            &lt;!&ndash;  <j-tree-select-->
<!--                v-decorator="[model.typeid]"-->
<!--                placeholder="父级节点"-->
<!--                dict="zy_devicetype,name,id"-->
<!--                pidField="pid"-->
<!--                pidValue=""-->
<!--              />&ndash;&gt;-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="购买时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="buydate">
              <j-date placeholder="请选择购买时间" v-model="model.buydate"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="经办人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="agent">
                <j-select-user-by-dep v-model="model.agent" />
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="存放位置" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="adress">-->
<!--              <a-input v-model="model.adress" placeholder="请输入存放位置"  ></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="设备状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <j-dict-select-tag type="radio" v-model="model.status" dictCode="devicestatus1"
                                 placeholder="设备状态"></j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="维护周期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="maintenancecycle">
              <a-input v-model="model.maintenancecycle" placeholder="请输入维护周期"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="管理人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="administrator">
              <j-select-user-by-dep v-model="model.administrator" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="上次维护时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="maintenancedate">
              <j-date placeholder="请选择上次维护时间" v-model="model.maintenancedate"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="3d模型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="img3d">
<!--              <j-upload v-model="model.img3d"   ></j-upload>-->
              <a-input v-model="model.img3d" placeholder="请输入模型地址"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设备品牌" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <j-dict-select-tag v-model="model.canshu" dictCode="zy_devicebrand,brand,id"
                                 placeholder="设备品牌" @change="canshu"></j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="类型编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="devicetypeCode ">
              <j-popup
                v-model="model.code1"
                field="code1"
                org-fields="model,id"
                dest-fields="code1,modelId"
                code="zy_devicemodel"
                :multi="false"
                :param="parm1"
                @input="popupCallback"
              />
            </a-form-model-item>
            <a-input  v-model="model.modelId" hidden/>
          </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: 'ZyDeviceForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        parm1:{
          brand_id:''
        },
        model:{
          status:1,
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          name:[
            { required: true, message: '请输入设备名称!'},
            {min: 1, max: 50, message: '最多输入50字！', trigger: 'blur'},
          ],
        },
        url: {
          add: "/device/zyDevice/add",
          edit: "/device/zyDevice/edit",
          queryById: "/device/zyDevice/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      canshu(){
        this.parm1.brand_id=this.model.canshu
        console.log(this.parm1)
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.model.code1=this.model.typeCode
        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>