<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="24">
            <el-form-item prop="plineName" label="产线描述">
              <el-input v-model="baseData.plineName" size="small"></el-input>
            </el-form-item>
          </el-col>
         </el-row>
         <el-row>
           <el-col :span="24">
            <el-form-item prop="companyNo" label="公司名称">
              <el-select v-model="baseData.companyNo" filterable style="width:100%" collapse-tags @change="selectDepartment" size="small">
                <el-option v-for="item in companyNameType" :key="item.orgCode" :label="item.orgName" :value="item.orgCode">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
         </el-row>
         <el-row>
          <el-col :span="24">
            <el-form-item prop="deptNo" label="部门名称">
             <el-select v-model="baseData.deptNo" filterable style="width:100%" @change="selectClass" collapse-tags size="small" >
                <el-option v-for="item in sectionNameType" :key="item.orgCode" :label="item.orgName" :value="item.orgCode">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
         </el-row>
         <el-row>
          <el-col :span="24">
            <el-form-item  label="班组名称">
             <el-select v-model="baseData.sectionNo" style="width:100%" filterable collapse-tags size="small">
                <el-option v-for="item in sectionClass" :key="item.orgCode" :label="item.orgName" :value="item.orgCode">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
         </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item prop="plineAbbr" label="产线简写">
             <el-input v-model="baseData.plineAbbr" size="small"></el-input>
            </el-form-item>
          </el-col>
         </el-row>
         <el-row>
          <el-col :span="24">
            <el-form-item 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'],
    data () {
        return {
            dialogTableVisible: false,
            tableTypeDsc: '新增产线信息',
            tableType: 0,
            seen: true,
            seenNo: true,
            beInvalid: false,
            companyNameTypeIndex: 0,
            deptNameTypeIndex: 0,
            sectionNameTypeIndex: 0,
            matClassNameType: [],
            companyNameType: [],
            deptNameType: [],
            sectionNameType: [],
            tableData: [],
            sectionClass:[],
            baseData: {
            },
            ruleValidate: {
              //matClassName: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              plineName: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              companyNo: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              deptNo: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              // transportTypeName: [{ required: true, message: '该项不能为空', trigger: 'change' }]
             }
        }
    },
    mounted () {
      //获取公司
  this.axios.post("pass/baseManagement/v1/sysorgs/querySysOrgCompanyAll",{}, {individualType: 'json'}).then(response => {
    this.companyNameType = response.data
  });
    },
    methods: {
      cancel () {
        let _this = this;
        _this.tableData = [];
        _this.baseData = {
        }
        _this.dialogTableVisible = false
      },
      selectTriggerC (val) {
        let _this = this;
        _this.baseData.companyNo = _this.companyNameType[val].companyNo
      },
      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] : '';
      },
    //根据选中的公司，查询部门名称数据
    selectDepartment(val){
      this.sectionNameType = []
      this.companyNameType.find(item =>{
        if(val === item.orgCode){
          this.sectionNameType = item.sysOrgList
        }
      })
    },
    //根据选中的部门名称，查询班组名称
    selectClass(val){
      this.sectionClass = []
      this.sectionNameType.find(item =>{
        if(val === item.orgCode){
          this.sectionClass = item.sysOrgList
        }
      })
    },
      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.baseData.createMan = userName;
             _this.baseData.createNo = userId;
             _this.baseData.companyName = this.getCode(this.baseData.companyNo,this.companyNameType,'orgCode','orgName')
              _this.baseData.deptName = this.getCode(this.baseData.deptNo,this.sectionNameType,'orgCode','orgName')
              _this.baseData.sectionName = this.getCode(this.baseData.sectionNo,this.sectionClass,'orgCode','orgName')
             _this.axios.post('pass/baseManagement/v1/limsbaseplines/', [_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.companyName = this.getCode(this.baseData.companyNo,this.companyNameType,'orgCode','orgName')
        _this.baseData.deptName = this.getCode(this.baseData.deptNo,this.sectionNameType,'orgCode','orgName')
         _this.baseData.sectionName = this.getCode(this.baseData.sectionNo,this.sectionClass,'orgCode','orgName')
        _this.axios.put('pass/baseManagement/v1/limsbaseplines/', _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)
          })
        }
        })
      },
      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 = {
        }
       _this.seen = true;
       _this.seenNo = true;
       _this.beInvalid = false;
     }
    },
    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 = '修改产线信息';
          _this.seen = false;
          _this.beInvalid = true;
          this.selectDepartment(this.baseData.companyNo)
          this.selectClass(this.baseData.deptNo)
        } else {
          _this.tableType = 0;
          _this.seenNo = false;
          _this.tableTypeDsc = '新增产线信息';
        }
      }
    }
}
</script>
<style lang="less">
.customWidth_syst{
  width: 600px !important
}
</style>
