@extends('layouts.app')
@section('title')
{{ __('Admin Dashboard') }}
@endsection
@section('css')
@if(request()->query('admin_tab') === 'workflow-templates')
@endif
@endsection
@section('content')
@php
$activeAdminTab = $adminTab ?? request()->query('admin_tab');
if (!in_array($activeAdminTab, ['users', 'workflow-templates', 'document-types', 'organizations', 'role-permissions', 'audit-trails'], true)) {
$activeAdminTab = null;
}
$adminPageTitle = [
'users' => 'Users',
'workflow-templates' => 'Workflow Templates',
'document-types' => 'Document Types',
'organizations' => 'Organizations',
'role-permissions' => 'Role Permissions',
'audit-trails' => 'Audit Logs',
][$activeAdminTab] ?? 'Dashboard';
$adminPageSubtitle = [
'users' => 'Manage user accounts, roles, organization assignments, and access status.',
'organizations' => 'Manage organizational units and the users assigned to them.',
'document-types' => 'Define document categories, request forms, required files, and workflow assignments.',
'workflow-templates' => 'Design reusable review and decision routes for document processing.',
'role-permissions' => 'Control what each role can access and do across the system.',
'audit-trails' => 'Review administrative and workflow activity for traceability and accountability.',
][$activeAdminTab] ?? 'Monitor documents, workflows, users, and system activity from one place.';
$locale = strtolower((string) app()->getLocale());
$localizedOrganizationName = function ($organization) use ($locale) {
$fallback = trim((string) ($organization->name ?? ''));
$english = trim((string) ($organization->name_en ?? ''));
$khmer = trim((string) ($organization->name_kh ?? ''));
if ($locale === 'kh') {
return $khmer !== '' ? $khmer : ($english !== '' ? $english : $fallback);
}
return $english !== '' ? $english : $fallback;
};
$activeOrganizations = ($organizations ?? collect())
->where('is_active', true)
->values();
$activeWorkflowTemplates = ($workflowTemplates ?? collect())
->where('is_active', true)
->values();
$activeOrganizationOptions = $activeOrganizations
->pluck('name')
->values();
$userOrganizationFilterOptions = ($organizations ?? collect())
->map(fn ($organization) => [
'value' => (string) ($organization->name ?? ''),
'label' => $localizedOrganizationName($organization),
])
->filter(fn ($organization) => trim($organization['value']) !== '')
->values();
$organizationDisplayMap = $activeOrganizations
->mapWithKeys(fn ($organization) => [
(string) $organization->name => $localizedOrganizationName($organization),
])
->toArray();
$allOrganizationDisplayMap = ($organizations ?? collect())
->flatMap(function ($organization) use ($localizedOrganizationName) {
$label = $localizedOrganizationName($organization);
return collect([
(string) ($organization->name ?? '') => $label,
(string) ($organization->name_en ?? '') => $label,
(string) ($organization->name_kh ?? '') => $label,
])->filter(fn ($value, $key) => trim((string) $key) !== '');
})
->toArray();
$organizationMap = $activeOrganizations->mapWithKeys(fn ($organization) => [
$organization->name => [
'id' => $organization->id ?? null,
]
])->toArray();
$generalWorkflowOrganizationOptions = $activeOrganizations
->map(fn ($organization) => [
'value' => (string) $organization->name,
'label' => $localizedOrganizationName($organization),
])
->values()
->toArray();
$rectorUserOptionsJs = ($rectorUsers ?? collect())
->map(fn ($user) => [
'id' => $user->id,
'name' => $user->name,
'role' => strtolower((string) ($user->role ?? 'rector')),
])
->values()
->toArray();
$rectorOnlyOptionsJs = collect($rectorUserOptionsJs)
->filter(fn ($user) => ($user['role'] ?? '') === 'rector')
->values()
->toArray();
$viceRectorOnlyOptionsJs = collect($rectorUserOptionsJs)
->filter(fn ($user) => ($user['role'] ?? '') === 'vice_rector')
->values()
->toArray();
$roleOrganizationOfficerMap = collect($userRoleOptions ?? [])
->keys()
->mapWithKeys(fn ($role) => [
(string) $role => \App\Support\WorkflowPermission::canUseAdminPermission(
(string) $role,
\App\Support\WorkflowPermission::ADMIN_PERMISSION_ORGANIZATION_OFFICER
),
])
->toArray();
$workflowTemplateKpiCount = (int) ($kpi['workflow_templates'] ?? ($workflowTemplates ?? collect())->count());
$activeWorkflowTemplateKpiCount = (int) ($kpi['active_workflow_templates'] ?? $activeWorkflowTemplates->count());
$rolePermissionKpiCount = (int) ($kpi['role_permissions'] ?? (is_countable($rolePermissionRows ?? null) ? count($rolePermissionRows) : 0));
$auditTrailKpiCount = (int) ($kpi['audit_trails'] ?? (is_countable($auditTrails ?? null) ? count($auditTrails) : 0));
$adminDocumentChartColors = ['primary', 'success', 'warning', 'info', 'danger', 'secondary'];
$adminDocumentChartCards = ($requests ?? collect())
->groupBy(fn ($request) => trim((string) ($request->document_type_label ?: $request->document_type ?: __('N/A'))))
->map(fn ($items, $label) => [
'label' => $label !== '' ? $label : __('N/A'),
'key' => md5(mb_strtolower((string) ($label !== '' ? $label : __('N/A')))),
'count' => $items->count(),
])
->sortByDesc('count')
->values()
->map(function ($card, $index) use ($adminDocumentChartColors) {
$card['color'] = $adminDocumentChartColors[$index % count($adminDocumentChartColors)];
return $card;
})
->all();
$adminDocumentChartMax = max(1, (int) collect($adminDocumentChartCards)->max('count'));
@endphp
{{ __($adminPageTitle) }}
{{ __($adminPageSubtitle) }}
@if(!in_array($activeAdminTab, ['users', 'workflow-templates', 'document-types', 'organizations', 'role-permissions', 'audit-trails'], true))
@include('dashboard.partials.admin.tabs.overview')
@endif
@if($activeAdminTab === 'role-permissions')
@include('dashboard.partials.admin.tabs.role-permissions')
@elseif($activeAdminTab === 'audit-trails')
@include('dashboard.partials.admin.tabs.audit-trails')
@elseif($activeAdminTab === 'users')
@include('dashboard.partials.admin.tabs.users')
@elseif($activeAdminTab === 'organizations')
@include('dashboard.partials.admin.tabs.organizations')
@elseif($activeAdminTab === 'document-types')
@include('dashboard.partials.admin.tabs.document-types')
@elseif($activeAdminTab === 'workflow-templates')
@include('dashboard.partials.admin.tabs.workflow-templates')
@else
@include('dashboard.partials.admin.tabs.documents')
@endif
@endsection
@section('script')
@include('dashboard.partials.admin.scripts.state-and-scroll')
@include('dashboard.partials.admin.scripts.audit-trails')
@include('dashboard.partials.admin.scripts.dashboard-table')
@include('dashboard.partials.admin.scripts.flash-messages')
@include('dashboard.partials.admin.scripts.role-permissions')
@include('dashboard.partials.admin.scripts.users-and-organizations')
@include('dashboard.partials.admin.scripts.document-types')
@include('dashboard.partials.admin.scripts.workflow-templates')
@endsection