<template>
    <div class="examination">
     <el-dialog @close="closeDialog" :visible.sync="dialogTableVisible" customClass="customWidth_syst">
        <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="10">
            <el-form-item prop="testItemNo" label="测试编号">
              <el-input v-model="baseData.testItemNo" readonly disabled size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item prop="testItemName" label="测试名称">
              <el-input v-model="baseData.testItemName" readonly disabled size="small"></el-input>
            </el-form-item>
          </el-col>
          </el-row> -->
          <el-row>
          <el-col :span="12">
            <el-form-item prop="testStdNo" label="测试方法编号">
              <el-input v-model="baseData.testStdNo" readonly disabled size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="testItemNo" label="测试名称">
              <el-select v-model="baseData.testItemNo" filterable collapse-tags size="small">
                <el-option v-for="(item,index) in testItemNameType" :key="index" :label="item.testItemName" :value="item.testItemNo">
                </el-option>
              </el-select>
          </el-form-item>
          </el-col>
          </el-row>
          <el-row>
          <el-col :span="12">
            <el-form-item prop="stdName" label="方法名称">
              <el-select v-model="baseData.stdNo" 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-col :span="12">
            <el-form-item prop="dspOrder" label="显示顺序">
              <el-input v-model="baseData.dspOrder"  size="small"></el-input>
            </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" 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,
            testItemNameTypeIndex: 0,
            stdNameType: [],
            testItemNameType: [],
            tableData: [],
            baseData: {
              // testItemNo: '',
              // testItemName: '',
              memo: '',
              testStdNo: '',
              stdDesc: '',
              dspOrder: ''
            },
            ruleValidate: {
              //testStdNo: [{ 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.getstdNameType ()
      this.getTestItemNameTypeList ()
    },
    methods: {
      cancel () {
        this.$refs['baseData'].resetFields();
        this.baseData = {}
        this.tableType = 0;
        this.tableTypeDsc = '新增测试方法';
        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.tableData = [];
         _this.$refs['baseData'].validate((valid) => {
          if (valid) {
             _this.baseData.createMan = userName;
             _this.baseData.createNo = userId;
             _this.baseData.stdName = this.getCode(this.baseData.stdNo,this.stdNameType,'stdNo','stdName')
             _this.baseData.testItemName = this.getCode(this.baseData.testItemNo,this.testItemNameType,'testItemNo','testItemName')
             _this.axios.post('pass/baseManagement/v1/limsstdtestmatchs/', [_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.stdName = this.getCode(this.baseData.stdNo,this.stdNameType,'stdNo','stdName')
        _this.baseData.testItemName = this.getCode(this.baseData.testItemNo,this.testItemNameType,'testItemNo','testItemName')
        _this.axios.put('pass/baseManagement/v1/limsstdtestmatchs/', _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 () {
      this.$refs['baseData'].resetFields();
      this.baseData = {}
      this.tableType = 0;
      this.tableTypeDsc = '新增测试方法';
     },
     getstdNameType () {
         this.axios.post('pass/baseManagement/v1/limsstdinfos/queryLimsStdInfoList',{object:{validFlag:1}, pageIndex:1,pageSize:99999,},{individualType: 'json'})
            .then(res => {
              this.stdNameType = res.data.list;
            })
      },
      getTestItemNameTypeList () {
        this.axios.get('pass/baseManagement/v1/limsstdtestitems/'+ '?validFlag=1')
            .then(res => {
              this.testItemNameType = res.data.list;
            })
      },
    },
    watch: {
      showFlag (arg) {
        let _this = this;
        _this.dialogTableVisible = true;
        if (_this.Params !== null) {
          _this.baseData = Object.assign({},_this.Params[0]) ;
          _this.tableType = 1;
          _this.tableTypeDsc = '修改测试方法';
        } else {
          _this.tableType = 0;
          _this.seenNo = false;
          _this.baseData.testItemNo = _this.Params2[0].testItemNo;
          _this.baseData.testItemName = _this.Params2[0].testItemName;
          // _this.baseData.merchNo = _this.Params.merchNo;
          _this.tableTypeDsc = '新增测试方法';
        }
      }
    }
}
</script>
<style lang="less">
.customWidth_syst{
  width: 600px !important
}
</style>


