<template>
  <div class="mod-config">
    <div>
        <span @click="favoriteFunction()">
         <icon-svg :name="favorite?'xiangqufill':'xiangqu'" class="sl-svg"></icon-svg>
        </span>
    </div>
    <el-form :inline="true" label-position="top" :model="searchData" @keyup.enter.native="getDataList()">
      <el-form-item :label="'机修人员编码'">
        <el-input v-model="searchData.adminID" clearable style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item :label="'机修人员姓名'">
        <el-input v-model="searchData.adminName" clearable style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item :label="'所属部门'">
        <el-input v-model="searchData.departmentName" clearable style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item :label="'在用'">
        <el-select filterable v-model="searchData.active" style="width: 130px">
          <el-option label="全部" value=""></el-option>
          <el-option label="是" value="Y"></el-option>
          <el-option label="否" value="N"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="' '">
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="addModal()">新增</el-button>
        <download-excel
          :fields="fields()"
          :data="exportData"
          type="xls"
          :name="exportName"
          :header="exportHeader"
          :footer="exportFooter"
          :fetch="createExportData"
          :before-generate="startDownload"
          :before-finish="finishDownload"
          worksheet="导出信息"
          class="el-button el-button--primary el-button--medium">
          {{ "导出" }}
        </download-excel>
      </el-form-item>
    </el-form>
    <el-table
      :height="height"
      :data="dataList"
      border
      v-loading="dataListLoading"
      style="width: 100%;">
<!--      <el-table-column-->
<!--        prop="adminID"-->
<!--        header-align="center"-->
<!--        align="center"-->
<!--        label="机修人员编码"-->
<!--        :width="flexColumnWidth('机修人员编码','adminID')">-->
<!--      </el-table-column>-->
<!--      <el-table-column-->
<!--        prop="adminName"-->
<!--        header-align="center"-->
<!--        align="center"-->
<!--        label="机修人员姓名"-->
<!--        :width="flexColumnWidth('机修人员姓名','adminName')">-->
<!--      </el-table-column>-->
<!--      <el-table-column-->
<!--        prop="active"-->
<!--        header-align="center"-->
<!--        align="center"-->
<!--        label="在用">-->
<!--        <template slot-scope="scope">-->
<!--          <div v-if="scope.row.active === 'Y'">-->
<!--            是-->
<!--          </div>-->
<!--          <div v-if="scope.row.active === 'N'">-->
<!--            否-->
<!--          </div>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column-->
<!--        prop="departmentName"-->
<!--        header-align="center"-->
<!--        align="left"-->
<!--        label="部门"-->
<!--        :width="flexColumnWidth('部门','departmentName')">-->
<!--      </el-table-column>-->
      <el-table-column
        v-for="(item,index) in columnList" :key="index"
        :sortable="item.columnSortable"
        :prop="item.columnProp"
        :header-align="item.headerAlign"
        :show-overflow-tooltip="item.showOverflowTooltip"
        :align="item.align"
        :fixed="item.fixed==''?false:item.fixed"
        :min-width="item.columnWidth"
        :label="item.columnLabel">
        <template slot-scope="scope">
          <span v-if="!item.columnHidden"> {{ scope.row[item.columnProp] }}</span>
          <span v-if="item.columnImage"><img :src="scope.row[item.columnProp]" style="width: 100px; height: 80px"/></span>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="100"
        label="操作">
        <template slot-scope="scope">
          <a type="text" size="small" @click="updateModal(scope.row)">修改</a>
          <a type="text" size="small" @click="deleteAdmin(scope.row)">删除</a>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[20, 50, 100, 200, 500]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <el-dialog title="机修人员" :close-on-click-modal="false" v-drag :visible.sync="modalFlag" width="430px">
      <el-form :inline="true" label-position="top" :model="modalData" :rules="rules" style="margin-left: 7px;margin-top: -5px;">
        <el-form-item label="机修人员编码:" prop="adminID" :rules="rules.adminIdType">
          <el-input v-model="modalData.adminID" :disabled="modalDisableFlag" style="width: 120px"></el-input>
        </el-form-item>
        <el-form-item label="机修人员姓名:" prop="adminName" :rules="rules.adminNameType">
          <el-input v-model="modalData.adminName" style="width: 120px"></el-input>
        </el-form-item>
        <el-form-item label="是否在用:" prop="active" :rules="rules.activeType">
          <el-select filterable v-model="modalData.active" style="width: 120px">
            <el-option label="是" value="Y"></el-option>
            <el-option label="否" value="N"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" :model="modalData" :rules="rules" style="margin-left: 7px;margin-top: -5px;">
        <el-form-item prop="departmentName" :rules="rules.departmentNameType">
          <span slot="label" style="" @click="getDepartmentList()"><a>部门</a></span>
          <el-input v-model="modalData.departmentName" style="width: 383px"></el-input>
        </el-form-item>
        <el-form-item label="手机号码:">
          <el-input v-model="modalData.phone" style="width: 189px"></el-input>
        </el-form-item>
        <el-form-item label="公司座机:">
          <el-input v-model="modalData.tel" style="width: 189px"></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" :model="modalData" :rules="rules" style="margin-left: 7px;margin-top: -5px;">
        <el-form-item label="邮箱:">
          <el-input v-model="modalData.email" style="width: 189px"></el-input>
        </el-form-item>
        <el-form-item prop="levelDesc" :rules="rules.levelDescType">
          <span slot="label" style="" @click="getBaseList(202)"><a herf="#">工种</a></span>
          <el-input v-model="modalData.levelDesc" style="width: 189px"></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" :model="modalData" :rules="rules" style="margin-left: 7px;margin-top: -5px;">
        <el-form-item label="登录账号:">
          <el-select v-model="modalData.mesUser" placeholder="请选择" style="width: 234px">
            <el-option
              v-for = "i in mesList"
              :key = "i.username"
              :label = "i.username"
              :value = "i.username">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-footer style="height:40px;margin-top: 10px;text-align:center">
        <el-button type="primary" @click="saveData()">保存</el-button>
        <el-button type="primary" @click="modalFlag = false">关闭</el-button>
      </el-footer>
    </el-dialog>

    <!-- 部门 -->
    <el-dialog title="部门清单" :close-on-click-modal="false" v-drag :visible.sync="departmentModelFlag" width="520px">
      <div class="rq">
        <el-form :inline="true" label-position="top" :model="departmentData">
          <el-form-item :label="'部门编码'">
            <el-input v-model="departmentData.deptId" clearable style="width: 120px"></el-input>
          </el-form-item>
          <el-form-item :label="'部门名称'">
            <el-input v-model="departmentData.deptName" clearable style="width: 120px"></el-input>
          </el-form-item>
          <el-form-item :label="' '">
            <el-button type="primary" @click="getDepartmentList()">查询</el-button>
          </el-form-item>
        </el-form>
        <el-table
          :height="300"
          :data="departmentList"
          ref="departmentTable"
          @row-click="departmentClickRow"
          @selection-change="selectionDepartment"
          :row-key="getRowKeys"
          border
          v-loading="dataListLoading"
          style="width: 100%;">
          <el-table-column
            type="selection"
            header-align="center"
            align="center"
            :reserve-selection="true"
            width="50">
          </el-table-column>
          <el-table-column
            v-for="(item,index) in departmentDetailList" :key="index"
            :sortable="item.columnSortable"
            :prop="item.columnProp"
            :header-align="item.headerAlign"
            :show-overflow-tooltip="item.showOverflowTooltip"
            :align="item.align"
            :fixed="item.fixed==''?false:item.fixed"
            :min-width="item.columnWidth"
            :label="item.columnLabel">
            <template slot-scope="scope">
              <span v-if="!item.columnHidden">{{scope.row[item.columnProp]}}</span>
              <span v-if="item.columnImage"><img :src="scope.row[item.columnProp]" style="width: 100px; height: 80px"/></span>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-footer style="height:40px;margin-top: 20px;text-align:center">
        <el-button type="primary" @click="confirmDepartment()">确认</el-button>
        <el-button type="primary" @click="departmentModelFlag = false">关闭</el-button>
      </el-footer>
    </el-dialog>

    <Chooselist ref="baseList" @getBaseData="getBaseData"></Chooselist>
  </div>
</template>

<script>
  import {
    eamAdminSearch,
    eamAdminSave,
    eamAdminEdit,
    eamAdminDelete,
    getEmpyDeptList,
    getMesList, // 获取可用的MES账号
  } from "@/api/eam/eam.js"
  import Chooselist from '@/views/modules/common/Chooselist_eam'
  import {userFavoriteList, saveUserFavorite, removeUserFavorite} from '@/api/userFavorite.js'
  export default {
    components: {
      Chooselist
    },
    watch: {
      searchData: {
        deep: true,
        handler: function (newV, oldV) {
          this.searchData.adminID = this.searchData.adminID.toUpperCase()
        }
      },
      modalData: {
        deep: true,
        handler: function (newV, oldV) {
          this.modalData.adminID = this.modalData.adminID.toUpperCase()
          this.modalData.levelID = this.modalData.levelID.toUpperCase()
          //this.modalData.department= this.modalData.department.toUpperCase()
        }
      }
    },
    data () {
      return {
        // 是否收藏
        favorite: false,
        // 导出 start
        exportData: [],
        exportName: "机修人员" + this.dayjs().format('YYYYMMDDHHmmss'),
        exportHeader: ["机修人员"],
        exportFooter: [],
        exportList:[],
        // 导出 end
        tagNo:'',
        searchData: {
          site: this.$store.state.user.site,
          adminID: '',
          adminName: '',
          departmentName: '',
          active: 'Y',
          page: 1,
          limit: 10,
        },
        pageIndex: 1,
        pageSize: 50,
        totalPage: 0,
        height: 200,
        dataList: [],
        dataListLoading: false,
        dataListSelections: [],
        modalFlag:false,
        modalDisableFlag:false,
        modalData:{
          flag:'',
          site: this.$store.state.user.site,
          adminID:'',
          adminName:'',
          active:'',
          department:'',
          departmentName:'',
          phone:'',
          tel:'',
          email:'',
          levelID:'',
          levelDesc: '',
          mesUser:'',
          createdBy: this.$store.state.user.name,
          updateBy: this.$store.state.user.name,
        },
        departmentList:[],
        // 展示列集
        columnList: [
          {
            userId: this.$store.state.user.name,
            functionId: 101001007,
            serialNumber: '101001007Table1AdminID',
            tableId: "101001007Table1",
            tableName: "机修人员表",
            columnProp: 'adminID',
            headerAlign: "center",
            align: "left",
            columnLabel: '机修人员编码',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 80,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101001007,
            serialNumber: '101001007Table1AdminName',
            tableId: "101001007Table1",
            tableName: "机修人员表",
            columnProp: 'adminName',
            headerAlign: "center",
            align: "left",
            columnLabel: '机修人员姓名',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 117,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101001007,
            serialNumber: '101001007Table1Active',
            tableId: "101001007Table1",
            tableName: "机修人员表",
            columnProp: 'active',
            headerAlign: "center",
            align: "center",
            columnLabel: '在用',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 70,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101001007,
            serialNumber: '101001007Table1DepartmentName',
            tableId: "101001007Table1",
            tableName: "机修人员表",
            columnProp: 'departmentName',
            headerAlign: "center",
            align: "left",
            columnLabel: '部门',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 370,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101001007,
            serialNumber: '101001007Table1Phone',
            tableId: "101001007Table1",
            tableName: "机修人员表",
            columnProp: 'phone',
            headerAlign: "center",
            align: "center",
            columnLabel: '手机号码',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 80,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101001007,
            serialNumber: '101001007Table1Tel',
            tableId: "101001007Table1",
            tableName: "机修人员表",
            columnProp: 'tel',
            headerAlign: "center",
            align: "center",
            columnLabel: '公司座机',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 90,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101001007,
            serialNumber: '101001007Table1Email',
            tableId: "101001007Table1",
            tableName: "机修人员表",
            columnProp: 'email',
            headerAlign: "center",
            align: "left",
            columnLabel: '邮箱',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 161,
          },

          {
            userId: this.$store.state.user.name,
            functionId: 101001007,
            serialNumber: '101001007Table1AdminLevelDesc',
            tableId: "101001007Table1",
            tableName: "机修人员表",
            columnProp: 'adminLevelDesc',
            headerAlign: "center",
            align: "left",
            columnLabel: '工种',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 143,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101001007,
            serialNumber: '101001007Table1MesUser',
            tableId: "101001007Table1",
            tableName: "机修人员表",
            columnProp: 'mesUser',
            headerAlign: "center",
            align: "left",
            columnLabel: '登录账号',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 70,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101001007,
            serialNumber: '101001007Table1CreatedDate',
            tableId: "101001007Table1",
            tableName: "机修人员表",
            columnProp: 'createdDate',
            headerAlign: "center",
            align: "center",
            columnLabel: '创建时间',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 125,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101001007,
            serialNumber: '101001007Table1CreatedBy',
            tableId: "101001007Table1",
            tableName: "机修人员表",
            columnProp: 'createdBy',
            headerAlign: "center",
            align: "left",
            columnLabel: '创建人',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 70,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101001007,
            serialNumber: '101001007Table1UpdateDate',
            tableId: "101001007Table1",
            tableName: "机修人员表",
            columnProp: 'updateDate',
            headerAlign: "center",
            align: "center",
            columnLabel: '更新时间',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 125,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101001007,
            serialNumber: '101001007Table1UpdateBy',
            tableId: "101001007Table1",
            tableName: "机修人员表",
            columnProp: 'updateBy',
            headerAlign: "center",
            align: "left",
            columnLabel: '更新人',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 70,
          },
        ],
        levelList:[],
        departmentModelFlag: false,
        departmentData: {
          site: this.$store.state.user.site,
          deptId: '',
          deptName: '',
        },
        departmentListSelections: [],
        departmentDetailList: [
          {
            columnProp: 'deptId',
            headerAlign: "center",
            align: "left",
            columnLabel: '部门编码',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
          },
          {
            columnProp: 'deptName',
            headerAlign: "center",
            align: "left",
            columnLabel: '部门名称',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
          },
        ],
        mesList: [],
        rules:{
          adminIdType:[
            {
              required: true,
              message: ' ',
              trigger: 'change'
            }
          ],
          adminNameType:[
            {
              required: true,
              message: ' ',
              trigger: 'change'
            }
          ],
          activeType:[
            {
              required: true,
              message: ' ',
              trigger: 'change'
            }
          ],
          departmentNameType:[
            {
              required: true,
              message: ' ',
              trigger: 'change'
            }
          ],
          levelDescType:[
            {
              required: true,
              message: ' ',
              trigger: 'change'
            }
          ],
        }
      }
    },
    mounted () {
      this.$nextTick(() => {
        this.height = window.innerHeight - 210
      })
    },
    created () {
      this.favoriteIsOk()
      this.getDataList()
      // 动态列
      this.getTableUserColumn(this.$route.meta.menuId+'table1',1)
    },
    methods: {
      /**
       *  自适应
       * @param label
       * @param prop
       * @returns {string}
       */
      flexColumnWidth (label, prop) {
        // 1.获取该列的所有数据
        const arr = this.dataList.map(x => x[prop])
        arr.push(label) // 把每列的表头也加进去算
        // 2.计算每列内容最大的宽度 + 表格的内间距（依据实际情况而定）
        return (this.getMaxLength(arr) + 25) + 'px'
      },
      getTextWidth (str) {
        let width = 0
        const html = document.createElement('span')
        html.innerText = str
        html.className = 'getTextWidth'
        document.querySelector('body').appendChild(html)
        width = document.querySelector('.getTextWidth').offsetWidth
        document.querySelector('.getTextWidth').remove()
        return width
      },
      getMaxLength (arr) {
        return arr.reduce((acc, item) => {
          if (item) {
            const calcLen = this.getTextWidth(item)
            if (acc < calcLen) {
              acc = calcLen
            }
          }
          return acc
        }, 0)
      },

      // 部门
      departmentClickRow(row){
        this.$refs.departmentTable.toggleRowSelection(row);
      },
      // 多选
      selectionDepartment (val) {
        this.departmentListSelections = val
      },
      getRowKeys(row) {
        //唯一值，一般都为id
        return row.deptId;
      },
      selectFlag(){
        return true;
      },

      // 确认多选部门
      confirmDepartment(){
        if(this.departmentListSelections.length === 0){
          this.$alert('请勾选部门！', '提示', {
            confirmButtonText: '确定'
          })
          return false
        }
        this.modalData.department = ''
        this.modalData.departmentName = ''
        for (let i = 0; i < this.departmentListSelections.length; i++) {
          this.modalData.department = this.modalData.department + ";" + this.departmentListSelections[i].deptId
          this.modalData.departmentName = this.modalData.departmentName + ";" + this.departmentListSelections[i].deptName
        }
        this.modalData.department = this.modalData.department.substring(1)
        this.modalData.departmentName = this.modalData.departmentName.substring(1)
        this.departmentModelFlag = false
      },

      // 获取基础数据列表S
      getBaseList (val) {
        this.tagNo = val
        this.$nextTick(() => {
          let strVal = ''
          if (val === 202) {
            strVal = this.modalData.levelID
          }
          this.$refs.baseList.init(val, strVal)
        })
      },
      /* 列表方法的回调 */
      getBaseData (val) {
        if (this.tagNo === 202) {
          this.modalData.levelID = val.AdminLevelID
          this.$set(this.modalData,'levelDesc',val.AdminLevelDesc);
        }
      },
      //导出excel
      async createExportData() {
        // this.searchData.limit = -1
        // this.searchData.page = 1
        await eamAdminSearch(this.searchData).then(({data}) => {
          this.exportList= data.rows
        })
        return this.exportList;
      },
      startDownload() {

      },
      finishDownload() {

      },
      fields() {
        let json = "{"
        this.columnList.forEach((item, index) => {
          if (index == this.columnList.length - 1) {
            json += "\"" + item.columnLabel + "\"" + ":" + "\"" + item.columnProp + "\""
          } else {
            json += "\"" + item.columnLabel + "\"" + ":" + "\"" + item.columnProp + "\"" + ","
          }
        })
        json += "}"
        let s = eval("(" + json + ")")
        return s
      },
      // 导出 end
      // 获取数据列表
      getDataList () {
        // this.searchData.limit = this.pageSize
        // this.searchData.page = this.pageIndex
        eamAdminSearch(this.searchData).then(({data}) => {
          if (data.code === 0) {
            this.dataList = data.rows
            // this.pageIndex = data.page.currPage
            // this.pageSize = data.page.pageSize
            // this.totalPage = data.page.totalCount
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      addModal(){
        this.modalData = {
          flag:'1',
          site: this.$store.state.user.site,
          adminID:'',
          adminName:'',
          active:'Y',
          departmentName: '',
          department:'',
          phone:'',
          tel:'',
          email:'',
          levelID:'',
          levelDesc:'',
          mesUser:'',
          createdBy: this.$store.state.user.name,
          updateBy: this.$store.state.user.name,
        };
        let inData = {
          site: this.$store.state.user.site,
        }
        getMesList(inData).then(({data}) => {
          this.mesList = data.rows
        })
        this.modalDisableFlag = false;
        this.modalFlag = true;
      },
      updateModal(row){
        this.modalData = {
          flag: '2',
          site: row.site,
          adminID: row.adminID,
          adminName: row.adminName,
          active: row.active,
          department: row.department,
          departmentName: row.departmentName,
          phone: row.phone,
          tel: row.tel,
          email: row.email,
          levelID: row.levelID,
          levelDesc: row.adminLevelDesc,
          mesUser: row.mesUser,
          updateBy: this.$store.state.user.name,
        };
        let inData = {
          site: this.$store.state.user.site,
        }
        getMesList(inData).then(({data}) => {
          this.mesList = data.rows
        })
        this.modalDisableFlag = true;
        this.modalFlag = true;
      },
      // 删除
      deleteAdmin (row) {
        this.$confirm(`是否删除这个机修人员?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'

        }).then(() => {
          eamAdminDelete(row).then(({data}) => {
            if (data && data.code === '0') {
              this.getDataList()
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                }
              })
            } else {
              this.$alert(data.msg, '错误', {
                confirmButtonText: '确定'
              })
            }
          })
        }).catch(() => {
        })
      },
      saveData(){
        if (this.modalData.adminID === '' || this.modalData.adminID == null) {
          this.$alert('请输入机修人员编码！', '错误', {
            confirmButtonText: '确定'
          })
          return false
        }
        if (this.modalData.adminName === '' || this.modalData.adminName == null) {
          this.$alert('请输入机修人员姓名！', '错误', {
            confirmButtonText: '确定'
          })
          return false
        }
        if (this.modalData.active === '' || this.modalData.active == null) {
          this.$alert('选择是否在用！', '错误', {
            confirmButtonText: '确定'
          })
          return false
        }
        if (this.modalData.department === '' || this.modalData.department == null) {
          this.$alert('请选择部门！', '错误', {
            confirmButtonText: '确定'
          })
          return false
        }
        if (this.modalData.levelID === '' || this.modalData.levelID == null) {
          this.$alert('请选择工种！', '错误', {
            confirmButtonText: '确定'
          })
          return false
        }
        if(this.modalData.flag === '1'){
          eamAdminSave(this.modalData).then(({data}) => {
            if (data && data.code === '0') {
              this.getDataList()
              this.modalFlag = false
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                }
              })
            } else {
              this.$alert(data.msg, '错误', {
                confirmButtonText: '确定'
              })
            }
          })
        }else {
          eamAdminEdit(this.modalData).then(({data}) => {
            if (data && data.code === '0') {
              this.getDataList()
              this.modalFlag=false
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                }
              })
            } else {
              this.$alert(data.msg, '错误', {
                confirmButtonText: '确定'
              })
            }
          })
        }
      },

      // 获取部门列表
      getDepartmentList(){
        // 先清空缓存选中
        this.$nextTick(() => this.$refs.departmentTable.clearSelection())
        // 拿到选中的部门编号
        let tempDataList = (this.modalData.department == null ? '' : this.modalData.department).split(';')
        // 查询所有部门
        getEmpyDeptList(this.departmentData).then(({data}) => {
          if (data && data.code === 0) {
            this.departmentList = data.rows
            this.departmentList.forEach(val => {
              // 回显选中的部门
              if (tempDataList.includes(val.deptId)) {
                this.$nextTick(() => this.$refs.departmentTable.toggleRowSelection(val, true))
              }
            })
            this.departmentModelFlag = true;
          }else {
            this.$alert(data.msg, '错误', {
              confirmButtonText: '确定'
            })
          }
        })
      },

      // 校验用户是否收藏
      favoriteIsOk() {
        let userFavorite = {
          userId: this.$store.state.user.id,
          languageCode: this.$i18n.locale
        }
        userFavoriteList(userFavorite).then(({data}) => {
          for (let i = 0; i < data.list.length; i++) {
            if(this.$route.meta.menuId==data.list[i].menuId){
              this.favorite = true
            }
          }
        })
      },
      // 收藏 OR 取消收藏
      favoriteFunction() {
        let userFavorite = {
          userId: this.$store.state.user.id,
          functionId: this.$route.meta.menuId,
        }
        if (this.favorite) {
          removeUserFavorite(userFavorite).then(({data}) => {
            this.$message.success(data.msg)
            this.favorite = false
          })
        } else {
          // 收藏
          saveUserFavorite(userFavorite).then(({data}) => {
            this.$message.success(data.msg)
            this.favorite = true
          })
        }
      },
      // 动态列开始 获取 用户保存的 格式列
      async getTableUserColumn(tableId, columnId) {
        let queryTableUser = {
          userId: this.$store.state.user.name,
          functionId: this.$route.meta.menuId,
          tableId: tableId,
          status: true,
          languageCode: this.$i18n.locale
        }
        await getTableUserListLanguage(queryTableUser).then(({data}) => {
          if (data.rows.length > 0) {
            //this.columnList1 = []
            switch (columnId) {
              case 1:
                this.columnList = data.rows
                break;
              // case 2:
              //   this.detailColumnList = data.rows
              //   break;
              // case 3:
              //   this.columnList2 = data.rows
              //   break;
              // case 4:
              //   this.columnList3 = data.rows
              //   break;
            }

          } else {
            this.getColumnList(tableId, columnId)
          }
        })

      },

      // 获取 tableDefault 列
      async getColumnList(tableId, columnId) {
        this.queryTable.tableId = tableId
        let   queryTable= {
          functionId: this.$route.meta.menuId,
          tableId: tableId,
          languageCode: this.$i18n.locale
        }
        await getTableDefaultListLanguage(queryTable).then(({data}) => {
          if (!data.rows.length == 0) {
            switch (columnId) {
              case 1:
                this.columnList = data.rows
                break;
              // case 2:
              //   this.detailColumnList = data.rows
              //   break;
              // case 3:
              //   this.columnList2 = data.rows
              //   break;
              // case 4:
              //   this.columnList3 = data.rows
              //   break;
            }
          } else {
            // this.showDefault = true.
          }
        })

      },
      //动态列结束
    },


  }
</script>
