<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='matClassNo' label="物料类型">
                <el-select v-model="formData.matClassNo" filterable @change="selctMatName" collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="(item,index) in matClassNameType" :key="index" :label="item.matClassName"
                    :value="item.matClassNo">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop='alarmTypeCode' label="预警类型">
                <el-select v-model="formData.alarmTypeCode" filterable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="(item,index) in warningType" :key="index" :label="item.baseName"
                    :value="item.baseCode">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item prop='matNo' label="物料名称">
              <el-select v-model="formData.matNo" filterable collapse-tags size="small" style="width: 100%;">
                <el-option v-for="(item,index) in matNameType" :key="index" :label="item.matName"
                  :value="item.matNo">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span='12'>
            <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-row>
        <el-row>
          <el-col :span='12'>
            <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,index) in anlyItemNameType" :key="index" :label="item.anlyItemName"
                    :value="item.anlyItemNo">
                  </el-option>
                </el-select>
            </el-form-item>
          </el-col>
          <el-col :span='12'>
            <el-form-item prop='anlyMethods' label="分析方法">
              <el-input v-model="formData.anlyMethods" size="small" placeholder="请输入" style="width: 100%;"></el-input>
            </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" :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 () {
      return {
        dialogTableVisible: false,
        tableTypeDsc: '新增预警头',
        tableType: 0,
        formData:{matNo:''},
        matNameType:[],
        warningType:[],
        matClassNameType:[],
        stdNameType: [],
        addLoading:false,
        anlyItemNameType: [],
        monitorRules: {
          matClassNo:[{ required: true, message: '该项不能为空', trigger: 'change'}],
          alarmTypeCode:[{ required: true, message: '该项不能为空', trigger: 'change'}],
          matNo:[{ required: true, message: '该项不能为空', trigger: 'change'}],
          stdNo:[{ required: true, message: '该项不能为空', trigger: 'change'}],
          anlyItemNo:[{ required: true, message: '该项不能为空', trigger: 'change'}],
          anlyMethods:[{ required: true, message: '该项不能为空', trigger: 'blur'}]
        }
      }
    },
    created(){
      this.tableWidth = window.innerHeight/2 - 130
    },
    mounted () {
      //物料类型
      this.axios.get("pass/baseManagement/v1/limsbasematerialsclasss/" + "?validFlag=1").then((res) => {
        this.matClassNameType = res.data.list;
      });
      // 预警类型
      this.axios.post("pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode",{validFlag:1,baseCode:4846}, {individualType: 'json'}).then(res => {
        this.warningType = 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/limsstdanlyitems/queryLimsStdAnlyItemPage',{object:{anlyItemNo:'',validFlag:1},pageIndex:1,pageSize:99999}, {individualType: 'json'}).then(res => {
        this.anlyItemNameType = res.data.list;
      })
    },
    methods: {
      selctMatName(val){
        this.matNameType = []
        this.formData.clearFlag ? '' : this.formData.matNo = ''
        if (!val) {
          return 
        }
        //物料名称
        this.axios.post('pass/baseManagement/v1/limsbasematerialss/queryLimsBaseMaterialsPage/',{object:{validFlag:1,matClassNo:val},pageIndex:1,pageSize:99999},{individualType: 'json'}).then(res => {
          this.matNameType = res.data.list;
        })
      },
      cancel () {
        this.formData = {matNo:''}
        this.dialogTableVisible = false
        this.addLoading = 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 () {
        this.$refs['monitorForm'].validate((valid) => {
          if (valid) {
            this.addLoading = true
            this.formData.matClassName = this.getCode(this.formData.matClassNo,this.stdNameType,'matClassNo','matClassName')
            this.formData.alarmTypeName = this.getCode(this.formData.alarmTypeCode,this.warningType,'baseCode','baseName')
            this.formData.matName = this.getCode(this.formData.matNo,this.matNameType,'matNo','matName')
            this.formData.stdName = this.getCode(this.formData.stdNo,this.stdNameType,'stdNo','stdName')
            this.formData.anlyItemName = this.getCode(this.formData.anlyItemNo,this.anlyItemNameType,'anlyItemNo','anlyItemName')
            this.axios.post('pass/baseManagement/v1/limsbasealarms/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.addLoading = false
        this.formData = {matNo:''}
      },
    },
    watch: {
      showFlag (arg) {
        if (this.Params !== null) {
          this.formData = Object.assign({},this.Params);
          this.formData.clearFlag = true
          this.tableType = 1;
          this.tableTypeDsc = '修改预警头';
          this.selctMatName(this.formData.matClassNo) 
          this.formData.clearFlag = false
        } else {
          this.tableType = 0;
          this.tableTypeDsc = '新增预警头';
        }
        this.dialogTableVisible = true;
      }
    }
}
</script>
<style lang="less">
</style>
