<template>
  <div class="mod-user">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.userName" placeholder="用户账号" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()" type="primary">{{ buttons.search || '查询' }}</el-button>
        <el-button v-if="isAuth('sys:user:save')" type="primary" @click="addOrUpdateHandle()">
          {{ buttons.add || '新增' }}
        </el-button>
        <el-button v-if="isAuth('sys:user:save')" :disabled="this.showButton" type="primary"
                   @click="specialAuthorize()"
        >{{ buttons.specialCompetencies || '特殊权限' }}
        </el-button>
        <el-button v-if="isAuth('sys:user:save')" :disabled="this.showButton" type="primary"
                   @click="siteAuthorize()"
        > {{ buttons.factoryAuthorization || '工厂授权' }}
        </el-button>
          <el-button v-if="isAuth('sys:user:save')" :disabled="this.showButton" type="primary"
                     @click="buAuthorize()"
          > {{ buttons.buAuthorization || 'BU授权' }}
        </el-button>
        <el-button v-if="isAuth('sys:user:save')" :disabled="this.showButton" type="primary"
                   @click="projectModel()"
        > {{ buttons.projectAuthorization || '项目授权' }}
        </el-button>

        <el-button v-if="isAuth('sys:user:save')" :disabled="this.showButton" type="primary"
                   @click="FieldListModel()"
        > {{ buttons.fieldAuthorization || '字段授权' }}
        </el-button>
        <el-button v-if="isAuth('sys:user:save')" :disabled="this.showButton" type="primary"
                   @click="roleAuthorize()"
        > {{ buttons.roleAuthorization || '角色授权' }}
        </el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="dataList"
      border
      :height="height"
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      highlight-current-row
      @current-change="handleCurrentChange"
      style="width: 100%;">

      <el-table-column
        prop="username"
        header-align="center"
        align="center"
        :label="buttons.username||'用户账号'">
      </el-table-column>
      <el-table-column
        prop="userDisplay"
        header-align="center"
        align="center"
        :label="buttons.userDisplay||'用户名'">
      </el-table-column>
      <el-table-column
        prop="buDesc"
        header-align="center"
        align="center"
        :label="'BU'">
      </el-table-column>
      <el-table-column
        prop="departmentName"
        header-align="center"
        align="center"
        :label="'部门'">
      </el-table-column>
      <el-table-column
        prop="postName"
        header-align="center"
        align="center"
        :label="'岗位'">
      </el-table-column>
      <el-table-column
        prop="email"
        header-align="center"
        align="center"
        :label="buttons.email||'邮箱'">
      </el-table-column>
      <el-table-column
        prop="mobile"
        header-align="center"
        align="center"
        :label="buttons.mobile||'手机号'">
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        :label="buttons.status||'状态'">
        <template slot-scope="scope">
          <el-link v-if="scope.row.status === 0" type="success">{{ buttons.disable || '禁用' }}</el-link>
          <el-link v-else type="danger">{{ buttons.normal || '正常' }}</el-link>
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        width="180"
        :label="buttons.createTime||'创建时间'">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        :label="buttons.cz||'操作'">
        <template slot-scope="scope">
<!--          <a type="text" size="small" @click="generateData(scope.row)">{{ buttons.auth || '审核' }}</a>-->
          <a type="text" size="small" @click="permissionList(scope.row)">权限清单</a>
          <a v-if="isAuth('sys:user:update')" type="text" size="small"
             @click="addOrUpdateHandle(scope.row.userId)">{{ buttons.edit || '修改' }}</a>
          <a v-if="isAuth('sys:user:delete')" type="text" size="small"
             @click="deleteHandle(scope.row.userId)">{{ buttons.delete || '删除' }}</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="closeDataDialog" @open="getUserAccessSiteInfoList" :close-on-click-modal="false" v-drag :visible.sync="permissionFlag" width="900px">
      <el-form label-position="top" style="margin-top: 1px; margin-left: 0px;">
        用户：   <el-input v-model="selectUser.username" style="width: 130px" readonly></el-input>
        名称：   <el-input v-model="selectUser.userDisplay" style="width: 130px" readonly></el-input>
        <el-tabs v-model="activeTable" style="margin-top: 0; width: 100%; min-height: 350px;box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);border: 2px;" @tab-click="tabClick" class="customer-tab">
          <!-- 工厂权限页签 -->
          <el-tab-pane label="工厂权限" name="factory_information" style="height: 350px;">
            <el-table
              :data="userSiteList"
              border
              style="width: 100%;height: 500px">
              <el-table-column
                prop="siteID"
                header-align="center"
                align="center"
                min-width="50"
                label="工厂编码">
              </el-table-column>
              <el-table-column
                prop="siteName"
                header-align="center"
                align="center"
                min-width="100"
                label="工厂名称">
              </el-table-column>
              <el-table-column
                prop="address"
                header-align="center"
                align="center"
                min-width="100"
                label="地址">
              </el-table-column>
            </el-table>
          </el-tab-pane>
          <!-- BU权限页签 -->
          <el-tab-pane label="BU权限" name="BU_information" style="height: 350px;">
            <el-table
              :data="userBUList"
              border
              style="width: 100%;height: 500px">
              <el-table-column
                prop="site"
                header-align="center"
                align="center"
                min-width="50"
                label="工厂编码">
              </el-table-column>
              <el-table-column
                prop="buNo"
                header-align="center"
                align="center"
                min-width="100"
                label="部门编码">
              </el-table-column>
              <el-table-column
                prop="buDesc"
                header-align="center"
                align="center"
                min-width="100"
                label="部门名称">
              </el-table-column>
            </el-table>
          </el-tab-pane>
          <!-- 项目权限页签 -->
          <el-tab-pane label="项目权限" name="project_information" style="height: 350px;">
            <el-table
              :data="userProjectList"
              border
              style="width: 100%;height: 500px">
              <el-table-column
                prop="site"
                header-align="center"
                align="center"
                min-width="50"
                label="工厂编码">
              </el-table-column>
              <el-table-column
                prop="buDesc"
                header-align="center"
                align="center"
                min-width="100"
                label="部门名称">
              </el-table-column>
              <el-table-column
                prop="projectId"
                header-align="center"
                align="center"
                min-width="50"
                label="项目号">
              </el-table-column>
              <el-table-column
                prop="status"
                header-align="center"
                align="center"
                min-width="100"
                label="项目状态">
              </el-table-column>
              <el-table-column
                prop="needDate"
                header-align="center"
                align="center"
                min-width="100"
                label="要求完成时间">
              </el-table-column>
            </el-table>
          </el-tab-pane>
          <!-- 字段权限页签 -->
          <el-tab-pane label="字段权限" name="field_function_information" style="height: 350px;">
            <el-table
              :data="userFieldList"
              border
              style="width: 100%;height: 500px">
              <el-table-column
                prop="functionName"
                header-align="center"
                align="center"
                min-width="50"
                label="功能名称">
              </el-table-column>
              <el-table-column
                prop="functionGroup"
                header-align="center"
                align="center"
                min-width="100"
                label="功能分组">
              </el-table-column>
              <el-table-column
                prop="fieldDesc"
                header-align="center"
                align="center"
                min-width="50"
                label="字段名称">
              </el-table-column>
            </el-table>
          </el-tab-pane>
          <!-- 特殊权限页签 -->
          <el-tab-pane label="特殊权限" name="security_information" style="height: 350px;">
            <el-table
              :data="securityList"
              border
              v-loading="dataListLoading"
              style="width: 100%;height: 500px">
              <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.columnHidden">
              <el-checkbox v-model="scope.row[item.columnProp] "></el-checkbox>
            </span>
                  <span v-if="item.columnImage"><img :src="scope.row[item.columnProp]"
                                                     style="width: 100px; height: 80px"/></span>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
          <!-- 角色权限页签 -->
          <el-tab-pane label="角色权限" name="role_information" style="height: 350px;">
            <el-table
              :data="userRoleList"
              border
              style="width: 100%;height: 500px">
              <el-table-column
                prop="site"
                header-align="center"
                align="center"
                min-width="50"
                label="工厂编码">
              </el-table-column>
              <el-table-column
                prop="roleNo"
                header-align="center"
                align="center"
                min-width="100"
                label="角色编码">
              </el-table-column>
              <el-table-column
                prop="roleDesc"
                header-align="center"
                align="center"
                min-width="100"
                label="角色名称">
              </el-table-column>
            </el-table>
          </el-tab-pane>
        </el-tabs>
      </el-form>
      <el-footer style="height:40px;margin-top: 20px;text-align:center">
        <el-button @click="permissionFlag = false" type="primary">关闭</el-button>
      </el-footer>
    </el-dialog>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <!--  审核权限   -->
    <el-dialog :close-on-click-modal="false" :close-on-press-escape="false" v-drag :title="inputSearch1"
               :visible.sync="setUp.reviewFlag" width="518px">
      <template>
        <el-transfer v-model="checked" :data="transferData"
                     :titles="[buttons.allAuth || '所有权限', buttons.currentAuth || '当前权限']"></el-transfer>
      </template>
      <span slot="footer" class="dialog-footer">
         <div style="margin-top: 5px">
        <el-button type="primary" :disabled="setUp.saveButton" @click="saveUserRoleFlag()">确定</el-button>
        <el-button type="primary" @click="setUp.reviewFlag = false">取消</el-button>
         </div>
    </span>
    </el-dialog>
    <!--  特殊权限  -->
    <el-dialog
      v-drag
      :title="'特殊性权限'"
      :visible.sync="specialVisible"
      width="820px"
      :append-to-body="true">
      <el-table
        :data="securityList"
        border
        :height="securityHeight"
        v-loading="dataListLoading"
        style="width: 100%;">
        <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.columnHidden">
              <el-checkbox v-model="scope.row[item.columnProp] "></el-checkbox>
            </span>
            <span v-if="item.columnImage"><img :src="scope.row[item.columnProp]"
                                               style="width: 100px; height: 80px"/></span>
          </template>
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="saveUserSecurity()">确定</el-button>
        <el-button @click="specialVisible = false" type="primary">取消</el-button>
    </span>
    </el-dialog>
    <!--  工厂授权  -->
    <el-dialog
      style="font-size: 12px"
      v-drag
      :title="'工厂授权'"
      :visible.sync="siteVisible"
      width="518px"
      :append-to-body="true"
      @close="closeDataDialog">
      <el-transfer class="rq" filterable v-model="selectSitList" :data="siteList" :titles="['未授权工厂', '已授权工厂']"></el-transfer>
      <span slot="footer" class="dialog-footer">
        <div style="margin-top: 5px">
                <el-button type="primary" @click="saveUserAssessSite()">确定</el-button>
                <el-button @click="siteVisible = false" type="primary">取消</el-button>
        </div>

    </span>
    </el-dialog>

    <!--  BU授权  -->
    <el-dialog
      style="font-size: 12px"
      v-drag
      :title="'BU授权'"
      :visible.sync="buVisible"
      width="518px"
      :append-to-body="true">
      <el-transfer class="rq" filterable v-model="selectBUList" :props="{
      key: 'buNo',
      label: 'buDesc'
    }"  :data="buList" :titles="['未授权BU', '已授权BU']"></el-transfer>
      <span slot="footer" class="dialog-footer">
        <div style="margin-top: 5px">
                <el-button type="primary" @click="saveBuAssessSite()">确定</el-button>
                <el-button @click="buVisible = false" type="primary">取消</el-button>
        </div>

    </span>
    </el-dialog>
    <!--  功能清单  -->
    <el-dialog
      width="600px"
      title="功能清单"
      :close-on-click-modal="false"
      :visible.sync="functionFlag">
      <el-form label-position="top" style="margin-top: 1px; margin-left: 0px;">
        用户：   <el-input v-model="selectUser.username" style="width: 130px" readonly></el-input>
        名称：   <el-input v-model="selectUser.userDisplay" style="width: 130px" readonly></el-input>
      </el-form>
      <el-table
        height="300"
        :data="functionList"
        border
        style="width: 100%">
        <el-table-column
          prop="functionName"
          header-align="center"
          align="left"
          min-width="80"
          label="功能名称">
        </el-table-column>
        <el-table-column
          prop="functionGroup"
          header-align="center"
          align="left"
          min-width="100"
          label="功能分组">
        </el-table-column>
        <el-table-column
          prop=""
          header-align="center"
          align="center"
          width="60"
          label="操作">
          <template slot-scope="scope" class="foo_container">
            <a type="text" size="small" @click="accessModel(scope.row)">授权</a>

          </template>
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="functionFlag = false">关闭</el-button>
      </span>
    </el-dialog>
    <!--  字段授权  -->
    <el-dialog
      style="font-size: 12px"
      v-drag
      :title="'字段授权'"
      :visible.sync="fieldFlag"
      width="518px"
      :append-to-body="true">
      <el-transfer v-model="selectFieldList"  class="rq" filterable :props="{
      key: 'fieldId',
      label: 'fieldDesc'
    }" :data="fieldList" :titles="['未选择', '已选择']"></el-transfer>
      <span slot="footer" class="dialog-footer">
        <div style="margin-top: 5px">
                <el-button type="primary" @click="saveAccessFieldListByUser()">确定</el-button>
                <el-button @click="fieldFlag = false" type="primary">取消</el-button>
        </div>
    </span>
    </el-dialog>
    <!--  角色授权  -->
    <el-dialog
      style="font-size: 12px"
      v-drag
      :title="'角色授权'"
      :visible.sync="roleVisible"
      width="518px"
      :append-to-body="true">
      <el-transfer class="rq" filterable v-model="selectRoleList" :props="{
      key: 'roleNo',
      label: 'roleDesc'
    }"  :data="roleList" :titles="['未授权角色', '已授权角色']"></el-transfer>
      <span slot="footer" class="dialog-footer">
        <div style="margin-top: 5px">
                <el-button type="primary" @click="saveRoleAssessSite()">确定</el-button>
                <el-button @click="roleVisible = false" type="primary">取消</el-button>
        </div>

    </span>
    </el-dialog>

    <el-dialog
      width="600px"
      title="项目权限"
      :close-on-click-modal="false"
      :visible.sync="projectRoleFlag">
      <el-table
        :data="projectRoleList"
        border
        :height="400"
        v-loading="false"
        style="width: 100%;">
        <el-table-column
          prop="projectId"
          header-align="center"
          align="center"
          min-width="50"
          label="项目号">
        </el-table-column>
        <el-table-column
          prop="projectDesc"
          header-align="center"
          align="center"
          min-width="100"
          label="项目描述">
        </el-table-column>
        <el-table-column
          prop=""
          header-align="center"
          align="center"
          min-width="25"
          label="">
          <template slot-scope="scope">
            <!-- 如果数据为Y，显示勾选框，否则不勾选 -->
            <el-checkbox v-model="scope.row.checkAll"  @change="changeAll(scope.row)"></el-checkbox>
          </template>
        </el-table-column>
        <el-table-column
          prop=""
          header-align="center"
          align="center"
          min-width="25"
          label="查询">
          <template slot-scope="scope">
            <!-- 如果数据为Y，显示勾选框，否则不勾选 -->
            <el-checkbox v-model="scope.row.searchCheck"  @change="changeCheck(scope.row,1)"></el-checkbox>
          </template>
        </el-table-column>
        <el-table-column
          prop=""
          header-align="center"
          align="center"
          min-width="25"
          label="修改">
          <template slot-scope="scope">
            <!-- 如果数据为Y，显示勾选框，否则不勾选 -->
            <el-checkbox v-model="scope.row.updateCheck"  @change="changeCheck(scope.row,2)"></el-checkbox>
          </template>
        </el-table-column>
        <el-table-column
          prop=""
          header-align="center"
          align="center"
          min-width="25"
          label="下载">
          <template slot-scope="scope">
            <!-- 如果数据为Y，显示勾选框，否则不勾选 -->
            <el-checkbox v-model="scope.row.downCheck"  @change="changeCheck(scope.row,3)"></el-checkbox>
          </template>
        </el-table-column>
        <el-table-column
          prop=""
          header-align="center"
          align="center"
          min-width="25"
          label="删除">
          <template slot-scope="scope">
            <!-- 如果数据为Y，显示勾选框，否则不勾选 -->
            <el-checkbox v-model="scope.row.deleteCheck"  @change="changeCheck(scope.row,4)"></el-checkbox>
          </template>
        </el-table-column>
      </el-table>

      <span slot="footer" class="dialog-footer">
        <div style="margin-top: 5px">
                <el-button type="primary" @click="saveAccessProject()">确定</el-button>
                <el-button @click="projectRoleFlag = false" type="primary">取消</el-button>
        </div>
    </span>
    </el-dialog>
  </div>
</template>

<script>
import {getUserAccessSiteList, saveUserAccessSiteList} from '@/api/factory/accessSite.js'
import {getSiteList} from '@/api/factory/site.js'
import {searchAccessBuList,searchAccessRoleList,saveAccessBU,saveAccessRole,searchAccessProjectForUser,saveAccessProject, getUserAccessSiteInfoList, getUserAccessBUInfoList, getUserAccessRoleInfoList, getUserAccessProjectInfoList} from '@/api/base/site.js'
import {
  searchFieldFunctionList,searchAccessFieldListByUser,saveAccessFieldListByUser,getUserAccessFieldFunctionInfoList
} from "@/api/base/field.js"
import AddOrUpdate from './user-add-or-update'
import {getUserBusinessRoleList, saveUserBusinessRole} from '@/api/auditManagement/auditType.js'
import {

  getUserSpecialSecurity,
  saveUserSpecialSecurity,
  updateUserSpecialSecurity,
} from '@/api/factory/userSpecialSecurity.js'
import {debounce, throttle} from "../../../utils/common";
import {
  searchFunctionButtonList,
} from "@/api/sysLanguage.js"
import {
  getBUList,
  getDepartmentList,
  getPostList,
  saveAccessSiteForSite,
  searchAccessSiteListBySite
} from '../../../api/base/site'

export default {
  data() {
    return {
      accessSiteVisible: false,
      projectRoleFlag: false,
      projectRoleList: [],
      accessSiteList: [],
      showButton: true,
      height: 200,
      securityHeight: 300,
      specialVisible: false,
      siteVisible: false,
      buVisible: false,
      roleVisible: false,
      currentData: {},
      userId: this.$store.state.user.name,
      transferData: [],
      checked: [],
      selectBUList: [],
      buList: [],
      selectRoleList: [],
      roleList:[],
      userSiteList: [],
      userBUList:[],
      userRoleList:[],
      userProjectList:[],
      userFieldList:[],
      userData: [],
      inputSearch1: '审核权限',
      //设置
      setUp: {
        reviewFlag: false,
        saveButton: false,
      },
      dataForm: {
        id: 0,
        userName: '',
        password: '',
        comfirmPassword: '',
        languageDefault: '',
        salt: '',
        email: '',
        mobile: '',
        buNo: '',
        buDesc: '',
        roleNo: '',
        roleDesc: '',
        departmentNo:'',
        departmentName:'',
        postNo:'',
        postName:'',
        roleIdList: [],
        status: 1,
        site: '',
        siteID: '',
        siteName:'',
        userDisplay: ''
      },
      // 初始页签
      activeTable: 'factory_information',
      columnList: [
        {
          userId: this.$store.state.user.name,
          serialNumber: 'security' + this.$route.meta.menuId + 'securitydesc',
          tableId: this.$route.meta.menuId + 'security',
          tableName: "securityTable",
          columnProp: "securitydesc",
          headerAlign: "center",
          align: "left",
          columnLabel: "参数描述",
          columnHidden: false,
          columnImage: false,
          columnWidth: 70,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
        },
        {
          userId: this.$store.state.user.name,
          serialNumber: 'security' + this.$route.meta.menuId + 'securitydesc',
          tableId: this.$route.meta.menuId + 'security',
          tableName: "securityTable",
          columnProp: "module",
          headerAlign: "center",
          align: "left",
          columnLabel: "模块",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          columnWidth: 20,
          sortLv: 0,
          status: true,
          fixed: '',
        },
        {
          userId: this.$store.state.user.name,
          serialNumber: 'security' + this.$route.meta.menuId + 'securitydesc',
          tableId: this.$route.meta.menuId + 'security',
          tableName: "securityTable",
          columnProp: "itemvalue",
          headerAlign: "center",
          align: "center",
          columnLabel: "参数值",
          columnHidden: true,
          columnImage: false,
          columnSortable: false,
          columnWidth: 10,
          sortLv: 0,
          status: true,
          fixed: '',
        },
      ],
      buttons: {
        cz: '操作',
        search: '查询',
        add: '添加',
        permissionList: '权限清单',
        edit: '编辑',
        delete: '删除',
        specialCompetencies: '特殊权限',
        factoryAuthorization: '工厂授权',
        buAuthorization: 'BU授权',
        roleAuthorization: '角色授权',
        projectAuthorization: '项目授权',
        createTime: '创建时间',
        username: '用户账号',
        userDisplay: '用户名',
        email: '邮箱',
        mobile: '手机号',
        status: '状态',
        disable: '禁用',
        normal: '正常',
        auth: '审核',
        allAuth: '所有权限',
        currentAuth: '当前权限',
        fieldAuthorization:'字段授权',
        role:'授权',
      },
      dataList: [],
      securityList: [],
      selectSpecialList: [],
      siteList: [],
      selectSitList: [],
      selectUser: {},
      pageIndex: 1,
      pageSize: 20,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      functionFlag:false,
      functionList:[],
      rowFunctionId:'',
      selectFieldList:[],
      fieldList:[],
      fieldFlag:false,
      permissionFlag:false,
    }
  },
  components: {
    AddOrUpdate
  },
  activated() {
    this.getDataList()
  },
  mounted() {
    this.$nextTick(() => {
      this.securityHeight = window.innerHeight - 300;
      this.height = window.innerHeight - 200;
    })
  },
  methods: {
    // 获取button的词典
    getFunctionButtonList() {
      let queryButton = {
        functionId: this.$route.meta.menuId,
        tableId: '*',
        languageCode: this.$i18n.locale,
        objectType: 'button'
      }
      searchFunctionButtonList(queryButton).then(({data}) => {
        if (data.code == 0 && data.data) {
          this.buttons = data.data
        }
      })
    },
    /**
     * 列表表格选择替换
     * @param tab
     * @param event
     */
    tabClick (tab, event) {
      // 刷新列表数据
      this.refreshCurrentTabTable()
    },
    refreshCurrentTabTable() {
      if (this.activeTable === 'factory_information') {
        this.getUserAccessSiteInfoList()
      }
      if (this.activeTable === 'BU_information') {
        this.getUserAccessBUInfoList()
      }
      if (this.activeTable === 'project_information'){
        this.getUserAccessProjectInfoList()
      }
      if (this.activeTable === 'field_function_information'){
        this.getUserAccessFieldFunctionInfoList()
      }
      if (this.activeTable === 'security_information'){
        this.getUserSpecialSecurity()
      }
      if (this.activeTable === 'role_information'){
        this.getUserAccessRoleInfoList()
      }
    },
    // 特殊权限
    specialAuthorize() {
      this.specialVisible = true
      this.searchUserSpecialSecurity()
    },
    // 获取特殊权限
    searchUserSpecialSecurity() {
      saveUserSpecialSecurity({userid: this.selectUser.username}).then(({data}) => {
        if (data.code === 0) {
          getUserSpecialSecurity(this.selectUser.username).then(({data}) => {
            if (data.code === 0) {
              this.securityList = data.dataList.map(item => {
                item.itemvalue = item.itemvalue == 'Y' ? true : false;
                return item;
              })
            }
          })
        }
      })
    },
    // 保存用户权限
    saveUserSecurity() {
      let securityList = JSON.parse(JSON.stringify(this.securityList)).map(item => {
        item.itemvalue = item.itemvalue === true ? 'Y' : 'N';
        return item;
      })
      updateUserSpecialSecurity(securityList).then(({data}) => {
        if (data.code === 0) {
          this.$message.success(data.msg)
          this.specialVisible = false
        } else {
          this.$message.success(data.msg)
        }
      })
    },
    // 授权工厂
    siteAuthorize() {
      this.siteVisible = true
      this.searchSiteList()

    },
    buAuthorize() {
      this.buVisible = true
      this.searchBuList()

    },
    roleAuthorize() {
      this.roleVisible = true
      this.searchRoleList()
    },
    //授权项目start
    projectModel(){
      let inData={
        site:this.$store.state.user.site,
        username:this.selectUser.username,
      }
      searchAccessProjectForUser(inData).then(({data}) => {
        this.projectRoleList=data.rows
      })
      this.projectRoleFlag=true
    },
    changeAll(row){
      if(row.checkAll){
        row.searchCheck=true;
        row.updateCheck=true;
        row.downCheck=true;
        row.deleteCheck=true;
        row.searchFlag='Y'
        row.updateFlag='Y'
        row.downFlag='Y'
        row.deleteFlag='Y'
      }else {
        row.searchCheck=false;
        row.updateCheck=false;
        row.downCheck=false;
        row.deleteCheck=false;
        row.searchFlag='N'
        row.updateFlag='N'
        row.downFlag='N'
        row.deleteFlag='N'
      }
    },
    changeCheck(row,type){
      if(type==1){
        if(row.searchCheck){
          row.searchFlag='Y'
        }else {
          row.searchFlag='N'
        }
      }
      if(type==2){
        if(row.updateCheck){
          row.updateFlag='Y'
        }else {
          row.updateFlag='N'
        }
      }
      if(type==3){
        if(row.downCheck){
          row.downFlag='Y'
        }else {
          row.downFlag='N'
        }
      }
      if(type==4){
        if(row.deleteCheck){
          row.deleteFlag='Y'
        }else {
          row.deleteFlag='N'
        }
      }
      if(row.searchCheck&&row.updateCheck&&row.downCheck&&row.deleteCheck){
        row.checkAll=true
      }else{
        row.checkAll=false
      }
      this.$forceUpdate();
    },
    saveAccessProject(){
      let inData={
        site:this.$store.state.user.site,
        username:this.selectUser.username,
        accessProjectList:this.projectRoleList
      }
      saveAccessProject(inData).then(({data}) => {
          if (data && data.code === 0) {
            this.$message.success( '操作成功')
            this.projectRoleFlag = false
          } else {
            this.$message.error(data.msg)
          }
        })
    },
    //授权项目end
    // 保存选中的授权工厂
    saveUserAssessSite() {
      let accessSiteList = this.selectSitList.map(item => {
        let itemSite = {
          userid: this.selectUser.username,
          site: item
        }
        return itemSite;
      })
      let accessSite = {
        userid: this.selectUser.username,
        accessSiteList: accessSiteList
      }

      saveUserAccessSiteList(accessSite).then(({data}) => {
        if (data.code === 0) {
          this.$message.success(data.msg)
          this.siteList = []
          this.selectSitList = []
          this.siteVisible = false
        } else {
          this.$message.warning(data.msg)
        }
      })
    },
    saveBuAssessSite(){
      if(this.selectBUList.length==0){
        this.$message.warning("请选择BU")
        return false;
      }
      let inList=[];
      for (let i = 0; i < this.selectBUList.length; i++) {
        let inData={
          site:this.$store.state.user.site,
          username:this.selectUser.username,
          buNo:this.selectBUList[i]
        }
        inList.push(inData)
      }
      saveAccessBU(inList).then(({data}) => {
        if (data && data.code === 0) {
          this.$message.success( '操作成功')
          this.buVisible = false
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    saveRoleAssessSite(){
      if(this.selectRoleList.length==0){
        this.$message.warning("请选择角色")
        return false;
      }
      let inList=[];
      for (let i = 0; i < this.selectRoleList.length; i++) {
        let inData={
          site:this.$store.state.user.site,
          username:this.selectUser.username,
          roleNo:this.selectRoleList[i]
        }
        inList.push(inData)
      }
      saveAccessRole(inList).then(({data}) => {
        if (data && data.code === 0) {
          this.$message.success( '操作成功')
          this.roleVisible = false
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    // 获取用户已授权的工厂列表
    searchUserAccessSiteList() {
      getUserAccessSiteList({userid: this.selectUser.username}).then(({data}) => {
        this.selectSitList = data.dataList
      })
    },
    // 获取所有可用工厂
    searchSiteList() {
      this.siteList=[]
      getSiteList({active: 'Y'}).then(({data}) => {
        if (data.code == 0) {
          this.siteList = data.dataList
          this.searchUserAccessSiteList()
        }
      })
    },
    searchBuList(){
      searchAccessBuList({site:this.$store.state.user.site,
        username: this.selectUser.username,
        active:'Y'}).then(({data}) => {
        this.selectBUList = data.selectBUList
        this.buList=data.buList
      })
    },
    searchRoleList(){
      searchAccessRoleList({site:this.$store.state.user.site,
        username: this.selectUser.username,
        active:'Y'}).then(({data}) => {
        this.selectRoleList = data.selectRoleList
        this.roleList=data.roleList
      })
    },
    // 选中行
    handleCurrentChange(val) {
      this.selectUser = val
      this.showButton = false
    },
    saveUserRoleFlag() {
      let saveList = []
      let currentList = this.checked
      // 当前未选择记录
      if (currentList.length == 0) {
        this.$confirm("当前未选择记录,是否继续？", '删除提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
        }).then(() => {
          let data = {
            userId: this.currentData.userId,
          }
          saveList.push(data);
          this.saveUserBusinessRole(saveList)
        }).catch(() => {
          return
        })
      } else {
        for (let i = 0; i < currentList.length; i++) {
          let data = {
            userId: this.currentData.userId,
            roleItemNo: currentList[i],
            selectFlag: 'Y',
          }
          saveList.push(data);
        }
        this.saveUserBusinessRole(saveList)
      }
    },
    saveUserBusinessRole(saveList) {
      saveUserBusinessRole(saveList).then(({data}) => {
        this.$alert(data.msg, '操作提示', {
          confirmButtonText: '确定',
          callback: action => {
            if (data.code == 0) {
              this.setUp.reviewFlag = false
            }
            this.setUp.saveButton = false
          }
        });
      })
    },
    generateData(row) {
      this.currentData = row
      let queryData = {
        active: 'Y',
        userId: this.currentData.userId,
      }
      getUserBusinessRoleList(queryData).then(({data}) => {
        let rows1Data = data.map.rows1
        let rows2Data = data.map.rows2
        let forData = []
        let checkedList = []
        for (let i = 0; i < rows1Data.length; i++) {
          forData.push({
            key: rows1Data[i].roleItemNo,
            label: rows1Data[i].roleDesc,
          });
        }
        for (let j = 0; j < rows2Data.length; j++) {
          checkedList.push(rows2Data[j].roleItemNo)
        }
        this.transferData = forData;
        this.checked = checkedList
        this.setUp.reviewFlag = true
      })
    },
    // 获取数据列表
    getDataList: throttle(function () {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/sys/user/list'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
          'username': this.dataForm.userName
        })
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    }, 1000),
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 权限清单模态框
    permissionList(row) {
      this.activeTable = 'factory_information'
      this.userData = row
      this.permissionFlag = true
    },
    // 获取用户工厂权限信息
    getUserAccessSiteInfoList () {
      getUserAccessSiteInfoList(this.userData).then(({data}) => {
        if (data && data.code === 0) {
          this.userSiteList = data.rows
        } else {
          this.$alert(data.msg, '错误', {
            confirmButtonText: '确定'
          })
        }
      })
    },
    // 获取用户BU权限信息
    getUserAccessBUInfoList () {
      getUserAccessBUInfoList(this.userData).then(({data}) => {
        if (data && data.code === 0) {
          this.userBUList = data.rows
        } else {
          this.$alert(data.msg, '错误', {
            confirmButtonText: '确定'
          })
        }
      })
    },
    // 获取用户角色权限信息
    getUserAccessRoleInfoList () {
      getUserAccessRoleInfoList(this.userData).then(({data}) => {
        if (data && data.code === 0) {
          this.userRoleList = data.rows
        } else {
          this.$alert(data.msg, '错误', {
            confirmButtonText: '确定'
          })
        }
      })
    },
    // 获取用户项目权限信息
    getUserAccessProjectInfoList () {
      getUserAccessProjectInfoList(this.userData).then(({data}) => {
        if (data && data.code === 0) {
          this.userProjectList = data.rows
        } else {
          this.$alert(data.msg, '错误', {
            confirmButtonText: '确定'
          })
        }
      })
    },
    // 获取用户字段权限信息
    getUserAccessFieldFunctionInfoList () {
      getUserAccessFieldFunctionInfoList(this.userData).then(({data}) => {
        if (data && data.code === 0) {
          this.userFieldList = data.rows
        } else {
          this.$alert(data.msg, '错误', {
            confirmButtonText: '确定'
          })
        }
      })
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle(id) {
      var userIds = id ? [id] : this.dataListSelections.map(item => {
        return item.userId
      })
      this.$confirm(`确定对[id=${userIds.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/sys/user/delete'),
          method: 'post',
          data: this.$http.adornData(userIds, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message.success('操作成功')
            this.getDataList()
          } else {
            this.$message.error(data.msg)
          }
        })
      }).catch(() => {
      })
    },
    FieldListModel(){
      searchFieldFunctionList().then(({data}) => {
        this.functionList=data.rows
      });
      this.functionFlag=true;
    },
    accessModel(row){
      this.rowFunctionId=row.functionId;
      searchAccessFieldListByUser({functionId:row.functionId,
        username: this.selectUser.username,
       }).then(({data}) => {
        this.selectFieldList = data.accessFieldList
        this.fieldList=data.fieldList
      })
      this.fieldFlag=true;
    },
    saveAccessFieldListByUser(){
      let inList=[];
      for (let i = 0; i < this.selectFieldList.length; i++) {
        let inData={
          username: this.selectUser.username,
          fieldId:this.selectFieldList[i]
        }
        inList.push(inData)
      }
      let saveData={
        username: this.selectUser.username,
        functionId:this.rowFunctionId,
        accessFieldList:inList,
      }
      saveAccessFieldListByUser(saveData).then(({data}) => {
        if (data && data.code === 0) {
          this.$message.success( '操作成功')
          this.fieldFlag = false
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    closeDataDialog() {
      this.userSiteList=[];
      this.userBUList=[];
      this.userProjectList=[];
      this.userFieldList=[];
      this.siteList=[];
      this.selectSitList=[];
      this.userRoleList=[];
    },
  },
  created() {
    this.getFunctionButtonList()
  }
}
</script>
<style>


.el-dialog__title {
  line-height: 24px;
  font-size: 16px;
  color: #303133;
}
</style>
