<template>
  <div class="app-container">
    <el-row>
      <el-col :span="6">
        <el-card>
          <el-button type="distribution" icon="el-icon-circle-plus-outline" size="small" @click="handleAdd('department_Form')">新增</el-button>
          <el-button type="warning" icon="el-icon-edit" size="small" plain @click="handleEdit()">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete" size="small" plain @click="deleteTable()">删除</el-button>
        </el-card>
      </el-col>
      <el-col :span="18">
        <el-card>
          <span>人员名称:</span>
          <el-input v-model="Name" placeholder="请输入人员名称" class="input-with-select" size="small" style="width: 200px; margin-right: 15px" clearable>
            <el-button slot="append" icon="el-icon-search" @click="fetchPeopleForName"/>
          </el-input>
          <el-button type="distribution" icon="el-icon-circle-plus-outline" size="small" @click="addPeople()">新增</el-button>
          <el-button type="danger" icon="el-icon-circle-close-outline" size="small" plain @click="Quit()">离职</el-button>
          <el-button type="text" size="small" style="float: right" plain @click="searchLeavingList">离职名单</el-button>
          <el-button v-popover:popover4 :disabled="disabledMove" type="success" icon="el-icon-sort" size="small" plain @click="moveGroup">移动</el-button>
          <!--<el-button type="info" plain size="small" icon="el-icon-upload2" @click="importPersonnel()">人员导入</el-button>-->
          <!--<el-button size="small" @click="postMaintain()">岗位维护</el-button>-->
          <el-popover
            ref="popover4"
            width="178"
            height="400"
            placement="bottom"
            trigger="click"
            @hide="hide()">
            <el-dropdown size="small" placement="right-start" style="margin-right: 0">
              <el-input
                v-model="nodeName1"
                :disabled="true"
                size="small"
                placeholder="请选择部门"/>
              <el-button type="text" size="small" style="margin-left: 50px; margin-top: 15px" @click="moveGroupConfim">确认</el-button>
              <el-dropdown-menu slot="dropdown">
                <el-tree
                  ref="treeForm"
                  :data="departmentData"
                  :props="defaultProps"
                  style="width: 200px;margin: 10px"
                  node-key="id"
                  accordion
                  @node-click="NodeClick"/>
              </el-dropdown-menu>
            </el-dropdown>
          </el-popover>
        </el-card>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="6">
        <el-card class="box-card" style="height: 82.5vh">
          <div slot="header" class="clearfix">
            <span>部门维护</span>
            <el-button style="float: right; padding: 3px 0" type="text" @click="expandALL()">展开</el-button>
          </div>
          <div style="height: 650px">
            <el-tree
              ref="treeForm"
              :data="departmentData"
              :props="defaultProps"
              show-checkbox
              node-key="id"
              check-strictly
              default-expand-all
              @check-change="handleClick"/>
          </div>
        </el-card>
      </el-col>
      <el-col :span="18">
        <el-card>
          <el-table
            ref="multipleTable"
            :data="List"
            tooltip-effect="dark"
            style="width: 100%;height: 77vh;overflow-y: auto"
            border
            size="mini"
            @selection-change="handleSelectionChange">
            <el-table-column
              align="center"
              type="selection"
              width="50"/>
            <!--            <el-table-column align="center" label="照片" width="50">-->
            <!--              <template slot-scope="scope">-->
            <!--                <el-popover-->
            <!--                  v-if="scope.row.文件标识 && scope.row.文件标识 !== 'null'"-->
            <!--                  placement="top-start"-->
            <!--                  width="200"-->
            <!--                  trigger="hover">-->
            <!--                  <el-button type="text" style="float:right" @click="deletePhoto(scope.row)">删除</el-button>-->
            <!--                  <el-image :src="scope.row.文件标识 && scope.row.文件标识 !== 'null' ? (readFile + scope.row.文件标识 + '.' + scope.row.文件后缀) : readFile + '123.jpg'">-->
            <!--                    <div slot="error" class="image-slot">-->
            <!--                      <i class="el-icon-picture-outline"/>-->
            <!--                    </div>-->
            <!--                  </el-image>-->
            <!--                  <el-image slot="reference" :src="scope.row.文件标识 && scope.row.文件标识 !== 'null' ? (readFile + scope.row.文件标识 + '.' + scope.row.文件后缀) : readFile + '123.jpg'" style="width:23px;">-->
            <!--                    <div slot="error" class="image-slot">-->
            <!--                      <i class="el-icon-picture-outline"/>-->
            <!--                    </div>-->
            <!--                  </el-image>-->
            <!--                </el-popover>-->
            <!--                <span v-else>—</span>-->
            <!--              </template>-->
            <!--            </el-table-column>-->
            <el-table-column align="center" label="部门名称" width="100">
              <template slot-scope="scope">
                {{ scope.row.节点名称 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="考勤编号" width="100">
              <template slot-scope="scope">
                {{ scope.row.考勤编号 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="密码" width="100">
              <template slot-scope="scope">
                ******
              </template>
            </el-table-column>
            <el-table-column align="center" label="姓名" width="100">
              <template slot-scope="scope">
                {{ scope.row.姓名 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="性别" width="100">
              <template slot-scope="scope">
                {{ scope.row.性别 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="身份证号" width="165">
              <template slot-scope="scope">
                {{ scope.row.身份证号 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="学历" width="100">
              <template slot-scope="scope">
                {{ scope.row.学历名称 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="入职时间" width="100">
              <template slot-scope="scope">
                {{ scope.row.入公司时间 !== '' && scope.row.入公司时间 !== null ? scope.row.入公司时间 = scope.row.入公司时间.split(' ')[0]: scope.row.入公司时间 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="出生日期" width="100">
              <template slot-scope="scope">
                {{ scope.row.出生日期 !== '' && scope.row.出生日期 !== null ? scope.row.出生日期 = scope.row.出生日期.split(' ')[0]: scope.row.出生日期 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="联系方式" width="110">
              <template slot-scope="scope">
                {{ scope.row.联系电话 }}
              </template>
            </el-table-column>
            <!--<el-table-column align="center" label="班组" width="110">-->
            <!--  <template slot-scope="scope">-->
            <!--    {{ scope.row.班组名称 }}-->
            <!--  </template>-->
            <!--</el-table-column>-->
            <el-table-column align="center" label="民族" width="100">
              <template slot-scope="scope">
                {{ scope.row.民族 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="家庭住址" width="200">
              <template slot-scope="scope">
                {{ scope.row.家庭住址 }}
              </template>
            </el-table-column>
            <el-table-column label="操作" fixed="right" align="center" width="280">
              <template slot-scope="scope">
                <!--                <el-button-->
                <!--                  :disabled="scope.row.文件标识 && scope.row.文件标识 !== 'null'"-->
                <!--                  size="mini"-->
                <!--                  type="primary"-->
                <!--                  icon="el-icon-picture"-->
                <!--                  @click="handlePicture(scope.row)">上传图片</el-button>-->
                <!--<el-button
                  size="mini"
                  type="primary"
                  icon="el-icon-s-grid"
                  @click="searchOpNamePermission (scope.row)">查看工位权限</el-button>-->
                <el-button
                  size="mini"
                  type="primary"
                  icon="el-icon-edit"
                  @click="handleEditPeople(scope.row)">编辑</el-button>
                <el-button
                  size="mini"
                  type="danger"
                  icon="el-icon-delete"
                  @click="resetPwd(scope.row)">重置密码</el-button>
                  <!--<el-button-->
                  <!--size="mini"-->
                  <!--type="danger"-->
                  <!--icon="el-icon-delete"-->
                  <!--@click="deletePeopleTable(scope.row)">删除</el-button>-->
              </template>
            </el-table-column>
          </el-table>
          <div class="block" style="margin-top: 10px;">
            <el-pagination
              :current-page="PageCurrent"
              :page-sizes="[10, 20, 30, 40, 50]"
              :page-size="PageSize"
              :total="total"
              layout="total, sizes, prev, pager, next, jumper"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"/>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <!--新增和编辑部门-->
    <el-dialog v-el-drag-dialog :title="department_Title" :visible.sync="department_DFV" center style="min-height: 200px" width="400px">
      <el-form ref="department_Form" :model="department_Form" :rules="rules" label-position="left" label-width="110px" class="demo-ruleForm">
        <el-form-item label="部门名称" prop="DepartmentName">
          <el-input v-model="department_Form.DepartmentName" size="small" style="width:200px"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button v-if="department_Title==='新增部门'" :disabled="handleAdd_disable" type="primary" @click="submitAdd('department_Form')">确定</el-button>
        <el-button v-else-if="department_Title==='编辑部门'" :disabled="handleAdd_disable" type="primary" @click="submitEdit('department_Form')">确定</el-button>
      </div>
    </el-dialog>
    <!--查看工位权限窗口-->
    <el-dialog v-el-drag-dialog :visible.sync="isShowSearchOpNamePermission" title="工位权限" center style="min-height: 200px" width="600px">
      <el-table
        v-if="isShowSearchOpNamePermission"
        ref="backWorkEasyBadSelectListTable"
        :row-class-name="tableRowClassName"
        :data="clickOpNamePermissionList"
        border
        size="small"
        height="365px"
        style="width: 100%; height: 280px; font-size: 20px; color: #000; border: 1px solid #9e9b9b"
      >
        <el-table-column
          type="index"
          label="排序"
          align="center"
          width="80"/>
        <el-table-column label="工位号" align="center">
          <template slot-scope="scope">
            {{ scope.row.OpName.toUpperCase() }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="有无权限">
          <template slot-scope="scope">
            {{ scope.row.value == 1 ? '有权限' : '无权限' }}
          </template>
        </el-table-column>

      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isShowSearchOpNamePermission = false">关闭</el-button>
      </div>
    </el-dialog>
    <el-dialog v-el-drag-dialog :title="people_title" :visible.sync="people_DFV" center style="min-height: 200px" width="800px">
      <el-form ref="people_form" :rules="rules" :model="people_form" label-position="left" label-width="110px" class="demo-ruleForm">
        <el-row>
          <el-col :span="12">
            <el-form-item label="工号" prop="AccountNumber">
              <el-input v-model="people_form.AccountNumber" size="small" style="width:200px"/>
            </el-form-item>
          </el-col>
          <el-col v-if="people_title === '新增人员'" :span="12">
            <el-form-item label="密码" prop="Password">
              <el-input v-model="people_form.Password" size="small" style="width:200px"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="姓名" prop="Name">
              <el-input v-model="people_form.Name" size="small" style="width:200px"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="Sex">
              <el-select v-model="people_form.Sex" clearable placeholder="请选择性别" style="width: 200px" size="small">
                <el-option
                  v-for="item in SexData"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出生日期" prop="Birthday">
              <el-date-picker
                v-model="people_form.Birthday"
                style="width: 200px"
                value-format="yyyy-MM-dd"
                size="small"
                type="date"
                placeholder="请选择出生日期"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份证号" prop="IDcard">
              <el-input v-model="people_form.IDcard" size="small" style="width:200px"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="入职时间" prop="TimeOfEntry">
              <el-date-picker
                v-model="people_form.TimeOfEntry"
                style="width: 200px"
                value-format="yyyy-MM-dd"
                size="small"
                type="date"
                placeholder="请选择入职时间"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系方式" prop="ContactInformation">
              <el-input v-model="people_form.ContactInformation" size="small" style="width:200px"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="学历" prop="Education">
              <el-select v-model="people_form.Education" clearable placeholder="请选择学历" style="width: 200px" size="small">
                <el-option
                  v-for="item in EducationDate"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="家庭住址" prop="Address">
              <el-input v-model="people_form.Address" size="small" style="width:200px"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="民族" prop="Nation">
              <el-input v-model="people_form.Nation" size="small" style="width:200px"/>
            </el-form-item>
          </el-col>
          <!--<el-col :span="12">-->
          <!--  <el-form-item label="班组" prop="Post">-->
          <!--    <el-select v-model="people_form.TeamValue" clearable placeholder="请选择班组" style="width: 200px" size="small">-->
          <!--      <el-option-->
          <!--        v-for="item in Team"-->
          <!--        :key="item.value"-->
          <!--        :label="item.label"-->
          <!--        :value="item.value"/>-->
          <!--    </el-select>-->
          <!--  </el-form-item>-->
          <!--</el-col>-->
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="btnLoading" @click="cancel">取消</el-button>
        <el-button v-if="people_title === '新增人员'" :disabled="addPeople_disable" :loading="btnLoading" type="primary" @click="submitAddPeople">确定</el-button>
        <el-button v-else-if="people_title === '编辑人员'" :disabled="addPeople_disable" :loading="btnLoading" type="primary" @click="submitEditPeople">确定</el-button>
      </div>
    </el-dialog>
    <!--导入人员弹窗-->
    <el-dialog :visible.sync="importPersonnelisible" title="人员导入" center style="min-height: 200px" width="900px">
      <div class="app-container">
        <upload-excel-component style="margin-left: 15%" @on-selected-file="selected" />
        <div style="margin-left: 395px">
          <span v-show="worksheet.length > 1" style="cursor: pointer;width: 50px;" @click="removeAll"><i
            class="el-icon-close" /></span>
          <el-upload :on-remove="handleRemove" :file-list="itemFile" style="width: 220px" action="" />
        </div>
        <div v-if="worksheet.length !== 0" style="margin: 3px 0 0 395px">
          <el-button icon="el-icon-upload2" type="primary" size="small" @click="basicPartsToSQLCheck">导入到数据库</el-button>
        </div>
      </div>
    </el-dialog>
    <!--离职名单-->
    <el-dialog v-el-drag-dialog :visible.sync="LeavingListVisiable" title="离职名单" center style="height: 800px" width="80%">
      <el-table
        :data="LeavingList"
        stripe
        tooltip-effect="dark"
        style="width: 100%"
        height="500"
        highlight-current-row
        border
        size="mini">
        <el-table-column align="center" label="部门名称" width="100">
          <template slot-scope="scope">
            {{ scope.row.节点名称 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="工号" width="100">
          <template slot-scope="scope">
            {{ scope.row.考勤编号 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="密码" width="100">
          <template slot-scope="scope">
            {{ scope.row.密码 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="姓名" width="100">
          <template slot-scope="scope">
            {{ scope.row.姓名 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="性别" width="100">
          <template slot-scope="scope">
            {{ scope.row.性别 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="身份证号" width="165">
          <template slot-scope="scope">
            {{ scope.row.身份证号 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="学历" width="100">
          <template slot-scope="scope">
            {{ scope.row.学历名称 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="入职时间" width="100">
          <template slot-scope="scope">
            {{ scope.row.入公司时间 !== '' && scope.row.入公司时间 !== null ? scope.row.入公司时间 = scope.row.入公司时间.split(' ')[0]: scope.row.入公司时间 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="出生日期" width="100">
          <template slot-scope="scope">
            {{ scope.row.出生日期 !== '' && scope.row.出生日期 !== null ? scope.row.出生日期 = scope.row.出生日期.split(' ')[0]: scope.row.出生日期 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="联系方式" width="110">
          <template slot-scope="scope">
            {{ scope.row.联系电话 }}
          </template>
        </el-table-column>
        <!--        <el-table-column align="center" label="岗位工种" width="100">-->
        <!--          <template slot-scope="scope">-->
        <!--            {{ scope.row.岗位名称 }}-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <el-table-column align="center" label="民族" width="100">
          <template slot-scope="scope">
            {{ scope.row.民族 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="家庭住址">
          <template slot-scope="scope">
            {{ scope.row.家庭住址 }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="100">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-circle-close-outline"
              @click="deletePeople(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin-top: 10px;">
        <el-pagination
          :current-page="PageCurrentN"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="PageSizeN"
          :total="totalN"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChangeN"
          @current-change="handleCurrentChangeN"/>
      </div>
    </el-dialog>
    <el-dialog v-el-drag-dialog :visible.sync="dialogFormVisible" title="上传图片" center style="height: 800px" width="30%">
      <el-upload
        id="invoiceScan"
        ref="upload"
        :multiple="true"
        :auto-upload="false"
        :on-success="uploadSuccess"
        :before-upload="beforeUpload"
        :on-exceed="warningExceed"
        :limit="limit"
        :action="upload+'?num='+ PersonId"
        list-type="picture-card">
        <i class="el-icon-plus"/>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible=false">取消</el-button>
        <el-button type="primary" style="width: 70px" @click="submitPerson()">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { fetchteamviwer, moveGroups, getTree, tree, getTablePeople, getTablePeopleForName, addDepartmentName, delList, editList, fetchEducationData, fetchPostData, addPeople, editPeople, quit, searchLeavingList } from '@/api/BasicData/PersonnelManagement'
import UploadExcelComponent from './UploadExcel.vue'
import { uploadPerson } from '@/utils/request'
import { readFile } from '@/utils/request'
import request from '@/utils/request'
import { isvalidUsername } from '@/utils/validate'
import { sleep } from '../../../utils/tool'
export default {
  name: 'UploadExcel',
  components: { UploadExcelComponent },
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!isvalidUsername(value)) {
        callback(new Error('请输入4-7位工号'))
      } else {
        callback()
      }
    }
    // const checkIDCard = (rule, value, callback) => {
    //   const IDCardReg = /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[0-2])(([0-2][1-9])|10|20|30|31)\d{3}(\d|X|x)$/
    //   // const sfzhReg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
    //   if (value) {
    //     if (IDCardReg.test(value)) {
    //       callback()
    //     } else {
    //       callback(new Error('身份证号格式不正确'))
    //     }
    //   } else {
    //     callback(new Error('请输入身份证号'))
    //   }
    // }
    return {
      Team: [],
      handleAdd_disable: false,
      addPeople_disable: false,
      limit: 999,
      readFile: readFile,
      disabledMove: true,
      PageCurrentN: 1,
      PageSizeN: 10,
      totalN: null,
      LeavingList: [],
      LeavingListVisiable: false,
      departmentData: [],
      expandAll: true,
      i: 1,
      id: '',
      PageCurrent: 1,
      PageSize: 50,
      List: [],
      worksheet: [],
      itemFile: [],
      total: null,
      nodeName: '',
      btnLoading: false,
      department_DFV: false,
      upload: uploadPerson,
      dialogFormVisible: false,
      isShowSearchOpNamePermission: false,
      clickOpNamePermissionList: [],
      PersonId: '',
      department_Form: {
        DepartmentName: ''
      },
      department_Title: '',
      Name: '',
      people_title: '',
      people_DFV: false,
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      people_form: {
        Name: '',
        AccountNumber: '',
        Password: '',
        Sex: null,
        Birthday: '',
        IDcard: '',
        TimeOfEntry: '',
        ContactInformation: '',
        Address: null,
        Nation: '',
        ForeignLanguageLevel: null,
        Title: null,
        PositionStatus: null,
        Education: null,
        Post: null,
        PeopleID: null,
        TeamValue: ''
      },
      rules: {
        Name: [{ required: true, message: '请输入', trigger: 'blur' }],
        // AccountNumber: [{ required: true, message: '请输入', trigger: 'blur' }],
        AccountNumber: [{ required: true, trigger: 'blur', validator: validateUsername }],
        Password: [{ required: true, message: '请输入', trigger: 'blur' }]
        // Sex: [{ required: true, message: '请输入', trigger: 'change' }],
        // Birthday: [{ required: true, message: '请输入', trigger: 'change' }],
        // IDcard: [{ required: true, message: '请输入', trigger: 'blur' }, {
        //   validator: checkIDCard,
        //   trigger: 'blur'
        // }],
        // TimeOfEntry: [{ required: true, message: '请输入', trigger: 'change' }],
        // ContactInformation: [{ required: true, message: '请输入', trigger: 'blur' }, { type: 'number',
        //   message: '手机号格式不正确',
        //   trigger: 'blur',
        //   transform(value) {
        //     var phonereg = 11 && /^((13|14|15|16|17|18|19)[0-9]{1}\d{8})$/
        //     if (!phonereg.test(value)) {
        //       return false
        //     } else {
        //       return Number(value)
        //     }
        //   }
        // }],
        // Address: [{ required: true, message: '请输入', trigger: 'change' }],
        // Nation: [{ required: true, message: '请输入', trigger: 'change' }],
        // ForeignLanguageLevel: [{ required: true, message: '请输入', trigger: 'change' }],
        // Title: [{ required: true, message: '请输入', trigger: 'change' }],
        // PositionStatus: [{ required: true, message: '请输入', trigger: 'change' }],
        // Education: [{ required: true, message: '请输入', trigger: 'change' }],
        // DepartmentName: [{ required: true, message: '请输入', trigger: 'blur' }]
      },
      SexData: [{
        value: 1,
        label: '男'
      }, {
        value: 2,
        label: '女'
      }],
      EducationDate: [],
      PostData: [],
      multipleSelection: [],
      nodeName1: '',
      count1: 0,
      count2: 0,
      importPersonnelisible: false
    }
  },
  created() {
    this.getTreeData()
    this.fetchEducationData()
    this.fetchPostData()
    this.fetchTeamViwer()
  },
  methods: {
    // 获取班组信息
    fetchTeamViwer() {
      fetchteamviwer().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.Team.push({
            label: response.data[i].班组名称,
            value: response.data[i].班组流水号
          })
        }
      })
    },
    // 删除离职人员信息
    deletePeople(row) {
      this.$confirm('此操作将永久删除该离职员工信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['考勤编号', row.考勤编号]
        var Data = this.CreateData('12', '人事档案管理_离职人员_删除数据', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('删除成功')
            this.searchLeavingList()
          } else {
            this.$message.error('删除失败')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 删除人员照片
    deletePhoto(row) {
      this.$confirm('此操作将永久删除该图片, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        request({
          url: '',
          method: 'post',
          data: {
            type: '4',
            name: `delete from 人员管理_图片 where 人员编号 = ${row.人员编号}`
          }
        }).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('删除成功')
            this.handleNodeClick()
          } else {
            this.$message.error('删除失败')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消删除'
        })
      })
    },
    fetchEducationData() {
      this.getSelect(fetchEducationData, ['学历名称', '学历编号'], 'EducationDate')
    },
    fetchPostData() {
      this.people_form.Post = ''
      this.PostData = []
      this.getSelect(fetchPostData, ['岗位名称', '岗位编号'], 'PostData')
    },
    getTreeData() {
      getTree().then(response => {
        const data = response.data
        this.departmentData = tree(data, 0)
      })
    },
    expandALL() {
      this.expandAll = !this.expandAll
      for (let i = 0; i < this.$refs.treeForm.store._getAllNodes().length; i++) {
        this.$refs.treeForm.store._getAllNodes()[i].expanded = this.expandAll
      }
    },
    handleNodeClick() {
      this.getTable(getTablePeople, [this.id, this.PageCurrent, this.PageSize], ['List', 'total'])
    },
    handleClick(data, checked) {
      if (this.$refs.treeForm.getCheckedKeys().length !== 0 && this.$refs.treeForm.getCheckedNodes().length !== 0) {
        const data = this.$refs.treeForm.getCheckedKeys()
        if (data.length === 1) {
          this.id = data[0]
          this.handleNodeClick()
        }
        this.nodeName = this.$refs.treeForm.getCheckedNodes()[0].label
      } else {
        this.id = ''
        this.handleNodeClick()
      }
      this.i++
      if (this.i % 2 === 1) {
        if (checked) {
          this.$refs.treeForm.setCheckedNodes([data])
        } else {
          this.$refs.treeForm.setCheckedNodes([])
        }
      }
    },
    uploadSuccess(res) { // 上传成功回调
      if (res[0].result === '1') {
        this.$message.success('增加成功')
        this.handleNodeClick()
        this.dialogFormVisible = false
        this.$refs.upload.clearFiles()
      } else {
        this.$message.error('增加失败')
      }
    },
    beforeUpload(file) { // 上传前检测
      const isJPG = /^image\/.*/.test(file.type)
      if (!isJPG) {
        this.$message.error('只能上传图片。。')
      }
      return isJPG
    },
    warningExceed() {
      this.$message.error('仅可上传五张照片')
    },
    handlePicture(row) {
      this.dialogFormVisible = true
      this.PersonId = row.人员编号
    },
    submitPerson() {
      this.$refs.upload.submit()
    },
    handleAdd(formName) {
      this.department_Form.DepartmentName = ''
      this.count1 = this.count1 + 1
      if (this.count1 !== 1) {
        this.$refs[formName].resetFields()
      }
      this.department_Title = '新增部门'
      this.handleAdd_disable = false
      this.department_DFV = true
    },
    handleEdit() {
      this.addForm('department_Form')
      if (this.id) {
        this.department_Title = '编辑部门'
        this.department_Form.DepartmentName = this.nodeName
        this.handleAdd_disable = false
        this.department_DFV = true
      } else {
        this.$message.warning(`请选择要修改的部门`)
      }
    },
    // 重置密码
    resetPwd(row) {
      console.log(row)
      this.$prompt('请输入新密码', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(({ value }) => {
        var param = []
        param[0] = ['密码', value]
        param[1] = ['人员编号', row.人员编号]
        var Data = this.CreateData('12', '人事档案管理_人员名单_重置密码_MESWORK', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result == '1') {
            this.$message.success('重置密码成功！')
          }
        })
      }).catch(() => {
      })
    },
    // 导入人员Excel
    importPersonnel() {
      this.importPersonnelisible = true
    },
    handleRemove(file) { // 移除文件
      for (let i = 0; i < this.itemFile.length; i++) {
        if (this.itemFile[i].name === file.name) {
          this.worksheet.splice(i, 1)
          this.itemFile.splice(i, 1)
        }
      }
    },
    async selected(data) { // 选择需要导入的文件
      console.log(data, 1111)
      this.worksheet = data.worksheet // 获取每个多选文件的表格坐标及值
      this.itemFile = data.itemFile
    },
    removeAll() {
      this.worksheet = []
      this.itemFile = []
    },
    async basicPartsToSQLCheck() {
      try {
        this.loading = true
        this.err = 0
        let departmentName, // 部门名称
          attendanceNumber, // 考勤编号
          password, // 密码
          name, // 姓名
          personnelNumber, // 人员编号
          J07ET010,
          J07ET030,
          J07ET040,
          J07ET050,
          J07ET060,
          J07MA000,
          J07MA005,
          J07MA010,
          J07MA020,
          J07MA030,
          J07MA040,
          J07MA050,
          J07MA060,
          J07MA070,
          J07MA075,
          J07MA080,
          J07MA090,
          J07MA100,
          J07MA110,
          J07MA115,
          J07MA120,
          J07MA140,
          J07MA150,
          J07PA010,
          J07PA020,
          J07PA030,
          J07PA040,
          J07PA050,
          J07PA060,
          J07PA070,
          J07PA080
        // 进行导入前操作
        var param = []
        var Data = this.CreateData('12', '人员管理_导入前处理', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result !== '1') {
            this.$message.error('人员导入初始化失败！')
          }
        })
        this.$message({
          message: '开始导入人员表单,请等待导入结束！',
          type: 'success'
        })
        for (let i = 0; i < 999; i++) {
          const attr = i + 2
          departmentName = this.worksheet[0][`A${attr}`] ? this.worksheet[0][`A${attr}`].v : 0
          attendanceNumber = this.worksheet[0][`B${attr}`] ? this.worksheet[0][`B${attr}`].v : 0
          password = this.worksheet[0][`C${attr}`] ? this.worksheet[0][`C${attr}`].v : '123456'
          name = this.worksheet[0][`D${attr}`] ? this.worksheet[0][`D${attr}`].v : 0
          personnelNumber = this.worksheet[0][`E${attr}`] ? this.worksheet[0][`E${attr}`].v : 0
          J07ET010 = this.worksheet[0][`F${attr}`] ? this.worksheet[0][`F${attr}`].v : 0
          J07ET030 = this.worksheet[0][`G${attr}`] ? this.worksheet[0][`G${attr}`].v : 0
          J07ET040 = this.worksheet[0][`H${attr}`] ? this.worksheet[0][`H${attr}`].v : 0
          J07ET050 = this.worksheet[0][`I${attr}`] ? this.worksheet[0][`I${attr}`].v : 0
          J07ET060 = this.worksheet[0][`J${attr}`] ? this.worksheet[0][`J${attr}`].v : 0
          J07MA000 = this.worksheet[0][`K${attr}`] ? this.worksheet[0][`K${attr}`].v : 0
          J07MA005 = this.worksheet[0][`L${attr}`] ? this.worksheet[0][`L${attr}`].v : 0
          J07MA010 = this.worksheet[0][`M${attr}`] ? this.worksheet[0][`M${attr}`].v : 0
          J07MA020 = this.worksheet[0][`N${attr}`] ? this.worksheet[0][`N${attr}`].v : 0
          J07MA030 = this.worksheet[0][`O${attr}`] ? this.worksheet[0][`O${attr}`].v : 0
          J07MA040 = this.worksheet[0][`P${attr}`] ? this.worksheet[0][`P${attr}`].v : 0
          J07MA050 = this.worksheet[0][`Q${attr}`] ? this.worksheet[0][`Q${attr}`].v : 0
          J07MA060 = this.worksheet[0][`R${attr}`] ? this.worksheet[0][`R${attr}`].v : 0
          J07MA070 = this.worksheet[0][`S${attr}`] ? this.worksheet[0][`S${attr}`].v : 0
          J07MA075 = this.worksheet[0][`T${attr}`] ? this.worksheet[0][`T${attr}`].v : 0
          J07MA080 = this.worksheet[0][`U${attr}`] ? this.worksheet[0][`U${attr}`].v : 0
          J07MA090 = this.worksheet[0][`V${attr}`] ? this.worksheet[0][`V${attr}`].v : 0
          J07MA100 = this.worksheet[0][`W${attr}`] ? this.worksheet[0][`W${attr}`].v : 0
          J07MA110 = this.worksheet[0][`X${attr}`] ? this.worksheet[0][`X${attr}`].v : 0
          J07MA115 = this.worksheet[0][`Y${attr}`] ? this.worksheet[0][`Y${attr}`].v : 0
          J07MA120 = this.worksheet[0][`Z${attr}`] ? this.worksheet[0][`Z${attr}`].v : 0
          J07MA140 = this.worksheet[0][`AA${attr}`] ? this.worksheet[0][`AA${attr}`].v : 0
          J07MA150 = this.worksheet[0][`AB${attr}`] ? this.worksheet[0][`AB${attr}`].v : 0
          J07PA010 = this.worksheet[0][`AC${attr}`] ? this.worksheet[0][`AC${attr}`].v : 0
          J07PA020 = this.worksheet[0][`AD${attr}`] ? this.worksheet[0][`AD${attr}`].v : 0
          J07PA030 = this.worksheet[0][`AE${attr}`] ? this.worksheet[0][`AE${attr}`].v : 0
          J07PA040 = this.worksheet[0][`AF${attr}`] ? this.worksheet[0][`AF${attr}`].v : 0
          J07PA050 = this.worksheet[0][`AG${attr}`] ? this.worksheet[0][`AG${attr}`].v : 0
          J07PA060 = this.worksheet[0][`AH${attr}`] ? this.worksheet[0][`AH${attr}`].v : 0
          J07PA070 = this.worksheet[0][`AI${attr}`] ? this.worksheet[0][`AI${attr}`].v : 0
          J07PA080 = this.worksheet[0][`AJ${attr}`] ? this.worksheet[0][`AJ${attr}`].v : 0
          if (!departmentName && !attendanceNumber && !name) {
            this.$message({
              message: '人员列表导入完毕！',
              type: 'success'
            })
            this.importPersonnelisible = false
            break
          } else if (!departmentName) { // 名称不能为空
            this.$notify.error(`部门不能为空`)
            break
          } else if (!attendanceNumber) {
            this.$notify.error(attr + `行工号不能为空`)
            break
          // } else if (!password) {
          //   this.$notify.error(`密码不能为空`)
          //   break
          } else if (!name) {
            this.$notify.error(attr + `行姓名不能为空`)
            break
          } else {
            await sleep(100)
            console.log(departmentName, attendanceNumber, password, name,
              'J07ET010', J07ET010,
              'J07ET030', J07ET030,
              'J07ET040', J07ET040,
              'J07ET050', J07ET050,
              'J07ET060', J07ET060,
              'J07MA000', J07MA000,
              'J07MA005', J07MA005,
              'J07MA010', J07MA010,
              'J07MA020', J07MA020,
              'J07MA030', J07MA030,
              'J07MA040', J07MA040,
              'J07MA050', J07MA050,
              'J07MA060', J07MA060,
              'J07MA070', J07MA070,
              'J07MA075', J07MA075,
              'J07MA080', J07MA080,
              'J07MA090', J07MA090,
              'J07MA100', J07MA100,
              'J07MA110', J07MA110,
              'J07MA115', J07MA115,
              'J07MA120', J07MA120,
              'J07MA140', J07MA140,
              'J07MA150', J07MA150,
              'J07PA010', J07PA010,
              'J07PA020', J07PA020,
              'J07PA030', J07PA030,
              'J07PA040', J07PA040,
              'J07PA050', J07PA050,
              'J07PA060', J07PA060,
              'J07PA070', J07PA070,
              'J07PA080', J07PA080,
            )
            const scope = this
            var param2 = []
            param2[0] = ['部门', departmentName]
            param2[1] = ['考勤编号', attendanceNumber]
            param2[2] = ['姓名', name]
            param2[3] = ['密码', password]
            param2[4] = ['J07ET010', J07ET010]
            param2[5] = ['J07ET030', J07ET030]
            param2[6] = ['J07ET040', J07ET040]
            param2[7] = ['J07ET050', J07ET050]
            param2[8] = ['J07ET060', J07ET060]
            param2[9] = ['J07MA000', J07MA000]
            param2[10] = ['J07MA005', J07MA005]
            param2[11] = ['J07MA010', J07MA010]
            param2[12] = ['J07MA020', J07MA020]
            param2[13] = ['J07MA030', J07MA030]
            param2[14] = ['J07MA040', J07MA040]
            param2[15] = ['J07MA050', J07MA050]
            param2[16] = ['J07MA060', J07MA060]
            param2[17] = ['J07MA070', J07MA070]
            param2[18] = ['J07MA075', J07MA075]
            param2[19] = ['J07MA080', J07MA080]
            param2[20] = ['J07MA090', J07MA090]
            param2[21] = ['J07MA100', J07MA100]
            param2[22] = ['J07MA110', J07MA110]
            param2[23] = ['J07MA115', J07MA115]
            param2[24] = ['J07MA120', J07MA120]
            param2[25] = ['J07MA140', J07MA140]
            param2[26] = ['J07MA150', J07MA150]
            param2[27] = ['J07PA010', J07PA010]
            param2[28] = ['J07PA020', J07PA020]
            param2[29] = ['J07PA030', J07PA030]
            param2[30] = ['J07PA040', J07PA040]
            param2[31] = ['J07PA050', J07PA050]
            param2[32] = ['J07PA060', J07PA060]
            param2[33] = ['J07PA070', J07PA070]
            param2[34] = ['J07PA080', J07PA080]
            param2[35] = ['角色编号', personnelNumber]
            var Data2 = scope.CreateData('12', '人员管理_导入', param2)
            this.ExecDatabase(Data2).then(response => {
              if (response.data[0].result === '1') {
                // this.$message({
                //   message: '导入成功',
                //   type: 'success'
                // })
              } else {
                this.$message.error(name + '导入失败！')
              }
            })
          }
        }
      } catch (e) {
        this.err++
        this.loading = false
        this.$notify.error(`导入数据错误,请检查导入文件是否正确`)
        console.log(`导入数据错误${e}`)
      }
    },
    submitAdd(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.handleAdd_disable = true
          this.department_DFV = false
          if (this.id) {
            addDepartmentName(this.id, this.department_Form.DepartmentName, 0).then(response => {
              if (response.data[0].result === '1') {
                this.getTreeData()
                this.$message.success('添加成功')
                this.i = 0
              } else {
                this.$message.error('添加失败')
              }
            })
          } else {
            addDepartmentName(0, this.department_Form.DepartmentName, 0).then(response => {
              if (response.data[0].result === '1') {
                this.getTreeData()
                this.$message.success('添加成功')
                this.i = 1
              } else {
                this.$message.error('添加失败')
              }
            })
          }
        } else {
          return false
        }
      })
    },
    submitEdit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.handleAdd_disable = true
          this.department_DFV = false
          editList(this.id, this.department_Form.DepartmentName).then(response => {
            if (response.data[0].result === '1') {
              this.getTreeData()
              this.$message.success('编辑成功')
            } else {
              this.$message.error('编辑失败')
            }
          })
        } else {
          return false
        }
      })
    },
    deleteTable() {
      if (this.id) {
        this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delList(this.id).then(response => {
            if (response.data[0].result === '1') {
              this.getTreeData()
              this.i = 1
              this.id = ''
              this.$message({
                message: '已被成功删除',
                type: 'success'
              })
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      } else {
        this.$message.warning(`请选择部门`)
      }
    },
    cancel() {
      this.department_DFV = false
      this.people_DFV = false
    },
    fetchPeopleForName() {
      if (this.Name) {
        getTablePeopleForName(this.Name).then(response => {
          for (let i = 0; i < response.data.length; i++) {
            // let Data = response.data
            if (response.data[i].入公司时间) {
              response.data[i].入公司时间 = response.data[i].入公司时间.split(' ')[0]
            }
            if (response.data[i].出生日期) {
              response.data[i].出生日期 = response.data[i].出生日期.split(' ')[0]
            }
          }
          this.List = response.data
        })
      } else {
        this.$message.warning(`请输入人员姓名`)
        this.List = []
      }
    },
    addPeople() {
      if (this.id) {
        this.people_title = '新增人员'
        this.addPeople_disable = false
        this.people_DFV = true
        this.addForm('people_form')
      } else {
        this.$message.warning(`请选择部门名称`)
      }
    },
    submitAddPeople() {
      this.addPeople_disable = true
      this.btnLoading = true
      addPeople(this.id, this.people_form.Sex, this.people_form.Education, this.people_form.Address, this.people_form.Nation, this.people_form.Post, this.people_form.AccountNumber, this.people_form.Password, this.people_form.Name, this.people_form.IDcard, this.people_form.TimeOfEntry, this.people_form.Birthday, this.people_form.ContactInformation, this.people_form.TeamValue).then(response => {
        this.btnLoading = false
        if (response.data[0].result === '1') {
          this.people_DFV = false
          this.i = 0
          this.handleNodeClick()
          this.$message.success('添加成功')
        } else {
          this.$message.error('添加失败')
          this.addPeople_disable = false
        }
      })
    },
    handleEditPeople(row) {
      this.people_title = '编辑人员'
      this.people_DFV = true
      this.addPeople_disable = false
      this.addForm('people_form')
      this.people_form.PeopleID = row.人员编号
      this.people_form.Sex = parseInt(row.性别编号)
      this.people_form.Education = parseInt(row.学历编号)
      this.people_form.Address = row.家庭住址
      this.people_form.Nation = row.民族
      if (row.岗位编号) {
        this.people_form.Post = parseInt(row.岗位编号)
      } else {
        this.people_form.Post = ''
      }
      this.people_form.AccountNumber = row.考勤编号
      this.people_form.Password = row.密码
      this.people_form.Name = row.姓名
      this.people_form.IDcard = row.身份证号
      this.people_form.TimeOfEntry = row.入公司时间
      this.people_form.Birthday = row.出生日期
      this.people_form.ContactInformation = row.联系电话
      if (row.班组流水号) {
        this.people_form.TeamValue = parseInt(row.班组流水号)
      } else {
        this.people_form.TeamValue = ''
      }
    },
    submitEditPeople() {
      this.addPeople_disable = true
      this.btnLoading = true
      editPeople(this.people_form.PeopleID, this.people_form.Sex, this.people_form.Education, this.people_form.Address, this.people_form.Nation, this.people_form.Post, this.people_form.AccountNumber, this.people_form.Password, this.people_form.Name, this.people_form.IDcard, this.people_form.TimeOfEntry, this.people_form.Birthday, this.people_form.ContactInformation, this.people_form.TeamValue).then(response => {
        this.btnLoading = false
        if (response.data[0].result === '1') {
          if (this.id !== '' && this.Name === '') {
            this.handleNodeClick()
          } else {
            this.feathPeopleForName()
          }
          this.$message.success('编辑成功')
          this.people_DFV = false
        } else {
          this.$message.error('编辑失败')
          this.addPeople_disable = false
        }
      })
    },
    // 查看工位权限
    searchOpNamePermission(row) {
      this.isShowSearchOpNamePermission = true
      this.clickOpNamePermissionList = []
      console.log(row)
      for (const key in row) {
        const value = row[key]
        if (key.indexOf('j07') !== -1) {
          this.clickOpNamePermissionList.push({
            OpName: key,
            value: value
          })
        }
      }
      console.log(this.clickOpNamePermissionList)
    },
    tableRowClassName({ row }) {
      if (row.value == 1) {
        return 'success'
      } else {
        return 'no-success'
      }
    },
    handleSizeChange(val) {
      this.PageSize = val
      this.PageCurrent = 1
      getTablePeople(this.id, this.PageCurrent, this.PageSize).then(response => {
        this.List = response.data.rows
        this.total = response.data.total
      })
    },
    handleCurrentChange(val) {
      this.PageCurrent = val
      getTablePeople(this.id, this.PageCurrent, this.PageSize).then(response => {
        this.List = response.data.rows
        this.total = response.data.total
      })
    },
    handleSizeChangeN(val) {
      this.PageSizeN = val
      this.PageCurrentN = 1
      this.getTable(searchLeavingList, [this.PageCurrentN, this.PageSizeN], ['LeavingList', 'totalN'])
    },
    handleCurrentChangeN(val) {
      this.PageCurrentN = val
      this.getTable(searchLeavingList, [this.PageCurrentN, this.PageSizeN], ['LeavingList', 'totalN'])
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
      this.multipleSelection.length === 0 ? this.disabledMove = true : this.disabledMove = false
    },
    Quit() {
      if (this.multipleSelection.length === 0) {
        this.$message.warning('请选择进行离职操作的员工')
      } else {
        this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          for (let i = 0; i < this.multipleSelection.length; i++) {
            quit(this.multipleSelection[i].人员编号).then(response => {
              if (response.data[0].result === '1') {
                this.$message.success('离职成功')
                getTablePeople(this.id, this.PageCurrent, this.PageSize).then(response => {
                  this.List = response.data.rows
                  this.total = response.data.total
                })
              } else { this.$message.error('离职失败') }
            })
          }
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消离职操作'
          })
        })
      }
    },
    searchLeavingList() {
      this.LeavingListVisiable = true
      this.getTable(searchLeavingList, [this.PageCurrentN, this.PageSizeN], ['LeavingList', 'totalN'])
    },
    moveGroupConfim() {
      this.id = this.node
      for (let i = 0; i < this.multipleSelection.length; i++) {
        moveGroups(this.multipleSelection[i].人员编号, this.node).then(response => {
          if (response.data[0].result === '1') {
            this.handleNodeClick()
            if (i === this.multipleSelection.length - 1) {
              this.$message.success('移组成功')
            }
          } else {
            this.$message.error('更新失败')
          }
        })
      }
    },
    hide() {
      this.nodeName1 = ''
      this.node = ''
    },
    moveGroup() {
      if (this.multipleSelection.length === 0) {
        this.$message.warning('请至少选择一个人员')
      }
    },
    NodeClick(data) {
      this.node = data.id
      this.nodeName1 = data.label
    }
  }
}
</script>

<style scoped>
::v-deep.el-table .success {
  background: #6cff7f !important;
  color: #000 !important;
}
::v-deep.el-table .no-success {
  background: #ff6c6c !important;
  color: #000 !important;
}
</style>

