<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 v-if="model.pid!=null" label="上级设备类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="typeCode" :rules="[{ required: true, message: '请输入设备类型编码' }]">
              <a-input disabled v-model="model.parentName" placeholder="请输入设备类型编码"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设备类型编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="typeCode" :rules="[{ required: true, message: '请输入设备类型编码' }]">
              <a-input v-model="model.typeCode" placeholder="请输入设备类型编码"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设备类型名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="typeName" :rules="[{ required: true, message: '请输入设备类型名称' }]">
              <a-input v-model="model.typeName" placeholder="请输入设备类型名称"  ></a-input>
<!--              <span style="font-weight: bolder;font-size: 12px;color: red">注： 设备只能是根节点，子节点均为部件</span>-->
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item v-if="model.pid!=null && model.calss==null" label="设备/部件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="calss" :rules="[{ required: true, message: '请选择数据类型' }]">
              <a-radio-group v-model="model.calss">
                <a-radio :value="'1'">设备</a-radio>
                <a-radio :value="'2'">部件</a-radio>
              </a-radio-group>
            </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: 'BuDeviceTypeForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        option:[
          { value:'1', label:'设备' },
          { value:'2', label:'部件' }
        ],
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/device/buDeviceType/add",
          edit: "/device/buDeviceType/edit",
          queryById: "/device/buDeviceType/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add (record) {
        // this.modelDefault.calss = '1'
        if(record.id){
          this.modelDefault.pid = record.id
          this.modelDefault.parentName = record.typeName
          // this.modelDefault.calss = '2'
        }
        if(record.pid!=null && record.calss=='2') {
          this.modelDefault.calss = '2';
        }
        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';
            }
            if(this.model.pid==null) {
              this.model.calss='1';
            }
            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>