<?php
require_once 'auth_check.php';
require_once 'db.php';

// Database se real dynamic counts fetch karne ke liye queries
$enq_count = 0;
$pkg_count = 0;
$hotel_count = 0;

if (isset($conn) && $conn instanceof mysqli) {
    $res_enq = mysqli_query($conn, "SELECT COUNT(*) as total FROM enquiries");
    if ($res_enq) {
        $enq_count = mysqli_fetch_assoc($res_enq)['total'];
    }

    $res_pkg = mysqli_query($conn, "SELECT COUNT(*) as total FROM packages");
    if ($res_pkg) {
        $pkg_count = mysqli_fetch_assoc($res_pkg)['total'];
    }

    $res_hotel = mysqli_query($conn, "SELECT COUNT(*) as total FROM hotels");
    if ($res_hotel) {
        $hotel_count = mysqli_fetch_assoc($res_hotel)['total'];
    }
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Dashboard | Wings Tour & Travels</title>

    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css">
    <!-- Google Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700&display=swap" rel="stylesheet">

    <style>
        :root {
            --brand-green: #12521f;
            --brand-saffron: #D4A359;
            --dark-navy: #0F172A;
            --sidebar-width: 260px;
            --bg-light: #F8FAFC;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Poppins', sans-serif;
        }

        body {
            background-color: var(--bg-light);
            display: flex;
            min-height: 100vh;
        }

        /* SIDEBAR STYLING */
        .admin-sidebar {
            width: var(--sidebar-width);
            background-color: var(--dark-navy);
            color: #ffffff;
            position: fixed;
            top: 0;
            bottom: 0;
            left: 0;
            display: flex;
            flex-direction: column;
            z-index: 100;
            box-shadow: 4px 0 15px rgba(0, 0, 0, 0.05);
        }

        .sidebar-brand {
            padding: 24px 20px;
            background: rgba(0, 0, 0, 0.25);
            text-align: center;
            border-bottom: 1px solid rgba(255, 255, 255, 0.05);
        }

        .sidebar-brand h2 {
            font-size: 22px;
            color: var(--brand-saffron);
            letter-spacing: 1.5px;
            font-weight: 700;
        }

        .sidebar-brand p {
            font-size: 11px;
            color: #94A3B8;
            margin-top: 2px;
        }

        .sidebar-menu {
            list-style: none;
            padding: 20px 0;
            flex: 1;
            overflow-y: auto;
        }

        .sidebar-menu li a {
            display: flex;
            align-items: center;
            gap: 14px;
            padding: 13px 24px;
            color: #CBD5E1;
            text-decoration: none;
            font-size: 14px;
            font-weight: 500;
            transition: all 0.25s ease;
        }

        .sidebar-menu li a:hover,
        .sidebar-menu li.active a {
            background: rgba(255, 255, 255, 0.08);
            color: #ffffff;
            border-left: 4px solid var(--brand-saffron);
        }

        .sidebar-menu li a i {
            font-size: 16px;
            width: 20px;
            text-align: center;
        }

        /* MAIN CONTENT AREA */
        .admin-main {
            margin-left: var(--sidebar-width);
            flex: 1;
            display: flex;
            flex-direction: column;
            width: calc(100% - var(--sidebar-width));
        }

        /* TOPBAR */
        .admin-topbar {
            background: #ffffff;
            height: 70px;
            padding: 0 35px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.03);
            position: sticky;
            top: 0;
            z-index: 99;
        }

        .topbar-welcome {
            font-size: 15px;
            color: var(--dark-navy);
            font-weight: 600;
        }

        .topbar-user {
            display: flex;
            align-items: center;
            gap: 16px;
        }

        .user-info {
            text-align: right;
        }

        .user-info .name {
            font-weight: 600;
            font-size: 14px;
            color: var(--dark-navy);
        }

        .user-info .role {
            font-size: 11px;
            color: #64748B;
        }

        .btn-logout {
            padding: 8px 16px;
            background: #FEF2F2;
            color: #EF4444;
            border-radius: 8px;
            text-decoration: none;
            font-size: 13px;
            font-weight: 600;
            display: inline-flex;
            align-items: center;
            gap: 6px;
            transition: all 0.2s ease;
            border: 1px solid #FCA5A5;
        }

        .btn-logout:hover {
            background: #EF4444;
            color: #ffffff;
        }

        /* CONTENT */
        .admin-content {
            padding: 35px;
        }

        /* WELCOME BANNER CARD */
        .welcome-banner {
            background: linear-gradient(135deg, var(--brand-green) 0%, #0d3d17 100%);
            color: #ffffff;
            padding: 30px;
            border-radius: 16px;
            margin-bottom: 30px;
            box-shadow: 0 10px 25px rgba(18, 82, 31, 0.15);
            display: flex;
            align-items: center;
            justify-content: space-between;
            position: relative;
            overflow: hidden;
        }

        .welcome-banner::after {
            content: '';
            position: absolute;
            right: -20px;
            bottom: -30px;
            width: 200px;
            height: 200px;
            background: rgba(255, 255, 255, 0.05);
            border-radius: 50%;
            pointer-events: none;
        }

        .welcome-text h1 {
            font-size: 24px;
            font-weight: 700;
            margin-bottom: 6px;
            color: #ffffff;
        }

        .welcome-text p {
            font-size: 13.5px;
            color: #E2E8F0;
            max-width: 500px;
            line-height: 1.5;
        }

        .welcome-badge {
            background: rgba(212, 163, 89, 0.2);
            border: 1px solid var(--brand-saffron);
            color: var(--brand-saffron);
            padding: 8px 16px;
            border-radius: 30px;
            font-size: 13px;
            font-weight: 600;
            white-space: nowrap;
        }

        .section-title {
            font-size: 18px;
            color: var(--dark-navy);
            margin-bottom: 20px;
            font-weight: 700;
        }

        /* DASHBOARD STATS CARDS */
        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
            gap: 24px;
            margin-bottom: 35px;
        }

        .stat-card {
            background: #ffffff;
            padding: 24px;
            border-radius: 14px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.03);
            display: flex;
            align-items: center;
            justify-content: space-between;
            border: 1px solid #E2E8F0;
            transition: transform 0.2s ease, box-shadow 0.2s ease;
        }

        .stat-card:hover {
            transform: translateY(-3px);
            box-shadow: 0 8px 25px rgba(0, 0, 0, 0.06);
        }

        .stat-info h3 {
            font-size: 26px;
            color: var(--dark-navy);
            font-weight: 700;
        }

        .stat-info p {
            font-size: 13px;
            color: #64748B;
            margin-top: 4px;
            font-weight: 500;
        }

        .stat-icon {
            width: 55px;
            height: 55px;
            border-radius: 12px;
            background: rgba(18, 82, 31, 0.08);
            color: var(--brand-green);
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 22px;
        }

        /* RESPONSIVE DESIGN */
        @media (max-width: 992px) {
            .welcome-banner {
                flex-direction: column;
                align-items: flex-start;
                gap: 15px;
            }
        }

        @media (max-width: 768px) {
            .admin-sidebar {
                width: 70px;
            }

            .sidebar-brand h2,
            .sidebar-brand p,
            .sidebar-menu li a span {
                display: none;
            }

            .admin-main {
                margin-left: 70px;
                width: calc(100% - 70px);
            }

            .sidebar-menu li a {
                justify-content: center;
                padding: 16px;
            }

            .admin-topbar {
                padding: 0 20px;
            }

            .admin-content {
                padding: 20px;
            }

            .user-info {
                display: none; /* Mobile par space bachane ke liye name/role hide kiya hai */
            }
        }

        /* ===== Premium mobile responsive enhancement ===== */
        html { scroll-behavior: smooth; }
        body { overflow-x: hidden; -webkit-font-smoothing: antialiased; }
        .mobile-menu-toggle,
        .mobile-menu-overlay { display: none; }

        @media (max-width: 1100px) {
            .admin-content { padding: 26px; }
            .stats-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 18px; }
            .stat-card { padding: 20px; }
        }

        @media (max-width: 768px) {
            body { display: block; min-height: 100vh; }

            /* Off-canvas sidebar keeps every existing menu link intact */
            .admin-sidebar {
                width: min(280px, 82vw);
                transform: translateX(-105%);
                transition: transform .28s ease;
                box-shadow: 12px 0 30px rgba(15, 23, 42, .22);
            }
            body.mobile-menu-open .admin-sidebar { transform: translateX(0); }

            .sidebar-brand {
                padding: 22px 18px;
            }
            .sidebar-brand h2 { font-size: 21px; }
            .sidebar-menu { padding: 12px 0 24px; }
            .sidebar-menu li a {
                justify-content: flex-start;
                min-height: 48px;
                padding: 12px 20px;
                gap: 13px;
            }
            .sidebar-brand h2,
            .sidebar-brand p,
            .sidebar-menu li a span { display: block; }

            .admin-main {
                margin-left: 0;
                width: 100%;
                min-width: 0;
            }

            .admin-topbar {
                height: 62px;
                padding: 0 14px 0 58px;
                gap: 10px;
                position: sticky;
                top: 0;
            }
            .topbar-welcome {
                font-size: 13px;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
            }
            .topbar-user { gap: 8px; }
            .user-info { display: none; }
            .btn-logout {
                min-height: 40px;
                padding: 8px 11px;
                border-radius: 9px;
                font-size: 0;
            }
            .btn-logout i { font-size: 15px; }

            .mobile-menu-toggle {
                position: fixed;
                left: 10px;
                top: 11px;
                z-index: 1200;
                width: 40px;
                height: 40px;
                display: grid;
                place-items: center;
                border: 1px solid #E2E8F0;
                border-radius: 11px;
                background: #ffffff;
                color: var(--dark-navy);
                box-shadow: 0 5px 16px rgba(15, 23, 42, .12);
                cursor: pointer;
                font-size: 17px;
            }
            .mobile-menu-toggle:active { transform: scale(.96); }

            .mobile-menu-overlay {
                position: fixed;
                inset: 0;
                z-index: 90;
                background: rgba(15, 23, 42, .48);
                backdrop-filter: blur(2px);
            }
            body.mobile-menu-open .mobile-menu-overlay { display: block; }

            .admin-content {
                padding: 14px 12px 28px;
            }

            .welcome-banner {
                padding: 20px;
                border-radius: 15px;
                margin-bottom: 20px;
                gap: 13px;
            }
            .welcome-text h1 {
                font-size: 19px;
                line-height: 1.3;
                margin-bottom: 6px;
            }
            .welcome-text p {
                font-size: 12px;
                line-height: 1.55;
            }
            .welcome-badge {
                padding: 7px 12px;
                font-size: 11px;
            }

            .section-title {
                font-size: 16px;
                margin-bottom: 12px;
            }

            .stats-grid {
                grid-template-columns: repeat(2, minmax(0, 1fr));
                gap: 10px;
                margin-bottom: 22px;
            }
            .stat-card {
                min-width: 0;
                padding: 15px 13px;
                border-radius: 13px;
                gap: 8px;
            }
            .stat-info { min-width: 0; }
            .stat-info h3 {
                font-size: 21px;
                line-height: 1.15;
            }
            .stat-info p {
                font-size: 10.5px;
                line-height: 1.35;
                margin-top: 4px;
            }
            .stat-icon {
                width: 40px;
                height: 40px;
                min-width: 40px;
                border-radius: 10px;
                font-size: 16px;
            }

            .mobile-menu-toggle:focus-visible,
            .btn-logout:focus-visible,
            .sidebar-menu a:focus-visible {
                outline: 3px solid rgba(212, 163, 89, .55);
                outline-offset: 2px;
            }
        }

        @media (max-width: 380px) {
            .admin-topbar {
                height: 58px;
                padding-left: 55px;
            }
            .admin-content {
                padding: 11px 9px 24px;
            }
            .welcome-banner {
                padding: 16px;
                border-radius: 13px;
            }
            .welcome-text h1 { font-size: 17px; }
            .welcome-text p { font-size: 11px; }
            .welcome-badge { font-size: 10px; padding: 6px 10px; }

            .stats-grid { gap: 8px; }
            .stat-card { padding: 13px 10px; border-radius: 11px; }
            .stat-info h3 { font-size: 19px; }
            .stat-info p { font-size: 9.5px; }
            .stat-icon {
                width: 34px;
                height: 34px;
                min-width: 34px;
                font-size: 14px;
                border-radius: 9px;
            }
        }

        @media (prefers-reduced-motion: reduce) {
            html { scroll-behavior: auto; }
            *, *::before, *::after {
                animation-duration: .01ms !important;
                animation-iteration-count: 1 !important;
                transition-duration: .01ms !important;
                scroll-behavior: auto !important;
            }
        }

    </style>
</head>

<body>

    <button class="mobile-menu-toggle" type="button" aria-label="Open navigation menu" aria-expanded="false" aria-controls="adminSidebar">
        <i class="fas fa-bars"></i>
    </button>
    <div class="mobile-menu-overlay" aria-hidden="true"></div>

    <!-- SIDEBAR -->
    <aside class="admin-sidebar" id="adminSidebar">
        <div class="sidebar-brand">
            <h2>WINGS</h2>
            <p>Admin Panel</p>
        </div>

        <ul class="sidebar-menu">
            <li class="active">
                <a href="dashboard.php"><i class="fas fa-chart-line"></i> <span>Dashboard</span></a>
            </li>
            <li>
                <a href="add_category.php"><i class="fas fa-tags"></i> <span>Categories</span></a>
            </li>
            <li>
                <a href="manage-media.php"><i class="fas fa-images"></i> <span>Manage Media</span></a>
            </li>
            <li>
                <a href="enquiries.php"><i class="fas fa-envelope-open-text"></i> <span>Bookings / Enquiries</span></a>
            </li>
            <li>
                <a href="manage-packages.php"><i class="fas fa-route"></i> <span>Tour Packages</span></a>
            </li>
            <li>
                <a href="manage-hotels.php"><i class="fas fa-hotel"></i> <span>Hotels & Stay</span></a>
            </li>
            <li>
                <a href="settings.php"><i class="fas fa-sliders"></i> <span>Site Settings</span></a>
            </li>
        </ul>
    </aside>

    <!-- MAIN WRAPPER -->
    <div class="admin-main">

        <!-- TOPBAR -->
        <header class="admin-topbar">
            <div class="topbar-welcome">
                <i class="fas fa-house-chimney" style="color: var(--brand-green); margin-right: 6px;"></i> Dashboard
            </div>
            <div class="topbar-user">
                <div class="user-info">
                    <div class="name"><?php echo htmlspecialchars($_SESSION['admin_user'] ?? 'Admin'); ?></div>
                    <div class="role">Super Admin</div>
                </div>
                <a href="logout.php" class="btn-logout"><i class="fas fa-power-off"></i> Logout</a>
            </div>
        </header>

        <!-- CONTENT -->
        <main class="admin-content">
            <!-- Welcome Hero Banner -->
            <div class="welcome-banner">
                <div class="welcome-text">
                    <h1>Welcome back, <?php echo htmlspecialchars($_SESSION['admin_user'] ?? 'Admin'); ?>! 👋</h1>
                    <p>Yahan se aap apne saare customer bookings, tour packages aur hotels ko asani se manage kar sakte hain.</p>
                </div>
                <div class="welcome-badge">
                    <i class="fas fa-calendar-alt"></i> Season <?php echo date('Y'); ?>
                </div>
            </div>

            <h2 class="section-title">Quick Overview</h2>

            <!-- STATS CARDS -->
            <div class="stats-grid">
                <div class="stat-card">
                    <div class="stat-info">
                        <h3><?php echo $enq_count; ?></h3>
                        <p>Total Enquiries</p>
                    </div>
                    <div class="stat-icon"><i class="fas fa-paper-plane"></i></div>
                </div>

                <div class="stat-card">
                    <div class="stat-info">
                        <h3><?php echo $pkg_count; ?></h3>
                        <p>Total Packages</p>
                    </div>
                    <div class="stat-icon"><i class="fas fa-map-location-dot"></i></div>
                </div>

                <div class="stat-card">
                    <div class="stat-info">
                        <h3><?php echo $hotel_count; ?></h3>
                        <p>Hotels Listed</p>
                    </div>
                    <div class="stat-icon"><i class="fas fa-hotel"></i></div>
                </div>

                <div class="stat-card">
                    <div class="stat-info">
                        <h3><?php echo date('Y'); ?></h3>
                        <p>Active Year</p>
                    </div>
                    <div class="stat-icon"><i class="fas fa-calendar-check"></i></div>
                </div>
            </div>

        </main>
    </div>


    <script>
        // Mobile navigation UI only; existing PHP/database/navigation logic remains unchanged.
        (function () {
            const body = document.body;
            const toggle = document.querySelector('.mobile-menu-toggle');
            const overlay = document.querySelector('.mobile-menu-overlay');
            const sidebar = document.getElementById('adminSidebar');

            if (!toggle || !overlay || !sidebar) return;

            function setMenu(open) {
                body.classList.toggle('mobile-menu-open', open);
                toggle.setAttribute('aria-expanded', open ? 'true' : 'false');
                toggle.setAttribute('aria-label', open ? 'Close navigation menu' : 'Open navigation menu');
                const icon = toggle.querySelector('i');
                if (icon) icon.className = open ? 'fas fa-xmark' : 'fas fa-bars';
                overlay.setAttribute('aria-hidden', open ? 'false' : 'true');
            }

            toggle.addEventListener('click', function () {
                setMenu(!body.classList.contains('mobile-menu-open'));
            });

            overlay.addEventListener('click', function () {
                setMenu(false);
            });

            sidebar.querySelectorAll('a').forEach(function (link) {
                link.addEventListener('click', function () {
                    if (window.innerWidth <= 768) setMenu(false);
                });
            });

            window.addEventListener('resize', function () {
                if (window.innerWidth > 768) setMenu(false);
            });

            document.addEventListener('keydown', function (event) {
                if (event.key === 'Escape') setMenu(false);
            });
        })();
    </script>

</body>

</html>