<?php
require_once __DIR__ . '/../../includes/functions.php';

if (!isLoggedIn() || !isAdmin()) {
    redirect(BASE_URL . 'login.php');
}

$adminUser = getUserById($_SESSION['user_id']);
$settings = getAllSettings();
$currentPage = basename($_SERVER['PHP_SELF'], '.php');

// Get counts for sidebar badges
$db = getDBConnection();
$stmt = $db->prepare("SELECT COUNT(*) as c FROM bookings WHERE status = 'pending'");
$stmt->execute();
$pendingBookings = $stmt->fetch()['c'];

$stmt = $db->prepare("SELECT COUNT(*) as c FROM enquiries WHERE status = 'new'");
$stmt->execute();
$newEnquiries = $stmt->fetch()['c'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= isset($pageTitle) ? htmlspecialchars($pageTitle, ENT_QUOTES, 'UTF-8') . ' - ' : '' ?>Admin Panel - Traveler</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&family=Playfair+Display:wght@400;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
    <link rel="stylesheet" href="<?= BASE_URL ?>assets/css/style.css">
    <link rel="stylesheet" href="<?= BASE_URL ?>assets/css/admin.css">
</head>
<body>
    <div class="admin-wrapper">
        <!-- Sidebar -->
        <aside class="admin-sidebar">
            <div class="sidebar-header">
                <div>
                    <?php if (($settings['logo_type'] ?? 'image') === 'text'): ?>
                        <?php $textLogo = $settings['text_logo'] ?? 'Traveler'; ?>
                        <?php $textLogoFont = $settings['text_logo_font'] ?? 'Playfair Display'; ?>
                        <?php $textLogoSize = $settings['text_logo_size'] ?? '24'; ?>
                        <?php $textLogoColor = $settings['text_logo_color'] ?? '#FF6B35'; ?>
                        <div class="brand-text" style="font-family: '<?= htmlspecialchars($textLogoFont, ENT_QUOTES, 'UTF-8') ?>', serif; font-size: <?= htmlspecialchars($textLogoSize, ENT_QUOTES, 'UTF-8') ?>px;">
                            <?= htmlspecialchars($textLogo, ENT_QUOTES, 'UTF-8') ?>
                        </div>
                    <?php else: ?>
                        <?php $logoImage = $settings['site_logo'] ?? ''; ?>
                        <?php if (!empty($logoImage) && file_exists(UPLOAD_PATH . 'general/' . $logoImage)): ?>
                            <img src="<?= htmlspecialchars(UPLOAD_URL . 'general/' . $logoImage, ENT_QUOTES, 'UTF-8') ?>" alt="Logo" style="max-height:35px; margin-bottom:5px;">
                        <?php else: ?>
                            <div class="brand-text">Travel<span>er</span></div>
                        <?php endif; ?>
                    <?php endif; ?>
                    <span class="admin-badge">Admin Panel</span>
                </div>
            </div>
            
            <nav class="sidebar-menu">
                <div class="menu-label">Main</div>
                <a href="<?= ADMIN_URL ?>index.php" class="<?= $currentPage === 'index' ? 'active' : '' ?>">
                    <i class="fas fa-tachometer-alt"></i> Dashboard
                </a>
                
                <a href="<?= ADMIN_URL ?>bookings.php" class="<?= $currentPage === 'bookings' ? 'active' : '' ?>">
                    <i class="fas fa-calendar-check"></i> Bookings
                    <?php if ($pendingBookings > 0): ?>
                        <span class="badge-count"><?= $pendingBookings ?></span>
                    <?php endif; ?>
                </a>
                
                <a href="<?= ADMIN_URL ?>enquiries.php" class="<?= $currentPage === 'enquiries' ? 'active' : '' ?>">
                    <i class="fas fa-question-circle"></i> Enquiries
                    <?php if ($newEnquiries > 0): ?>
                        <span class="badge-count"><?= $newEnquiries ?></span>
                    <?php endif; ?>
                </a>
                
                <div class="menu-label">Tour Management</div>
                <div class="has-submenu <?= in_array($currentPage, ['places', 'place-add', 'place-edit', 'packages-list', 'package-add', 'package-edit']) ? 'open' : '' ?>">
                    <a href="javascript:void(0)" onclick="toggleSubmenu(this)">
                        <i class="fas fa-suitcase-rolling"></i> Tour Packages
                    </a>
                    <div class="submenu <?= in_array($currentPage, ['places', 'place-add', 'place-edit', 'packages-list', 'package-add', 'package-edit']) ? 'open' : '' ?>">
                        <a href="<?= ADMIN_URL ?>places.php" class="<?= $currentPage === 'places' ? 'active' : '' ?>">
                            <i class="fas fa-map-marker-alt"></i> Places
                        </a>
                        <a href="<?= ADMIN_URL ?>packages-list.php" class="<?= in_array($currentPage, ['packages-list', 'package-add', 'package-edit']) ? 'active' : '' ?>">
                            <i class="fas fa-box-open"></i> Packages
                        </a>
                    </div>
                </div>
                
                <div class="menu-label">Finance</div>
                <a href="<?= ADMIN_URL ?>transactions.php" class="<?= $currentPage === 'transactions' ? 'active' : '' ?>">
                    <i class="fas fa-credit-card"></i> Transactions
                </a>
                
                <div class="menu-label">Users</div>
                <a href="<?= ADMIN_URL ?>users.php" class="<?= $currentPage === 'users' ? 'active' : '' ?>">
                    <i class="fas fa-users"></i> User Management
                </a>
                
                <div class="menu-label">System</div>
                <a href="<?= ADMIN_URL ?>settings.php" class="<?= $currentPage === 'settings' ? 'active' : '' ?>">
                    <i class="fas fa-cog"></i> Settings
                </a>
                
                <a href="<?= BASE_URL ?>" target="_blank">
                    <i class="fas fa-external-link-alt"></i> View Website
                </a>
                
                <a href="<?= BASE_URL ?>logout.php">
                    <i class="fas fa-sign-out-alt"></i> Logout
                </a>
            </nav>
        </aside>
        
        <!-- Sidebar Overlay (Mobile) -->
        <div class="sidebar-overlay" onclick="toggleSidebar()"></div>
        
        <!-- Main Content -->
        <main class="admin-main">
            <!-- Top Bar -->
            <header class="admin-topbar">
                <div class="topbar-left">
                    <button class="sidebar-toggle" onclick="toggleSidebar()">
                        <i class="fas fa-bars"></i>
                    </button>
                    <div class="topbar-search">
                        <i class="fas fa-search"></i>
                        <input type="text" placeholder="Search...">
                    </div>
                </div>
                
                <div class="topbar-right">
                    <button class="topbar-icon">
                        <i class="fas fa-bell"></i>
                        <?php if ($pendingBookings + $newEnquiries > 0): ?>
                            <span class="notification-dot"></span>
                        <?php endif; ?>
                    </button>
                    
                    <div class="topbar-profile">
                        <?php 
                        $adminAvatarUrl = !empty($adminUser['profile_image']) 
                            ? getImageUrl($adminUser['profile_image'], 'profiles')
                            : 'https://ui-avatars.com/api/?name=' . urlencode($adminUser['name']) . '&background=FF6B35&color=fff&size=80';
                        ?>
                        <img src="<?= $adminAvatarUrl ?>" alt="Admin">
                        <div class="profile-info">
                            <div class="profile-name"><?= htmlspecialchars($adminUser['name'], ENT_QUOTES, 'UTF-8') ?></div>
                            <div class="profile-role">Administrator</div>
                        </div>
                    </div>
                </div>
            </header>
            
            <!-- Content Area -->
            <div class="admin-content">
                <?php $flash = getFlash(); ?>
                <?php if ($flash): ?>
                    <div class="alert alert-<?= htmlspecialchars($flash['type'], ENT_QUOTES, 'UTF-8') ?>">
                        <i class="fas fa-<?= $flash['type'] === 'success' ? 'check-circle' : 'exclamation-circle' ?>"></i>
                        <?= htmlspecialchars($flash['message'], ENT_QUOTES, 'UTF-8') ?>
                        <button class="close-alert">&times;</button>
                    </div>
                <?php endif; ?>
