<template>
    <div class="examination">
     <el-dialog @close="closeDialog" :visible.sync="dialogTableVisible" customClass="customWidth_test">
        <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='8'>
              <el-form-item prop="testItemNo" label="测试项目编号">
               <el-input v-model="formData.testItemNo" readonly disabled size="small" style="width: 100%;"></el-input>
               </el-form-item>
            </el-col>
            <el-col :span='8'>
                <el-form-item prop="testPostCode" label="测试岗位">
               <el-select v-model="formData.testPostCode" filterable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="(item,index) in testPostNameType" :key="index" :label="item.postName"
                    :value="item.postNo">
                  </el-option>
                </el-select>
               </el-form-item>
            </el-col>
            <el-col :span='8'>
              <el-form-item prop="isTestTypeIndex" label="是否免检">
               <el-select v-model="formData.isTest" filterable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="(item,index) in isTestType" :key="index" :label="item.isTest"
                    :value="item.id">
                  </el-option>
                </el-select>
               </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span='8'>
              <el-form-item prop="testItemName" label="测试项目名称">
               <el-input v-model="formData.testItemName" size="small" placeholder="请输入"></el-input>
               </el-form-item>
            </el-col>
            <el-col :span='8'>
              <el-form-item prop="eqpTypeCode" label="测试设备">
               <el-select v-model="formData.eqpTypeCode" filterable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="(item,index) in eqpTypeNameType" :key="index" :label="item.baseName"
                    :value="item.baseCode">
                  </el-option>
                </el-select>
               </el-form-item>
            </el-col>
            <el-col :span='8'>
              <el-form-item prop="isCtrlTypeIndex" label="是否控制">
                <el-select v-model="formData.isCtrl" filterable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="(item,index) in isCtrlType" :key="index" :label="item.isCtrl"
                    :value="item.id">
                  </el-option>
                </el-select>
               </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span='8'>
              <el-form-item prop="testItemDesc" label="测试项目描述">
               <el-input v-model="formData.testItemDesc" placeholder="请输入" size="small" style="width: 100%;"></el-input>
               </el-form-item>
            </el-col>
            <el-col :span='8'>
              <el-form-item prop="testCnt" label="测试次数">
               <el-input v-model="formData.testCnt" placeholder="请输入" size="small" style="width: 100%;"></el-input>
               </el-form-item>
            </el-col>
            <el-col :span='8'>
              <el-form-item prop="isAutoTypeIndex" label="是否自动执行">
               <el-select v-model="formData.isAuto" filterable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="(item,index) in isAutoType" :key="index" :label="item.isAuto"
                    :value="item.id">
                  </el-option>
                </el-select>
               </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span='8'>
               <el-form-item prop="testItemEng" label="测试项目英文">
               <el-input v-model="formData.testItemEng" placeholder="请输入" size="small" style="width: 100%;"></el-input>
               </el-form-item>
            </el-col>
            <el-col :span='8'>
              <el-form-item prop="testLimit" label="测试时限(h)">
               <el-input v-model="formData.testLimit" size="small" placeholder="请输入" style="width: 100%;"></el-input>
               </el-form-item>
            </el-col>
            <el-col :span='8'>
              <el-form-item prop="isSonTypeIndex" label="是否子任务">
               <el-select v-model="formData.isSon" filterable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="(item,index) in isSonType" :key="index" :label="item.isSon"
                    :value="item.id">
                  </el-option>
                </el-select>
               </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span='8'>
              <el-form-item prop="itemTypeCode" label="项目类型">
               <el-select v-model="formData.itemTypeCode" filterable collapse-tags @change="selectType" 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='8'>
               <el-form-item prop="doOrder" label="执行顺序">
               <el-input v-model="formData.doOrder" placeholder="请输入" size="small" style="width: 100%;"></el-input>
               </el-form-item>
            </el-col>
            <el-col :span='8'>
              <el-form-item prop="isSynTypeIndex" label="是否同步">
               <el-select v-model="formData.isSyn" filterable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="(item,index) in isSynType" :key="index" :label="item.isSyn"
                    :value="item.id">
                  </el-option>
                </el-select>
               </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span='8'>
              <el-form-item label="作业点">
               <el-select v-model="formData.pointNo" filterable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="(item,index) in pointNameType" :key="index" :label="item.pointName"
                    :value="item.pointNo">
                  </el-option>
                </el-select>
               </el-form-item>
            </el-col>
            <el-col :span='8'>
              <el-form-item prop="testDirection" label="测试样号">
               <el-input v-model="formData.testDirection" placeholder="请输入" size="small" style="width: 100%;"></el-input>
               </el-form-item>
            </el-col>
             <el-col :span='8'>
                <el-form-item prop="isMsgTypeIndex" label="是否推送">
               <el-select v-model="formData.isMsg" filterable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="(item,index) in isMsgType" :key="index" :label="item.isMsg"
                    :value="item.id">
                  </el-option>
                </el-select>
               </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span='8'>
                  <el-form-item prop="functionCode" label="功能类型">
               <el-select v-model="formData.functionCode" filterable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="(item,index) in functionType" :key="index" :label="item.baseName"
                    :value="item.baseCode">
                  </el-option>
                </el-select>
               </el-form-item>
            </el-col>
            <el-col :span='8'>
                  <el-form-item label="业务规则">
               <el-select v-model="formData.ruleNo" clearable filterable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="(item,index) in RuleType" :key="index" :label="item.ruleName"
                    :value="item.ruleNo">
                  </el-option>
                </el-select>
               </el-form-item>
            </el-col>
            <el-col :span='8'>
              <el-form-item prop="isIndexTypeIndex" label="是否含指标">
               <el-select v-model="formData.isIndex" filterable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="(item,index) in isIndexType" :key="index" :label="item.isIndex"
                    :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
           <el-row>
                <el-col :span='16'>
                  <el-form-item prop="alarmNo" label="备注">
                   <el-input v-model="formData.memo" size="small" placeholder="请输入" style="width: 100%;"></el-input>
                   </el-form-item>
                </el-col>
                <el-col :span='8'>
                  <el-form-item prop="isReferTypeIndex" label="是否引用">
               <el-select v-model="formData.isRefer" filterable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="(item,index) in isReferType" :key="index" :label="item.isRefer"
                    :value="item.id">
                  </el-option>
                </el-select>
               </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 () {
        return {
            dialogTableVisible: false,
            tableTypeDsc: '新增测试项目',
            tableType: 0,
            seen: true,
            seenNo: true,
            beInvalid: false,
            testItemName: '',
            testCnt: '1',
            testItemNo: '',
            testItemDesc: '',
            testItemEng: '',
            testLimit: '',
            memo: '',
            itemTemp: '',
            doOrder: '',
            testLocate: '',
            RuleType:[],
            testPostNameTypeIndex: 0,//测试岗位
            isTestTypeIndex: 1,//是否免检
            eqpTypeNameTypeIndex: 0,//测试设备
            isCtrlTypeIndex: 1,//是否控制
            isIndexTypeIndex: 1,//是否含指标
            isAutoTypeIndex: 1,//是否自动执行
            isReferTypeIndex: 1,//是否引用
            isSonTypeIndex: 1,//是否子任务
            itemTypeNameTypeIndex: 0,//项目类型
            isSynTypeIndex: 1,//是否同步
            isMsgTypeIndex: 1,//是否推送
            pointNameTypeIndex: 0,//作业点
            testPostNameType: [],
            isTestType: [
              {id: '0', isTest: '否'},
              {id: '1', isTest: '是'}
            ],
            eqpTypeNameType: [],
            isCtrlType: [
              {id: '0', isCtrl: '否'},
              {id: '1', isCtrl: '是'}
            ],
            isIndexType: [
              {id: '0', isIndex: '否'},
              {id: '1', isIndex: '是'}
            ],
            isAutoType: [
              {id: '0', isAuto: '否'},
              {id: '1', isAuto: '是'}
            ],
            isReferType: [
              {id: '0', isRefer: '否'},
              {id: '1', isRefer: '是'}
            ],
            isSonType: [
              {id: '0', isSon: '否'},
              {id: '1', isSon: '是'}
            ],
            itemTypeNameType: [],
            isSynType: [
              {id: '0', isSyn: '否'},
              {id: '1', isSyn: '是'}
            ],
            isMsgType: [
              {id: '0', isMsg: '否'},
              {id: '1', isMsg: '是'}
            ],
            formData: {
              isTest:'1',
              isCtrl:'1',
              isAuto:'1',
              isSon:'1',
              isSyn:'1',
              isMsg:'1',
              isIndex:'1',
              isRefer:'1',
              pointNo:''
            },
            pointNameType: [],
            functionType:[],
            monitorForm: {
            tableData: [{}],
             monitorRules: {
               testItemName: [{ required: true, message: '该项不能为空', trigger: ['blur']}],
               itemTypeCode:[{ required: true, message: '该项不能为空', trigger: 'change'}],
               testPostCode:[{ required: true, message: '该项不能为空', trigger: 'change'}],
               eqpTypeCode:[{ required: true, message: '该项不能为空', trigger: 'change'}],
               functionCode:[{ required: true, message: '该项不能为空', trigger: 'change'}]
             }
            }
        }
    },
    mounted () {
      this.axios.post("pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode",{validFlag:1,baseCode:4840}, {individualType: 'json'}).then(res => {
        this.functionType = res.data
      })
      this.axios.post("pass/baseManagement/v1/limsbaserules/queryLimsBaseRuleInfoPage",{object:{validFlag:1},pageIndex:1,pageSize:9999}, {individualType: 'json'}).then(res => {
        this.RuleType = res.data.list
      })
      this.gettestPostNameTypeList ()
      this.geteqpTypeNameType ()
      this.getItemTypeNameType ()
      this.getpointNameType ()
    },
    methods: {
      cancel () {
        this.formData = {
              isTest:'1',
              isCtrl:'1',
              isAuto:'1',
              isSon:'1',
              isSyn:'1',
              isMsg:'1',
              isIndex:'1',
              isRefer:'1',
              pointNo:''
            },
        this.monitorForm.tableData = [{}]
        this.dialogTableVisible = false
      },
      gettestPostNameTypeList () {
          let obj ={
        'object':{validFlag:1},
        'pageIndex':1,
        'pageSize':99999
      }
        this.axios.post('pass/baseManagement/v1/limsbaseposts/queryLimsBasePostPage/',obj, {individualType: 'json'}).then(res => {
              this.testPostNameType = res.data.list;
            })
      },
      geteqpTypeNameType () {
        this.axios.post("pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode",{validFlag:1,baseCode:4808}, {individualType: 'json'}).then(res => {
              this.eqpTypeNameType = res.data
        })
      },
      getItemTypeNameType () {
        this.axios.post("pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode",{validFlag:1,baseCode:4804}, {individualType: 'json'}).then(res => {
              this.itemTypeNameType = res.data
            })
      },
      getpointNameType () {
       
      },
      selectType(val){
        this.formData.clearFlag ? '' : this.formData.pointNo = ''
        this.pointNameType = []
        if (!val) {
          return
        }
        let obj ={
          object:{pointTypeCode:val,validFlag:1},
          pageIndex:1,
          pageSize:9999
        }
         this.axios.post('pass/baseManagement/v1/limsbasepoints/queryLimsBasePointPage',obj, {individualType: 'json'}) 
            .then(res => {
              this.pointNameType = 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 () {
         this.$refs['monitorForm'].validate((valid) => {
          if (valid) {
            this.formData.ruleName = this.getCode(this.formData.ruleNo,this.RuleType,'ruleNo','ruleName')
            this.formData.itemTypeName = this.getCode(this.formData.itemTypeCode,this.itemTypeNameType,'baseCode','baseName')
            this.formData.testPostName = this.getCode(this.formData.testPostCode,this.testPostNameType,'postNo','postName')
            this.formData.eqpTypeName = this.getCode(this.formData.eqpTypeCode,this.eqpTypeNameType,'baseCode','baseName')
            this.formData.pointName = this.getCode(this.formData.pointNo,this.pointNameType,'pointNo','pointName')
            this.formData.functionName = this.getCode(this.formData.functionCode,this.functionType,'baseCode','baseName')
            this.axios.post('pass/baseManagement/v1/limsstdtestitems/saveLimsStdTestItem', this.formData, {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 () {
        this.$refs['monitorForm'].validate((valid) => {
        if (valid) {
          this.formData.ruleName = this.getCode(this.formData.ruleNo,this.RuleType,'ruleNo','ruleName')
          this.formData.itemTypeName = this.getCode(this.formData.itemTypeCode,this.itemTypeNameType,'baseCode','baseName')
          this.formData.testPostName = this.getCode(this.formData.testPostCode,this.testPostNameType,'postNo','postName')
          this.formData.eqpTypeName = this.getCode(this.formData.eqpTypeCode,this.eqpTypeNameType,'baseCode','baseName')
          this.formData.pointName = this.getCode(this.formData.pointNo,this.pointNameType,'pointNo','pointName')
          this.formData.functionName = this.getCode(this.formData.functionCode,this.functionType,'baseCode','baseName')
       this.axios.post('pass/baseManagement/v1/limsstdtestitems/saveLimsStdTestItem', this.formData, {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)
          })
        }
        })
      },
      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 () {
       this.formData = {
              isTest:'1',
              isCtrl:'1',
              isAuto:'1',
              isSon:'1',
              isSyn:'1',
              isMsg:'1',
              isIndex:'1',
              isRefer:'1',
              pointNo:''
            },
       this.monitorForm.tableData = [{}];
       this.seen = true;
       this.seenNo = true;
       this.beInvalid = false;
     },
    },
    watch: {
      showFlag (arg) {
        this.dialogTableVisible = true;
        if (this.Params !== null) {
          this.formData = Object.assign({},this.Params[0]);
          this.formData.clearFlag = true
          this.tableType = 1;
          this.tableTypeDsc = '修改测试项目';
          this.selectType(this.formData.itemTypeCode)
          this.formData.clearFlag = false
        } else {
          this.tableType = 0;
          this.seenNo = false;
          this.formData = {
              isTest:'0',
              isCtrl:'1',
              isAuto:'0',
              isSon:'0',
              isSyn:'1',
              isMsg:'0',
              isIndex:'1',
              isRefer:'0',
              pointNo:''
            },
          this.tableTypeDsc = '新增测试项目';
        }
      }
    }
}
</script>
<style lang="less">
.customWidth_test{
  width: 1000px !important
}
</style>
