<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="monitorForm.monitorRules" ref="monitorForm"> 
        <el-row>
          <el-col :span='6'>
            <el-form-item prop="testItemNo" label="任务名称">
               <el-select v-model="formData.testItemNo" @change="selectfx" filterable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="item in testItemNameType" :key="item.testItemNo" :label="item.testItemName"
                    :value="item.testItemNo">
                  </el-option>
                </el-select>
              </el-form-item>
          </el-col>
          <el-col :span='6'>
            <el-form-item prop="maxSign1" label="上限符1">
                 <el-select v-model="formData.maxSign1" 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='6'>
             <el-form-item prop="maxSign2" label="上限符2">
                 <el-select v-model="formData.maxSign2" 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='6'>
            <el-form-item prop="maxSign3" label="上限符3">
                 <el-select v-model="formData.maxSign3" 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-row>
        <el-row>
          <el-col :span='6'>
            <el-form-item prop="anlyItemNo" label="分析项目">
               <el-select v-model="formData.anlyItemNo" clearable filterable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="item in anlyItemNameType" :key="item.anlyItemNo" :label="item.anlyItemName"
                    :value="item.anlyItemNo">
                  </el-option>
                </el-select>
            </el-form-item>
            
          </el-col>
          <el-col :span='6'>
            <el-form-item  label="上限值1">
               <el-input v-model="formData.maxValue1" style="width:100%" size="small" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span='6'>
            <el-form-item  label="上限值2">
              <el-input v-model="formData.maxValue2" size="small" placeholder="请输入" style="width: 100%;"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span='6'>
            <el-form-item  label="上限值3">
              <el-input v-model="formData.maxValue3" size="small" placeholder="请输入" style="width: 100%;"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span='6'>
            <el-form-item prop='stdNo' label="标准号">
               <el-select v-model="formData.stdNo" clearable filterable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="(item,index) in stdNameType" :key="index" :label="item.stdName"
                    :value="item.stdNo">
                  </el-option>
                </el-select>
              </el-form-item>
          </el-col>
          <el-col :span='6'>
             <el-form-item prop="minSign1" label="下限符1">
                 <el-select v-model="formData.minSign1" 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='6'>
             <el-form-item prop="minSign2" label="下限符2">
                 <el-select v-model="formData.minSign2" 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='6'>
             <el-form-item prop="minSign3" label="下限符3">
                 <el-select v-model="formData.minSign3" 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-row>
        <el-row>
          <el-col :span='6'>
             <el-form-item prop='gradeNo' label="等级/牌号">
               <el-select v-model="formData.gradeNo" clearable filterable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="(item,index) in gradeNameType" :key="index" :label="item.gradeName"
                    :value="item.gradeNo">
                  </el-option>
                </el-select>
              </el-form-item>
          </el-col>
          <el-col :span='6'>
            <el-form-item label="下限值1">
               <el-input v-model="formData.minValue1" size="small" placeholder="请输入" style="width: 100%;"></el-input>
              </el-form-item>
          </el-col>
          <el-col :span='6'>
            <el-form-item  label="下限值2">
              <el-input v-model="formData.minValue2" size="small" placeholder="请输入" style="width: 100%;"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span='6'>
            <el-form-item  label="下限值3">
              <el-input v-model="formData.minValue3" size="small" placeholder="请输入" style="width: 100%;"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span='6'>
            <el-form-item prop="defaultValue" label="默认值">
              <el-input v-model="formData.defaultValue" size="small" placeholder="请输入" style="width: 100%;"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span='6'>
            <el-form-item prop="alarmLevelTypeIndex1" label="预警级别1">
                <el-select v-model="formData.alarmLevel1" clearable filterable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="(item,index) in alarmLevelType" :key="index" :label="item.alarmLevel"
                    :value="item.alarmLevel">
                  </el-option>
                </el-select>
            </el-form-item>
          </el-col>
          <el-col :span='6'>
            <el-form-item prop="alarmLevelTypeIndex2" label="预警级别2">
              <el-select v-model="formData.alarmLevel2" clearable filterable collapse-tags size="small" style="width: 100%;">
                <el-option v-for="(item,index) in alarmLevelType" :key="index" :label="item.alarmLevel"
                  :value="item.alarmLevel">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span='6'>
            <el-form-item prop="alarmLevelTypeIndex3" label="预警级别3">
               <el-select v-model="formData.alarmLevel3" clearable filterable collapse-tags size="small" style="width: 100%;">
                 <el-option v-for="(item,index) in alarmLevelType" :key="index" :label="item.alarmLevel"
                   :value="item.alarmLevel">
                 </el-option>
               </el-select>
             </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span='24'>
            <el-form-item  label="备注">
              <el-input v-model="formData.memo" 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" @click="tableType === 0 ? addData():modifyData()">确定</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 minValue1 = (rule, value, callback) => {
	        if(this.formData.minValue1){
	          if(Number(this.formData.minValue1) < Number(this.formData.maxValue1)){
	            callback();
	          }else{
	            callback(new Error('下限不能大于上限'));
	          }
	        }else{
	          callback();
          }
       };
        var minValue2 = (rule, value, callback) => {
	        if(this.formData.minValue1){
	          if(Number(this.formData.minValue2) < Number(this.formData.maxValue2)){
	            callback();
	          }else{
	            callback(new Error('下限不能大于上限'));
	          }
	        }else{
	          callback();
          }
       };
        var minValue3 = (rule, value, callback) => {
	        if(this.formData.minValue3){
	          if(Number(this.formData.minValue3) < Number(this.formData.maxValue3)){
	            callback();
	          }else{
	            callback(new Error('下限不能大于上限'));
	          }
	        }else{
	          callback();
          }
       };
        return {
            dialogTableVisible: false,
            tableTypeDsc: '新增预警项目',
            tableType: 0,
            seen: true,
            seenNo: true,
            beInvalid: false,
            // maxSignTypeIndex1: 0,
            // maxSignTypeIndex2: 0,
            // maxSignTypeIndex3: 0,
            // minSignTypeIndex1: 0,
            // minSignTypeIndex2: 0,
            // minSignTypeIndex3: 0,
            stdNameTypeIndex: 0,
            gradeNameTypeIndex: 0,
            testItemNameTypeIndex: 0,
            anlyItemNameTypeIndex: 0,
            alarmLevelTypeIndex1: 0,
            alarmLevelTypeIndex2: 0,
            alarmLevelTypeIndex3: 0,
            alarmNo: '',
            formData: {
            maxValue1: '',
            maxValue2: '',
            maxValue3: '',
            minValue1: '',
            minValue2: '',
            minValue3: '',
            },
            defaultValue: '',
            maxSign1: '<=',
            maxSign2: '<=',
            maxSign3: '<=',
            minSign1: '>',
            minSign2: '>',
            minSign3: '>',
            maxSignType: [
              {maxSign: '<='},
              {maxSign: '<'}
            ],
            minSignType: [
              {minSign: '>='},
              {minSign: '>'}
            ],
            stdNameType: [],
            gradeNameType: [],
            testItemNameType: [],
            anlyItemNameType: [],
            alarmLevelType: [
              {alarmLevel: '红'},
              {alarmLevel: '黄'},
              {alarmLevel: '蓝'}
            ],
            monitorForm: {
            tableData: [{}],
            monitorRules: {
              testItemNo:[{ required: true, message: '该项不能为空', trigger: 'change'}],
              anlyItemNo:[{ required: true, message: '该项不能为空', trigger: 'change'}],
              gradeNo:[{ required: true, message: '该项不能为空', trigger: 'change'}],
              stdNo:[{ required: true, message: '该项不能为空', trigger: 'change'}]
            }
            }
        }
    },
    created(){
    this.tableWidth = window.innerHeight/2 - 130
  },
    mounted () {
      this.axios.post('pass/baseManagement/v1/limsstdtestitems/queryLimsStdTestItemInfoAll', {individualType: 'json'}).then(res => {
       this.testItemNameType = res.data
      })
      this.axios.post('pass/baseManagement/v1/limsstdinfos/queryLimsStdInfoList/',{object:{validFlag:1},pageIndex:1,pageSize:9999},{individualType: 'json'}).then(res => {
        this.stdNameType = res.data.list;
      })
      this.axios.post('pass/baseManagement/v1/limsbasegrades/queryLimsBaseGradePage',{object:{grandTypeCode:'',gradeName:''},pageIndex:1,pageSize:99999}, {individualType: 'json'}).then(res => {
        this.gradeNameType = res.data.list;
      })
    },
    methods: {
      selectfx(val){
        console.log(val);
      this.anlyItemNameType = []
      this.testItemNameType.find(item =>{
        if(val === item.testItemNo){
          this.anlyItemNameType = item.lstidList
        }
      })
    },
      cancel () {
        let _this = this;
        _this.monitorForm.tableData = [{}]
        this.formData = {}
        _this.dialogTableVisible = false
      },
      getCode (code, arr, codeKey, nameKey) {
        if (!code) {
          return '';
        }
        let targetRow = arr.find(item => {
          return item[codeKey] === code;
        });
        // console.log(targetRow, 'targetRow');
        return targetRow ? targetRow[nameKey] : '';
    },
      addData () {
        let _this = this;
         _this.$refs['monitorForm'].validate((valid) => {
          if (valid) {
            this.formData.stdName = this.getCode(this.formData.stdNo,this.stdNameType,'stdNo','stdName') 
            this.formData.gradeName = this.getCode(this.formData.gradeNo,this.gradeNameType,'gradeNo','gradeName')
            this.formData.testItemName = this.getCode(this.formData.testItemNo,this.testItemNameType,'testItemNo','testItemName')
            this.formData.anlyItemName = this.getCode(this.formData.anlyItemNo,this.anlyItemNameType,'anlyItemNo','anlyItemName')
             _this.axios.post('pass/baseManagement/v1/limsbaseanlyalarms/saveLimsBaseAnlyAlarm',this.formData, {individualType: 'json'}).then(res => {
                if (res.succeed) {
                  _this.dialogTableVisible = false;
                 this.$message.success('添加成功')
                 _this.$emit('refresh')
                } else {
                 this.$message.error(res.message)
                }
                }).catch(e => {
                 this.$message.error(e.message)
               })
         }
       })
      },
      modifyData () {
        let _this = this;
        _this.$refs['monitorForm'].validate((valid) => {
        if (valid) {
          this.formData.stdName = this.getCode(this.formData.stdNo,this.stdNameType,'stdNo','stdName')
          this.formData.gradeName = this.getCode(this.formData.gradeNo,this.gradeNameType,'gradeNo','gradeName')
          this.formData.testItemName = this.getCode(this.formData.testItemNo,this.testItemNameType,'testItemNo','testItemName')
          this.formData.anlyItemName = this.getCode(this.formData.anlyItemNo,this.anlyItemNameType,'anlyItemNo','anlyItemName')
          _this.axios.post('pass/baseManagement/v1/limsbaseanlyalarms/saveLimsBaseAnlyAlarm', this.formData, {individualType: 'json'}).then(res => {
           if (res.succeed) {
             _this.dialogTableVisible = false;
            this.$message.success('修改成功')
            _this.$emit('refresh')
           } else {
            this.$message.error(res.message)
           }
           }).catch(e => {
            this.$message.error(e.message)
          })
        }
        })
      },
      renderHeader (h) {
         return (
             <div style="cursor: pointer ;line-height:20px" onClick={() => this.addRow()}>
             <i class="el-icon-plus" style="font-size:20px; color:rgb(18, 52, 243)"></i>
             </div>
         )
      },
     closeDialog () {
       let _this = this;
       _this.monitorForm.tableData = [{}];
        this.formData = {}
     },
      tableHeaderCellStyle () {
      return 'background-color: rgb(249,249,249) !important;color: rgb(73, 72, 72);border-bottom: 1px solid #DCDFE7;border-top:3px solid #3ac1ea;'
      }
    },
    watch: {
      showFlag (arg) {
        let _this = this;
        _this.dialogTableVisible = true;
        if (_this.Params !== null) {
          _this.formData = Object.assign({},_this.Params[0]);
          _this.tableType = 1;
          _this.tableTypeDsc = '修改预警项目';
          this.selectfx(this.formData.testItemNo) 
        } else {
          _this.tableType = 0;
          _this.seenNo = false;
          _this.tableTypeDsc = '新增预警项目';
        }
      }
    }
}
</script>
<style lang="less">
.customWidth_analy{
  width: 1000px !important
}
</style>
