<template>
    <div class="examination">
     <el-dialog @close="closeDialog" :visible.sync="dialogTableVisible" customClass="customWidth_anpr">
        <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="90px" :rules="monitorForm.monitorRules"  ref="monitorForm" >
          <el-row>
            <el-col :span='6'>
              <el-form-item prop="anlyItemNo" label="分析项目编号">
               <el-input v-model="formData.anlyItemNo" size="small" readonly disabled style="width: 100%;"></el-input>
               </el-form-item>
            </el-col>
            <el-col :span='6'>
              <el-form-item prop="unitNo" label="度量单位">
               <el-select v-model="formData.unitNo" clearable filterable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="(item,index) in unitNameType" :key="index" :label="item.unitName"
                    :value="item.unitNo" placeholder="请输入">
                  </el-option>
                </el-select>
               </el-form-item>
            </el-col>
            <el-col :span='6'>
              <el-form-item prop="minSignTypeIndex" label="下限符">
               <!-- <el-input v-model="minSign" size="small" readonly disabled style="width: 100%;"></el-input> -->
                <el-select v-model="formData.minSign" 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="isRareTypeIndex" label="是否贵重">
               <el-select v-model="formData.isRare" filterable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="(item,index) in isRareType" :key="index" :label="item.isRare"
                    :value="item.id">
                  </el-option>
                </el-select>
               </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span='6'>
              <el-form-item prop="anlyItemName" label="分析项目名称">
               <el-input v-model="formData.anlyItemName" size="small" style="width: 100%;"></el-input>
             </el-form-item>
            </el-col>
            <el-col :span='6'>
              <el-form-item prop="dataTypeCode" label="数据类型">
               <el-select v-model="formData.dataTypeCode" filterable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="(item,index) in dataTypeNameType" :key="index" :label="item.baseName"
                    :value="item.baseCode">
                  </el-option>
                </el-select>
               </el-form-item>
            </el-col>
            <el-col :span='6'>
              <el-form-item label="下限值">
             <el-input v-model="formData.minValue" size="small" placeholder="请输入" style="width: 100%;"></el-input>
             </el-form-item>
            </el-col>
            <el-col :span='6'>
              <el-form-item prop="isLoginTypeIndex" label="是否必检">
                <el-select v-model="formData.isLogin" filterable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="(item,index) in isLoginType" :key="index" :label="item.isLogin"
                    :value="item.id">
                  </el-option>
                </el-select>
                </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span='6'>
               <el-form-item prop="anlyItemAlias" label="分析项目别名">
               <el-input v-model="formData.anlyItemAlias" size="small" style="width: 100%;" placeholder="请输入"></el-input>
               </el-form-item>
            </el-col>
            <el-col :span='6'>
              <el-form-item prop="outputTypeNameTypeIndex" label="输出类型">
               <el-select v-model="formData.outputTypeCode" filterable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="(item,index) in outputTypeNameType" :key="index" :label="item.baseName"
                    :value="item.baseCode">
                  </el-option>
                </el-select>
               </el-form-item>
            </el-col>
            <el-col :span='6'>
              <el-form-item prop="maxSignTypeIndex" label="上限符">
               <!-- <el-input v-model="maxSign" size="small" readonly disabled style="width: 100%;"></el-input> -->
                <el-select v-model="formData.maxSign" 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="isAlarmTypeIndex" label="是否报警">
               <el-select v-model="formData.isAlarm" filterable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="(item,index) in isAlarmType" :key="index" :label="item.isAlarm"
                    :value="item.id">
                  </el-option>
                </el-select>
               </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span ='6'>
              <el-form-item prop="anlyItemDesc" label="分析项目描述">
               <el-input v-model="formData.anlyItemDesc" size="small" style="width: 100%;" placeholder="请输入"></el-input>
               </el-form-item>
            </el-col>
            <el-col :span ='6'>
              <el-form-item prop="reviseRuleNameTypeIndex" label="修约规则">
               <el-select v-model="formData.reviseRuleNo" filterable clearable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="(item,index) in reviseRuleNameType" :key="index" :label="item.reviseRuleName"
                    :value="item.reviseRuleNo">
                  </el-option>
                </el-select>
               </el-form-item>
            </el-col>
            <el-col :span ='6'>
              <el-form-item label="上限值">
              <el-input v-model="formData.maxValue" size="small" placeholder="请输入" style="width: 100%;"></el-input>
             </el-form-item>
            </el-col>
            <el-col :span ='6'>
              <el-form-item prop="isComputeTypeIndex" label="是否计算">
               <el-select v-model="formData.isCompute" filterable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="(item,index) in isComputeType" :key="index" :label="item.isCompute"
                    :value="item.id">
                  </el-option>
                </el-select>
               </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span='6'>
              <el-form-item prop="anlyItemEng" label="分析项目英文">
               <el-input v-model="formData.anlyItemEng" size="small" style="width: 100%;" placeholder="请输入"></el-input>
               </el-form-item>
            </el-col>
            <el-col :span='6'>
              <el-form-item prop="itemTypeCode" label="项目类型">
               <el-select v-model="formData.itemTypeCode" filterable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="(item,index) in itemTypeNameType" :key="index" :label="item.baseName"
                    :value="item.baseCode">
                  </el-option>
                </el-select>
               </el-form-item>
            </el-col>
            <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="isMidTypeIndex" label="是否中间项">
               <el-select v-model="formData.isMid" filterable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="(item,index) in isMidType" :key="index" :label="item.isMid"
                    :value="item.id">
                  </el-option>
                </el-select>
               </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span='6'>
              <el-form-item prop="dspOrder" label="显示顺序">
               <el-input v-model="formData.dspOrder" size="small" style="width: 100%;" placeholder="请输入"></el-input>
               </el-form-item>
            </el-col>
            <el-col :span='6'>
              <el-form-item prop="formulaNameTypeIndex" label="计算公式">
               <el-select v-model="formData.formulaNo" filterable clearable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="(item,index) in formulaNameType" :key="index" :label="item.formulaName"
                    :value="item.formulaNo">
                  </el-option>
                </el-select>
               </el-form-item>
            </el-col>
            <el-col :span='6'>
              <el-form-item prop="anlyCnt" label="分析次数">
               <el-input v-model="formData.anlyCnt" size="small" style="width: 100%;" placeholder="请输入"></el-input>
               </el-form-item>
            </el-col>
            <el-col :span='6'>
              <el-form-item prop="isReportTypeIndex" label="是否报出">
               <el-select v-model="formData.isReport" filterable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="(item,index) in isReportType" :key="index" :label="item.isReport"
                    :value="item.id">
                  </el-option>
                </el-select>
               </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span='24'>
              <el-form-item prop="memo" 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 maxValueVerify = (rule, value, callback) => {
	        if(this.formData.maxValue){
	          if(Number(this.formData.maxValue) > Number(this.formData.minValue)){
	            callback();
	          }else{
	            callback(new Error('上限不能小于下限'));
	          }
	        }else{
	          callback();
          }
       };
        return {
            dialogTableVisible: false,
            tableTypeDsc: '新增分析项目',
            tableType: 0,
            seen: true,
            seenNo: true,
            beInvalid: false,
            anlyItemNo: '',
            // anlyItemName: '',
            // minValue: '',
            anlyItemAlias: '',
            anlyItemDesc: '',
            // maxValue: '',
            anlyItemEng: '',
            dspOrder: '',
            memo: '',
            anlyCnt: '1',
            defaultValue: '',
            formData:{
              isRare:'0',
              isLogin:'0',
              isAlarm:'0',
              isCompute:'0',
              isReport:'1',
              isMid:'1'
            },
            unitNameTypeIndex: 0,//度量单位
            minSignTypeIndex: 0,//下限符
            isRareTypeIndex: 0,//是否贵重
            dataTypeNameTypeIndex: 0,//数据类型
            isLoginTypeIndex: 0,// 是否必检
            outputTypeNameTypeIndex: 0,//输出类型
            maxSignTypeIndex: 0,//上限符
            isAlarmTypeIndex: 0,//是否报警
            reviseRuleNameTypeIndex: 0,//修约规则
            isComputeTypeIndex: 0,//是否计算
            itemTypeNameTypeIndex: 0,//项目类型
            isReportTypeIndex: 1,//是否报出
            isMidTypeIndex: 1,//是否中间项
            formulaNameTypeIndex: 0,//计算公式
            unitNameType: [],
            // minSign: '>',
            // maxSign: '<=',
            minSignType: [
              {minSign: '>='},
              {minSign: '>'}
            ],
            isRareType: [
              {id: '0', isRare: '否'},
              {id: '1', isRare: '是'}
            ],
            dataTypeNameType: [],
            isLoginType: [
              {id: '0', isLogin: '否'},
              {id: '1', isLogin: '是'}
            ],
            outputTypeNameType: [],
            maxSignType: [
              {maxSign: '<='},
              {maxSign: '<'}
            ],
            formData: {
              anlyItemName: '',
              minValue: '',
              maxValue: '',
            },
            isAlarmType: [
              {id: '0', isAlarm: '否'},
              {id: '1', isAlarm: '是'}
            ],
            reviseRuleNameType: [],
            isComputeType: [
              {id: '0', isCompute: '否'},
              {id: '1', isCompute: '是'}
            ],
            itemTypeNameType: [],
            isReportType: [
              {id: '0', isReport: '否'},
              {id: '1', isReport: '是'}
            ],
            isMidType: [
              {id: '0', isMid: '否'},
              {id: '1', isMid: '是'}
            ],
            formulaNameType: [],
            monitorForm: {
            tableData: [{}],
            monitorRules: {
            anlyItemName: [{ required: true, message: '该项不能为空', trigger: 'blur'}],
            unitNo: [{ required: true, message: '该项不能为空', trigger: 'change'}],
            dataTypeCode: [{ required: true, message: '该项不能为空', trigger: 'change'}],
            itemTypeCode: [{ required: true, message: '该项不能为空', trigger: 'change'}]
            }
            }
        }
    },
    mounted () {
      this.getUnitNameTypeList ()
      this.getDataTypeNameType ()
      this.getOutputTypeNameType ()
      this.getReviseRuleNameType ()
      this.getItemTypeNameType ()
      this.getFormulaNameType ()
    },
    methods: {
      cancel () {
        let _this = this;
        this.formData = {
              isRare:'0',
              isLogin:'0',
              isAlarm:'0',
              isCompute:'0',
              isReport:'1',
              isMid:'1',
              anlyCnt: 1
            },
        _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] : '';
    },
      getUnitNameTypeList () {
        this.axios.post("pass/baseManagement/v1/limsbaseunits/queryLimsBaseUnitPage",{object:{validFlag:1},pageIndex:1,pageSize:9999}, {individualType: 'json'}).then(res => {
          this.unitNameType = res.data.list;
        })
      },
      getDataTypeNameType () {
        this.axios.post("pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode",{validFlag:1,baseCode:4825}, {individualType: 'json'}).then(res => {
              this.dataTypeNameType = res.data
            })
      },
      getOutputTypeNameType () {
        this.axios.post("pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode",{validFlag:1,baseCode:4810}, {individualType: 'json'}).then(res => {
              this.outputTypeNameType = res.data
            })
      },
      getReviseRuleNameType () { 
        this.axios.post('pass/baseManagement/v1/limsbasereviserules/queryLimsBaseReviseRulePage',{object: {reviseRuleName: "",validFlag:1}, pageIndex: 1, pageSize: 9999}, {individualType: 'json'}).then(res => {
          this.reviseRuleNameType = res.data.list;
        }).catch(e => {
                // this.dataLoading = false;
        });
      },
      getItemTypeNameType () {
        this.axios.post("pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode",{validFlag:1,baseCode:4804}, {individualType: 'json'}).then(res => {
              this.itemTypeNameType = res.data
            })
      },
      getFormulaNameType () {
        this.axios.post('pass/baseManagement/v1/limsbaseformulas/queryLimsBaseFormulaPage',{object: {formulaNo: "", formulaName: "",validFlag:1},pageIndex:1,pageSize: 9999},{individualType: 'json'}).then(res => {
          this.formulaNameType = res.data.list;
        })
      },
      open(mag, typ, time) {
        this.$message({
          showClose: true,
          message: mag,
          type: typ,
          duration: time
        });
      },
      addData () {
        let _this = this;
         _this.$refs['monitorForm'].validate((valid) => {
          if (valid) {
             this.formData.unitName = this.getCode(this.formData.unitNo,this.unitNameType,'unitNo','unitName')
             this.formData.dataTypeName = this.getCode(this.formData.dataTypeCode,this.dataTypeNameType,'baseCode','baseName')
             this.formData.outputTypeName = this.getCode(this.formData.outputTypeCode,this.outputTypeNameType,'baseCode','baseName')
             this.formData.reviseRuleName = this.getCode(this.formData.reviseRuleNo,this.reviseRuleNameType,'reviseRuleNo','reviseRuleName')
             this.formData.itemTypeName = this.getCode(this.formData.itemTypeCode,this.itemTypeNameType,'baseCode','baseName')
             this.formData.formulaName = this.getCode(this.formData.formulaNo,this.formulaNameType,'formulaNo','formulaName')
             _this.axios.post('pass/baseManagement/v1/limsstdanlyitems/saveLimsStdAnlyItem', this.formData, {individualType: 'json'}).then(res => {
                if (res.succeed) {
                  this.open('添加成功', 'success', 1200)
                  _this.dialogTableVisible = false;
                 _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.unitName = this.getCode(this.formData.unitNo,this.unitNameType,'unitNo','unitName')
             this.formData.dataTypeName = this.getCode(this.formData.dataTypeCode,this.dataTypeNameType,'baseCode','baseName')
             this.formData.outputTypeName = this.getCode(this.formData.outputTypeCode,this.outputTypeNameType,'baseCode','baseName')
             this.formData.reviseRuleName = this.getCode(this.formData.reviseRuleNo,this.reviseRuleNameType,'reviseRuleNo','reviseRuleName')
             this.formData.itemTypeName = this.getCode(this.formData.itemTypeCode,this.itemTypeNameType,'baseCode','baseName')
             this.formData.formulaName = this.getCode(this.formData.formulaNo,this.formulaNameType,'formulaNo','formulaName')
        _this.axios.post('pass/baseManagement/v1/limsstdanlyitems/saveLimsStdAnlyItem', this.formData, {individualType: 'json'}).then(res => {
           if (res.succeed) {
             this.open('修改成功', 'success', 1200)
             _this.dialogTableVisible = false;
            _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.formData = {
              isRare:'0',
              isLogin:'0',
              isAlarm:'0',
              isCompute:'0',
              isReport:'1',
              isMid:'1',
              anlyCnt: 1
            },
       _this.monitorForm.tableData = [{}];
       _this.seen = true;
       _this.seenNo = true;
       _this.beInvalid = false;
     },
      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.seen = false;
          _this.beInvalid = true;
        } else {
          this.formData = {
              isRare:'0',
              isLogin:'0',
              isAlarm:'0',
              isCompute:'0',
              isReport:'1',
              isMid:'1',
              anlyCnt: 1
            },
          _this.tableType = 0;
          _this.seenNo = false;
          _this.tableTypeDsc = '新增分析项目';
        }
      }
    }
}
</script>
<style lang="less">
.customWidth_anpr{
  width: 1100px !important
}
</style>
