<?php
/** @var array<int,array<string,mixed>> $modules */
/** @var array<string,array<string,mixed>> $allModules */
/** @var array<int,array<string,mixed>> $accessMatrixRoles */
/** @var array<string,mixed> $hospital */
/** @var array<string,mixed> $user */
$isAr = current_lang() === 'ar';
$tr = static function (string $en, string $ar) use ($isAr): string { return $isAr ? $ar : $en; };
$modules = is_array($modules ?? null) ? $modules : [];
$allModules = is_array($allModules ?? null) ? $allModules : [];
$accessMatrixRoles = is_array($accessMatrixRoles ?? null) ? $accessMatrixRoles : [];
$user = is_array($user ?? null) ? $user : [];
$showRoleMatrix = !empty($showRoleMatrix);
$developerBadge = (string)($developerBadge ?? '');
$currentRole = (string)($currentRole ?? '—');
$hospitalName = (string)($hospital['name'] ?? strtoupper((string)($hospitalCode ?? '')));
$icon = static function (string $key): string {
    $paths = [
        'executive' => '<path d="M4 19h16M6 16V9m4 7V5m4 11v-3m4 3V8"/><path d="m6 7 4-4 4 6 4-4"/>',
        'statistics' => '<path d="M4 19h16M7 16v-5m5 5V5m5 11V8"/><path d="M5 8h4m6-4h4"/>',
        'cases' => '<circle cx="11" cy="11" r="6"/><path d="m16 16 4 4M8 11h6m-3-3v6"/>',
        'investigation' => '<path d="M7 4h10v16H7z"/><path d="M9 2h6v4H9zM10 10h4m-4 4h4"/>',
        'reports' => '<path d="M5 3h10l4 4v14H5z"/><path d="M15 3v5h5M8 12h8m-8 4h8"/>',
        'governance' => '<circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.7 1.7 0 0 0 .34 1.88l.06.06-2.12 2.12-.06-.06a1.7 1.7 0 0 0-1.88-.34 1.7 1.7 0 0 0-1.03 1.55V20h-3v-.09a1.7 1.7 0 0 0-1.03-1.55 1.7 1.7 0 0 0-1.88.34l-.06.06-2.12-2.12.06-.06A1.7 1.7 0 0 0 7 15.4a1.7 1.7 0 0 0-1.55-1.03H5v-3h.09A1.7 1.7 0 0 0 6.64 10 1.7 1.7 0 0 0 6.3 8.12l-.06-.06L8.36 5.94l.06.06A1.7 1.7 0 0 0 10.3 6.3a1.7 1.7 0 0 0 1.03-1.55V4h3v.09a1.7 1.7 0 0 0 1.03 1.55 1.7 1.7 0 0 0 1.88-.34l.06-.06 2.12 2.12-.06.06a1.7 1.7 0 0 0-.34 1.88 1.7 1.7 0 0 0 1.55 1.03H21v3h-.09A1.7 1.7 0 0 0 19.4 15Z"/>',
    ];
    return '<svg viewBox="0 0 24 24" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round">' . ($paths[$key] ?? $paths['executive']) . '</svg>';
};
$accessIcon = static function (string $level): string {
    if ($level === 'full') { return '✓'; }
    if ($level === 'limited') { return '−'; }
    return '×';
};
?>
<link rel="stylesheet" href="assets/css/diar-workspace.css?v=diar-6b-2-r3-1">

<section class="diar-premium-page diar-platform-home" data-diar-r3-portal>
    <header class="diar-platform-hero">
        <div class="diar-platform-copy">
            <span class="diar-premium-eyebrow"><?= e($tr('DRUG INVENTORY ANOMALY & RISK INTELLIGENCE', 'ذكاء شذوذ ومخاطر مخزون الأدوية')) ?></span>
            <h1><?= e($tr('Medication Risk Intelligence', 'منظومة ذكاء مخاطر الأدوية')) ?></h1>
            <p><?= e($tr(
                'An integrated system for identifying unusual medication movements, managing investigations and supporting evidence-based operational decisions.',
                'منظومة متكاملة لتحديد حركات الأدوية غير المعتادة وإدارة التحقيقات ودعم القرارات التشغيلية القائمة على الأدلة.'
            )) ?></p>
            <div class="diar-platform-trust">
                <span><?= e($tr('Protected analytical review', 'مراجعة تحليلية محمية')) ?></span>
                <span><?= e($tr('Preserved monthly evidence', 'أدلة شهرية محفوظة')) ?></span>
                <span><?= e($tr('Direct hospital data access', 'وصول مباشر إلى بيانات المستشفى')) ?></span>
            </div>
        </div>
        <div class="diar-platform-visual" aria-hidden="true">
            <div class="diar-shield-orbit"><span>✓</span></div>
            <i class="visual-chart">▥</i><i class="visual-report">◫</i>
        </div>
        <aside class="diar-context-card">
            <strong><?= e($tr('Current access', 'صلاحياتي الحالية')) ?></strong><?php if ($developerBadge !== ''): ?><span class="diar-developer-badge"><?= e($developerBadge) ?></span><?php endif; ?>
            <dl>
                <div><dt><?= e($tr('Branch', 'الفرع')) ?></dt><dd><?= e($hospitalName) ?></dd></div>
                <div><dt><?= e($tr('User role', 'دور المستخدم')) ?></dt><dd><?= e($currentRole) ?></dd></div>
                <div><dt><?= e($tr('User', 'المستخدم')) ?></dt><dd><?= e((string)($user['name'] ?? '—')) ?></dd></div>
            </dl>
            <?php if (\App\Core\AccessControl::allows('security.access_control.manage')): ?>
                <a href="<?= e(url_to('access-control')) ?>"><?= e($tr('Manage access', 'إدارة الصلاحيات')) ?></a>
            <?php endif; ?>
        </aside>
    </header>

    <section class="diar-premium-module-grid" aria-label="<?= e($tr('Available DIAR sections', 'أقسام DIAR المتاحة')) ?>">
        <?php foreach ($modules as $module): $key=(string)$module['key']; ?>
            <a class="diar-premium-module tone-<?= e((string)$module['tone']) ?>" href="<?= e(url_to((string)$module['route'])) ?>">
                <div class="module-card-head">
                    <span class="module-icon"><?= $icon($key) ?></span>
                    <span class="module-number"><?= e((string)$module['number']) ?></span>
                    <div><h2><?= e((string)$module['label']) ?></h2><p><?= e((string)$module['description']) ?></p></div>
                    <b aria-hidden="true">›</b>
                </div>
                <ul>
                    <?php foreach ((array)$module['subpages'] as $subpage): ?><li><?= e((string)$subpage) ?></li><?php endforeach; ?>
                </ul>
                <?php if ($showRoleMatrix): ?><footer>
                    <span><?= e($tr('Access for', 'متاح لأدوار')) ?></span>
                    <div><?php foreach ((array)$module['role_labels'] as $roleLabel): ?><em><?= e((string)$roleLabel) ?></em><?php endforeach; ?></div>
                </footer><?php endif; ?>
            </a>
        <?php endforeach; ?>
    </section>

    <?php if ($showRoleMatrix): ?>
        <section class="diar-access-matrix-card">
            <div class="matrix-heading">
                <div><span class="diar-premium-eyebrow"><?= e($tr('ROLE-AWARE ACCESS', 'الوصول حسب الدور')) ?></span><h2><?= e($tr('Access by role', 'الصلاحيات حسب الدور')) ?></h2><p><?= e($tr('Access is applied consistently according to each user’s approved role.', 'تُطبّق الصلاحيات بصورة موحدة وفق الدور المعتمد لكل مستخدم.')) ?></p></div>
                <div class="matrix-legend"><span class="is-full">✓ <?= e($tr('Granted', 'مسموح')) ?></span><span class="is-limited">− <?= e($tr('View only', 'عرض فقط')) ?></span><span class="is-none">× <?= e($tr('No access', 'غير مسموح')) ?></span></div>
            </div>
            <div class="matrix-scroll">
                <table class="diar-access-matrix">
                    <thead><tr><th><?= e($tr('Page group', 'مجموعة الصفحات')) ?></th><?php foreach ($accessMatrixRoles as $role): ?><th><span class="role-code tone-<?= e((string)$role['tone']) ?>"><?= e((string)$role['code']) ?></span><?= e((string)$role['label']) ?></th><?php endforeach; ?></tr></thead>
                    <tbody>
                    <?php foreach ($allModules as $moduleKey => $module): ?>
                        <tr><th><span class="matrix-module-number tone-<?= e((string)$module['tone']) ?>"><?= e((string)$module['number']) ?></span><?= e((string)$module['label']) ?></th>
                            <?php foreach ($accessMatrixRoles as $role): $level=(string)($role['access'][$moduleKey] ?? 'none'); ?>
                                <td><span class="matrix-access is-<?= e($level) ?>" title="<?= e($level) ?>"><?= e($accessIcon($level)) ?></span></td>
                            <?php endforeach; ?>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </section>
    <?php else: ?>
        <section class="diar-my-access-card">
            <div><span class="diar-premium-eyebrow"><?= e($tr('YOUR ACCESS', 'صلاحياتك')) ?></span><h2><?= e($tr('Only permitted pages appear in your menu', 'تظهر في قائمتك الصفحات المسموح بها فقط')) ?></h2></div>
            <strong><?= e((string)count($modules)) ?></strong>
        </section>
    <?php endif; ?>
</section>
