import { createListPageModule } from '../core/app-list-page-module.js'; import { badgeHtml } from './app-list-utils.js'; createListPageModule({ configId: 'admin-roles-index', moduleId: 'admin-roles-index', missingGridMessage: 'Roles grid init failed: Grid.js missing', initErrorMessage: 'Roles grid init failed', setup: ({ config, gridjs, appBase, initListPage }) => { const gridSearch = config.gridSearch ?? null; const filterSchema = config.filterSchema ?? []; const filterChipMeta = config.filterChipMeta ?? []; const labels = config.labels ?? {}; const formatBadge = (value) => badgeHtml(gridjs, value); const formatCount = (value) => { const number = Number(value ?? 0); return Number.isFinite(number) ? number : 0; }; const { gridConfig } = initListPage({ grid: { gridjs, container: '#roles-grid', dataUrl: 'admin/roles/data', appBase, columns: [ { name: labels.description || 'Description', sort: true, formatter: (cell) => { if (!cell || typeof cell !== 'object') { return gridjs.html(String(cell ?? '')); } const label = cell.label ?? ''; return gridjs.html(`${label}`); }, }, { name: labels.status || 'Status', sort: true, formatter: (cell) => cell ? gridjs.html(`${labels.active || 'Active'}`) : gridjs.html(`${labels.inactive || 'Inactive'}`), }, { name: labels.code || 'Code', sort: true, formatter: (cell) => gridjs.html(String(cell ?? '')), }, { name: labels.activeUsers || 'Active users', sort: false, formatter: (cell) => formatCount(cell), }, { name: labels.inactiveUsers || 'Inactive users', sort: false, formatter: (cell) => formatCount(cell), }, { name: labels.permissions || 'Permissions', sort: false, formatter: (cell) => formatCount(cell), }, { name: labels.created || 'Created', sort: true, formatter: (cell) => formatBadge(cell), }, { name: labels.modified || 'Modified', sort: true, formatter: (cell) => formatBadge(cell), }, { name: 'UUID', hidden: true, }, ], sortColumns: ['description', 'active', 'code', null, null, null, 'created', 'modified', null], paginationLimit: 10, language: config.gridLang ?? {}, mapData: (data) => data.data.map((row) => [ { label: row.description, is_default: row.is_default ? 1 : 0, }, row.active, row.code, row.active_users ?? 0, row.inactive_users ?? 0, row.permission_count ?? 0, row.created, row.modified, row.uuid, ]), rowInteraction: { linkColumn: 0 }, search: gridSearch, filterSchema, urlSync: true, rowDataset: (row) => ({ uuid: row?.cells?.[8]?.data, }), rowDblClick: { getUrl: (rowData) => { const uuid = rowData?.cells?.[8]?.data; return uuid ? new URL(`admin/roles/edit/${uuid}`, appBase).toString() : ''; }, }, }, filters: { mode: 'drawer', chipMeta: filterChipMeta, watchInputs: ['#role-search'], }, }) || {}; return gridConfig; }, });