<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="90px" :rules="ruleValidate" :model="baseData">
         <el-row>
          <el-col :span="12">
            <el-form-item prop="testItemName" label="测试名称">
              <el-input v-model="baseData.testItemName" style="width:100%" readonly disabled size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="stdUsedCode" label="标准用途类型">
              <el-select v-model="baseData.stdUsedCode" @change='selectStdname' style="width:100%" filterable collapse-tags size="small">
                <el-option v-for="(item,index) in stdUsedType" :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="dspOrder" label="显示顺序">
                <el-input v-model="baseData.dspOrder" style="width:100%"  size="small"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="stdName" label="方法名称">
                <el-select v-model="baseData.stdNo" clearable style="width:100%" filterable collapse-tags size="small">
                  <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="24">
            <el-form-item prop="memo" 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', 'Params2'],
    data () {
        return {
            dialogTableVisible: false,
            tableTypeDsc: '新增测试方法',
            tableType: 0,
            seen: true,
            seenNo: true,
            beInvalid: false,
            stdNameTypeIndex: 0,
            stdNameType: [],
            stdUsedType:[],
            tableData: [],
            baseData: {
              testItemNo: '',
              testItemName: '',
              memo: '',
              testStdNo: '',
              stdDesc: '',
              dspOrder: ''
            },
            ruleValidate: {
              stdUsedCode: [{ required: true, message: '该项不能为空', trigger: 'change' }]
              //dataTypeName: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              //testItemNo: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              // dataTypeName: [{ required: true, message: '该项不能为空', trigger: 'change' }]
             }
        }
    },
    mounted () {
      this.axios.post("pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode",{validFlag:1,baseCode:4805}, {individualType: 'json'}).then(response => {
        this.stdUsedType = response.data
      });
    },
    methods: {
      cancel () {
        let _this = this;
        _this.baseData = {}
        _this.dialogTableVisible = false
      },
      selectStdname(val){
        this.axios.post('pass/baseManagement/v1/limsstdinfos/queryLimsStdInfoList',{object:{validFlag:1,stdUsedCode:val}, pageIndex:1,pageSize:99999,},{individualType: 'json'}).then(res => {
          this.stdNameType = res.data.list;
        })
      },
      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.stdUsedName = this.getCode(this.baseData.stdUsedCode,this.stdUsedType,'baseCode','baseName')
            this.baseData.stdName = this.getCode(this.baseData.stdNo,this.stdNameType,'stdNo','stdName')
            _this.axios.post('pass/baseManagement/v1/limsstdtestmatchs/saveLimsStdTestMatch', 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.stdUsedName = this.getCode(this.baseData.stdUsedCode,this.stdUsedType,'baseCode','baseName')
          this.baseData.stdName = this.getCode(this.baseData.stdNo,this.stdNameType,'stdNo','stdName')
          _this.axios.post('pass/baseManagement/v1/limsstdtestmatchs/saveLimsStdTestMatch', _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)
          })
        }
        })
      },
     closeDialog () {
       let _this = this;
       _this.baseData = {}
       _this.seen = true;
       _this.seenNo = true;
       _this.beInvalid = false;
     },
    },
    watch: {
      showFlag (arg) {
        let _this = this;
        if (_this.Params !== null) {
          _this.baseData = Object.assign({},_this.Params[0]);
          this.selectStdname(this.baseData.stdUsedCode)
          _this.tableType = 1;
          _this.tableTypeDsc = '修改测试方法';
          _this.seen = false;
          _this.beInvalid = true;
        } else {
          _this.tableType = 0;
          _this.seenNo = false;
          _this.baseData = Object.assign({},{testItemNo:this.Params2[0].testItemNo,testItemName:this.Params2[0].testItemName});
          // _this.baseData.merchNo = _this.Params.merchNo;
          _this.tableTypeDsc = '新增测试方法';
        }
        this.dialogTableVisible = true;
      }
    }
}
</script>
<style lang="less">
.customWidth_syst{
  width: 600px !important
}
</style>

