<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="deviceName">
              <a-input v-model="model.deviceName" placeholder="请输入设备名称" :disabled="editdisabled" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设备类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deviceType">
              <a-input v-model="model.deviceType" placeholder="请输入设备类型"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设备编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deviceNo">
              <a-input v-model="model.deviceNo" placeholder="请输入设备编号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设备状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deviceStatus">
              <j-dict-select-tag type="list" v-model="model.deviceStatus" dictCode="device_status" placeholder="请选择设备状态" :disabled="editdisabled"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="维护时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="repairTime">
              <j-date placeholder="请选择维护时间"  v-model="model.repairTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="维护人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="repairer">
              <a-input v-model="model.repairer" placeholder="请输入维护人"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="维护记录" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="record">
              <a-input v-model="model.record" placeholder="请输入维护记录"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设备规格" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deviceSpecification">
              <a-input v-model="model.deviceSpecification" placeholder="请输入设备规格"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设备型号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deviceModel">
              <a-input v-model="model.deviceModel" placeholder="请输入设备型号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="计量有效期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deviceLife">
              <a-input v-model="model.deviceLife" placeholder="请输入计量有效期"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="是否已维护" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isRepair">
              <j-dict-select-tag type="list" v-model="model.isRepair" dictCode="is_repair" placeholder="请选择是否已维护" />
            </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: 'ItdmDeviceForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      },
      //禁止编辑
      editdisabled: {
        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: {
           deviceName: [
              { required: true, message: '请输入设备名称!'},
           ],
           deviceNo: [
              { required: true, message: '请输入设备编号!'},
           ],
           deviceStatus: [
              { required: true, message: '请输入设备状态!'},
           ],
        },
        url: {
          add: "/itdmDevice/itdmDevice/add",
          edit: "/itdmDevice/itdmDevice/edit",
          queryById: "/itdmDevice/itdmDevice/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>