<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Check if user is logged in
if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
    header('Location: ' . BASE_URL . '/admin/login');
    exit;
}

$admin_name = $_SESSION['admin_name'] ?? $_SESSION['admin_username'] ?? 'Admin';
$current_page = basename($_SERVER['PHP_SELF'], ".php");
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>
        <?php echo $page_title ?? 'Admin Dashboard'; ?> - Crest Chimney
    </title>
    <link rel="stylesheet"
        href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="/admin/css/admin-style.css">
    <?php if (isset($extra_css))
        echo $extra_css; ?>
</head>

<body>
    <div class="sidebar-overlay" id="sidebarOverlay"></div>
    <div class="admin-layout">
        <!-- Sidebar -->
        <aside class="sidebar" id="sidebar">
            <div class="sidebar-header">
                <div class="sidebar-logo">C</div>
                <div class="sidebar-brand">Crest Chimney</div>
            </div>

            <nav class="sidebar-nav">
                <div class="nav-label">Main Menu</div>
                <a href="/admin/dashboard" class="nav-link <?php echo $current_page == 'dashboard' ? 'active' : ''; ?>">
                    <i class="fas fa-th-large"></i>
                    Dashboard
                </a>
                <a href="/admin/appointments"
                    class="nav-link <?php echo $current_page == 'appointments' ? 'active' : ''; ?>">
                    <i class="fas fa-calendar-alt"></i>
                    Appointments
                </a>

                <div class="nav-label">Content Management</div>
                <a href="/admin/services" class="nav-link <?php echo $current_page == 'services' ? 'active' : ''; ?>">
                    <i class="fas fa-tools"></i>
                    Services
                </a>
                <a href="/admin/service-categories"
                    class="nav-link <?php echo $current_page == 'service-categories' ? 'active' : ''; ?>">
                    <i class="fas fa-folder-open"></i>
                    Categories
                </a>
                <a href="/admin/location" class="nav-link <?php echo $current_page == 'location' ? 'active' : ''; ?>">
                    <i class="fas fa-map-marker-alt"></i>
                    Our Location
                </a>
                <a href="/admin/team" class="nav-link <?php echo $current_page == 'team' ? 'active' : ''; ?>">
                    <i class="fas fa-users"></i>
                    Our Team
                </a>
                <a href="/admin/reviews" class="nav-link <?php echo $current_page == 'reviews' ? 'active' : ''; ?>">
                    <i class="fas fa-star"></i>
                    Reviews
                </a>
                <a href="/admin/faq" class="nav-link <?php echo $current_page == 'faq' ? 'active' : ''; ?>">
                    <i class="fas fa-question-circle"></i>
                    FAQs
                </a>

                <div class="nav-label">Site Settings</div>
                <a href="/admin/settings" class="nav-link <?php echo $current_page == 'settings' ? 'active' : ''; ?>">
                    <i class="fas fa-cog"></i>
                    General Settings
                </a>
                <a href="/admin/seo" class="nav-link <?php echo $current_page == 'seo' ? 'active' : ''; ?>">
                    <i class="fas fa-search"></i>
                    SEO Management
                </a>
                <a href="/admin/profile" class="nav-link <?php echo $current_page == 'profile' ? 'active' : ''; ?>">
                    <i class="fas fa-user-circle"></i>
                    My Profile
                </a>
            </nav>
        </aside>

        <!-- Main Content Area -->
        <main class="main-content">
            <!-- Topbar -->
            <header class="topbar">
                <div class="topbar-left" style="display: flex; align-items: center;">
                    <button class="menu-toggle" id="menuToggle">
                        <i class="fas fa-bars"></i>
                    </button>
                    <h1>
                        <?php echo $page_title ?? 'Dashboard'; ?>
                    </h1>
                </div>

                <div class="topbar-right">
                    <a href="/admin/profile" class="user-profile" style="text-decoration: none; color: inherit;">
                        <div class="user-avatar">
                            <?php echo strtoupper(substr($admin_name, 0, 1)); ?>
                        </div>
                        <div class="user-info">
                            <span class="user-name">
                                <?php echo htmlspecialchars($admin_name); ?>
                            </span>
                            <span class="user-role">Administrator</span>
                        </div>
                    </a>
                    <a href="/admin/logout" class="btn-logout">
                        <i class="fas fa-sign-out-alt"></i>
                        Logout
                    </a>
                </div>
            </header>

            <div class="content-wrapper">