<!-- 新增班组维护弹窗 -->
<template>
  <div class="resource">
    <el-dialog @close="closeDialog" :visible.sync="dialogTableVisible">
      <div slot="title" class="titleBox">
        <i
          :class="formType === 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">{{
          formTypeDsc
        }}</span>
      </div>
      <!-- 表单 -->
      <el-form ref="formData" label-width="65px" size="mini" :rules="ruleValidate" :model="formData">
        <el-row>
          <!-- <el-col :span="8" >
            <el-form-item label="职工编号" prop="empNo">
              <el-input v-model="formData.empNo" style="width:100%" :disabled="true"></el-input>
            </el-form-item>
          </el-col> -->
          <!-- <el-col :span="8" >
            <el-form-item label="职工姓名" prop="empName" style="white-space:nowrap">
              <el-input v-model="formData.empName" style="width:100%" :disabled="true"></el-input>
            </el-form-item>
          </el-col> -->
          <el-col :span="8" >
            <el-form-item label="班值" prop="wrkGrpCode">
              <el-select v-model="formData.wrkGrpCode" filterable style="width:100%" clearable placeholder="请选择">
                <el-option
                v-for="(item, index) in wrkGrpNameList"
                :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="岗位" prop="postNo">
              <el-select v-model="formData.postNo" filterable style="width:100%" clearable placeholder="请选择">
                  <el-option
                  v-for="(item, index) in usePostList"
                  :key="index"
                  :label="item.postName"
                  :value="item.postNo">
                  </el-option>
              </el-select>
            </el-form-item>
          </el-col>    
        </el-row>
        <el-row>
          <el-col :span="8" >
            <el-form-item label="公司名称" prop="companyNo">
              <el-select v-model="formData.companyNo" filterable style="width:100%" collapse-tags @change="selectDepartment(formData.companyNo,1)" 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-col :span="8" >
            <el-form-item prop="deptNo" label="部门名称">
              <el-select v-model="formData.deptNo" filterable clearable style="width:100%" @change="selectClass(formData.deptNo,1)" 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-col :span="8">
            <el-form-item label="班组名称">
             <el-select v-model="formData.sectionNo" clearable  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 style="width: 100%;" label="备注" prop="memo">
              <el-input size="mini" v-model="formData.memo"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form> 
      <span slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          icon="el-icon-check"
          size="mini"
          @click="formType === 0 ? addData() : modifyData()"
          >确定</el-button
        >
        <el-button
          size="mini"
          type="primary"
          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: ["showFlag7", "Params", "Params7"],
  data() {
    return {
      dialogTableVisible: false,
      formTypeDsc: "修改班组维护信息",
      formType: 0,
      seen: true,
      seenNo: true,
      beInvalid: false,
      empNos: [],
      empNames: [],
      formData: {
        empNo: "",
        deptNo: "",
        empName: "",
        wrkGrpName: "",
        grpId: "",
        companyNo: "",
        companyName: "",
        sectionNo: "",
        sectionName: "",
        memo: "",
      },
      ruleValidate: {
        companyNo: [{ required: true, message: '该项不能为空', trigger: 'change' }],
        deptNo: [{ required: true, message: '该项不能为空', trigger: 'change' }],
        wrkGrpCode: [{ required: true, message: '该项不能为空', trigger: 'change' }]
      },
      wrkGrpNameList: [], //班组
      usePostList: [],//岗位
      companyNameType:[],
      sectionNameType:[],
      sectionClass:[]
    };
  },
  mounted() {
    this.axios.post('pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode',{validFlag:1,baseCode:4836}, {individualType: 'json'}).then((res) => {
      this.wrkGrpNameList = res.data;
    });
    //获取公司
    this.axios.post("pass/baseManagement/v1/sysorgs/querySysOrgCompanyAll",{}, {individualType: 'json'}).then(response => {
      this.companyNameType = response.data
    });
    let obj ={
      'object':{validFlag:1},
      'pageIndex':1,
      'pageSize':99999
    }
    this.axios.post('pass/baseManagement/v1/limsbaseposts/queryLimsBasePostPage/',obj, {individualType: 'json'}).then(res => {
      this.usePostList = res.data.list;
    })
  },
  methods: {
    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,index){
      this.sectionNameType = []
      this.companyNameType.find(item =>{
        if(val === item.orgCode){
          this.sectionNameType = item.sysOrgList
          if (index === 1) {
            this.formData.deptNo = item.sysOrgList.length > 0 ? item.sysOrgList[0].orgCode : ''
            this.selectClass(this.formData.deptNo, 1)
          }
        }
      })
    },
    //根据选中的部门名称，查询班组名称
    selectClass(val,index){
      this.sectionClass = []
      this.sectionNameType.find(item =>{
        if(val === item.orgCode){
          this.sectionClass = item.sysOrgList
          if (index === 1) {
            this.formData.sectionNo = item.sysOrgList.length > 0 ? item.sysOrgList[0].orgCode : ''
          }
        }
      })
    },
    cancel() {
      this.formData = {};
      this.dialogTableVisible = false;
    },
    clearList(){
      this.empNos = []
      this.empNames = []
    },
    async addData(){
        this.formData.createMan = userName;
        this.formData.createNo = userId;
        let flag = false;
        for(let i = 0;i < this.empNos.length;i++){
          this.formData.empNo = this.empNos[i];
          this.formData.empName = this.empNames[i];
          const cfs = await this.axios.post('pass/baseManagement/v1/limslrcempwrkgrps/', this.formData, { individualType: 'json' });
          if(cfs.succeed){
            flag = true;
          }else{
            flag = false;
            this.$message.error(res.message)
          }
        }
        if(flag){
          this.dialogTableVisible = false;
          this.$message.success('添加成功')
          this.$emit('refresh')
        }
        this.clearList();
      },
    // addData() {
    //   this.$refs["formData"].validate((valid) => {
    //     if (valid) {
    //       this.formData.createMan = userName;
    //       this.formData.createNo = userId;
    //       this.formData.wrkGrpName = this.getCode(this.formData.wrkGrpCode,this.wrkGrpNameList,'baseCode','baseName')
    //       this.formData.companyName = this.getCode(this.formData.companyNo,this.companyNameType,'orgCode','orgName')
    //       this.formData.deptName = this.getCode(this.formData.deptNo,this.sectionNameType,'orgCode','orgName')
    //       this.formData.sectionName = this.getCode(this.formData.sectionNo,this.sectionClass,'orgCode','orgName')
    //       this.formData.postName = this.getCode(this.formData.postNo,this.usePostList,'postNo','postName')
    //       this.axios.post("pass/baseManagement/v1/limslrcempwrkgrps/", 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["formData"].validate((valid) => {
        if (valid) {
          this.formData.wrkGrpName = this.getCode(this.formData.wrkGrpCode,this.wrkGrpNameList,'baseCode','baseName')
          this.formData.companyName = this.getCode(this.formData.companyNo,this.companyNameType,'orgCode','orgName')
          this.formData.deptName = this.getCode(this.formData.deptNo,this.sectionNameType,'orgCode','orgName')
          this.formData.sectionName = this.getCode(this.formData.sectionNo,this.sectionClass,'orgCode','orgName')
          this.formData.postName = this.getCode(this.formData.postNo,this.usePostList,'postNo','postName')
          this.axios.put("pass/baseManagement/v1/limslrcempwrkgrps/", 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);
            });
        }
      });
    },
    closeDialog() {
      this.formData = {};
      this.seen = true;
      this.seenNo = true;
      this.beInvalid = false;
    },
  },
  watch: {
    showFlag7(arg) {
      if (this.Params7 !== null) {
        this.formData = Object.assign({},this.Params7)
        this.selectDepartment(this.formData.companyNo, 0)
        this.selectClass(this.formData.deptNo, 0)
        this.formType = 1;
        this.formTypeDsc = "修改班组维护信息";
        this.beInvalid = true;
        this.seen = false;
      } else {
        this.formType = 0;
        this.seenNo = false;
        console.log(this.Params.length)
        for(let i = 0;i < this.Params.length; i++){
            this.empNos.push(this.Params[i].empNo);
            this.empNames.push(this.Params[i].empName);
          }
        // this.formData.empNo = this.Params.empNo;
        // this.formData.empName = this.Params.empName;
        this.formTypeDsc = "新增班组维护信息";
      }
      this.dialogTableVisible = true;
    },
  },
};
</script>
<style lang="less">
.tb-row {
  margin-bottom: 2px !important;
}
</style>
