<?php
// Start session if not already started
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Check if user is logged in, if not redirect to login page
if (!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true) {
    header("location: login.php");
    exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Panel - Real Estate</title>
    
    <!-- Google Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Nunito:wght@300;400;600;700&display=swap" rel="stylesheet">
    
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    
    <!-- Custom styles -->
    <link href="assets/css/admin-style.css" rel="stylesheet">
    
    <!-- DataTables CSS -->
    <link href="https://cdn.datatables.net/1.11.5/css/dataTables.bootstrap5.min.css" rel="stylesheet">
    
    <!-- Custom styles for this page -->
    <style>
        .sidebar {
            background: linear-gradient(180deg, #4e73df 10%, #224abe 100%);
        }
        .sidebar .nav-link {
            color: rgba(255, 255, 255, 0.8);
            font-weight: 600;
        }
        .sidebar .nav-link:hover, .sidebar .nav-link.active {
            color: #fff;
            background: rgba(255, 255, 255, 0.1);
        }
        .sidebar .nav-link i {
            margin-right: 0.5rem;
        }
    </style>
</head>
<body id="page-top">
    <!-- Page Wrapper -->
   <!-- Sidebar -->
<div class="sidebar">
    <div class="d-flex flex-column h-100">
        <!-- Brand Logo -->
        <a href="index.php" class="brand-link text-decoration-none d-flex align-items-center p-3">
            <span class="brand-text fs-4 fw-bold text-white">Admin Panel</span>
        </a>

        <!-- Sidebar Menu -->
        <nav class="mt-2 px-2">
            <ul class="nav nav-pills flex-column">
                <li class="nav-item">
                    <a href="index.php" class="nav-link text-white <?= basename($_SERVER['PHP_SELF']) == 'index.php' ? 'active' : '' ?>">
                        <i class="fas fa-tachometer-alt me-2"></i> Dashboard
                    </a>
                </li>
                
                <!-- Properties Section -->
                <li class="nav-item">
                    <a href="properties.php" class="nav-link text-white <?= basename($_SERVER['PHP_SELF']) == 'properties.php' ? 'active' : '' ?>">
                        <i class="fas fa-home me-2"></i> Properties
                    </a>
                </li>
                
                <!-- Gallery Section -->
                <li class="nav-item">
                    <a href="gallery.php" class="nav-link text-white <?= basename($_SERVER['PHP_SELF']) == 'gallery.php' ? 'active' : '' ?>">
                        <i class="fas fa-images me-2"></i> Gallery
                    </a>
                </li>
                
                <!-- Messages Section -->
                <li class="nav-item">
                    <a href="contacts.php" class="nav-link text-white <?= basename($_SERVER['PHP_SELF']) == 'contacts.php' ? 'active' : '' ?>">
                        <i class="fas fa-envelope me-2"></i> Messages
                        <?php if(isset($new_messages_count) && $new_messages_count > 0): ?>
                            <span class="badge bg-danger rounded-pill float-end"><?= $new_messages_count ?></span>
                        <?php endif; ?>
                    </a>
                </li>
                
                <!-- Categories Section -->
                <li class="nav-item">
                    <a href="categories.php" class="nav-link text-white <?= basename($_SERVER['PHP_SELF']) == 'categories.php' ? 'active' : '' ?>">
                        <i class="fas fa-tags me-2"></i> Categories
                    </a>
                </li>
                
                <!-- Visit Requests Section -->
                <li class="nav-item">
                    <a href="visit_requests.php" class="nav-link text-white <?= basename($_SERVER['PHP_SELF']) == 'visit_requests.php' ? 'active' : '' ?>">
                        <i class="fas fa-calendar-check me-2"></i> Visit Requests
                    </a>
                </li>
                
                <!-- Settings Section -->
                <li class="nav-item">
                    <a href="settings.php" class="nav-link text-white <?= basename($_SERVER['PHP_SELF']) == 'settings.php' ? 'active' : '' ?>">
                        <i class="fas fa-cog me-2"></i> Settings
                    </a>
                </li>
            </ul>
        </nav>

        <!-- User Profile -->
        <div class="mt-auto p-3 border-top border-secondary">
            <div class="d-flex align-items-center text-white">
                <img src="https://ui-avatars.com/api/?name=<?= urlencode($_SESSION['username'] ?? 'Admin') ?>&background=4361ee&color=fff" 
                     alt="User" 
                     class="rounded-circle me-2" 
                     width="40" 
                     height="40">
                <div>
                    <div class="fw-bold"><?= htmlspecialchars($_SESSION['username'] ?? 'Admin') ?></div>
                    <small class="text-muted">Administrator</small>
                </div>
            </div>
            <div class="mt-3">
                <a href="logout.php" class="btn btn-outline-light btn-sm w-100">
                    <i class="fas fa-sign-out-alt me-1"></i> Logout
                </a>
            </div>
        </div>
    </div>
</div>

<!-- Main Content -->
<div class="content-wrapper">
    <!-- Top Navigation -->
    <nav class="navbar navbar-expand navbar-light bg-white topbar static-top shadow">
        <!-- Sidebar Toggle (Topbar) -->
        <button id="sidebarToggleTop" class="btn btn-link d-md-none rounded-circle mr-3">
            <i class="fa fa-bars"></i>
        </button>

        <!-- Topbar Navbar -->
        <ul class="navbar-nav ml-auto">
            <!-- Nav Item - User Information -->
            <li class="nav-item dropdown no-arrow">
                <a class="nav-link dropdown-toggle" href="#" id="userDropdown" role="button" 
                   data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    <span class="mr-2 d-none d-lg-inline text-gray-600 small">
                        <?= htmlspecialchars($_SESSION['username'] ?? 'Admin') ?>
                    </span>
                    <img class="img-profile rounded-circle" 
                         src="https://ui-avatars.com/api/?name=<?= urlencode($_SESSION['username'] ?? 'Admin') ?>&background=4361ee&color=fff"
                         width="32" height="32">
                </a>
                <!-- Dropdown - User Information -->
                <div class="dropdown-menu dropdown-menu-right shadow" aria-labelledby="userDropdown">
                    <a class="dropdown-item" href="profile.php">
                        <i class="fas fa-user fa-sm fa-fw mr-2 text-gray-400"></i>
                        Profile
                    </a>
                    <div class="dropdown-divider"></div>
                    <a class="dropdown-item" href="logout.php">
                        <i class="fas fa-sign-out-alt fa-sm fa-fw mr-2 text-gray-400"></i>
                        Logout
                    </a>
                </div>
            </li>
        </ul>
    </nav>
    <!-- End of Topbar -->

    <!-- Begin Page Content -->
    <div class="container-fluid">
        <!-- Page Heading -->
        <div class="d-sm-flex align-items-center justify-content-between mb-4">
            <h1 class="h3 mb-0 text-gray-800"><?= $page_title ?? 'Dashboard' ?></h1>
            <?php if (!in_array(basename($_SERVER['PHP_SELF']), ['settings.php', 'profile.php'])): ?>
            <a href="#" class="d-none d-sm-inline-block btn btn-sm btn-primary shadow-sm">
                <i class="fas fa-download fa-sm text-white-50"></i> Generate Report
            </a>
            <?php endif; ?>
        </div>
        <script>
            document.addEventListener('DOMContentLoaded', function() {
    const sidebarToggle = document.getElementById('sidebarToggleTop');
    const sidebar = document.querySelector('.sidebar');
    const contentWrapper = document.querySelector('.content-wrapper');
    
    if (sidebarToggle && sidebar && contentWrapper) {
        sidebarToggle.addEventListener('click', function(e) {
            e.preventDefault();
            sidebar.classList.toggle('active');
            contentWrapper.classList.toggle('active');
        });
    }
});
</script>
