<template>
  <div class="scroll">
    <div class="middle">
      <div class="filter">
        <div class="condition">
          <span>角色 :</span>
          <el-input
            clearable
            v-model.trim="searchInput.keyword"
            class="w-50 m-2"
            placeholder="请输入角色"
            style="width: 180px"
          />
        </div>
        <div class="condition">
          <span>创建时间 :</span>
          <el-date-picker
            v-model="searchInput.createTime"
            unlink-panels
            type="datetimerange"
            range-separator="-"
            start-placeholder="起始时间"
            end-placeholder="结束时间"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择日期"
          />
        </div>
        <el-button
          style="margin-left: 20px"
          color="rgba(38, 151, 255, 1)"
          type="primary"
          class="search"
          @click="searchBtn"
          ><span>查询</span></el-button
        >
        <el-button @click="resetBtn" plain color="rgba(43, 153, 255, 1)"
          >重置</el-button
        >
      </div>
      <!-- 按钮列表 -->
      <div class="gongneng">
        <el-button
          type="primary"
          style="margin-left: 0"
          color="rgba(38, 151, 255, 1)"
          @click="addlist"
          >新增</el-button
        >
      </div>
    </div>
    <div class="footer" v-loading="loading">
      <el-table
        :row-class-name="tableRowClassName"
        :data="tableData.list"
        @selection-change="handleSelectionChange"
        style="width: 100%"
        :header-cell-style="{
          background: 'rgba(240, 243, 247, 1)',
          height: '50px',
          border: 0,
        }"
      >
        <!-- <el-table-column type="selection" align="center" width="55" /> -->
        <el-table-column type="index" align="center" label="序号" width="60" />
        <el-table-column align="center" prop="roleName" label="角色" />
        <el-table-column
          show-overflow-tooltip
          align="center"
          prop="remark"
          label="描述"
        />
        <el-table-column align="center" prop="userNum" label="用户数" />
        <el-table-column align="center" prop="createTime" label="创建时间" />
        <el-table-column align="center" label="操作" fixed="right" width="200">
          <template #default="{ row }">
            <el-button type="primary" @click="updateS(row)" link
              >编辑</el-button
            >
            <el-button type="danger" @click="deleteS(row)" link>删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <div class="pageSize">
        <span></span>
        <el-pagination
          background
          :current-page="currentPage"
          :page-size="pageSize"
          :page-sizes="[10, 20, 30, 40]"
          layout="total,sizes, prev, pager, next, jumper, slot"
          :total="total"
          @size-change="handleSizeChange"
          @update:current-page="handleCurrentChange"
        />
      </div>
    </div>
  </div>
  <!-- 添加账号弹窗 -->
  <el-dialog
    class="addStaff"
    v-model="addDialogVisible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :title="dialongTitle"
    align-center
    width="1000"
    :before-close="cancelAdd"
    destroy-on-close
    draggable
  >
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      :rules="rules"
      label-width="100px"
      class="demo-ruleForm"
      :size="formSize"
      label-position="left"
      status-icon
    >
      <el-form-item label="角色名称 :" prop="roleName">
        <el-input
          clearable
          v-model.trim="ruleForm.roleName"
          class="w-50 m-2"
          placeholder="请输入角色名称"
        />
      </el-form-item>
      <el-form-item label="角色描述 :" prop="remark">
        <el-input
          clearable
          v-model.trim="ruleForm.remark"
          class="w-50 m-2"
          placeholder="请输入角色描述"
        />
      </el-form-item>
      <p style="margin: 0 0 18px 10px">角色权限配置 :</p>

      <el-table
        :data="menuList.list"
        default-expand-all
        row-key="id"
        border
        @selection-change="handleSelectionChange"
        style="width: 100%"
      >
        <!-- <el-table-column type="selection" align="center" width="55" /> -->
        <el-table-column
          align="center"
          width="150"
          prop="menuName"
          label="一级菜单"
        >
        </el-table-column>
        <el-table-column
          align="center"
          width="150"
          prop="menuName"
          label="二级菜单"
          class-name="second-menu-column"
        >
          <template #default="{ row }">
            <p class="level" v-for="i in row.childs" :key="i.id">
              {{ i.menuName }}
            </p>
          </template>
        </el-table-column>
        <el-table-column
          class-name="third-menu-column"
          align="left"
          prop="createTime"
          label="功能"
        >
          <template #default="{ row }">
            <div class="level" v-for="i in row.childs" :key="i.id">
              <div v-if="i.menuName == '校友信息管理'">
                <p>
                  <el-checkbox
                    class="level"
                    label="全选"
                    v-model="i.canAll"
                    :true-value="1"
                    :false-value="0"
                    @change="handleCheckAllChange($event, i, '校友信息管理')"
                  >
                  </el-checkbox>
                </p>
                <el-checkbox
                  v-for="menu in menuPermissionMap.xyxxgl"
                  :key="menu.keyword"
                  v-model="i[menu.keyword]"
                  :label="menu.name"
                  size="large"
                  :true-value="1"
                  :false-value="0"
                  @change="handleCheckChange($event, i, '校友信息管理')"
                />
              </div>
              <div v-if="i.menuName == '校友组织列表'">
                <p>
                  <el-checkbox
                    class="level"
                    label="全选"
                    v-model="i.canAll"
                    @change="handleCheckAllChange($event, i, '校友组织列表')"
                    :true-value="1"
                    :false-value="0"
                  >
                  </el-checkbox>
                </p>
                <el-checkbox
                  v-for="menu in menuPermissionMap.xyzzlb"
                  :key="menu.keyword"
                  v-model="i[menu.keyword]"
                  :label="menu.name"
                  size="large"
                  :true-value="1"
                  :false-value="0"
                  @change="handleCheckChange($event, i, '校友组织列表')"
                />
                <div>
                  <span>组织城市 : </span>
                  <el-select
                  clearable
                    v-model="i.provinces"
                    placeholder="请选择省"
                    style="width: 140px"
                    @change="provincesChange($event, i, '校友组织列表')"
                  >
                    <el-option
                      v-for="item in provinceList"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id"
                    />
                  </el-select>
                  <span> - </span>
                  <el-select
                  clearable
                    v-model="i.citys"
                    placeholder="请选择市"
                    style="width: 140px"
                  >
                    <el-option
                      v-for="item in xyzzlbCityList"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id"
                    />
                  </el-select>
                </div>
              </div>
              <div v-if="i.menuName == '校友活动列表'">
                <p>
                  <el-checkbox
                    class="level"
                    label="全选"
                    v-model="i.canAll"
                    @change="handleCheckAllChange($event, i, '校友活动列表')"
                    :true-value="1"
                    :false-value="0"
                  >
                  </el-checkbox>
                </p>
                <el-checkbox
                  v-for="menu in menuPermissionMap.xyhdlb"
                  :key="menu.keyword"
                  v-model="i[menu.keyword]"
                  :label="menu.name"
                  size="large"
                  :true-value="1"
                  :false-value="0"
                  @change="handleCheckChange($event, i, '校友活动列表')"
                />
              </div>
              <div v-if="i.menuName == '广场信息管理'">
                <p>
                  <el-checkbox
                    class="level"
                    label="全选"
                    v-model="i.canAll"
                    @change="handleCheckAllChange($event, i, '广场信息管理')"
                    :true-value="1"
                    :false-value="0"
                  >
                  </el-checkbox>
                </p>
                <el-checkbox
                  v-for="menu in menuPermissionMap.gcxxgl"
                  :key="menu.keyword"
                  v-model="i[menu.keyword]"
                  :label="menu.name"
                  size="large"
                  :true-value="1"
                  :false-value="0"
                  @change="handleCheckChange($event, i, '广场信息管理')"
                />
              </div>
              <div v-if="i.menuName == '组织分类管理'">
                <p>
                  <el-checkbox
                    class="level"
                    label="全选"
                    v-model="i.canAll"
                    @change="handleCheckAllChange($event, i, '组织分类管理')"
                    :true-value="1"
                    :false-value="0"
                  >
                  </el-checkbox>
                </p>
                <el-checkbox
                  v-for="menu in menuPermissionMap.zzflgl"
                  :key="menu.keyword"
                  v-model="i[menu.keyword]"
                  :label="menu.name"
                  size="large"
                  :true-value="1"
                  :false-value="0"
                  @change="handleCheckChange($event, i, '组织分类管理')"
                />
              </div>
              <div v-if="i.menuName == '校企合作管理'">
                <p>
                  <el-checkbox
                    class="level"
                    label="全选"
                    v-model="i.canAll"
                    @change="handleCheckAllChange($event, i, '校企合作管理')"
                    :true-value="1"
                    :false-value="0"
                  >
                  </el-checkbox>
                </p>
                <el-checkbox
                  v-for="menu in menuPermissionMap.xqhzgl"
                  :key="menu.keyword"
                  v-model="i[menu.keyword]"
                  :label="menu.name"
                  size="large"
                  :true-value="1"
                  :false-value="0"
                  @change="handleCheckChange($event, i, '校企合作管理')"
                />
              </div>
              <div v-if="i.menuName == '校友企业管理'">
                <p>
                  <el-checkbox
                    class="level"
                    label="全选"
                    v-model="i.canAll"
                    @change="handleCheckAllChange($event, i, '校友企业管理')"
                    :true-value="1"
                    :false-value="0"
                  >
                  </el-checkbox>
                </p>
                <el-checkbox
                  v-for="menu in menuPermissionMap.xyqygl"
                  :key="menu.keyword"
                  v-model="i[menu.keyword]"
                  :label="menu.name"
                  size="large"
                  :true-value="1"
                  :false-value="0"
                  @change="handleCheckChange($event, i, '校友企业管理')"
                />
              </div>
              <div v-if="i.menuName == '校友风采管理'">
                <p>
                  <el-checkbox
                    class="level"
                    label="全选"
                    v-model="i.canAll"
                    @change="handleCheckAllChange($event, i, '校友风采管理')"
                    :true-value="1"
                    :false-value="0"
                  >
                  </el-checkbox>
                </p>
                <el-checkbox
                  v-for="menu in menuPermissionMap.xyfcgl"
                  :key="menu.keyword"
                  v-model="i[menu.keyword]"
                  :label="menu.name"
                  size="large"
                  :true-value="1"
                  :false-value="0"
                  @change="handleCheckChange($event, i, '校友风采管理')"
                />
              </div>
              <div v-if="i.menuName == '新闻聚焦列表'">
                <p>
                  <el-checkbox
                    class="level"
                    label="全选"
                    v-model="i.canAll"
                    @change="handleCheckAllChange($event, i, '新闻聚焦列表')"
                    :true-value="1"
                    :false-value="0"
                  >
                  </el-checkbox>
                </p>
                <el-checkbox
                  v-for="menu in menuPermissionMap.xwjjlb"
                  :key="menu.keyword"
                  v-model="i[menu.keyword]"
                  :label="menu.name"
                  size="large"
                  :true-value="1"
                  :false-value="0"
                  @change="handleCheckChange($event, i, '新闻聚焦列表')"
                />
              </div>
              <div v-if="i.menuName == '入会申请审核'">
                <p>
                  <el-checkbox
                    class="level"
                    label="全选"
                    v-model="i.canAll"
                    @change="handleCheckAllChange($event, i, '入会申请审核')"
                    :true-value="1"
                    :false-value="0"
                  >
                  </el-checkbox>
                </p>
                <el-checkbox
                  v-for="menu in menuPermissionMap.rhshsh"
                  :key="menu.keyword"
                  v-model="i[menu.keyword]"
                  :label="menu.name"
                  size="large"
                  :true-value="1"
                  :false-value="0"
                  @change="handleCheckChange($event, i, '入会申请审核')"
                />
                <div>
                  <span>组织城市 : </span>
                  <el-select
                  clearable
                    v-model="i.provinces"
                    placeholder="请选择省"
                    style="width: 140px"
                    @change="provincesChange($event, i, '入会申请审核')"
                  >
                    <el-option
                      v-for="item in provinceList"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id"
                    />
                  </el-select>
                  <span> - </span>
                  <el-select
                  clearable
                    v-model="i.citys"
                    placeholder="请选择市"
                    style="width: 140px"
                  >
                    <el-option
                      v-for="item in rhshshCityList"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id"
                    />
                  </el-select>
                </div>
              </div>
              <div v-if="i.menuName == '活动报名审核'">
                <p>
                  <el-checkbox
                    class="level"
                    label="全选"
                    v-model="i.canAll"
                    @change="handleCheckAllChange($event, i, '活动报名审核')"
                    :true-value="1"
                    :false-value="0"
                  >
                  </el-checkbox>
                </p>
                <el-checkbox
                  v-for="menu in menuPermissionMap.hdbmsh"
                  :key="menu.keyword"
                  v-model="i[menu.keyword]"
                  :label="menu.name"
                  size="large"
                  :true-value="1"
                  :false-value="0"
                  @change="handleCheckChange($event, i, '活动报名审核')"
                />
              </div>
              <div v-if="i.menuName == '新闻分类管理'">
                <p>
                  <el-checkbox
                    class="level"
                    label="全选"
                    v-model="i.canAll"
                    @change="handleCheckAllChange($event, i, '新闻分类管理')"
                    :true-value="1"
                    :false-value="0"
                  >
                  </el-checkbox>
                </p>
                <el-checkbox
                  v-for="menu in menuPermissionMap.xwflgl"
                  :key="menu.keyword"
                  v-model="i[menu.keyword]"
                  :label="menu.name"
                  size="large"
                  :true-value="1"
                  :false-value="0"
                  @change="handleCheckChange($event, i, '新闻分类管理')"
                />
              </div>
              <div v-if="i.menuName == '返校服务管理'">
                <p>
                  <el-checkbox
                    class="level"
                    label="全选"
                    v-model="i.canAll"
                    @change="handleCheckAllChange($event, i, '返校服务管理')"
                    :true-value="1"
                    :false-value="0"
                  >
                  </el-checkbox>
                </p>
                <el-checkbox
                  v-for="menu in menuPermissionMap.fxfwgl"
                  :key="menu.keyword"
                  v-model="i[menu.keyword]"
                  :label="menu.name"
                  size="large"
                  :true-value="1"
                  :false-value="0"
                  @change="handleCheckChange($event, i, '返校服务管理')"
                />
              </div>
              <div v-if="i.menuName == '电子校友卡管理'">
                <p>
                  <el-checkbox
                    class="level"
                    label="全选"
                    v-model="i.canAll"
                    @change="handleCheckAllChange($event, i, '电子校友卡管理')"
                    :true-value="1"
                    :false-value="0"
                  >
                  </el-checkbox>
                </p>
                <el-checkbox
                  v-for="menu in menuPermissionMap.dzxykgl"
                  :key="menu.keyword"
                  v-model="i[menu.keyword]"
                  :label="menu.name"
                  size="large"
                  :true-value="1"
                  :false-value="0"
                  @change="handleCheckChange($event, i, '电子校友卡管理')"
                />
              </div>
              <div v-if="i.menuName == '校友相册列表'">
                <p>
                  <el-checkbox
                    class="level"
                    label="全选"
                    v-model="i.canAll"
                    @change="handleCheckAllChange($event, i, '校友相册列表')"
                    :true-value="1"
                    :false-value="0"
                  >
                  </el-checkbox>
                </p>
                <el-checkbox
                  v-for="menu in menuPermissionMap.xyxclb"
                  :key="menu.keyword"
                  v-model="i[menu.keyword]"
                  :label="menu.name"
                  size="large"
                  :true-value="1"
                  :false-value="0"
                  @change="handleCheckChange($event, i, '校友相册列表')"
                />
              </div>
              <div v-if="i.menuName == '相册分类管理'">
                <p>
                  <el-checkbox
                    class="level"
                    label="全选"
                    v-model="i.canAll"
                    @change="handleCheckAllChange($event, i, '相册分类管理')"
                    :true-value="1"
                    :false-value="0"
                  >
                  </el-checkbox>
                </p>
                <el-checkbox
                  v-for="menu in menuPermissionMap.xcflgl"
                  :key="menu.keyword"
                  v-model="i[menu.keyword]"
                  :label="menu.name"
                  size="large"
                  :true-value="1"
                  :false-value="0"
                  @change="handleCheckChange($event, i, '相册分类管理')"
                />
              </div>
              <div v-if="i.menuName == '照片上传审核'">
                <p>
                  <el-checkbox
                    class="level"
                    label="全选"
                    v-model="i.canAll"
                    @change="handleCheckAllChange($event, i, '照片上传审核')"
                    :true-value="1"
                    :false-value="0"
                  >
                  </el-checkbox>
                </p>
                <el-checkbox
                  v-for="menu in menuPermissionMap.zpscsh"
                  :key="menu.keyword"
                  v-model="i[menu.keyword]"
                  :label="menu.name"
                  size="large"
                  :true-value="1"
                  :false-value="0"
                  @change="handleCheckChange($event, i, '照片上传审核')"
                />
              </div>
              <div v-if="i.menuName == '角色管理'">
                <p>
                  <el-checkbox
                    class="level"
                    label="全选"
                    v-model="i.canAll"
                    @change="handleCheckAllChange($event, i, '角色管理')"
                    :true-value="1"
                    :false-value="0"
                  >
                  </el-checkbox>
                </p>
                <el-checkbox
                  v-for="menu in menuPermissionMap.jsgl"
                  :key="menu.keyword"
                  v-model="i[menu.keyword]"
                  :label="menu.name"
                  size="large"
                  :true-value="1"
                  :false-value="0"
                  @change="handleCheckChange($event, i, '角色管理')"
                />
              </div>
              <div v-if="i.menuName == '用户管理'">
                <p>
                  <el-checkbox
                    class="level"
                    label="全选"
                    v-model="i.canAll"
                    @change="handleCheckAllChange($event, i, '用户管理')"
                    :true-value="1"
                    :false-value="0"
                  >
                  </el-checkbox>
                </p>
                <el-checkbox
                  v-for="menu in menuPermissionMap.yhgl"
                  :key="menu.keyword"
                  v-model="i[menu.keyword]"
                  :label="menu.name"
                  size="large"
                  :true-value="1"
                  :false-value="0"
                  @change="handleCheckChange($event, i, '用户管理')"
                />
              </div>
              <div v-if="i.menuName == '操作日志'">
                <p>
                  <el-checkbox
                    class="level"
                    label="全选"
                    v-model="i.canAll"
                    @change="handleCheckAllChange($event, i, '操作日志')"
                    :true-value="1"
                    :false-value="0"
                  >
                  </el-checkbox>
                </p>
                <el-checkbox
                  v-for="menu in menuPermissionMap.czrz"
                  :key="menu.keyword"
                  v-model="i[menu.keyword]"
                  :label="menu.name"
                  size="large"
                  :true-value="1"
                  :false-value="0"
                  @change="handleCheckChange($event, i, '操作日志')"
                />
              </div>
              <div v-if="i.menuName == '系统设置'">
                <p>
                  <el-checkbox
                    class="level"
                    label="全选"
                    v-model="i.canAll"
                    @change="handleCheckAllChange($event, i, '系统设置')"
                    :true-value="1"
                    :false-value="0"
                  >
                  </el-checkbox>
                </p>
                <el-checkbox
                  v-for="menu in menuPermissionMap.xtsz"
                  :key="menu.keyword"
                  v-model="i[menu.keyword]"
                  :label="menu.name"
                  size="large"
                  :true-value="1"
                  :false-value="0"
                  @change="handleCheckChange($event, i, '系统设置')"
                />
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-form-item class="options">
        <el-button @click="cancelAdd">取消</el-button>
        <el-button
          color="rgba(0, 97, 255, 1)"
          class="queding"
          type="primary"
          @click="submitAdd(ruleFormRef)"
        >
          确定
        </el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script setup>
import {
  ref,
  watch,
  reactive,
  nextTick,
  onBeforeMount,
  onUnmounted,
} from "vue";
import { useRouter } from "vue-router";
import { genFileId, ElMessage, ElMessageBox } from "element-plus";
import { dayjs } from "element-plus";
import lodash from "lodash";

import { storeToRefs } from "pinia";
import { useCounterStore } from "@/stores/index";

import {
  getQueryRolesPage,
  addRole,
  deleteRole,
  updateRole,
  getAllMenus,
  getByIdMenu,
  getProvince,
  getCity,
} from "@/api/role";

const router = useRouter();
const store = useCounterStore();

// 为避免解构时失去响应性
const { name, age, isCollapse, realAge, collegeRole } = storeToRefs(store);

const props = { label: "name", value: "id", children: "children" };

// 表格数据
const loading = ref(false);
const tableData = reactive({
  list: [{}],
});
const dialongTitle = ref("新增角色"); // 弹窗标题

const searchInput = reactive({
  keyword: "",
  createTime: "",
}); // 搜索按钮数据

const currentPage = ref(1); // 当前页
const pageSize = ref(10);
const total = ref(0); // 当前总数

const addDialogVisible = ref(false); // 控制添加账号弹窗
const menuList = reactive({
  list: [],
});
const menuListCopy = reactive({
  list: [],
});

const provinceList = ref([]);
const xyzzlbCityList = ref([]); // 校友组织列表
const rhshshCityList = ref([]); // 入会申请审核

// 表单数据
const formSize = ref("default");
const ruleFormRef = ref();
const ruleForm = reactive({
  roleName: "",
  remark: "", //备注
  id: "",
});
// 表单验证
const rules = reactive({
  roleName: [{ required: true, message: "角色名称不能为空", trigger: "blur" }],
  // remark: [{ required: true, message: "描述不能为空", trigger: "blur" }],
});

const menuPermissionMap = {
  xyxxgl: [
    {
      name: "查看权限",
      keyword: "canView",
      value: 1,
    },
    {
      name: "新增",
      keyword: "canInsert",
      value: 1,
    },
    {
      name: "编辑权限",
      keyword: "canUpdate",
      value: 1,
    },
    {
      name: "删除权限",
      keyword: "canDeleted",
      value: 1,
    },
    {
      name: "导出权限",
      keyword: "canExport",
      value: 1,
    },
    {
      name: "导入权限",
      keyword: "canImport",
      value: 1,
    },
  ], // 校友信息管理
  gcxxgl: [
    {
      name: "查看权限",
      keyword: "canView",
      value: 1,
    },
    {
      name: "详情权限",
      keyword: "canDetail",
      value: 1,
    },
    {
      name: "审核权限",
      keyword: "canApply",
      value: 1,
    },
    {
      name: "删除权限",
      keyword: "canDeleted",
      value: 1,
    },
  ], // 广场信息管理
  xyzzlb: [
    {
      name: "查看权限",
      keyword: "canView",
      value: 1,
    },
    {
      name: "详情权限",
      keyword: "canDetail",
      value: 1,
    },
    {
      name: "创建组织",
      keyword: "canInsert",
      value: 1,
    },
    {
      name: "编辑权限",
      keyword: "canUpdate",
      value: 1,
    },
    {
      name: "删除权限",
      keyword: "canDeleted",
      value: 1,
    },
  ], // 校友组织列表
  zzflgl: [
    {
      name: "查看权限",
      keyword: "canView",
      value: 1,
    },
    {
      name: "创建分类",
      keyword: "canInsert",
      value: 1,
    },
    {
      name: "编辑权限",
      keyword: "canUpdate",
      value: 1,
    },
    {
      name: "删除权限",
      keyword: "canDeleted",
      value: 1,
    },
  ], // 组织分类管理
  rhshsh: [
    {
      name: "查看权限",
      keyword: "canView",
      value: 1,
    },
    {
      name: "详情权限",
      keyword: "canDetail",
      value: 1,
    },

    {
      name: "审核权限",
      keyword: "canApply",
      value: 1,
    },
    {
      name: "删除权限",
      keyword: "canDeleted",
      value: 1,
    },
  ], // 入会申请审核
  xyhdlb: [
    {
      name: "查看权限",
      keyword: "canView",
      value: 1,
    },
    {
      name: "详情权限",
      keyword: "canDetail",
      value: 1,
    },
    {
      name: "创建活动",
      keyword: "canInsert",
      value: 1,
    },
    {
      name: "编辑权限",
      keyword: "canUpdate",
      value: 1,
    },

    {
      name: "删除权限",
      keyword: "canDeleted",
      value: 1,
    },
  ], // 校友活动列表
  hdbmsh: [
    {
      name: "查看权限",
      keyword: "canView",
      value: 1,
    },
    {
      name: "详情权限",
      keyword: "canDetail",
      value: 1,
    },
    {
      name: "审核权限",
      keyword: "canApply",
      value: 1,
    },
    {
      name: "删除权限",
      keyword: "canDeleted",
      value: 1,
    },
  ], // 活动报名审核
  xqhzgl: [
    {
      name: "查看权限",
      keyword: "canView",
      value: 1,
    },
    {
      name: "详情权限",
      keyword: "canDetail",
      value: 1,
    },

    {
      name: "审核权限",
      keyword: "canApply",
      value: 1,
    },
    {
      name: "删除权限",
      keyword: "canDeleted",
      value: 1,
    },
  ], // 校企合作管理
  xyqygl: [
    {
      name: "查看权限",
      keyword: "canView",
      value: 1,
    },
    {
      name: "详情权限",
      keyword: "canDetail",
      value: 1,
    },

    {
      name: "审核权限",
      keyword: "canApply",
      value: 1,
    },
    {
      name: "删除权限",
      keyword: "canDeleted",
      value: 1,
    },
  ], // 校友企业管理
  xyfcgl: [
    {
      name: "查看权限",
      keyword: "canView",
      value: 1,
    },

    {
      name: "新增",
      keyword: "canInsert",
      value: 1,
    },
    {
      name: "编辑权限",
      keyword: "canUpdate",
      value: 1,
    },

    {
      name: "删除权限",
      keyword: "canDeleted",
      value: 1,
    },
  ], // 校友风采管理
  xwjjlb: [
    {
      name: "查看权限",
      keyword: "canView",
      value: 1,
    },
    {
      name: "发布新闻",
      keyword: "canInsert",
      value: 1,
    },
    {
      name: "编辑权限",
      keyword: "canUpdate",
      value: 1,
    },

    {
      name: "删除权限",
      keyword: "canDeleted",
      value: 1,
    },
  ], // 新闻聚焦列表
  xwflgl: [
    {
      name: "查看权限",
      keyword: "canView",
      value: 1,
    },

    {
      name: "创建分类",
      keyword: "canInsert",
      value: 1,
    },
    {
      name: "编辑权限",
      keyword: "canUpdate",
      value: 1,
    },

    {
      name: "删除权限",
      keyword: "canDeleted",
      value: 1,
    },
  ], // 新闻分类管理
  fxfwgl: [
    {
      name: "查看权限",
      keyword: "canView",
      value: 1,
    },
    {
      name: "详情权限",
      keyword: "canDetail",
      value: 1,
    },

    {
      name: "审核权限",
      keyword: "canApply",
      value: 1,
    },
    {
      name: "删除权限",
      keyword: "canDeleted",
      value: 1,
    },
  ], // 返校服务管理
  dzxykgl: [
    {
      name: "查看权限",
      keyword: "canView",
      value: 1,
    },
    {
      name: "详情权限",
      keyword: "canDetail",
      value: 1,
    },

    {
      name: "审核权限",
      keyword: "canApply",
      value: 1,
    },
    {
      name: "删除权限",
      keyword: "canDeleted",
      value: 1,
    },
  ], // 电子校友卡管理
  xyxclb: [
    {
      name: "查看权限",
      keyword: "canView",
      value: 1,
    },
    {
      name: "上传照片",
      keyword: "canInsert",
      value: 1,
    },
    {
      name: "编辑权限",
      keyword: "canUpdate",
      value: 1,
    },

    {
      name: "删除权限",
      keyword: "canDeleted",
      value: 1,
    },
  ], // 校友相册列表
  xcflgl: [
    {
      name: "查看权限",
      keyword: "canView",
      value: 1,
    },

    {
      name: "创建分类",
      keyword: "canInsert",
      value: 1,
    },
    {
      name: "编辑权限",
      keyword: "canUpdate",
      value: 1,
    },

    {
      name: "删除权限",
      keyword: "canDeleted",
      value: 1,
    },
  ], // 相册分类管理
  zpscsh: [
    {
      name: "查看权限",
      keyword: "canView",
      value: 1,
    },
    {
      name: "详情权限",
      keyword: "canDetail",
      value: 1,
    },

    {
      name: "审核权限",
      keyword: "canApply",
      value: 1,
    },
    {
      name: "删除权限",
      keyword: "canDeleted",
      value: 1,
    },
  ], // 照片上传审核
  jsgl: [
    {
      name: "查看权限",
      keyword: "canView",
      value: 1,
    },
    {
      name: "新增",
      keyword: "canInsert",
      value: 1,
    },
    {
      name: "编辑权限",
      keyword: "canUpdate",
      value: 1,
    },

    {
      name: "删除权限",
      keyword: "canDeleted",
      value: 1,
    },
  ], // 角色管理
  yhgl: [
    {
      name: "查看权限",
      keyword: "canView",
      value: 1,
    },

    {
      name: "新增",
      keyword: "canInsert",
      value: 1,
    },
    {
      name: "编辑权限",
      keyword: "canUpdate",
      value: 1,
    },
    {
      name: "删除权限",
      keyword: "canDeleted",
      value: 1,
    },
  ], // 用户管理
  czrz: [
    {
      name: "查看权限",
      keyword: "canView",
      value: 1,
    },
  ], // 操作日志
  xtsz: [
    {
      name: "查看权限",
      keyword: "canView",
      value: 1,
    },
    {
      name: "编辑权限",
      keyword: "canUpdate",
      value: 1,
    },
  ], // 系统设置
};
const keyToMapKey = {
  校友信息管理: "xyxxgl",
  广场信息管理: "gcxxgl",
  校友组织列表: "xyzzlb",
  组织分类管理: "zzflgl",
  入会申请审核: "rhshsh",
  校友活动列表: "xyhdlb",
  活动报名审核: "hdbmsh",
  校企合作管理: "xqhzgl",
  校友企业管理: "xyqygl",
  校友风采管理: "xyfcgl",
  新闻聚焦列表: "xwjjlb",
  新闻分类管理: "xwflgl",
  返校服务管理: "fxfwgl",
  电子校友卡管理: "dzxykgl",
  校友相册列表: "xyxclb",
  相册分类管理: "xcflgl",
  照片上传审核: "zpscsh",
  角色管理: "jsgl",
  用户管理: "yhgl",
  操作日志: "czrz",
  系统设置: "xtsz",
};
// 基于menuPermissionMap的通用全选逻辑（替代switch）
const handleCheckAllChange = (val, i, key) => {
  console.log(val, i, key);
  const checkVal = val;
  // 获取当前模块的权限字段列表
  const permissionList = menuPermissionMap[keyToMapKey[key]] || [];
  console.log(permissionList);

  // 遍历字段统一赋值
  permissionList.forEach((item) => {
    i[item.keyword] = checkVal;
  });
  console.log(i);
};
const handleCheckChange = (val, i, key) => {
  console.log(val, i, key);
  // 获取当前模块的权限字段列表
  const permissionList = menuPermissionMap[keyToMapKey[key]] || [];
  // 判断所有字段是否全为1
  const isAllSelected = permissionList.every((item) => i[item.keyword] == 1);
  // 更新全选状态
  i.canAll = isAllSelected ? 1 : 0;
  console.log(i);
};

// 获取账户列表
const getList = async () => {
  loading.value = true;
  let params = {
    currentPage: currentPage.value, // 当前页
    pageCount: pageSize.value, // 一页数据条数
    keyword: searchInput.keyword, // 关键字
  };
  if (searchInput.createTime) {
    params.startTime = searchInput.createTime[0];
    params.endTime = searchInput.createTime[1];
  }
  let res = await getQueryRolesPage(params);
  console.log(res);
  if (res.code == 200) {
    tableData.list = res.data.list;
    total.value = res.data.totalCount;
    loading.value = false;
  } else {
    loading.value = false;
  }
};

// 搜索功能
const searchBtn = lodash.debounce(async () => {
  currentPage.value = 1;
  getList();
}, 300);
const resetBtn = lodash.debounce(async () => {
  searchInput.name = "";
  searchInput.createTime = null;
  currentPage.value = 1;
  getList();
}, 300);

// 添加账号
const addlist = () => {
  menuListCopy.list = [];
  allMenus();
  dialongTitle.value = "新增角色";
  addDialogVisible.value = true;
  ruleForm.school = null;
  xyzzlbCityList.value = [];
  rhshshCityList.value = [];
  ruleForm.roleName = "";
  ruleForm.remark = "";
};
// 添加账号
const updateS = async (row) => {
  console.log(row);
  menuListCopy.list = [];
  dialongTitle.value = "编辑角色";
  addDialogVisible.value = true;
  ruleForm.roleName = row.roleName;
  ruleForm.remark = row.remark;
  ruleForm.id = row.id;
  let params = {
    id: row.id,
  };
  let res = await getByIdMenu(params);
  console.log(res, "根据id查询角色");

  // 处理所有异步城市接口请求
  const asyncTasks = [];
  if (res.code == 200) {
    res.data.menus.forEach(async (menu) => {
      if (menu.menuName === "校友组织管理") {
        menu.childs.forEach((child) => {
          if (child.menuName == "校友组织列表") {
            child.provinces = child.provinces[0];
            let params = {
              provinceId: child.provinces,
            };
            asyncTasks.push(
              getCity(params).then((res) => {
                xyzzlbCityList.value = res.data;
                child.citys = child.citys[0];
              }),
            );
          }
          if (child.menuName == "入会申请审核") {
            child.provinces = child.provinces[0];
            let params = {
              provinceId: child.provinces,
            };
            asyncTasks.push(
              getCity(params).then((res) => {
                rhshshCityList.value = res.data;
                child.citys = child.citys[0];
              }),
            );
          }
        });
      }
    });
    await Promise.all(asyncTasks);
    menuList.list = res.data.menus;
    // menuList.list = res.data.menus;
    console.log(menuList.list);
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.message,
      center: true,
    });
  }
};
const deleteS = async (row) => {
  ElMessageBox.confirm("是否删除此数据?", "提示!!!", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      let params = {
        id: row.id,
      };
      let res = await deleteRole(params);
      if (res.code == 200) {
        getList()
        ElMessage({
          type: "success",
          showClose: true,
          message: res.message,
          center: true,
        });
      } else {
        ElMessage({
          type: "error",
          showClose: true,
          message: res.message,
          center: true,
        });
      }
    })
    .catch(() => {});
};

// 添加数据的时候 将省市数据改为数组
const menuListRegroupArray = () => {
  menuListCopy.list = JSON.parse(JSON.stringify(menuList.list));
  menuListCopy.list.forEach((menu) => {
    if (menu.menuName === "校友组织管理") {
      menu.childs.forEach((child) => {
        child.provinces = Array.isArray(child.provinces)
          ? child.provinces
          : child.provinces
            ? [child.provinces]
            : [];
        child.citys = Array.isArray(child.citys)
          ? child.citys
          : child.citys
            ? [child.citys]
            : [];
      });
    }
  });
};
// 编辑数据的时候 将省市数据改为数字
const menuListRegroupNumber = () => {
  menuList.list.forEach((menu) => {
    if (menu.menuName === "校友组织管理") {
      menu.childs.forEach((child) => {
        child.provinces = Array.isArray(child.provinces)
          ? child.provinces.length > 0
            ? Number(child.provinces[0])
            : 0
          : child.provinces
            ? Number(child.provinces)
            : 0;
        child.citys = Array.isArray(child.citys)
          ? child.citys.length > 0
            ? Number(child.citys[0])
            : 0
          : child.citys
            ? Number(child.citys)
            : 0;
      });
    }
  });
};
// 确认添加员工
const submitAdd = lodash.debounce(async (formEl) => {
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      menuListRegroupArray();
      let data = {
        roleName: ruleForm.roleName,
        remark: ruleForm.remark,
        menus: menuListCopy.list,
      };
      let res = "";
      if (dialongTitle.value == "新增角色") {
        res = await addRole(data);
      } else {
        data.id = ruleForm.id;
        res = await updateRole(data);
      }
      if (res.code == 200) {
        addDialogVisible.value = false;
        ElMessage({
          type: "success",
          showClose: true,
          message: res.message,
          center: true,
        });
        getList();
      } else {
        ElMessage({
          type: "error",
          showClose: true,
          message: res.message,
          center: true,
        });
      }
    } else {
      console.log("error submit!", fields);
    }
  });
}, 1000);

const cancelAdd = () => {
  addDialogVisible.value = false;
};

// 表格斑马纹颜色修改
const tableRowClassName = ({ row, rowIndex }) => {
  if (rowIndex % 2 === 0) {
    return "even";
  } else if (rowIndex % 2 !== 0) {
    return "odd";
  }
  return "";
};
// 每页显示条数
const handleSizeChange = (value) => {
  console.log(value, "每页显示条数");
  pageSize.value = value;
  getList();
};
// 分页
const handleCurrentChange = (value) => {
  currentPage.value = value;
  getList();
};

const allMenus = async () => {
  let res = await getAllMenus();
  console.log(res, "所有菜单权限");
  if (res.code == 200) {
    menuList.list = res.data;
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.message,
      center: true,
    });
  }
};
const provinceData = async () => {
  let res = await getProvince();
  console.log(res, "省数据");
  if (res.code == 200) {
    provinceList.value = res.data;
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.message,
      center: true,
    });
  }
};
const cityData = async (provinceId) => {
  let params = {
    provinceId,
  };
  let res = await getCity(params);
  console.log(res, "市数据");
  if (res.code == 200) {
    return res.data;
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.message,
      center: true,
    });
  }
};
const provincesChange = async (val, i, key) => {
  let params = {
    provinceId: val,
  };
  if (key == "校友组织列表") {
    xyzzlbCityList.value = [];
    i.citys = null;
    let res = await getCity(params);
    xyzzlbCityList.value = res.data;
  } else if (key == "入会申请审核") {
    rhshshCityList.value = [];
    i.citys = null;
    let res = await getCity(params);
    rhshshCityList.value = res.data;
  }
  console.log(i);
};
onBeforeMount(() => {
  getList();
  allMenus();
  provinceData();
});
onUnmounted(() => {
  // document.removeEventListener("keyup", Enters);
});
</script>

<style scoped lang="scss">
.scroll {
  width: calc(100% - 60px);
  height: calc(100% - 61px);
  margin: 0 auto;
  display: flex;
  flex-direction: column;

  .middle {
    width: calc(100%);
    color: #000;
    .filter {
      display: flex;
      flex-wrap: wrap;
      align-items: center;
      .search {
        margin-left: 0 !important;
        color: #fff;
      }
      .condition {
        display: flex;
        align-items: center;
        margin: 10px 30px 10px 0;
        :deep(.el-input .el-input__inner) {
          font-size: 14px;
        }
        .el-select {
          width: 220px;
        }
        span {
          margin: 0 10px 0 0;
        }
      }
    }
    .gongneng {
      margin: 10px 0;
      .el-button {
        color: #fff;
        margin-right: 15px;
      }
    }
  }
  .footer {
    width: calc(100%);
    flex: 1;
    margin: 0 auto;
    overflow: auto;

    .el-table--fit {
      height: calc(100% - 60px);
      :deep(.el-table__header-wrapper) {
        background-color: #000;
        font-size: 15px;
        color: #000;
        .cell {
          color: #000;
        }
      }
      :deep(.el-table__row) {
        height: 50px;
        font-size: 15px;
        color: #000;
      }
      :deep(.el-table__row td) {
        padding: 0;
        border: 0;
      }

      .el-button--primary {
        margin-left: 5px;
      }
      :deep(.el-table__body .even) {
        background-color: #fff;
      }
      :deep(.el-table__body .odd) {
        background-color: rgba(240, 243, 247, 1);
      }
    }

    .pageSize {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin: 0 30px;
      height: 60px;

      span {
        color: #000;
      }

      .el-pagination {
        // width: 1600px;
        :deep(.el-pagination__total) {
          color: #000;
        }

        :deep(.el-pagination__goto) {
          color: #000;
        }

        :deep(.el-pagination__classifier) {
          color: #000;
        }

        :deep(.el-input__wrapper) {
          border: 1px solid rgba(0, 0, 0, 1);
          border-radius: 5px;
          box-shadow: none;
        }

        :deep(.el-pager li) {
          margin: 0 5px;
          border: 1px solid rgba(0, 0, 0, 1);
          border-radius: 5px;
          background-color: transparent;
        }

        :deep(.el-pager li.is-active) {
          // background-color: rgba(0, 97, 255, 0.8);
          border: 1px solid rgba(0, 97, 255, 1);
          color: rgba(0, 97, 255, 1);
        }

        :deep(.btn-prev) {
          margin-right: 5px;
          border: 1px solid rgba(0, 0, 0, 1);
          border-radius: 5px;
          background-color: transparent;
        }

        :deep(.btn-next) {
          margin-left: 5px;
          border: 1px solid rgba(0, 0, 0, 1);
          border-radius: 5px;
          background-color: transparent;
        }
      }
    }
  }
}

// 添加员工弹窗样式
.addStaff {
  .el-dialog__body {
    padding: 20px 20px 10px 20px;
    .el-input {
      width: 400px;
      .el-input__suffix-inner {
        color: rgba(61, 81, 232, 1);
      }
    }
    .el-textarea {
      width: 400px;
    }
    .el-select {
      width: 400px;
    }
    .el-tree {
      width: 400px;
    }
    .options {
      margin: 20px 0 20px 0;
      display: flex;
      flex-direction: row-reverse;
      .queding {
        margin-left: 20px;
        background: linear-gradient(
          90deg,
          rgba(38, 151, 255, 1) 0%,
          rgba(102, 181, 255, 1) 100%
        );
        border: none;
      }
      .quxiao {
        border: 1px solid rgba(43, 151, 252, 1);
        color: rgba(43, 151, 252, 1);
      }
    }
    $el-table-border-color: #b3b3b3;
    .el-table--fit {
      border: 1px solid $el-table-border-color;
      height: 600px;
      overflow: auto;
      :deep(.el-table__row) {
        height: 50px;
        td {
          border-right: 1px solid $el-table-border-color;
          border-bottom: 1px solid $el-table-border-color;
        }
        .second-menu-column {
          padding: 0;
          .cell {
            padding: 0;

            .level {
              display: flex;
              flex-direction: column;
              justify-content: center;
              align-items: center;
              margin: 0;
              width: 100%;
              height: 105px;
              &:not(:last-child) {
                border-bottom: 1px solid $el-table-border-color;
                box-sizing: border-box;
              }
            }
          }
        }
        .third-menu-column {
          padding: 0;
          .cell {
            padding: 0;

            .level {
              margin: 0;
              padding-left: 10px;
              width: 100%;
              height: 105px;
              p {
                margin: 0;
              }
              &:not(:last-child) {
                border-bottom: 1px solid $el-table-border-color;
                box-sizing: border-box;
              }
              .el-checkbox {
                padding: 0;
                height: 35px;
              }
            }
          }
        }
        &:hover {
          background-color: transparent !important;
          td {
            background-color: transparent !important;
          }
        }
      }
      :deep(.el-table__header-wrapper) {
        th {
          border-right: 1px solid $el-table-border-color;
          border-bottom: 1px solid $el-table-border-color;
        }
      }
    }
  }
}
</style>
