import { ref } from 'vue' import { defineStore } from 'pinia' export const usePermissionStore = defineStore('permissions', () => { const permissions = ref({}) const isAdmin = ref(false) const loaded = ref(false) async function loadPermissions() { try { const response = await fetch('/api/permissions', { credentials: 'include' }) if (response.ok) { const data = await response.json() permissions.value = data.permissions isAdmin.value = data.isAdmin loaded.value = true } } catch (error) { console.error('Failed to load permissions:', error) } } function hasPermission(module, action) { if (isAdmin.value) return true return permissions.value[module]?.[action] || false } function canView(module) { return hasPermission(module, 'view') } function canCreate(module) { return hasPermission(module, 'create') } function canEdit(module) { return hasPermission(module, 'edit') } function canDelete(module) { return hasPermission(module, 'delete') } function canExport(module) { return hasPermission(module, 'export') } function canManage(module) { return hasPermission(module, 'manage') } return { permissions, isAdmin, loaded, loadPermissions, hasPermission, canView, canCreate, canEdit, canDelete, canExport, canManage } })