<template>
    <div class="examination">
     <el-dialog @close="closeDialog" :visible.sync="dialogTableVisible" width='70%'>
        <div slot="title" class="titleBox">
         <i :class="tableType === 0? 'el-icon-document':'el-icon-edit-outline'" style="font-size: 20px"></i>
        <!-- <img style="width: 25px; height: 25px" src='../../../../assets/img/imgScreen/more.png' /> -->
        <span style="margin-left:10px;font-size:18px">{{tableTypeDsc}}</span>
        </div>
        <el-form :model="formData" label-width="80px" :rules="monitorRules" ref="monitorForm"> 
        <el-row>
          <el-col :span='12'>
            <el-form-item prop="maxSign" label="上限符号">
                 <el-select v-model="formData.maxSign" clearable filterable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="(item,index) in maxSignType" :key="index" :label="item.maxSign"
                    :value="item.maxSign">
                  </el-option>
                </el-select>
              </el-form-item>
          </el-col>
          <el-col :span='12'>
            <el-form-item prop="maxValue" label="上限值">
               <el-input v-model="formData.maxValue" oninput ="value=value.replace(/[^0-9.]/g,'')" style="width:100%" size="small" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span='12'>
            <el-form-item prop="minSign" label="下限符号">
              <el-select v-model="formData.minSign" clearable filterable collapse-tags size="small" style="width: 100%;">
                <el-option v-for="(item,index) in minSignType" :key="index" :label="item.minSign"
                  :value="item.minSign">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span='12'>
            <el-form-item prop="minValue" label="下限值">
               <el-input v-model="formData.minValue" oninput ="value=value.replace(/[^0-9.]/g,'')" size="small" placeholder="请输入" style="width: 100%;"></el-input>
              </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span='24'>
            <el-form-item prop="alarmValue" label="超差值">
               <el-input v-model="formData.alarmValue" oninput ="value=value.replace(/[^0-9./]/g,'')" size="small" placeholder="请输入" style="width: 100%;"></el-input>
              </el-form-item>
          </el-col>
        </el-row>
       </el-form>
       <span slot="footer" class="dialog-footer">
        <el-button type="goon" icon="el-icon-check" size="mini" :loading='addLoading' @click="addData()">确定</el-button>
        <el-button size="mini" type="goon" icon="el-icon-remove-outline" @click="cancel">取消</el-button>
      </span>
    </el-dialog>
    </div>
</template>
<script>
import {
  getCookie
} from '@/utils/util.js';
import {
  cookieUserId,
  cookieUserName
} from '@/config/config.js';
let userName = getCookie(cookieUserName);
let userId = getCookie(cookieUserId);
export default {
  props: ['showFlag', 'Params'],
    data () {
      var minValue = (rule, value, callback) => {
	        if(this.formData.minValue){
	          if(Number(this.formData.minValue) < Number(this.formData.maxValue)){
	            callback();
	          }else{
	            callback(new Error('下限不能大于上限'));
	          }
	        }else{
	          callback();
          }
      };
      return {
        dialogTableVisible: false,
        tableTypeDsc: '新增预警项目',
        tableType: 0,
        formData: {},
        addLoading:false,
        maxSignType: [
          {maxSign: '<='},
          {maxSign: '<'}
        ],
        minSignType: [
          {minSign: '>='},
          {minSign: '>'}
        ],
        monitorRules: {
          maxSign:[{ required: true, message: '该项不能为空', trigger: 'change'}],
          minSign:[{ required: true, message: '该项不能为空', trigger: 'change'}],
          maxValue:[{ required: true, message: '该项不能为空', trigger: 'blur'}],
          minValue:[{ required: true, message: '下限值不能大于上限值', trigger: 'blur',validator: minValue,}],
          alarmValue:[{ required: true, message: '该项不能为空', trigger: 'blur'}]
        }  
      }
    },
    created(){
    this.tableWidth = window.innerHeight/2 - 130
    },
    methods: {
      selectfx(val){
        this.anlyItemNameType = []
        this.testItemNameType.find(item =>{
          if(val === item.testItemNo){
            this.anlyItemNameType = item.lstidList
          }
        })
      },
      cancel () {
        this.formData = {}
        this.dialogTableVisible = false
      },
      addData () {
        this.$refs['monitorForm'].validate((valid) => {
          if (valid) {
            this.addLoading = true
            this.axios.post('pass/baseManagement/v1/limsbasealarmds/insertupdate',this.formData, {individualType: 'json'}).then(res => {
              if (res.code == 0 || res.code == 200) {
                this.dialogTableVisible = false;
                this.$message.success(res.message)
                this.$emit('refresh')
              } else {
                this.$message.error(res.message)
              }
              this.addLoading = false
            }).catch(e => {
              this.addLoading = false
              this.$message.error(e.message)
            })
          }
        })
      },
      closeDialog () {
        this.formData = {}
      },
    },
    watch: {
      showFlag (arg) {
        if (this.Params.id) {
          this.formData = Object.assign({},this.Params);
          this.tableType = 1;
          this.tableTypeDsc = '修改预警项目';
        } else {
          this.formData.alNo =  this.Params.alNo
          this.tableType = 0;
          this.tableTypeDsc = '新增预警项目';
        }
        this.dialogTableVisible = true;
      }
    }
}
</script>
<style lang="less">
.customWidth_analy{
  width: 1000px !important
}
</style>
