<?php
require_once 'auth_check.php';
require_once 'db.php';

/** @var mysqli $conn Connection initialized by db.php. */
$conn = $GLOBALS['conn'] ?? $conn ?? null;

$message = ''; 
$error = '';

// 1. Create Main Menus Table
if ($conn) {
    mysqli_query($conn, "CREATE TABLE IF NOT EXISTS `main_menus` (
        `id` INT PRIMARY KEY AUTO_INCREMENT,
        `menu_name` VARCHAR(100) NOT NULL,
        `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    )");

    // 2. Create Dropdown Options Table (Price & Description removed)
    mysqli_query($conn, "CREATE TABLE IF NOT EXISTS `dropdown_options` (
        `id` INT PRIMARY KEY AUTO_INCREMENT,
        `main_menu_id` INT NOT NULL,
        `option_name` VARCHAR(255) NOT NULL,
        `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    )");

    // 3. Create Slider Media Table (For Slider Images & Videos)
    mysqli_query($conn, "CREATE TABLE IF NOT EXISTS `dropdown_media` (
        `id` INT PRIMARY KEY AUTO_INCREMENT,
        `dropdown_option_id` INT NOT NULL,
        `media_type` VARCHAR(20) NOT NULL, -- 'image' or 'video'
        `file_path` VARCHAR(255) NOT NULL,
        `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    )");

    // 4. Create Destinations Table (Linked with Dropdown Option ID)
    mysqli_query($conn, "CREATE TABLE IF NOT EXISTS `destinations` (
        `id` INT PRIMARY KEY AUTO_INCREMENT,
        `dropdown_option_id` INT NOT NULL,
        `title` VARCHAR(150) NOT NULL,
        `image` VARCHAR(255) NOT NULL,
        `rating` INT DEFAULT 5,
        `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    )");
}

// --- HANDLE POST REQUESTS ---
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    
    // A. Add Main Header Menu
    if (isset($_POST['add_main_menu'])) {
        $menu_name = trim($_POST['menu_name'] ?? '');
        if (!empty($menu_name)) {
            $stmt = mysqli_prepare($conn, "INSERT INTO `main_menus` (`menu_name`) VALUES (?)");
            if ($stmt) {
                mysqli_stmt_bind_param($stmt, "s", $menu_name);
                if (mysqli_stmt_execute($stmt)) {
                    $message = "Main Header Menu successfully add ho gaya!";
                } else {
                    $error = "Error: " . mysqli_error($conn);
                }
                mysqli_stmt_close($stmt);
            }
        } else {
            $error = "Main Header Menu ka naam khaali nahi ho sakta.";
        }
    }

    // B. Add or Update Dropdown Option
    if (isset($_POST['save_dropdown_option'])) {
        $edit_id = intval($_POST['edit_id'] ?? 0);
        $main_menu_id = intval($_POST['main_menu_id'] ?? 0);
        $option_name = trim($_POST['option_name'] ?? '');

        if ($main_menu_id > 0 && !empty($option_name)) {
            if ($edit_id > 0) {
                $stmt = mysqli_prepare($conn, "UPDATE `dropdown_options` SET `main_menu_id` = ?, `option_name` = ? WHERE id = ?");
                if ($stmt) {
                    mysqli_stmt_bind_param($stmt, "isi", $main_menu_id, $option_name, $edit_id);
                    $success_msg = "Dropdown option successfully update ho gaya!";
                }
            } else {
                $stmt = mysqli_prepare($conn, "INSERT INTO `dropdown_options` (`main_menu_id`, `option_name`) VALUES (?, ?)");
                if ($stmt) {
                    mysqli_stmt_bind_param($stmt, "is", $main_menu_id, $option_name);
                    $success_msg = "Dropdown option successfully save ho gaya!";
                }
            }

            if (isset($stmt) && mysqli_stmt_execute($stmt)) {
                $message = $success_msg;
            } else {
                $error = "Error: " . mysqli_error($conn);
            }
            if ($stmt) {
                mysqli_stmt_close($stmt);
            }
        } else {
            $error = "Kripya Main Menu aur Option Name dono fill karein.";
        }
    }

    // C. Add Slider Media (Image or Video for Slider)
    if (isset($_POST['add_slider_media'])) {
        $dropdown_option_id = intval($_POST['dropdown_option_id'] ?? 0);
        $media_type = trim($_POST['media_type'] ?? 'image'); // 'image' or 'video'

        if ($dropdown_option_id > 0 && isset($_FILES['media_file']) && $_FILES['media_file']['error'] === UPLOAD_ERR_OK) {
            $upload_dir = '../uploads/slider_media/';
            if (!is_dir($upload_dir)) {
                mkdir($upload_dir, 0777, true);
            }

            $ext = strtolower(pathinfo($_FILES['media_file']['name'], PATHINFO_EXTENSION));
            
            $allowed_images = ['jpg', 'jpeg', 'png', 'webp', 'gif'];
            $allowed_videos = ['mp4', 'webm', 'ogg', 'mov'];

            $is_valid = false;
            if ($media_type === 'image' && in_array($ext, $allowed_images)) {
                $is_valid = true;
            } elseif ($media_type === 'video' && in_array($ext, $allowed_videos)) {
                $is_valid = true;
            }

            if ($is_valid) {
                $file_name = 'slide_' . time() . '_' . rand(1000, 9999) . '.' . $ext;
                move_uploaded_file($_FILES['media_file']['tmp_name'], $upload_dir . $file_name);
                
                $db_file_path = 'uploads/slider_media/' . $file_name;
                
                $stmt = mysqli_prepare($conn, "INSERT INTO `dropdown_media` (`dropdown_option_id`, `media_type`, `file_path`) VALUES (?, ?, ?)");
                if ($stmt) {
                    mysqli_stmt_bind_param($stmt, "iss", $dropdown_option_id, $media_type, $db_file_path);
                    if (mysqli_stmt_execute($stmt)) {
                        $message = "Slider media successfully add ho gaya!";
                    } else {
                        $error = "Error: " . mysqli_error($conn);
                    }
                    mysqli_stmt_close($stmt);
                }
            } else {
                $error = "Kripya select kiye gaye media type ke anusar valid file format upload karein.";
            }
        } else {
            $error = "Dropdown option aur media file dono zaroori hain.";
        }
    }

    // D. Add Destination Image (Mapped to Dropdown Option)
    if (isset($_POST['add_destination'])) {
        $dropdown_option_id = intval($_POST['dropdown_option_id'] ?? 0);
        $title = trim($_POST['dest_title'] ?? '');
        $rating = intval($_POST['dest_rating'] ?? 5);
        $dest_image = '';

        if ($dropdown_option_id > 0 && !empty($title) && isset($_FILES['dest_image']) && $_FILES['dest_image']['error'] === UPLOAD_ERR_OK) {
            $upload_dir = '../uploads/destinations/';
            if (!is_dir($upload_dir)) {
                mkdir($upload_dir, 0777, true);
            }

            $ext = strtolower(pathinfo($_FILES['dest_image']['name'], PATHINFO_EXTENSION));
            $allowed = ['jpg', 'jpeg', 'png', 'webp', 'gif'];

            if (in_array($ext, $allowed)) {
                $dest_image = 'dest_' . time() . '_' . rand(1000, 9999) . '.' . $ext;
                move_uploaded_file($_FILES['dest_image']['tmp_name'], $upload_dir . $dest_image);
                
                $db_image_path = 'uploads/destinations/' . $dest_image;
                
                $stmt = mysqli_prepare($conn, "INSERT INTO `destinations` (`dropdown_option_id`, `title`, `image`, `rating`) VALUES (?, ?, ?, ?)");
                if ($stmt) {
                    mysqli_stmt_bind_param($stmt, "issi", $dropdown_option_id, $title, $db_image_path, $rating);
                    if (mysqli_stmt_execute($stmt)) {
                        $message = "Destination image successfully add ho gayi!";
                    } else {
                        $error = "Error: " . mysqli_error($conn);
                    }
                    mysqli_stmt_close($stmt);
                }
            } else {
                $error = "Kripya destination ke liye valid image format upload karein.";
            }
        } else {
            $error = "Dropdown option, destination ka naam aur image sabhi zaroori hain.";
        }
    }
}

// --- DELETE LOGIC ---
if (isset($_GET['del_menu'])) {
    $m_id = intval($_GET['del_menu']);
    mysqli_query($conn, "DELETE FROM `main_menus` WHERE id = $m_id");
    header("Location: add_category.php?msg=deleted");
    exit();
}

if (isset($_GET['del_option'])) {
    $o_id = intval($_GET['del_option']);
    mysqli_query($conn, "DELETE FROM `dropdown_options` WHERE id = $o_id");
    header("Location: add_category.php?msg=deleted");
    exit();
}

if (isset($_GET['del_media'])) {
    $media_id = intval($_GET['del_media']);
    $get_media = mysqli_query($conn, "SELECT file_path FROM `dropdown_media` WHERE id = $media_id");
    if ($media_row = mysqli_fetch_assoc($get_media)) {
        if (!empty($media_row['file_path']) && file_exists('../' . $media_row['file_path'])) {
            unlink('../' . $media_row['file_path']);
        }
    }
    mysqli_query($conn, "DELETE FROM `dropdown_media` WHERE id = $media_id");
    header("Location: add_category.php?msg=deleted");
    exit();
}

if (isset($_GET['del_dest'])) {
    $d_id = intval($_GET['del_dest']);
    $get_img = mysqli_query($conn, "SELECT image FROM `destinations` WHERE id = $d_id");
    if ($img_row = mysqli_fetch_assoc($get_img)) {
        if (!empty($img_row['image']) && file_exists('../' . $img_row['image'])) {
            unlink('../' . $img_row['image']);
        }
    }
    mysqli_query($conn, "DELETE FROM `destinations` WHERE id = $d_id");
    header("Location: add_category.php?msg=deleted");
    exit();
}

// Fetch edit data if edit_id is passed
$edit_data = null;
if (isset($_GET['edit_option'])) {
    $edit_id = intval($_GET['edit_option']);
    $edit_res = mysqli_query($conn, "SELECT * FROM `dropdown_options` WHERE id = $edit_id");
    if ($edit_res) {
        $edit_data = mysqli_fetch_assoc($edit_res);
    }
}

// Fetch Data for Display
$main_menus_res = mysqli_query($conn, "SELECT * FROM `main_menus` ORDER BY id DESC");
$options_res = mysqli_query($conn, "SELECT o.*, m.menu_name FROM `dropdown_options` o JOIN `main_menus` m ON o.main_menu_id = m.id ORDER BY o.id DESC");
$media_res = mysqli_query($conn, "SELECT sm.*, o.option_name, m.menu_name FROM `dropdown_media` sm JOIN `dropdown_options` o ON sm.dropdown_option_id = o.id JOIN `main_menus` m ON o.main_menu_id = m.id ORDER BY sm.id DESC");
$destinations_res = mysqli_query($conn, "SELECT d.*, o.option_name, m.menu_name FROM `destinations` d JOIN `dropdown_options` o ON d.dropdown_option_id = o.id JOIN `main_menus` m ON o.main_menu_id = m.id ORDER BY d.id DESC");
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Menu & Slider Manager | Wings Admin</title>
    
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css">
    <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;
        }

        .admin-sidebar {
            width: var(--sidebar-width) !important;
            background-color: var(--dark-navy) !important;
            color: #ffffff !important;
            position: fixed !important;
            top: 0 !important;
            bottom: 0 !important;
            left: 0 !important;
            display: flex !important;
            flex-direction: column !important;
            z-index: 1000 !important;
            box-shadow: 4px 0 15px rgba(0, 0, 0, 0.1) !important;
        }

        .admin-main {
            margin-left: var(--sidebar-width);
            flex: 1;
            display: flex;
            flex-direction: column;
            width: calc(100% - var(--sidebar-width));
        }

        .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-left {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .mobile-toggle-btn {
            display: none;
            background: none;
            border: none;
            font-size: 20px;
            color: var(--dark-navy);
            cursor: pointer;
        }

        .topbar-title {
            font-size: 15px;
            color: var(--dark-navy);
            font-weight: 600;
        }

        .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;
            border: 1px solid #FCA5A5;
        }

        .admin-content {
            padding: 35px;
        }

        .page-title {
            font-size: 22px;
            color: var(--dark-navy);
            margin-bottom: 25px;
            font-weight: 700;
        }
        
        .boxes-grid { 
            display: grid; 
            grid-template-columns: 1fr 1fr; 
            gap: 25px; 
            margin-bottom: 30px; 
            align-items: start; 
        }

        .card { 
            background: #ffffff; 
            padding: 24px; 
            border-radius: 14px; 
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.03); 
            border: 1px solid #E2E8F0; 
        }

        .card-full { 
            background: #ffffff; 
            padding: 24px; 
            border-radius: 14px; 
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.03); 
            border: 1px solid #E2E8F0; 
            margin-bottom: 30px; 
        }
        
        .card-title { 
            font-size: 16px; 
            font-weight: 600; 
            color: var(--dark-navy); 
            margin-bottom: 18px; 
            display: flex; 
            align-items: center; 
            gap: 8px; 
            border-bottom: 2px solid #F1F5F9; 
            padding-bottom: 12px; 
        }
        
        .form-group { 
            margin-bottom: 18px; 
        }

        .form-group label { 
            display: block; 
            font-size: 13px; 
            font-weight: 600; 
            color: var(--dark-navy); 
            margin-bottom: 8px; 
        }

        .form-group input, 
        .form-group select, 
        .form-group textarea { 
            width: 100%; 
            padding: 12px 14px; 
            border: 1.5px solid #CBD5E1; 
            border-radius: 10px; 
            font-size: 14px; 
            outline: none; 
            background: #fff; 
        }
        
        .btn-submit { 
            background: var(--brand-green); 
            color: #fff; 
            padding: 12px 20px; 
            border: none; 
            border-radius: 10px; 
            font-weight: 600; 
            cursor: pointer; 
            font-size: 14px; 
            display: inline-flex; 
            align-items: center; 
            gap: 8px;
        }

        .btn-submit:hover {
            background: #0d3d17;
        }
        
        .custom-table-container {
            background: #ffffff;
            border-radius: 14px;
            border: 1px solid #E2E8F0;
            overflow-x: auto;
        }

        .custom-table { 
            width: 100%; 
            border-collapse: collapse; 
            font-size: 13.5px; 
            white-space: nowrap;
        }

        .custom-table th, 
        .custom-table td { 
            padding: 14px 18px; 
            text-align: left; 
            border-bottom: 1px solid #E2E8F0; 
            vertical-align: middle; 
            color: #334155;
        }

        .custom-table th { 
            background: #F1F5F9; 
            color: var(--dark-navy); 
            font-weight: 600; 
        }
        
        .thumb { 
            width: 45px; 
            height: 45px; 
            border-radius: 8px; 
            object-fit: cover; 
            border: 1px solid #E2E8F0; 
        }

        .no-thumb { 
            width: 45px; 
            height: 45px; 
            border-radius: 8px; 
            background: #F1F5F9; 
            display: flex; 
            align-items: center; 
            justify-content: center; 
            color: #94A3B8; 
        }

        .btn-action { 
            padding: 6px 12px; 
            border-radius: 6px; 
            text-decoration: none; 
            font-size: 12px; 
            font-weight: 600; 
            display: inline-flex;
            align-items: center;
            gap: 5px;
            margin-right: 5px; 
        }

        .btn-edit { 
            color: #2563EB; 
            background: #EFF6FF; 
            border: 1px solid #BFDBFE;
        }

        .btn-del { 
            color: #EF4444; 
            background: #FEF2F2; 
            border: 1px solid #FCA5A5;
        }
        
        .alert-success { 
            padding: 12px 16px; 
            background: #DCFCE7; 
            color: #166534; 
            border-radius: 8px; 
            font-size: 13px; 
            margin-bottom: 20px; 
            border: 1px solid #BBF7D0;
        }
        .alert-error { 
            padding: 12px 16px; 
            background: #FEF2F2; 
            color: #991B1B; 
            border-radius: 8px; 
            font-size: 13px; 
            margin-bottom: 20px; 
            border: 1px solid #FCA5A5;
        }

        @media (max-width: 992px) { 
            .boxes-grid { grid-template-columns: 1fr; } 
        }

        /* ===== Premium Responsive Override ===== */
        html, body { overflow-x: hidden; }
        img, video { max-width: 100%; height: auto; }
        .admin-main { min-width: 0; }
        .card, .card-full { min-width: 0; }
        .form-group input, .form-group select, .form-group textarea,
        .btn-submit, .btn-action { min-height: 44px; }
        .custom-table-container { -webkit-overflow-scrolling: touch; scrollbar-width: thin; }

        @media (max-width: 992px) {
            :root { --sidebar-width: 240px; }
            .admin-sidebar { transform: translateX(-100%); transition: transform .25s ease; }
            .admin-sidebar.sidebar-open { transform: translateX(0); }
            .admin-main { margin-left: 0; width: 100%; }
            .mobile-toggle-btn { display: inline-flex; width: 44px; height: 44px; align-items:center; justify-content:center; border-radius:10px; }
            .mobile-toggle-btn:active { background:#F1F5F9; }
            .admin-topbar { padding: 0 20px; }
            .admin-content { padding: 24px; }
            .boxes-grid { grid-template-columns: 1fr; }
            .card-full form > div[style*="grid-template-columns"] {
                grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
            }
        }

        @media (max-width: 767px) {
            .admin-topbar { height: 62px; padding: 0 12px; }
            .topbar-left { gap: 8px; min-width: 0; }
            .topbar-title {
                font-size: 12px;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
            }
            .topbar-title i { margin-right: 3px !important; }
            .btn-logout {
                min-height: 40px;
                padding: 8px 11px;
                font-size: 0;
                border-radius: 10px;
                flex: 0 0 auto;
            }
            .btn-logout i { font-size: 14px; }
            .admin-content { padding: 14px 12px 24px; }
            .page-title { font-size: 18px; line-height: 1.35; margin-bottom: 16px; }

            .card, .card-full {
                padding: 15px;
                border-radius: 12px;
                margin-bottom: 16px;
            }
            .card-title {
                font-size: 14px;
                line-height: 1.4;
                margin-bottom: 14px;
                padding-bottom: 10px;
            }
            .form-group { margin-bottom: 14px; }
            .form-group label { font-size: 12px; margin-bottom: 6px; }
            .form-group input, .form-group select, .form-group textarea {
                padding: 11px 12px;
                font-size: 13px;
                border-radius: 9px;
                min-height: 44px;
            }
            .btn-submit {
                width: 100%;
                justify-content: center;
                padding: 11px 14px;
                font-size: 13px;
                min-height: 44px;
            }

            /* Inline multi-column upload forms become single column */
            .card-full form > div[style*="grid-template-columns"] {
                display: grid !important;
                grid-template-columns: 1fr !important;
                gap: 0 !important;
                align-items: stretch !important;
            }
            .card-full form > div[style*="grid-template-columns"] > div {
                width: 100%;
            }
            .card-full form > div[style*="grid-template-columns"] .btn-submit {
                margin-top: 2px;
            }

            /* Existing menu list */
            .card ul { padding-left: 0 !important; list-style: none; }
            .card li {
                gap: 8px;
                padding: 8px 0;
                border-bottom: 1px solid #F1F5F9;
            }
            .card li:last-child { border-bottom: 0; }
            .card li span { min-width: 0; overflow-wrap: anywhere; }
            .btn-action {
                min-height: 40px;
                padding: 8px 10px;
                font-size: 11px;
                margin-right: 0;
                white-space: nowrap;
            }

            .custom-table-container {
                border-radius: 10px;
                overflow-x: auto;
            }
            .custom-table {
                min-width: 580px;
                font-size: 12px;
            }
            .custom-table th, .custom-table td {
                padding: 10px 12px;
            }
            .thumb, .no-thumb { width: 42px; height: 42px; }

            .alert-success, .alert-error {
                padding: 10px 12px;
                font-size: 12px;
                line-height: 1.45;
                margin-bottom: 14px;
            }
        }

        @media (max-width: 380px) {
            .admin-topbar { padding: 0 9px; }
            .topbar-title { font-size: 11px; }
            .mobile-toggle-btn { width: 40px; height: 40px; }
            .btn-logout { min-width: 40px; padding: 7px; }
            .admin-content { padding: 12px 9px 20px; }
            .page-title { font-size: 16px; }
            .card, .card-full { padding: 12px; }
            .card-title { font-size: 13px; }
            .form-group input, .form-group select { font-size: 12px; }
        }

        @media (prefers-reduced-motion: reduce) {
            *, *::before, *::after {
                scroll-behavior: auto !important;
                transition-duration: .01ms !important;
                animation-duration: .01ms !important;
                animation-iteration-count: 1 !important;
            }
        }

    </style>
</head>
<body>

    <?php include 'sidebar.php'; ?>

    <div class="admin-main">
        <header class="admin-topbar">
            <div class="topbar-left">
                <button class="mobile-toggle-btn" id="sidebarToggle" onclick="toggleSidebar()">
                    <i class="fas fa-bars"></i>
                </button>
                <div class="topbar-title">
                    <i class="fas fa-tags" style="color: var(--brand-green); margin-right: 6px;"></i> Website Menu & Slider Controller
                </div>
            </div>
            <a href="logout.php" class="btn-logout"><i class="fas fa-power-off"></i> Logout</a>
        </header>

        <main class="admin-content">
            <h1 class="page-title">Manage Header Menus, Dropdowns & Sliders</h1>

            <?php if (!empty($message) || isset($_GET['msg'])): ?>
                <div class="alert-success"><?php echo !empty($message) ? $message : 'Action successfully complete ho gaya!'; ?></div>
            <?php endif; ?>
            <?php if (!empty($error)): ?>
                <div class="alert-error"><?php echo $error; ?></div>
            <?php endif; ?>

            <div class="boxes-grid">
                
                <!-- BOX 1: Main Header Menu -->
                <div class="card">
                    <div class="card-title"><i class="fas fa-bars"></i> 1. Add Main Header Menu</div>
                    <form action="" method="POST">
                        <div class="form-group">
                            <label>Main Menu Name (Jaise: Domestic, International)</label>
                            <input type="text" name="menu_name" placeholder="Enter Main Menu Name" required>
                        </div>
                        <button type="submit" name="add_main_menu" class="btn-submit"><i class="fas fa-plus"></i> Save Main Menu</button>
                    </form>

                    <div style="margin-top: 25px;">
                        <strong style="font-size: 13.5px; color: var(--dark-navy);">Existing Main Menus:</strong>
                        <ul style="margin-top: 10px; padding-left: 18px; font-size: 13.5px;">
                            <?php 
                            mysqli_data_seek($main_menus_res, 0);
                            while($m = mysqli_fetch_assoc($main_menus_res)): 
                            ?>
                                <li style="margin-bottom: 8px; display: flex; justify-content: space-between; align-items: center; color: #334155;">
                                    <span><?php echo htmlspecialchars($m['menu_name']); ?></span>
                                    <a href="add_category.php?del_menu=<?php echo $m['id']; ?>" class="btn-action btn-del" onclick="return confirm('Delete main menu?')"><i class="fas fa-trash"></i> Delete</a>
                                </li>
                            <?php endwhile; ?>
                        </ul>
                    </div>
                </div>

                <!-- BOX 2: Dropdown Option (Price & Description removed) -->
                <div class="card">
                    <div class="card-title">
                        <i class="fas fa-list-ul"></i> 
                        <?php echo $edit_data ? 'Edit Dropdown Option' : '2. Add Dropdown Option'; ?>
                    </div>

                    <form action="" method="POST">
                        <input type="hidden" name="edit_id" value="<?php echo $edit_data['id'] ?? 0; ?>">

                        <div class="form-group">
                            <label>Kis Main Menu ke under aayega?</label>
                            <select name="main_menu_id" required>
                                <option value="">-- Select Main Menu --</option>
                                <?php 
                                mysqli_data_seek($main_menus_res, 0);
                                while($m = mysqli_fetch_assoc($main_menus_res)): 
                                    $selected = ($edit_data && $edit_data['main_menu_id'] == $m['id']) ? 'selected' : '';
                                ?>
                                    <option value="<?php echo $m['id']; ?>" <?php echo $selected; ?>><?php echo htmlspecialchars($m['menu_name']); ?></option>
                                <?php endwhile; ?>
                            </select>
                        </div>

                        <div class="form-group">
                            <label>Dropdown Option Name (e.g. Agra, Manali)</label>
                            <input type="text" name="option_name" value="<?php echo htmlspecialchars($edit_data['option_name'] ?? ''); ?>" placeholder="e.g. Agra" required>
                        </div>

                        <?php if ($edit_data): ?>
                            <button type="submit" name="save_dropdown_option" class="btn-submit" style="background:#D97706;"><i class="fas fa-sync-alt"></i> Update Option</button>
                        <?php else: ?>
                            <button type="submit" name="save_dropdown_option" class="btn-submit"><i class="fas fa-save"></i> Save Option</button>
                        <?php endif; ?>
                    </form>
                </div>

            </div>

            <!-- BOX 3: Add Slider Image or Video (Linked to Dropdown Option) -->
            <div class="card-full" style="border: 2px dashed var(--brand-green); background: #F8FAF7;">
                <div class="card-title"><i class="fas fa-photo-film" style="color: var(--brand-green);"></i> 3. Add Slider Image / Video (Linked with Dropdown Option)</div>
                <form action="" method="POST" enctype="multipart/form-data">
                    <div style="display: grid; grid-template-columns: 2fr 1.5fr 2fr auto; gap: 15px; align-items: flex-end;">
                        <div class="form-group" style="margin-bottom: 0;">
                            <label>Select Dropdown Option</label>
                            <select name="dropdown_option_id" required>
                                <option value="">-- Select Option --</option>
                                <?php 
                                $opt_dropdown_res = mysqli_query($conn, "SELECT o.*, m.menu_name FROM `dropdown_options` o JOIN `main_menus` m ON o.main_menu_id = m.id ORDER BY m.menu_name, o.option_name");
                                while($opt_item = mysqli_fetch_assoc($opt_dropdown_res)): 
                                ?>
                                    <option value="<?php echo $opt_item['id']; ?>"><?php echo htmlspecialchars($opt_item['menu_name'] . ' -> ' .$opt_item['option_name']); ?></option>
                                <?php endwhile; ?>
                            </select>
                        </div>
                        <div class="form-group" style="margin-bottom: 0;">
                            <label>Media Type</label>
                            <select name="media_type" required>
                                <option value="image">Image</option>
                                <option value="video">Video</option>
                            </select>
                        </div>
                        <div class="form-group" style="margin-bottom: 0;">
                            <label>Upload File (Image/Video)</label>
                            <input type="file" name="media_file" accept="image/*,video/*" required>
                        </div>
                        <div>
                            <button type="submit" name="add_slider_media" class="btn-submit"><i class="fas fa-upload"></i> Upload Slider</button>
                        </div>
                    </div>
                </form>

                <!-- Slider Media List Table -->
                <div style="margin-top: 20px;" class="custom-table-container">
                    <table class="custom-table">
                        <thead>
                            <tr>
                                <th>Preview</th>
                                <th>Menu -> Option</th>
                                <th>Media Type</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if ($media_res && mysqli_num_rows($media_res) > 0): ?>
                                <?php while($med = mysqli_fetch_assoc($media_res)): ?>
                                    <tr>
                                        <td>
                                            <?php if ($med['media_type'] === 'image'): ?>
                                                <?php if (!empty($med['file_path']) && file_exists('../' .$med['file_path'])): ?>
                                                    <img src="../<?php echo $med['file_path']; ?>" class="thumb">
                                                <?php else: ?>
                                                    <div class="no-thumb"><i class="fas fa-image"></i></div>
                                                <?php endif; ?>
                                            <?php else: ?>
                                                <div class="no-thumb" style="background: #E2E8F0; color: #0F172A;"><i class="fas fa-video"></i></div>
                                            <?php endif; ?>
                                        </td>
                                        <td><span style="color: var(--brand-saffron); font-weight:600;"><?php echo htmlspecialchars($med['menu_name'] . ' / ' .$med['option_name']); ?></span></td>
                                        <td><strong><?php echo ucfirst($med['media_type']); ?></strong></td>
                                        <td>
                                            <a href="add_category.php?del_media=<?php echo $med['id']; ?>" class="btn-action btn-del" onclick="return confirm('Delete this slider media?')"><i class="fas fa-trash"></i> Delete</a>
                                        </td>
                                    </tr>
                                <?php endwhile; ?>
                            <?php else: ?>
                                <tr><td colspan="4" style="text-align: center; color: #94A3B8; padding: 20px;">Koi slider media add nahi ki gayi hai.</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>

            <!-- BOX 4: Add Destination Image (Mapped to Specific Dropdown Option) -->
            <div class="card-full" style="border: 2px dashed #0284C7; background: #F0F9FF;">
                <div class="card-title"><i class="fas fa-image" style="color: #0284C7;"></i> 4. Add Destination Image (Linked with Dropdown Option)</div>
                <form action="" method="POST" enctype="multipart/form-data">
                    <div style="display: grid; grid-template-columns: 2fr 2fr 2fr 1fr auto; gap: 15px; align-items: flex-end;">
                        <div class="form-group" style="margin-bottom: 0;">
                            <label>Select Dropdown Option</label>
                            <select name="dropdown_option_id" required>
                                <option value="">-- Select Option --</option>
                                <?php 
                                mysqli_data_seek($opt_dropdown_res, 0);
                                while($opt_item = mysqli_fetch_assoc($opt_dropdown_res)): 
                                ?>
                                    <option value="<?php echo $opt_item['id']; ?>"><?php echo htmlspecialchars($opt_item['menu_name'] . ' -> ' .$opt_item['option_name']); ?></option>
                                <?php endwhile; ?>
                            </select>
                        </div>
                        <div class="form-group" style="margin-bottom: 0;">
                            <label>Destination Title</label>
                            <input type="text" name="dest_title" placeholder="Destination Title" required>
                        </div>
                        <div class="form-group" style="margin-bottom: 0;">
                            <label>Destination Image</label>
                            <input type="file" name="dest_image" accept="image/*" required>
                        </div>
                        <div class="form-group" style="margin-bottom: 0;">
                            <label>Rating (Stars)</label>
                            <select name="dest_rating">
                                <option value="5">5 Stars</option>
                                <option value="4">4 Stars</option>
                                <option value="3">3 Stars</option>
                            </select>
                        </div>
                        <div>
                            <button type="submit" name="add_destination" class="btn-submit" style="background: #0284C7;"><i class="fas fa-upload"></i> Upload</button>
                        </div>
                    </div>
                </form>

                <!-- Destinations List Table -->
                <div style="margin-top: 20px;" class="custom-table-container">
                    <table class="custom-table">
                        <thead>
                            <tr>
                                <th>Image</th>
                                <th>Menu -> Dropdown Option</th>
                                <th>Destination Title</th>
                                <th>Rating</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if ($destinations_res && mysqli_num_rows($destinations_res) > 0): ?>
                                <?php while($dest = mysqli_fetch_assoc($destinations_res)): ?>
                                    <tr>
                                        <td>
                                            <?php if (!empty($dest['image']) && file_exists('../' .$dest['image'])): ?>
                                                <img src="../<?php echo $dest['image']; ?>" class="thumb">
                                            <?php else: ?>
                                                <div class="no-thumb"><i class="fas fa-image"></i></div>
                                            <?php endif; ?>
                                        </td>
                                        <td><span style="color: var(--brand-saffron); font-weight:600;"><?php echo htmlspecialchars($dest['menu_name'] . ' / ' .$dest['option_name']); ?></span></td>
                                        <td><strong><?php echo htmlspecialchars($dest['title']); ?></strong></td>
                                        <td><?php echo $dest['rating']; ?> <i class="fas fa-star" style="color:#FFFF33;"></i></td>
                                        <td>
                                            <a href="add_category.php?del_dest=<?php echo $dest['id']; ?>" class="btn-action btn-del" onclick="return confirm('Delete this destination?')"><i class="fas fa-trash"></i> Delete</a>
                                        </td>
                                    </tr>
                                <?php endwhile; ?>
                            <?php else: ?>
                                <tr><td colspan="5" style="text-align: center; color: #94A3B8; padding: 20px;">Koi destination image add nahi ki gayi hai.</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>

            <!-- BOX 5: Existing Dropdowns List Box -->
            <div class="card-full">
                <div class="card-title"><i class="fas fa-table"></i> 5. All Dropdown Options List</div>
                <div class="custom-table-container">
                    <table class="custom-table">
                        <thead>
                            <tr>
                                <th>Main Menu</th>
                                <th>Dropdown Option</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if ($options_res && mysqli_num_rows($options_res) > 0): ?>
                                <?php while($opt = mysqli_fetch_assoc($options_res)): ?>
                                    <tr>
                                        <td><strong><?php echo htmlspecialchars($opt['menu_name']); ?></strong></td>
                                        <td><?php echo htmlspecialchars($opt['option_name']); ?></td>
                                        <td>
                                            <a href="add_category.php?edit_option=<?php echo $opt['id']; ?>" class="btn-action btn-edit"><i class="fas fa-edit"></i> Edit</a>
                                            <a href="add_category.php?del_option=<?php echo $opt['id']; ?>" class="btn-action btn-del" onclick="return confirm('Delete this option?')"><i class="fas fa-trash"></i> Delete</a>
                                        </td>
                                    </tr>
                                <?php endwhile; ?>
                            <?php else: ?>
                                <tr><td colspan="3" style="text-align: center; color: #94A3B8; padding: 25px;">Koi dropdown option add nahi kiya gaya hai.</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>

        </main>
    </div>

    <script>
        function toggleSidebar() {
            const sidebar = document.querySelector('.admin-sidebar');
            if (sidebar) {
                sidebar.classList.toggle('sidebar-open');
            }
        }
    </script>
</body>
</html>