<?php
require_once __DIR__ . '/../config/app.php';
requireLogin();
$currentUser = getCurrentUser();
$currentPage = basename($_SERVER['PHP_SELF'], '.php');
?>
<!DOCTYPE html>
<html lang="fr" dir="ltr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= $pageTitle ?? 'Tableau de bord' ?> - <?= APP_NAME ?></title>
    <link href="<?= APP_URL ?>/assets/css/bootstrap.min.css" rel="stylesheet">
    <link href="<?= APP_URL ?>/assets/css/bootstrap-icons.min.css" rel="stylesheet">
    <link href="<?= APP_URL ?>/assets/css/select2.min.css" rel="stylesheet">
    <link href="<?= APP_URL ?>/assets/css/select2-bootstrap-5-theme.min.css" rel="stylesheet">
    <style>
        :root {
            --sidebar-width: 260px;
            --sidebar-bg: #0f172a;
            --sidebar-hover: #1e293b;
            --sidebar-active: #065f46;
            --primary-green: #065f46;
            --primary-blue: #0c4a6e;
        }
        body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background: #f1f5f9; }
        
        /* Sidebar */
        .sidebar {
            position: fixed; top: 0; left: 0; bottom: 0;
            width: var(--sidebar-width);
            background: var(--sidebar-bg);
            color: #fff;
            overflow-y: auto;
            z-index: 1040;
            transition: transform 0.3s ease;
        }
        .sidebar-brand {
            padding: 1.25rem 1rem;
            background: linear-gradient(135deg, var(--primary-blue), var(--primary-green));
            text-align: center;
            border-bottom: 1px solid rgba(255,255,255,0.1);
        }
        .sidebar-brand h5 { margin: 0; font-weight: 700; font-size: 1rem; }
        .sidebar-brand small { opacity: 0.8; font-size: 0.75rem; }
        .sidebar-nav { padding: 0.5rem 0; }
        .sidebar-nav .nav-section {
            padding: 0.75rem 1rem 0.25rem;
            font-size: 0.7rem;
            text-transform: uppercase;
            letter-spacing: 1px;
            color: #64748b;
            font-weight: 600;
        }
        .sidebar-nav .nav-link {
            color: #94a3b8;
            padding: 0.6rem 1rem;
            display: flex;
            align-items: center;
            gap: 0.75rem;
            font-size: 0.875rem;
            border-radius: 0;
            transition: all 0.2s;
            border-left: 3px solid transparent;
        }
        .sidebar-nav .nav-link:hover {
            color: #fff;
            background: var(--sidebar-hover);
        }
        .sidebar-nav .nav-link.active {
            color: #fff;
            background: rgba(6,95,70,0.3);
            border-left-color: #10b981;
        }
        .sidebar-nav .nav-link i { font-size: 1.1rem; width: 24px; text-align: center; }
        
        /* Main content */
        .main-content {
            margin-left: var(--sidebar-width);
            min-height: 100vh;
        }
        
        /* Top navbar */
        .top-navbar {
            background: #fff;
            border-bottom: 1px solid #e2e8f0;
            padding: 0.75rem 1.5rem;
            display: flex;
            justify-content: space-between;
            align-items: center;
            position: sticky;
            top: 0;
            z-index: 1030;
        }
        
        /* Content area */
        .content-area { padding: 1.5rem; }
        
        /* Cards */
        .stat-card {
            border: none;
            border-radius: 16px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.08);
            transition: transform 0.2s, box-shadow 0.2s;
        }
        .stat-card:hover { transform: translateY(-2px); box-shadow: 0 8px 25px rgba(0,0,0,0.1); }
        .stat-card .card-body { padding: 1.25rem; }
        .stat-icon {
            width: 48px; height: 48px;
            border-radius: 12px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 1.5rem;
        }
        
        /* Table styles */
        .table-card {
            border: none;
            border-radius: 16px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.08);
            overflow: hidden;
        }
        .table-card .card-header {
            background: #fff;
            border-bottom: 1px solid #e2e8f0;
            padding: 1rem 1.25rem;
            font-weight: 600;
        }
        .table { margin-bottom: 0; }
        .table th {
            background: #f8fafc;
            font-weight: 600;
            font-size: 0.8rem;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            color: #475569;
            border-bottom: 2px solid #e2e8f0;
            white-space: nowrap;
        }
        .table td { vertical-align: middle; font-size: 0.9rem; }
        
        /* Badges */
        .badge { font-weight: 500; padding: 0.4em 0.8em; border-radius: 6px; }
        
        /* Buttons */
        .btn { border-radius: 8px; font-size: 0.875rem; }
        .btn-primary { background: var(--primary-green); border-color: var(--primary-green); }
        .btn-primary:hover { background: #047857; border-color: #047857; }
        
        /* Form styling */
        .form-control, .form-select { border-radius: 8px; border: 2px solid #e2e8f0; }
        .form-control:focus, .form-select:focus { border-color: var(--primary-green); box-shadow: 0 0 0 0.2rem rgba(6,95,70,0.15); }
        
        /* Mobile responsive */
        .sidebar-toggle { display: none; }
        .sidebar-overlay {
            display: none;
            position: fixed; inset: 0;
            background: rgba(0,0,0,0.5);
            z-index: 1035;
        }
        
        @media (max-width: 991.98px) {
            .sidebar { transform: translateX(-100%); }
            .sidebar.show { transform: translateX(0); }
            .sidebar-overlay.show { display: block; }
            .main-content { margin-left: 0; }
            .sidebar-toggle { display: inline-flex; }
        }
        
        /* Print styles */
        @media print {
            .sidebar, .top-navbar, .btn, .no-print { display: none !important; }
            .main-content { margin-left: 0; }
        }

        /* Alert cards */
        .alert-card { border-left: 4px solid; border-radius: 8px; }
    </style>
</head>
<body>

<!-- Sidebar overlay (mobile) -->
<div class="sidebar-overlay" id="sidebarOverlay" onclick="toggleSidebar()"></div>

<!-- Sidebar -->
<nav class="sidebar" id="sidebar">
    <div class="sidebar-brand">
        <div style="font-size:1.8rem;">🕌</div>
        <h5>UpTours</h5>
        <small>Gestion Hajj & Omra</small>
    </div>
    <div class="sidebar-nav">
        <div class="nav-section">Principal</div>
        <a href="<?= APP_URL ?>/dashboard.php" class="nav-link <?= $currentPage=='dashboard'?'active':'' ?>">
            <i class="bi bi-grid-1x2-fill"></i> Tableau de bord
        </a>
        
        <div class="nav-section">Gestion</div>
        <a href="<?= APP_URL ?>/voyageurs.php" class="nav-link <?= $currentPage=='voyageurs'?'active':'' ?>">
            <i class="bi bi-people-fill"></i> Voyageurs & Pèlerins
        </a>
        <a href="<?= APP_URL ?>/programmes.php" class="nav-link <?= $currentPage=='programmes'?'active':'' ?>">
            <i class="bi bi-calendar-event-fill"></i> Programmes & Voyages
        </a>
        <a href="<?= APP_URL ?>/inscriptions.php" class="nav-link <?= $currentPage=='inscriptions'?'active':'' ?>">
            <i class="bi bi-journal-check"></i> Inscriptions
        </a>
        <a href="<?= APP_URL ?>/groupes.php" class="nav-link <?= $currentPage=='groupes'?'active':'' ?>">
            <i class="bi bi-diagram-3-fill"></i> Groupes
        </a>
        <a href="<?= APP_URL ?>/services.php" class="nav-link <?= $currentPage=='services'?'active':'' ?>">
            <i class="bi bi-briefcase-fill"></i> Services (Billets, Visas...)
        </a>
        
        <div class="nav-section">Logistique</div>
        <a href="<?= APP_URL ?>/hotels.php" class="nav-link <?= $currentPage=='hotels'?'active':'' ?>">
            <i class="bi bi-building-fill"></i> Hôtels
        </a>
        <a href="<?= APP_URL ?>/vols.php" class="nav-link <?= $currentPage=='vols'?'active':'' ?>">
            <i class="bi bi-airplane-fill"></i> Vols
        </a>
        
        <div class="nav-section">Finance</div>
        <a href="<?= APP_URL ?>/devis.php" class="nav-link <?= $currentPage=='devis'?'active':'' ?>">
            <i class="bi bi-file-earmark-text-fill"></i> Devis / Pro-formas
        </a>
        <a href="<?= APP_URL ?>/factures.php" class="nav-link <?= $currentPage=='factures'?'active':'' ?>">
            <i class="bi bi-file-earmark-ruled-fill"></i> Factures
        </a>
        <a href="<?= APP_URL ?>/paiements.php" class="nav-link <?= $currentPage=='paiements'?'active':'' ?>">
            <i class="bi bi-cash-stack"></i> Paiements & Reçus
        </a>
        <a href="<?= APP_URL ?>/depenses.php" class="nav-link <?= $currentPage=='depenses'?'active':'' ?>">
            <i class="bi bi-graph-down-arrow"></i> Dépenses
        </a>
        
        <div class="nav-section">Outils</div>
        <a href="<?= APP_URL ?>/documents.php" class="nav-link <?= $currentPage=='documents'?'active':'' ?>">
            <i class="bi bi-folder-fill"></i> Documents
        </a>
        <a href="<?= APP_URL ?>/rapports.php" class="nav-link <?= $currentPage=='rapports'?'active':'' ?>">
            <i class="bi bi-bar-chart-fill"></i> Rapports
        </a>
        <a href="<?= APP_URL ?>/imprimer.php" class="nav-link <?= $currentPage=='imprimer'?'active':'' ?>">
            <i class="bi bi-printer-fill"></i> Imprimer les listes
        </a>
        
        <?php if ($_SESSION['user_role'] === 'admin'): ?>
        <div class="nav-section">Administration</div>
        <a href="<?= APP_URL ?>/utilisateurs.php" class="nav-link <?= $currentPage=='utilisateurs'?'active':'' ?>">
            <i class="bi bi-shield-lock-fill"></i> Utilisateurs
        </a>
        <a href="<?= APP_URL ?>/logs.php" class="nav-link <?= $currentPage=='logs'?'active':'' ?>">
            <i class="bi bi-clock-history"></i> Journal d'activité
        </a>
        <a href="<?= APP_URL ?>/parametres.php" class="nav-link <?= $currentPage=='parametres'?'active':'' ?>">
            <i class="bi bi-gear-fill"></i> Paramètres
        </a>
        <?php endif; ?>
    </div>
</nav>

<!-- Main Content -->
<div class="main-content">
    <!-- Top Navbar -->
    <div class="top-navbar">
        <div class="d-flex align-items-center gap-3">
            <button class="btn btn-outline-secondary btn-sm sidebar-toggle" onclick="toggleSidebar()">
                <i class="bi bi-list fs-5"></i>
            </button>
            <h5 class="mb-0 text-dark"><?= $pageTitle ?? 'Tableau de bord' ?></h5>
        </div>
        <div class="d-flex align-items-center gap-3">
            <span class="text-muted d-none d-md-inline small">
                <i class="bi bi-calendar3 me-1"></i><?= strftime('%d/%m/%Y') ?: date('d/m/Y') ?>
            </span>
            <div class="dropdown">
                <button class="btn btn-light btn-sm dropdown-toggle" data-bs-toggle="dropdown">
                    <i class="bi bi-person-circle me-1"></i>
                    <span class="d-none d-md-inline"><?= sanitize($_SESSION['user_name'] ?? '') ?></span>
                </button>
                <ul class="dropdown-menu dropdown-menu-end">
                    <li><span class="dropdown-item-text small text-muted"><?= sanitize($_SESSION['user_role'] ?? '') ?></span></li>
                    <li><hr class="dropdown-divider"></li>
                    <li><a class="dropdown-item" href="<?= APP_URL ?>/profil.php"><i class="bi bi-person me-2"></i>Mon profil</a></li>
                    <li><a class="dropdown-item text-danger" href="<?= APP_URL ?>/logout.php"><i class="bi bi-box-arrow-right me-2"></i>Déconnexion</a></li>
                </ul>
            </div>
        </div>
    </div>
    
    <!-- Content -->
    <div class="content-area">
        <?php if ($successMsg = flash('success')): ?>
            <div class="alert alert-success alert-dismissible fade show"><i class="bi bi-check-circle me-2"></i><?= $successMsg ?><button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>
        <?php endif; ?>
        <?php if ($errorMsg = flash('error')): ?>
            <div class="alert alert-danger alert-dismissible fade show"><i class="bi bi-exclamation-triangle me-2"></i><?= $errorMsg ?><button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>
        <?php endif; ?>
