<script setup lang="tsx">
import { usePermission } from "./utils/hook";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import Refresh from "~icons/ep/refresh";

defineOptions({
  name: "SystemPermission"
});

const {
  loading,
  permissionList,
  permissionModules,
  activeModule,
  getPermissionTypeTag,
  refresh
} = usePermission();

// 表格列定义
const columns: TableColumnList = [
  {
    label: "权限ID",
    prop: "id",
    width: 80
  },
  {
    label: "权限名称",
    prop: "name",
    minWidth: 120
  },
  {
    label: "权限标识",
    prop: "code",
    minWidth: 150
  },
  {
    label: "所属模块",
    prop: "moduleName",
    minWidth: 100
  },
  {
    label: "类型",
    prop: "type",
    minWidth: 80,
    cellRenderer: ({ row }) => {
      const tag = getPermissionTypeTag(row.type);
      return <el-tag type={tag.type}>{tag.text}</el-tag>;
    }
  },
  {
    label: "路径",
    prop: "path",
    minWidth: 120,
    showOverflowTooltip: true
  },
  {
    label: "排序",
    prop: "sort",
    width: 80
  },
  {
    label: "状态",
    prop: "status",
    minWidth: 80,
    cellRenderer: ({ row }) => (
      <el-tag type={row.status === 1 ? "success" : "danger"}>
        {row.status === 1 ? "启用" : "停用"}
      </el-tag>
    )
  }
];

// 根据模块过滤权限
const filteredPermissions = computed(() => {
  if (!activeModule.value) return permissionList.value;
  return permissionList.value.filter(p => p.moduleCode === activeModule.value);
});

// 计算属性
import { computed } from "vue";
</script>

<template>
  <div class="main">
    <el-form class="search-form bg-bg_color w-full pl-8 pt-[12px] overflow-auto">
      <el-form-item label="权限模块：">
        <el-radio-group v-model="activeModule">
          <el-radio-button value="">全部</el-radio-button>
          <el-radio-button
            v-for="module in permissionModules"
            :key="module.code"
            :value="module.code"
          >
            {{ module.name }}
          </el-radio-button>
        </el-radio-group>
      </el-form-item>
    </el-form>

    <PureTableBar
      title="权限管理"
      :columns="columns"
      @refresh="refresh"
    >
      <template #buttons>
        <el-button
          type="primary"
          :icon="useRenderIcon(Refresh)"
          :loading="loading"
          @click="refresh"
        >
          刷新
        </el-button>
      </template>
      <template v-slot="{ size, dynamicColumns }">
        <pure-table
          row-key="id"
          adaptive
          :adaptiveConfig="{ offsetBottom: 108 }"
          align-whole="center"
          table-layout="auto"
          :loading="loading"
          :size="size"
          :data="filteredPermissions"
          :columns="dynamicColumns"
          :header-cell-style="{
            background: 'var(--el-fill-color-light)',
            color: 'var(--el-text-color-primary)'
          }"
        />
      </template>
    </PureTableBar>

    <!-- 权限说明 -->
    <el-card shadow="never" class="mt-4">
      <template #header>
        <span class="font-bold">权限说明</span>
      </template>
      <el-descriptions :column="1" border>
        <el-descriptions-item label="菜单权限">
          控制用户是否能看到某个菜单项，在路由配置中进行控制
        </el-descriptions-item>
        <el-descriptions-item label="按钮权限">
          控制用户是否能看到或操作某个按钮，使用 v-auth 指令或 hasPerms 函数进行控制
        </el-descriptions-item>
        <el-descriptions-item label="权限分配">
          在角色管理页面，可以为每个角色分配对应的权限
        </el-descriptions-item>
      </el-descriptions>
    </el-card>
  </div>
</template>

<style lang="scss" scoped>
.main-content {
  margin: 24px 24px 0 !important;
}

.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}
</style>
