<?php
/**
 * Super Admin Layout — Gmind Institute (v2 with Pattern System)
 *
 * Upload to: /gmind/super-admin/Views/layout.php (OVERWRITE)
 */

$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$userInitial = strtoupper(mb_substr(htmlspecialchars($currentUser['name'] ?? 'S', ENT_QUOTES, 'UTF-8'), 0, 1));
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= htmlspecialchars($title ?? 'Super Admin', ENT_QUOTES, 'UTF-8') ?> | Gmind Super Admin</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@600;700&family=Inter:wght@400;500;600&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="<?= $bp ?>/css/admin.css">

    <!-- Bootstrap 5 CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css">

    <meta name="csrf-token" content="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
    <style>
        .admin-sidebar { background: linear-gradient(180deg, #2c1e4a 0%, #1a1333 100%) !important; }
        .admin-sidebar-brand { background: linear-gradient(135deg, #6c3fe8, #4a2db8) !important; }
        .admin-nav-link.active { background-color: rgba(108, 63, 232, 0.25) !important; border-left-color: #6c3fe8 !important; color: #fff !important; }
        .admin-nav-link:hover { background-color: rgba(108, 63, 232, 0.15) !important; }
        .admin-topbar { background: #fff; border-bottom: 2px solid #6c3fe8; }
        .admin-page-title { color: #2c1e4a; }
        .admin-btn-primary { background: #6c3fe8; }
        .admin-btn-primary:hover { background: #4a2db8; }
        .admin-badge-super { background: #6c3fe8; color: #fff; padding: 2px 8px; border-radius: 10px; font-size: 0.7rem; font-weight: 600; }
        .sa-badge-count { background: #e84118; color: #fff; font-size: 10px; font-weight: 700; min-width: 18px; height: 18px; border-radius: 9px; display: inline-flex; align-items: center; justify-content: center; padding: 0 5px; margin-left: 6px; }
        .admin-nav-link.pattern-highlight {
            background-color: rgba(232, 63, 99, 0.15) !important;
            border-left: 3px solid #e83f63 !important;
        }
        .admin-nav-link.pattern-highlight:hover {
            background-color: rgba(232, 63, 99, 0.25) !important;
        }
        .admin-content .container-fluid { padding: 0; }
        .admin-content .card { margin-bottom: 1rem; }
        .toast { min-width: 300px; }
    </style>
</head>
<body class="admin-body">
    <div class="admin-sidebar-overlay" id="sidebarOverlay"></div>

    <aside class="admin-sidebar" id="adminSidebar">
        <div class="admin-sidebar-header">
            <a href="<?= $bp ?>/super-admin/dashboard" class="admin-logo">
                <span class="admin-logo-text">GMIND</span>
                <span class="admin-logo-subtext">SUPER ADMIN</span>
            </a>
            <button class="admin-sidebar-close" id="sidebarClose" aria-label="Close sidebar">&times;</button>
        </div>

        <nav class="admin-sidebar-nav">
            <ul class="admin-nav-list">
                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/super-admin/dashboard" class="admin-nav-link <?= ($currentPage ?? '') === 'dashboard' ? 'active' : '' ?>">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="3" y="3" width="7" height="7"/><rect x="14" y="3" width="7" height="7"/><rect x="3" y="14" width="7" height="7"/><rect x="14" y="14" width="7" height="7"/></svg>
                        <span>Dashboard</span>
                    </a>
                </li>

                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/super-admin/branches" class="admin-nav-link <?= ($currentPage ?? '') === 'branches' ? 'active' : '' ?>">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0118 0z"/><circle cx="12" cy="10" r="3"/></svg>
                        <span>Branches</span>
                    </a>
                </li>

                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/super-admin/branch-admins" class="admin-nav-link <?= ($currentPage ?? '') === 'branch_admins' ? 'active' : '' ?>">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M17 21v-2a4 4 0 00-4-4H5a4 4 0 00-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M23 21v-2a4 4 0 00-3-3.87"/><path d="M16 3.13a4 4 0 010 7.75"/></svg>
                        <span>Branch Admins</span>
                    </a>
                </li>

                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/super-admin/branch-applications" class="admin-nav-link <?= ($currentPage ?? '') === 'branch_applications' ? 'active' : '' ?>">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M14 2H6a2 2 0 00-2 2v16a2 2 0 002 2h12a2 2 0 002-2V8z"/><polyline points="14 2 14 8 20 8"/><line x1="16" y1="13" x2="8" y2="13"/><line x1="16" y1="17" x2="8" y2="17"/></svg>
                        <span>Franchise Applications</span>
                    </a>
                </li>

                <!-- ===== PHASE 5: CERTIFICATE PATTERNS ===== -->
                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/super-admin/patterns" class="admin-nav-link pattern-highlight <?= ($currentPage ?? '') === 'patterns' ? 'active' : '' ?>">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M12 2L2 7l10 5 10-5-10-5z"/><path d="M2 17l10 5 10-5"/><path d="M2 12l10 5 10-5"/></svg>
                        <span>Certificate Patterns <span class="admin-badge-super" style="background:#e83f63;">NEW</span></span>
                    </a>
                </li>

                <!-- ===== ASSIGN PATTERNS TO COURSES (NEW) ===== -->
                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/super-admin/patterns/course-assignment" class="admin-nav-link <?= ($currentPage ?? '') === 'course_assignment' ? 'active' : '' ?>">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M10 13a5 5 0 007.54.54l3-3a5 5 0 00-7.07-7.07l-1.72 1.71"/><path d="M14 11a5 5 0 00-7.54-.54l-3 3a5 5 0 007.07 7.07l1.71-1.71"/></svg>
                        <span>Assign to Courses <span class="admin-badge-super" style="background:#e83f63;">NEW</span></span>
                    </a>
                </li>

                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/super-admin/course-subjects" class="admin-nav-link <?= ($currentPage ?? '') === 'course_subjects' ? 'active' : '' ?>">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z"/><polyline points="22,6 12,13 2,6"/></svg>
                        <span>Course Subjects</span>
                    </a>
                </li>

                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/super-admin/cert-marksheet-mapping" class="admin-nav-link <?= ($currentPage ?? '') === 'cert_marksheet_mapping' ? 'active' : '' ?>">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="3" y="3" width="18" height="18" rx="2"/><circle cx="8.5" cy="8.5" r="1.5"/><polyline points="21 15 16 10 5 21"/></svg>
                        <span>Old Mapping (Legacy)</span>
                    </a>
                </li>

                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/super-admin/certificate-templates" class="admin-nav-link <?= ($currentPage ?? '') === 'certificate_templates' ? 'active' : '' ?>">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M14 2H6a2 2 0 00-2 2v16a2 2 0 002 2h12a2 2 0 002-2V8z"/><polyline points="14 2 14 8 20 8"/></svg>
                        <span>All Templates (Old)</span>
                    </a>
                </li>

                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/super-admin/announcements" class="admin-nav-link <?= ($currentPage ?? '') === 'announcements' ? 'active' : '' ?>">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z"/><polyline points="22,6 12,13 2,6"/></svg>
                        <span>Announcements</span>
                    </a>
                </li>

                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/super-admin/documents" class="admin-nav-link <?= ($currentPage ?? '') === 'documents' ? 'active' : '' ?>">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M9 11H5a2 2 0 00-2 2v7h18v-7a2 2 0 00-2-2h-4"/><polyline points="9 11 12 14 15 11"/><line x1="12" y1="2" x2="12" y2="14"/></svg>
                        <span>Document Approvals</span>
                    </a>
                </li>

                <!-- ===== SUPER ADMIN RESULT CREATION (Auto-Approve) ===== -->
                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/super-admin/cert-gen" class="admin-nav-link pattern-highlight <?= ($currentPage ?? '') === 'sa_cert_gen' ? 'active' : '' ?>">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M14 2H6a2 2 0 00-2 2v16a2 2 0 002 2h12a2 2 0 002-2V8z"/><polyline points="14 2 14 8 20 8"/><line x1="12" y1="18" x2="12" y2="12"/><polyline points="9 15 12 18 15 15"/></svg>
                        <span>Create Result <span class="admin-badge-super" style="background:#16a34a;">AUTO</span></span>
                    </a>
                </li>

                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/super-admin/settings" class="admin-nav-link <?= ($currentPage ?? '') === 'settings' ? 'active' : '' ?>">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 00.33 1.82l.06.06a2 2 0 010 2.83 2 2 0 01-2.83 0l-.06-.06a1.65 1.65 0 00-1.82-.33 1.65 1.65 0 00-1 1.51V21a2 2 0 01-4 0v-.09A1.65 1.65 0 009 19.4a1.65 1.65 0 00-1.82.33l-.06.06a2 2 0 01-2.83 0 2 2 0 010-2.83l.06-.06a1.65 1.65 0 00.33-1.82 1.65 1.65 0 00-1.51-1H3a2 2 0 010-4h.09A1.65 1.65 0 004.6 9a1.65 1.65 0 00-.33-1.82l-.06-.06a2 2 0 010-2.83 2 2 0 012.83 0l.06.06a1.65 1.65 0 001.82.33H9a1.65 1.65 0 001-1.51V3a2 2 0 014 0v.09a1.65 1.65 0 001 1.51 1.65 1.65 0 001.82-.33l.06-.06a2 2 0 012.83 0 2 2 0 010 2.83l-.06.06a1.65 1.65 0 00-.33 1.82V9a1.65 1.65 0 001.51 1H21a2 2 0 010 4h-.09a1.65 1.65 0 00-1.51 1z"/></svg>
                        <span>System Settings</span>
                    </a>
                </li>

                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/admin" class="admin-nav-link" style="border-top:1px solid rgba(255,255,255,0.1);margin-top:12px;padding-top:16px;">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="2" y1="12" x2="22" y2="12"/><path d="M12 2a15.3 15.3 0 014 10 15.3 15.3 0 01-4 10 15.3 15.3 0 01-4-10 15.3 15.3 0 014-10z"/></svg>
                        <span>Website Admin</span>
                    </a>
                </li>

                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/" class="admin-nav-link">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M3 9l9-7 9 7v11a2 2 0 01-2 2H5a2 2 0 01-2-2z"/><polyline points="9 22 9 12 15 12 15 22"/></svg>
                        <span>View Website</span>
                    </a>
                </li>
            </ul>
        </nav>

        <div class="admin-sidebar-footer">
            <form method="POST" action="<?= $bp ?>/super-admin/logout" class="admin-logout-form">
                <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                <button type="submit" class="admin-nav-link admin-logout-link">
                    <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M9 21H5a2 2 0 01-2-2V5a2 2 0 012-2h4"/><polyline points="16 17 21 12 16 7"/><line x1="21" y1="12" x2="9" y2="12"/></svg>
                    <span>Logout</span>
                </button>
            </form>
        </div>
    </aside>

    <div class="admin-main">
        <header class="admin-topbar">
            <div class="admin-topbar-left">
                <button class="admin-hamburger" id="sidebarToggle" aria-label="Toggle sidebar">
                    <span></span><span></span><span></span>
                </button>
                <div class="admin-breadcrumb">
                    <a href="<?= $bp ?>/super-admin/dashboard">Super Admin</a>
                    <span class="admin-breadcrumb-sep">/</span>
                    <span class="admin-breadcrumb-current"><?= htmlspecialchars($title ?? 'Dashboard', ENT_QUOTES, 'UTF-8') ?></span>
                </div>
            </div>
            <div class="admin-topbar-right">
                <div class="admin-user-info">
                    <div class="admin-user-avatar" style="background:linear-gradient(135deg,#6c3fe8,#4a2db8);color:#fff;">
                        <?= $userInitial ?>
                    </div>
                    <span class="admin-user-name"><?= htmlspecialchars($currentUser['name'] ?? 'Super Admin', ENT_QUOTES, 'UTF-8') ?></span>
                    <span class="admin-badge-super">SUPER ADMIN</span>
                </div>
            </div>
        </header>

        <?php if (!empty($flash_error)): ?>
        <div class="admin-alert admin-alert-error">
            <span><?= htmlspecialchars($flash_error, ENT_QUOTES, 'UTF-8') ?></span>
            <button class="admin-alert-close" aria-label="Close">&times;</button>
        </div>
        <?php endif; ?>
        <?php if (!empty($flash_success)): ?>
        <div class="admin-alert admin-alert-success">
            <span><?= htmlspecialchars($flash_success, ENT_QUOTES, 'UTF-8') ?></span>
            <button class="admin-alert-close" aria-label="Close">&times;</button>
        </div>
        <?php endif; ?>
        <?php if (!empty($flash_warning)): ?>
        <div class="admin-alert admin-alert-warning">
            <span><?= htmlspecialchars($flash_warning, ENT_QUOTES, 'UTF-8') ?></span>
            <button class="admin-alert-close" aria-label="Close">&times;</button>
        </div>
        <?php endif; ?>
        <?php if (!empty($flash_info)): ?>
        <div class="admin-alert admin-alert-info">
            <span><?= htmlspecialchars($flash_info, ENT_QUOTES, 'UTF-8') ?></span>
            <button class="admin-alert-close" aria-label="Close">&times;</button>
        </div>
        <?php endif; ?>

        <main class="admin-content">
            <div class="admin-page-header">
                <h1 class="admin-page-title"><?= htmlspecialchars($title ?? 'Dashboard', ENT_QUOTES, 'UTF-8') ?></h1>
            </div>
            <?= $content ?? '' ?>
        </main>
    </div>

    <div class="admin-modal-overlay" id="deleteModal">
        <div class="admin-modal">
            <div class="admin-modal-header">
                <h3 class="admin-modal-title">Confirm Delete</h3>
                <button class="admin-modal-close" id="deleteModalClose" aria-label="Close">&times;</button>
            </div>
            <div class="admin-modal-body">
                <p>Are you sure you want to delete this item?</p>
            </div>
            <div class="admin-modal-footer">
                <button class="admin-btn admin-btn-secondary" id="deleteModalCancel">Cancel</button>
                <form method="POST" id="deleteForm" style="display:inline">
                    <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                    <button type="submit" class="admin-btn admin-btn-danger">Delete</button>
                </form>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
    <script src="<?= $bp ?>/js/admin.js"></script>
</body>
</html>
