<template>
    <div class="examination">
     <el-dialog @close="closeDialog" :visible.sync="dialogTableVisible">
        <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 ref="baseData" label-width="80px" :rules="ruleValidate" :model="baseData">
         <el-row>
          <el-col :span="8">
            <el-form-item prop="mtName" label="对照名称">
              <el-input v-model="baseData.mtName" style="width:100%" size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
             <el-form-item prop="mtType" label="对照类型">
              <el-select v-model="mtTypeTypeIndex" style="width:100%" filterable @change="selectData" collapse-tags size="small">
                <el-option v-for="(item,index) in mtTypeType" :key="index" :label="item.mtType" :value="index">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
             <el-form-item prop="dataSourceName" label="对照来源">
              <el-select v-model="baseData.dataSourceCode" style="width:100%" filterable collapse-tags size="small">
                <el-option v-for="item in dataSourceNameType" :key="item.baseCode" :label="item.baseName" :value="item.baseCode">
                </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="baseData.memo" style="width:100%" size="small"></el-input>
            </el-form-item>
          </el-col>
         </el-row>
       </el-form>
       <span slot="footer" class="dialog-footer">
        <el-button type="goon" size="mini" icon="el-icon-check" @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 () {
        return {
            dialogTableVisible: false,
            tableTypeDsc: '新增项目对照信息',
            tableType: 0,
            seen: true,
            seenNo: true,
            beInvalid: false,
            anlyItemNameTypeIndex: 0,
            mtTItemNameTypeIndex: 0,
            mtAItemNameTypeIndex: 0,
            mtTypeTypeIndex: 0,
            dataSourceNameTypeIndex: '',
            anlyItemNameType: [],
            mtTItemNameType: [],
            mtAItemNameType: [],
            mtTypeType: [
              {id: 0, mtType: '系统接口'},
              {id: 1, mtType: '设备接口'}
            ],
            dataSourceNameType: [],
            tableData: [
            ],
            baseData: {
              mtNo: '',
              mtName: '',
              imdmodel: {
                testItemNo: '',
                testItemName: '',
                anlyItemNo: '',
                anlyItemName: '',
                mtTItemNo: '',
                mtTItemName: '',
                mtAItemNo: '',
                mtAItemName: ''
              }
            },
            ruleValidate: {
              // mtAItemName: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              mtName: [{ required: true, message: '该项不能为空', trigger: 'change' }]
              // merchAtrrName: [{ required: true, message: '该项不能为空', trigger: 'change' }]
             }
        }
    },
    mounted () {
      this.selectData(this.mtTypeTypeIndex)
      this.getDataSourceName()
      this.getAnlyItemNameTypeList();
    },
    methods: {
      cancel () {
        let _this = this;
        _this.tableData = [];
        _this.anlyItemNameTypeIndex = 0,
        _this.mtTItemNameTypeIndex = 0,
        _this.mtAItemNameTypeIndex = 0,
        _this.mtTypeTypeIndex = 0,
        _this.dataSourceNameTypeIndex = '',
        _this.baseData = {
        }
        _this.dialogTableVisible = false
      },
      selectData (val) {
        let code = val == 0 ? 4807 : val == 1 ? 4808 : ''
        if (!code) {
          return
        }
       this.axios.post('pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode', {validFlag: 1, baseCode: code}, {individualType: 'json'}).then(response => {
        this.dataSourceNameType = response.data
        });
      },
       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.tableData = [];
         _this.$refs['baseData'].validate((valid) => {
          if (valid) {
             _this.baseData.createMan = userName;
             _this.baseData.createNo = userId;
             _this.baseData.mtType = _this.mtTypeTypeIndex;
             _this.baseData.dataSourceName = this.getCode(_this.baseData.dataSourceCode, this.dataSourceNameType, 'baseCode', 'baseName')
             _this.axios.post('pass/baseManagement/v1/limsbaseitemmatchs/', [_this.baseData], {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.tableData = [];
        _this.$refs['baseData'].validate((valid) => {
        if (valid) {
        _this.baseData.updateMan = userName;
        _this.baseData.updateNo = userId;
        _this.baseData.mtType = _this.mtTypeTypeIndex;
        _this.baseData.dataSourceName = this.getCode(_this.baseData.dataSourceCode, this.dataSourceNameType, 'baseCode', 'baseName')
        _this.tableData.push(_this.baseData)
        _this.axios.put('pass/baseManagement/v1/limsbaseitemmatchs/', _this.tableData[0], {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)
          })
        }
        })
      },
     closeDialog () {
       let _this = this;
       _this.tableData = [];
       _this.anlyItemNameTypeIndex = 0,
       _this.mtTItemNameTypeIndex = 0,
       _this.mtAItemNameTypeIndex = 0,
       _this.mtTypeTypeIndex = 0,
       _this.dataSourceNameTypeIndex = 0,
       _this.baseData = {
       }
       _this.seen = true;
       _this.seenNo = true;
       _this.beInvalid = false;
     }
    },
    watch: {
      showFlag (arg) {
        let _this = this;
        _this.dialogTableVisible = true;
        if (_this.Params !== null) {
          _this.baseData = _this.Params[0];
          _this.tableType = 1;
          _this.tableTypeDsc = '修改项目对照信息';
          _this.seen = false;
          _this.beInvalid = true;
          _this.mtTypeTypeIndex = Number(_this.Params[0].mtType);
          this.selectData(this.mtTypeTypeIndex)
        } else {
          _this.tableType = 0;
          _this.seenNo = false;
          _this.tableTypeDsc = '新增项目对照信息';
        }
      }
    }
}
</script>
<style lang="less">
.customWidth_syst{
  width: 600px !important
}
</style>
