<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="deviceId" :rules="[{ required: true, message: '请选择设备' }]">
<!--              <a-input v-model="model.deviceId" placeholder="请输入设备id"  ></a-input>-->
              <j-select-device v-model="model.deviceId" placeholder="请选择设备名称" :multiple="false"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="风险项名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="riskId" :rules="[{ required: true, message: '请选择风险项' }]">
<!--              <a-input v-model="model.riskId" placeholder="请输入风险项id"  ></a-input>-->
              <j-select-risk-item v-model="model.riskId" placeholder="请选择风险项名称" :multiple="false" @change="ifRequire(model.riskId)"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="单项判断中间值" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="multiple" :rules="this.oneRequire">
              <a-input v-model="model.multiple" placeholder="请输入单项判断中间值"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="下限值" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="valueOne" :rules="this.twoRequire">
              <a-input v-model="model.valueOne" placeholder="请输入下限值"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="上限值" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="valueTwo" :rules="this.twoRequire">
              <a-input v-model="model.valueTwo" placeholder="请输入上限值"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备用一" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="valueThree">
              <a-input v-model="model.valueThree" placeholder="请输入备用一"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备用二" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="valueFour">
              <a-input v-model="model.valueFour" placeholder="请输入备用二"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="是否启用" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="enabled">
              <a-switch v-model="switchValue" checked-children="开" un-checked-children="关" @change="handleSwitchChange" />
            </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 JSelectDevice from '@comp/jeecgbiz/JSelectDevice'
  import JSelectRiskItem from '@comp/jeecgbiz/JSelectRiskItem'
  export default {
    name: 'BuRiskItemValueForm',
    components: {
      JSelectDevice,
      JSelectRiskItem,
    },
    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/buRiskItemValue/add",
          edit: "/device/buRiskItemValue/edit",
          // queryById: "/device/buRiskItemValue/queryById",
          queryById: "/device/buRiskItem/queryById"
        },
        switchValue: false,
        oneRequire:[{}],
        twoRequire:[{}],
        // mulRequire:[{}],
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      handleSwitchChange(checked) {
        this.model.enabled =  checked == true?"0":"1";
      },
      ifRequire(riskId){
        let param = {id:riskId};
        getAction(this.url.queryById, param, 'get').then(res =>{
          if(res.success){
            console.log("res.result.amount",res.result.amount)
            if(res.result.amount==1){
              this.oneRequire = [{ required: true, message: '请输入单项判断中间值' }]
              this.twoRequire = [{ required: false}]
            }else if(res.result.amount==2){
              this.twoRequire = [{ required: true, message: '请输入极值' }]
              this.oneRequire = [{ required: false}]
            }else{
              this.oneRequire = [{ required: true, message: '请输入单项判断中间值' }]
              this.twoRequire = [{ required: true, message: '请输入极值' }]
            }
          }else {
            this.oneRequire = [{ required: false}]
            this.twoRequire = [{ required: false}]
          }
        })
      },
      add (deviceId) {
        this.model.deviceId = deviceId
        this.model.enabled = '1'
        this.modelDefault = JSON.parse(JSON.stringify(this.model));
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.switchValue = record.enabled=="0"?true:false
        this.model = Object.assign({}, record);
        this.ifRequire(this.model.riskId)
        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 >