<template>
  <PageContainer title="平台权限管理">
    <QueryToolbar>
      <el-button v-if="canRead" type="primary" @click="load">刷新</el-button>
    </QueryToolbar>
    <el-table :data="rows">
      <el-table-column prop="permissionCode" label="权限码" min-width="260" />
      <el-table-column prop="permissionName" label="权限名称" min-width="220" />
      <el-table-column prop="module" label="模块" width="160" />
    </el-table>
  </PageContainer>
</template>

<script setup lang="ts">
import { computed, onMounted, ref } from "vue";
import PageContainer from "../../components/PageContainer.vue";
import QueryToolbar from "../../components/QueryToolbar.vue";
import { ElMessage } from "element-plus";
import { fetchPlatformPermissions } from "../../api/modules";
import { PERMS } from "../../constants/permissions";
import { useAuthStore } from "../../stores/auth";

const authStore = useAuthStore();
const rows = ref<any[]>([]);
const canRead = computed(() => authStore.hasPermission(PERMS.platform.permissionRead));

const load = async () => {
  try {
    const resp = await fetchPlatformPermissions();
    rows.value = resp?.data?.list || [];
  } catch (e: any) {
    rows.value = [];
    ElMessage.error(e?.response?.data?.message || "平台权限数据加载失败");
  }
};

onMounted(load);
</script>
