<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="20">
            <el-form-item prop="pointNo" label="化验室代码">
              <el-input v-model="baseData.pointNo" readonly disabled size="small"></el-input>
            </el-form-item>
          </el-col>
         </el-row>
         <el-row>
           <el-col :span="10">
            <el-form-item prop="pointName" label="化验室名称">
              <el-input v-model="baseData.pointName" size="small"></el-input>
            </el-form-item>
          </el-col>
           <el-col :span="10">
            <el-form-item prop="matClassName" label="物料类型">
              <el-select v-model="matClassNameTypeIndex" filterable collapse-tags size="small">
                <el-option v-for="(item,index) in matClassNameType" :key="index" :label="item.matClassName" :value="index">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="10">
            <el-form-item prop="transportTypeName" label="运输类型">
             <el-select v-model="transportTypeNameTypeIndex" filterable collapse-tags size="small">
                <el-option v-for="(item,index) in transportTypeNameType" :key="index" :label="item.transportTypeName" :value="index">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col> -->
         </el-row>
         <el-row>
          <el-col :span="10">
            <el-form-item prop="companyNo" label="公司代码">
              <el-input v-model="baseData.companyNo"  readonly disabled size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item prop="companyName" label="公司名称">
             <el-select v-model="companyNameTypeIndex" filterable collapse-tags size="small" @change="selectTriggerC(companyNameTypeIndex)">
                <el-option v-for="(item,index) in companyNameType" :key="index" :label="item.companyName" :value="index">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
         </el-row>
         <el-row>
          <el-col :span="10">
            <el-form-item prop="sectionNo" label="厂区代码">
              <el-input v-model="baseData.sectionNo"  readonly disabled size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item prop="sectionName" label="厂区名称">
             <el-select v-model="sectionNameTypeIndex" filterable collapse-tags size="small" @change="selectTriggerS(sectionNameTypeIndex)">
                <el-option v-for="(item,index) in sectionNameType" :key="index" :label="item.sectionName" :value="index">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
         </el-row>
         <el-row>
          <el-col :span="10">
            <el-form-item prop="deptNo" label="部门代码">
              <el-input v-model="baseData.deptNo"  readonly disabled size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item prop="deptName" label="部门名称">
             <el-select v-model="deptNameTypeIndex" filterable collapse-tags size="small" @change="selectTriggerD(deptNameTypeIndex)">
                <el-option v-for="(item,index) in deptNameType" :key="index" :label="item.deptName" :value="index">
                </el-option>
              </el-select>
            </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,
            matClassNameTypeIndex: 0,
            companyNameTypeIndex: 0,
            deptNameTypeIndex: 0,
            sectionNameTypeIndex: 0,
            transportTypeNameTypeIndex: 0,
            matClassNameType: [],
            companyNameType: [
              {companyNo: '483801', companyName: '公司名称1'},
              {companyNo: '483802', companyName: '公司名称2'},
              {companyNo: '483803', companyName: '公司名称3'}
            ],
            deptNameType: [
              {deptNo: '483801', deptName: '部门名称1'},
              {deptNo: '483802', deptName: '部门名称2'},
              {deptNo: '483803', deptName: '部门名称3'}
            ],
            sectionNameType: [
              {sectionNo: '483801', sectionName: '厂区名称1'},
              {sectionNo: '483802', sectionName: '厂区名称2'},
              {sectionNo: '483803', sectionName: '厂区名称3'}
            ],
            transportTypeNameType: [
              {transportTypeCode: '483801', transportTypeName: '运输类型1'},
              {transportTypeCode: '483802', transportTypeName: '运输类型2'},
              {transportTypeCode: '483803', transportTypeName: '运输类型3'}
            ],
            tableData: [],
            baseData: {
              pointNo: '',
              pointName: '',
              deptNo: '483801',
              sectionNo: '483801',
              companyNo: '483801',
            },
            ruleValidate: {
              //matClassName: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              pointName: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              // transportTypeName: [{ required: true, message: '该项不能为空', trigger: 'change' }]
             }
        }
    },
    mounted () {
      this.getmatClassNameType ()
    },
    methods: {
      cancel () {
        let _this = this;
        _this.tableData = [];
        _this.matClassNameTypeIndex = 0,
        _this.transportTypeNameTypeIndex = 0,
        _this.baseData = {
              pointNo: '',
              pointName: '',
              deptNo: '483801',
              sectionNo: '483801',
              companyNo: '483801',
        }
        _this.dialogTableVisible = false
        _this.$emit('refresh')
      },
      selectTriggerC (val) {
        let _this = this;
        _this.baseData.companyNo = _this.companyNameType[val].companyNo
      },
      selectTriggerD (val) {
        let _this = this;
        _this.baseData.deptNo = _this.deptNameType[val].deptNo
      },
      selectTriggerS (val) {
        let _this = this;
        _this.baseData.sectionNo = _this.sectionNameType[val].sectionNo
      },
      addData () {
        let _this = this;
         _this.tableData = [];
         _this.$refs['baseData'].validate((valid) => {
          if (valid) {
             _this.dialogTableVisible = false;
             _this.baseData.createMan = userName;
             _this.baseData.createNo = userId;
             _this.baseData.pointTypeCode = 480403;
             _this.baseData.pointTypeName = '化验室';
             _this.baseData.companyName = _this.companyNameType[_this.companyNameTypeIndex].companyName;
             _this.baseData.companyNo = _this.companyNameType[_this.companyNameTypeIndex].companyNo;
             _this.baseData.deptName = _this.deptNameType[_this.deptNameTypeIndex].deptName;
             _this.baseData.deptNo = _this.deptNameType[_this.deptNameTypeIndex].deptNo;
             _this.baseData.sectionName = _this.sectionNameType[_this.sectionNameTypeIndex].sectionName;
             _this.baseData.sectionNo = _this.sectionNameType[_this.sectionNameTypeIndex].sectionNo;
             _this.baseData.matClassNo = _this.matClassNameType[_this.matClassNameTypeIndex].matClassNo;
             _this.baseData.matClassName = _this.matClassNameType[_this.matClassNameTypeIndex].matClassName;
            //  _this.baseData.transportTypeCode = _this.transportTypeNameType[_this.transportTypeNameTypeIndex].transportTypeCode;
            //  _this.baseData.transportTypeName = _this.transportTypeNameType[_this.transportTypeNameTypeIndex].transportTypeName;
             _this.tableData.push(_this.baseData)
             _this.axios.post('pass/baseManagement/v1/limsbasepoints/', _this.tableData, {individualType: 'json'}).then(res => {
                if (res.succeed) {
                 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.dialogTableVisible = false;
        _this.baseData.updateMan = userName;
        _this.baseData.updateNo = userId;
        _this.baseData.companyName = _this.companyNameType[_this.companyNameTypeIndex].companyName;
        _this.baseData.companyNo = _this.companyNameType[_this.companyNameTypeIndex].companyNo;
        _this.baseData.deptName = _this.deptNameType[_this.deptNameTypeIndex].deptName;
        _this.baseData.deptNo = _this.deptNameType[_this.deptNameTypeIndex].deptNo;
        _this.baseData.sectionName = _this.sectionNameType[_this.sectionNameTypeIndex].sectionName;
        _this.baseData.sectionNo = _this.sectionNameType[_this.sectionNameTypeIndex].sectionNo;
        _this.baseData.matClassNo = _this.matClassNameType[_this.matClassNameTypeIndex].matClassNo;
        _this.baseData.matClassName = _this.matClassNameType[_this.matClassNameTypeIndex].matClassName;
        // _this.baseData.transportTypeCode = _this.transportTypeNameType[_this.transportTypeNameTypeIndex].transportTypeCode;
        // _this.baseData.transportTypeName = _this.transportTypeNameType[_this.transportTypeNameTypeIndex].transportTypeName;
        _this.tableData.push(_this.baseData)
        _this.axios.put('pass/baseManagement/v1/limsbasepoints/', _this.baseData, {individualType: 'json'}).then(res => {
           if (res.succeed) {
            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 () {
       let _this = this;
       _this.tableData = [];
       _this.baseData = {
              pointNo: '',
              pointName: '',
              deptNo: '483801',
              sectionNo: '483801',
              companyNo: '483801',
        }
       _this.seen = true;
       _this.seenNo = true;
       _this.beInvalid = false;
       _this.$emit('refresh')
     },
      getmatClassNameType () {
        this.axios.get('pass/baseManagement/v1/limsbasematerialsclasss/'+ '?validFlag=1')
            .then(res => {
              this. matClassNameType = res.data.list;
            })
     },
      tableHeaderCellStyle () {
      return 'background-color: rgb(249,249,249) !important;color: rgb(73, 72, 72);border-bottom: 1px solid #DCDFE7;border-top:3px solid #3ac1ea;'
      }
    },
    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;
          // for (let j = 0; j < _this.transportTypeNameType.length; j++) {
          // if (_this.transportTypeNameType[j].transportTypeName === _this.baseData.transportTypeName) {
          //    _this.transportTypeNameTypeIndex = j;
          // }
          // }
          for (let j = 0; j < _this.matClassNameType.length; j++) {
          if (_this.matClassNameType[j].matClassName === _this.baseData.matClassName) {
             _this.matClassNameTypeIndex = j;
          }
          }
          for (let j = 0; j < _this.companyNameType.length; j++) {
          if (_this.companyNameType[j].companyName === _this.baseData.companyName) {
             _this.companyNameTypeIndex = j;
          }
          }
          for (let j = 0; j < _this.deptNameType.length; j++) {
          if (_this.deptNameType[j].deptName === _this.baseData.deptName) {
             _this.deptNameTypeIndex = j;
          }
          }
          for (let j = 0; j < _this.sectionNameType.length; j++) {
          if (_this.sectionNameType[j].sectionName === _this.baseData.sectionName) {
             _this.sectionNameTypeIndex = j;
          }
          }
        } else {
          _this.tableType = 0;
          _this.seenNo = false;
          _this.tableTypeDsc = '新增化验室';
        }
      }
    }
}
</script>
<style lang="less">
.customWidth_syst{
  width: 600px !important
}
</style>
