<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="riskName" :rules="[{ required: true, message: '请输入风险名称' }]">
              <a-input v-model="model.riskName" placeholder="请输入风险名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="传感器类名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sensorType" :rules="[{ required: true, message: '请选择传感器类型' }]">
<!--              <a-input v-model="model.sensorType" placeholder="请输入传感器类名"  ></a-input>-->
<!--              <j-select-sensor-type v-model="model.sensorType" placeholder="请选择传感器类型" :multiple="false"/>-->
              <j-dict-select-tag type="list" v-model="model.sensorType" :trigger-change="true" dictCode="bu_sensor_type,name,id" placeholder="请选择传感器类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="风险说明" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="riskExplain">
              <a-input v-model="model.riskExplain" placeholder="请输入风险说明"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="风险危害" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="riskHarm">
              <a-input  v-model="model.riskHarm" placeholder="请输入风险危害" type="textarea" :autosize="{ minRows: 1, maxRows: 10 }" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="处理建议" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dealAdvice">
              <a-input v-model="model.dealAdvice" placeholder="请输入处理建议" type="textarea" :autosize="{ minRows: 1, maxRows: 10 }" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="算法名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="methodName">
              <a-input v-model="model.methodName" placeholder="请输入算法名"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="影响字段" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="affectsFields">
              <a-input v-model="model.affectsFields" placeholder="请输入影响字段"   ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="极限值个数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="amount" :rules="[{ required: true, message: '请输入极限值个数' }]">
              <a-input v-model="model.amount" placeholder="请输入极限值个数" type="number"></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 JSelectSensorType from "@comp/jeecgbiz/JSelectSensorType";
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'BuRiskItemForm',
    components: {
      JSelectSensorType,
      JDictSelectTag
    },
    props: {
      //表单禁用
      disabled: {
        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: {
        },
        url: {
          add: "/device/buRiskItem/add",
          edit: "/device/buRiskItem/edit",
          queryById: "/device/buRiskItem/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>