<?php
session_start();
require_once __DIR__ . '/../db_config.php';

// Auth Check (Sample)
// if (!isset($_SESSION['admin_logged_in'])) { header('Location: login.php'); exit; }

// Active Tab Handle karne ke liye
$tab = $_GET['tab'] ?? 'dashboard';

// Simple Delete Handler Example
if (isset($_GET['action']) && $_GET['action'] == 'delete') {
    $type = $_GET['type'] ?? '';
    $id = intval($_GET['id'] ?? 0);

    if ($type == 'slider' && $id > 0) {
         $conn->query("DELETE FROM sliders WHERE id = $id");
    } elseif ($type == 'video' && $id > 0) {
         $conn->query("DELETE FROM videos WHERE id = $id");
    } elseif ($type == 'category' && $id > 0) {
         $conn->query("DELETE FROM categories WHERE id = $id");
    } elseif ($type == 'inquiry' && $id > 0) {
         $conn->query("DELETE FROM inquiries WHERE id = $id");
    }
    header("Location: index.php?tab=" . $tab);
    exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>All-in-One Master Admin Dashboard</title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700&display=swap" rel="stylesheet">
    <style>
        :root {
            --sidebar-bg: #0F172A;
            --sidebar-hover: #1E293B;
            --accent-gold: #D4A359;
            --bg-light: #F8FAFC;
            --text-dark: #1E293B;
            --sidebar-width: 260px;
        }
        * { box-sizing: border-box; margin: 0; padding: 0; font-family: 'Poppins', sans-serif; }
        body { display: flex; min-height: 100vh; background: var(--bg-light); color: var(--text-dark); }

        /* Left Sidebar Styles */
        .admin-sidebar {
            width: var(--sidebar-width);
            background: var(--sidebar-bg);
            color: #fff;
            display: flex;
            flex-direction: column;
            position: fixed;
            top: 0;
            bottom: 0;
            left: 0;
            z-index: 1000;
            box-shadow: 4px 0 20px rgba(0, 0, 0, 0.15);
            transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        }
        .sidebar-header {
            padding: 24px 20px;
            font-size: 18px;
            font-weight: 700;
            color: var(--accent-gold);
            border-bottom: 1px solid rgba(255,255,255,0.08);
            background: rgba(0, 0, 0, 0.2);
            letter-spacing: 0.5px;
        }
        .sidebar-menu { list-style: none; margin-top: 15px; flex: 1; overflow-y: auto; }
        .sidebar-menu li a {
            display: flex;
            align-items: center;
            gap: 14px;
            padding: 13px 22px;
            color: #94A3B8;
            text-decoration: none;
            font-size: 14px;
            font-weight: 500;
            transition: all 0.25s ease;
            border-left: 4px solid transparent;
        }
        .sidebar-menu li a:hover,
        .sidebar-menu li a.active {
            background: var(--sidebar-hover);
            color: #ffffff;
            border-left-color: var(--accent-gold);
        }
        .sidebar-menu li a i {
            color: var(--accent-gold);
            width: 20px;
            text-align: center;
        }

        /* Right Content Container Layout */
        .admin-main {
            margin-left: var(--sidebar-width);
            flex: 1;
            display: flex;
            flex-direction: column;
            min-width: 0;
            transition: margin 0.3s ease;
        }

        /* Topbar Header */
        .admin-topbar {
            background: #ffffff;
            height: 70px;
            padding: 0 30px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            box-shadow: 0 2px 10px rgba(0,0,0,0.04);
            position: sticky;
            top: 0;
            z-index: 99;
        }
        .topbar-left {
            display: flex;
            align-items: center;
            gap: 15px;
        }
        .mobile-nav-toggle {
            display: none;
            background: var(--sidebar-bg);
            color: #fff;
            border: none;
            width: 40px;
            height: 40px;
            border-radius: 8px;
            cursor: pointer;
            font-size: 16px;
            align-items: center;
            justify-content: center;
            transition: background 0.2s;
        }
        .mobile-nav-toggle:hover {
            background: var(--accent-gold);
            color: var(--sidebar-bg);
        }

        /* Mobile Backdrop Overlay */
        .sidebar-backdrop {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(15, 23, 42, 0.6);
            z-index: 99;
            backdrop-filter: blur(2px);
            opacity: 0;
            transition: opacity 0.3s ease;
        }

        .admin-content { flex: 1; padding: 30px; overflow-y: auto; }
        .content-header { margin-bottom: 25px; display: flex; justify-content: space-between; align-items: center; }
        .content-header h1 { font-size: 22px; color: var(--sidebar-bg); font-weight: 700; }

        /* General Card & Table UI */
        .admin-card { background: #fff; border-radius: 14px; padding: 24px; box-shadow: 0 4px 20px rgba(0,0,0,0.03); border: 1px solid #E2E8F0; margin-bottom: 25px; }
        .table-container { width: 100%; overflow-x: auto; }
        .admin-table { width: 100%; border-collapse: collapse; margin-top: 15px; white-space: nowrap; font-size: 13.5px; }
        .admin-table th, .admin-table td { padding: 14px 18px; text-align: left; border-bottom: 1px solid #E2E8F0; color: #334155; }
        .admin-table th { background: #F8FAFC; color: var(--sidebar-bg); font-weight: 600; }
        
        /* Action Buttons */
        .btn-del { color: #EF4444; background: #FEF2F2; padding: 6px 12px; border-radius: 6px; cursor: pointer; text-decoration: none; font-weight: 600; font-size: 12px; display: inline-flex; align-items: center; gap: 5px; transition: background 0.2s; }
        .btn-del:hover { background: #FEE2E2; }

        .btn-add { background: #12521f; color: #fff; border: none; padding: 11px 22px; border-radius: 8px; cursor: pointer; font-weight: 600; text-decoration: none; display: inline-block; transition: opacity 0.2s; }
        .btn-add:hover { opacity: 0.9; }

        .form-group { margin-bottom: 18px; }
        .form-group label { display: block; margin-bottom: 6px; font-size: 13px; font-weight: 600; color: var(--sidebar-bg); }
        .form-group input, .form-group select { width: 100%; padding: 11px 14px; border: 1px solid #CBD5E1; border-radius: 8px; font-size: 13.5px; outline: none; transition: border-color 0.2s; }
        .form-group input:focus, .form-group select:focus { border-color: var(--accent-gold); }

        @media (max-width: 992px) {
            .admin-sidebar { transform: translateX(-100%); }
            .admin-sidebar.sidebar-open { transform: translateX(0); }
            .admin-main { margin-left: 0; }
            .mobile-nav-toggle { display: flex; }
            .sidebar-backdrop.show-backdrop { display: block; opacity: 1; }
            .admin-content { padding: 20px 15px; }
            .admin-topbar { padding: 0 15px; }
        }

        /* ===== Premium Responsive UI Enhancements ===== */
        html, body { overflow-x: hidden; }
        body { -webkit-tap-highlight-color: transparent; }
        button, a, input, select { -webkit-tap-highlight-color: transparent; }
        .admin-table img { max-width: 100%; height: auto; }

        @media (max-width: 992px) {
            .admin-sidebar { transform: translateX(-100%); }
            .admin-sidebar.sidebar-open { transform: translateX(0); }
            .admin-main { margin-left: 0; width: 100%; }
            .mobile-nav-toggle { display: flex; min-width: 44px; min-height: 44px; }
            .admin-topbar { height: 62px; padding: 0 14px; }
            .admin-content { padding: 20px 15px; }
            .content-header { gap: 12px; flex-wrap: wrap; margin-bottom: 18px; }
            .content-header h1 { font-size: 20px; line-height: 1.3; }
            .admin-card { padding: 18px; border-radius: 13px; margin-bottom: 18px; }
            .form-group input, .form-group select { min-height: 46px; padding: 11px 13px; font-size: 14px; }
            .btn-add, .btn-del { min-height: 44px; }
            .btn-add { padding: 11px 18px; }
            .btn-del { padding: 9px 12px; }
            .admin-table th, .admin-table td { padding: 12px 14px; }
        }

        @media (max-width: 767px) {
            .admin-topbar strong { font-size: 14px !important; }
            .admin-topbar > a { font-size: 0 !important; width: 42px; height: 42px; justify-content: center; border-radius: 10px; background: #FEF2F2; }
            .admin-topbar > a i { font-size: 16px; margin: 0 !important; }
            .admin-content { padding: 16px 12px 24px; }
            .content-header { margin-bottom: 14px; }
            .content-header h1 { font-size: 18px; }
            .admin-card { padding: 15px; border-radius: 12px; }
            .admin-card h3 { font-size: 15px !important; }
            .table-container {
                overflow-x: auto;
                -webkit-overflow-scrolling: touch;
                border-radius: 10px;
                scrollbar-width: thin;
            }
            .admin-table { min-width: 650px; font-size: 12.5px; margin-top: 8px; }
            .admin-table th, .admin-table td { padding: 11px 12px; }
            .admin-table th { font-size: 12px; }
            .admin-table td { white-space: nowrap; }
            .admin-table img { width: 60px !important; height: 40px !important; }
            .btn-del { font-size: 11px; min-height: 40px; }
            .form-group { margin-bottom: 14px; }
            .form-group label { font-size: 12.5px; margin-bottom: 6px; }
            .btn-add { width: 100%; text-align: center; min-height: 46px; }
        }

        @media (max-width: 380px) {
            .admin-topbar { padding: 0 10px; }
            .mobile-nav-toggle { min-width: 42px; width: 42px; height: 42px; }
            .admin-content { padding: 13px 9px 20px; }
            .admin-card { padding: 13px; }
            .content-header h1 { font-size: 17px; }
            .admin-table { min-width: 610px; }
            .admin-table th, .admin-table td { padding: 10px; }
        }

        @media (prefers-reduced-motion: reduce) {
            *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; }
        }
    </style>
</head>
<body>

    <!-- Mobile Backdrop Overlay -->
    <div class="sidebar-backdrop" id="sidebarBackdrop" onclick="toggleSidebar()"></div>

    <!-- LEFT SIDEBAR -->
    <aside class="admin-sidebar" id="adminSidebar">
        <div class="sidebar-header">
            <i class="fa-solid fa-gauge"></i> Admin Panel
        </div>
        <ul class="sidebar-menu">
            <li><a href="?tab=dashboard" class="<?= $tab == 'dashboard' ? 'active' : '' ?>"><i class="fa-solid fa-chart-line"></i> Dashboard</a></li>
            <li><a href="?tab=orders" class="<?= $tab == 'orders' ? 'active' : '' ?>"><i class="fa-solid fa-cart-shopping"></i> Booking Orders</a></li>
            <li><a href="?tab=categories" class="<?= $tab == 'categories' ? 'active' : '' ?>"><i class="fa-solid fa-list"></i> Categories</a></li>
            <li><a href="?tab=sliders" class="<?= $tab == 'sliders' ? 'active' : '' ?>"><i class="fa-solid fa-images"></i> Slider Images</a></li>
            <li><a href="?tab=videos" class="<?= $tab == 'videos' ? 'active' : '' ?>"><i class="fa-solid fa-video"></i> Video Gallery</a></li>
        </ul>
    </aside>

    <!-- RIGHT CONTENT AREA -->
    <div class="admin-main">
        <header class="admin-topbar">
            <div class="topbar-left">
                <button class="mobile-nav-toggle" onclick="toggleSidebar()" aria-label="Toggle Navigation">
                    <i class="fas fa-bars"></i>
                </button>
                <strong style="color: var(--sidebar-bg); font-size: 16px;">Control Center</strong>
            </div>
            <a href="login.php?action=logout" style="color: #EF4444; text-decoration: none; font-weight: 600; font-size: 13px; display: flex; align-items: center; gap: 6px;">
                <i class="fas fa-sign-out-alt"></i> Logout
            </a>
        </header>

        <main class="admin-content">

            <?php if ($tab == 'dashboard'): ?>
                <div class="content-header"><h1>Dashboard Overview</h1></div>
                <div class="admin-card">
                    <h3 style="color: var(--sidebar-bg); margin-bottom: 8px;">Welcome to Control Panel!</h3>
                    <p style="color: #64748B; font-size: 14px;">Left menu se aap sabhi dynamic modules aur bookings easily manage kar sakte hain[cite: 16].</p>
                </div>

            <?php elseif ($tab == 'orders'): ?>
                <div class="content-header"><h1>Booking Orders / Inquiries</h1></div>
                <div class="admin-card">
                    <div class="table-container">
                        <table class="admin-table">
                            <thead>
                                <tr>
                                    <th>ID</th><th>Name</th><th>Email</th><th>Phone</th><th>Date</th><th>Passanger</th><th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $res = $conn->query("SELECT * FROM inquiries ORDER BY id DESC");
                                while($row = $res->fetch_assoc()):
                                ?>
                                <tr>
                                    <td><strong>#<?= $row['id'] ?></strong></td>
                                    <td><?= htmlspecialchars($row['name']) ?></td>
                                    <td><?= htmlspecialchars($row['email']) ?></td>
                                    <td><?= htmlspecialchars($row['phone']) ?></td>
                                    <td><?= htmlspecialchars($row['travel_date']) ?></td>
                                    <td><?= htmlspecialchars($row['passengers']) ?></td>
                                    <td><a href="?tab=orders&action=delete&type=inquiry&id=<?= $row['id'] ?>" class="btn-del" onclick="return confirm('Delete this record?')"><i class="fas fa-trash"></i> Delete</a></td>
                                </tr>
                                <?php endwhile; ?>
                            </tbody>
                        </table>
                    </div>
                </div>

            <?php elseif ($tab == 'categories'): ?>
                <div class="content-header"><h1>Manage Categories</h1></div>
                <!-- Add Category Form -->
                <div class="admin-card">
                    <h3 style="margin-bottom: 15px; font-size: 16px; color: var(--sidebar-bg);">Add New Category</h3>
                    <form action="add_category.php" method="POST">
                        <div class="form-group">
                            <label>Category Name</label>
                            <input type="text" name="category_name" required placeholder="e.g. Helicopter Packages">
                        </div>
                        <button type="submit" class="btn-add">Save Category</button>
                    </form>
                </div>
                <!-- Category List -->
                <div class="admin-card">
                    <h3 style="margin-bottom: 15px; font-size: 16px; color: var(--sidebar-bg);">Existing Categories</h3>
                    <div class="table-container">
                        <table class="admin-table">
                            <thead><tr><th>ID</th><th>Category Name</th><th>Action</th></tr></thead>
                            <tbody>
                                <?php
                                $res = $conn->query("SELECT * FROM categories ORDER BY id DESC");
                                while($row = $res->fetch_assoc()):
                                ?>
                                <tr>
                                    <td><strong>#<?= $row['id'] ?></strong></td>
                                    <td><?= htmlspecialchars($row['name']) ?></td>
                                    <td><a href="?tab=categories&action=delete&type=category&id=<?= $row['id'] ?>" class="btn-del" onclick="return confirm('Delete?')"><i class="fas fa-trash"></i> Delete</a></td>
                                </tr>
                                <?php endwhile; ?>
                            </tbody>
                        </table>
                    </div>
                </div>

            <?php elseif ($tab == 'sliders'): ?>
                <div class="content-header"><h1>Manage Sliders</h1></div>
                <!-- Add Slider Form -->
                <div class="admin-card">
                    <h3 style="margin-bottom: 15px; font-size: 16px; color: var(--sidebar-bg);">Upload New Slider Image</h3>
                    <form action="upload_slider.php" method="POST" enctype="multipart/form-data">
                        <div class="form-group">
                            <label>Slider Title</label>
                            <input type="text" name="title" placeholder="Slider Caption">
                        </div>
                        <div class="form-group">
                            <label>Select Image</label>
                            <input type="file" name="slider_image" required accept="image/*">
                        </div>
                        <button type="submit" class="btn-add">Upload Image</button>
                    </form>
                </div>
                <!-- Slider List -->
                <div class="admin-card">
                    <h3 style="margin-bottom: 15px; font-size: 16px; color: var(--sidebar-bg);">Slider Images List</h3>
                    <div class="table-container">
                        <table class="admin-table">
                            <thead><tr><th>ID</th><th>Preview</th><th>Title</th><th>Action</th></tr></thead>
                            <tbody>
                                <?php
                                $res = $conn->query("SELECT * FROM sliders ORDER BY id DESC");
                                while($row = $res->fetch_assoc()):
                                ?>
                                <tr>
                                    <td><strong>#<?= $row['id'] ?></strong></td>
                                    <td><img src="../<?= $row['image_url'] ?>" width="70" height="45" style="border-radius:6px; object-fit: cover; border: 1px solid #E2E8F0;"></td>
                                    <td><?= htmlspecialchars($row['title']) ?></td>
                                    <td><a href="?tab=sliders&action=delete&type=slider&id=<?= $row['id'] ?>" class="btn-del" onclick="return confirm('Delete?')"><i class="fas fa-trash"></i> Delete</a></td>
                                </tr>
                                <?php endwhile; ?>
                            </tbody>
                        </table>
                    </div>
                </div>

            <?php elseif ($tab == 'videos'): ?>
                <div class="content-header"><h1>Manage Video Gallery</h1></div>
                <!-- Add Video Form -->
                <div class="admin-card">
                    <h3 style="margin-bottom: 15px; font-size: 16px; color: var(--sidebar-bg);">Add Video (YouTube Embed/URL)</h3>
                    <form action="add_video.php" method="POST">
                        <div class="form-group">
                            <label>Video Title</label>
                            <input type="text" name="title" required placeholder="Tour Promo Video">
                        </div>
                        <div class="form-group">
                            <label>Video Link / Embed URL</label>
                            <input type="text" name="video_url" required placeholder="https://www.youtube.com/embed/xyz">
                        </div>
                        <button type="submit" class="btn-add">Save Video</button>
                    </form>
                </div>
            <?php endif; ?>

        </main>
    </div>

    <!-- JavaScript for Mobile Sidebar Toggle -->
    <script>
        function toggleSidebar() {
            const sidebar = document.getElementById('adminSidebar');
            const backdrop = document.getElementById('sidebarBackdrop');
            if (sidebar) {
                sidebar.classList.toggle('sidebar-open');
            }
            if (backdrop) {
                backdrop.classList.toggle('show-backdrop');
            }
        }
    </script>
</body>
</html>