<?php
include 'auth.php';
include '../includes/header.php';
// Add basic auth or session check here if needed

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $title = sanitize($conn, $_POST['title']);
    $country_id = (int)$_POST['country_id'];
    $tour_name = sanitize($conn, $_POST['tour_name']);
    $duration = sanitize($conn, $_POST['duration']);
    $travel_period = sanitize($conn, $_POST['travel_period']);
    $price = (float)$_POST['price'];
    $landmarks = sanitize($conn, $_POST['landmarks']);
    $includes = sanitize($conn, $_POST['includes']);
    $sort_order = isset($_POST['sort_order']) ? (int)$_POST['sort_order'] : 0;
    $tour_type = isset($_POST['tour_type']) && in_array($_POST['tour_type'], ['group', 'private']) ? $_POST['tour_type'] : 'group';

    // Handle Main Image
    $main_image = "";
    if (!empty($_FILES['main_image']['name'])) {
        $main_image = time() . '_' . $_FILES['main_image']['name'];
        move_uploaded_file($_FILES['main_image']['tmp_name'], "../uploads/" . $main_image);
    }

    // Handle Brochure PDF
    $brochure_pdf = "";
    if (!empty($_FILES['brochure_pdf']['name'])) {
        $brochure_pdf = time() . '_' . $_FILES['brochure_pdf']['name'];
        move_uploaded_file($_FILES['brochure_pdf']['tmp_name'], "../uploads/" . $brochure_pdf);
    }

    $sql = "INSERT INTO packages (title, country_id, tour_name, duration, travel_period, price, landmarks, includes, main_image, brochure_pdf, sort_order, tour_type) 
            VALUES ('$title', $country_id, '$tour_name', '$duration', '$travel_period', $price, '$landmarks', '$includes', '$main_image', '$brochure_pdf', $sort_order, '$tour_type')";
    
    if ($conn->query($sql)) {
        $package_id = $conn->insert_id;
        
        // Handle Gallery
        if (!empty($_FILES['gallery']['name'][0])) {
            foreach ($_FILES['gallery']['tmp_name'] as $key => $tmp_name) {
                $img_name = time() . '_' . $_FILES['gallery']['name'][$key];
                move_uploaded_file($tmp_name, "../uploads/" . $img_name);
                $conn->query("INSERT INTO package_gallery (package_id, image) VALUES ($package_id, '$img_name')");
            }
        }
        echo "<script>alert('Package added successfully!'); window.location='packages.php';</script>";
    }
}
?>

<div class="container-fluid py-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2 class="fw-bold mb-0">Add New Tour Package</h2>
        <a href="packages.php" class="btn btn-outline-secondary rounded-pill px-4">
            <i class="fas fa-arrow-left me-2"></i> Back to List
        </a>
    </div>

    <div class="card border-0 shadow-sm" style="border-radius: 20px;">
        <div class="card-body p-5">
            <form method="POST" enctype="multipart/form-data">
                <!-- Basic Information -->
                <div class="mb-5">
                    <h5 class="fw-bold text-primary mb-4 border-bottom pb-2">1. Basic Information</h5>
                    <div class="row g-4">
                        <div class="col-md-8">
                            <label class="form-label fw-bold text-muted small text-uppercase">Tour Title</label>
                            <input type="text" name="title" class="form-control form-control-lg border-0 bg-light" placeholder="e.g., Majestic India Pilgrimage" style="border-radius: 12px;" required>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label fw-bold text-muted small text-uppercase">Destination</label>
                            <select name="country_id" class="form-select form-select-lg border-0 bg-light" style="border-radius: 12px;" required>
                                <option value="">Select Country</option>
                                <?php
                                $dests = $conn->query("SELECT * FROM destinations ORDER BY name");
                                while($d = $dests->fetch_assoc()) echo "<option value='{$d['id']}'>".htmlspecialchars($d['name'])."</option>";
                                ?>
                            </select>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label fw-bold text-muted small text-uppercase">Tour Type</label>
                            <select name="tour_type" class="form-select form-select-lg border-0 bg-light" style="border-radius: 12px;" required>
                                <option value="group">Group Tour</option>
                                <option value="private">Private Tour</option>
                            </select>
                        </div>
                    </div>
                </div>

                <!-- Logistics & Pricing -->
                <div class="mb-5">
                    <h5 class="fw-bold text-primary mb-4 border-bottom pb-2">2. Logistics & Pricing</h5>
                    <div class="row g-4">
                        <div class="col-md-3">
                            <label class="form-label fw-bold text-muted small text-uppercase">Duration</label>
                            <input type="text" name="duration" class="form-control border-0 bg-light py-3" placeholder="e.g., 7 Days / 6 Nights" style="border-radius: 12px;" required>
                        </div>
                        <div class="col-md-3">
                            <label class="form-label fw-bold text-muted small text-uppercase">Travel Period</label>
                            <input type="text" name="travel_period" class="form-control border-0 bg-light py-3" placeholder="e.g., Sept 2025 - Dec 2025" style="border-radius: 12px;" required>
                        </div>
                        <div class="col-md-3">
                            <label class="form-label fw-bold text-muted small text-uppercase">Price (LKR)</label>
                            <div class="input-group">
                                <span class="input-group-text border-0 bg-light" style="border-radius: 12px 0 0 12px;">LKR</span>
                                <input type="number" name="price" class="form-control border-0 bg-light py-3" placeholder="0.00" style="border-radius: 0 12px 12px 0;" required>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <label class="form-label fw-bold text-muted small text-uppercase">Sort Order</label>
                            <input type="number" name="sort_order" class="form-control border-0 bg-light py-3" placeholder="e.g., 0" value="0" style="border-radius: 12px;" required>
                        </div>
                    </div>
                </div>

                <!-- Detailed Content -->
                <div class="mb-5">
                    <h5 class="fw-bold text-primary mb-4 border-bottom pb-2">3. Detailed Content</h5>
                    <div class="row g-4">
                        <div class="col-md-12">
                            <label class="form-label fw-bold text-muted small text-uppercase">Tour Overview / Full Description</label>
                            <textarea name="tour_name" class="form-control border-0 bg-light" rows="5" placeholder="Describe the soul of this journey..." style="border-radius: 15px;" required></textarea>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label fw-bold text-muted small text-uppercase">Key Landmarks</label>
                            <textarea name="landmarks" class="form-control border-0 bg-light" rows="6" placeholder="List the iconic places to visit..." style="border-radius: 15px;" required></textarea>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label fw-bold text-muted small text-uppercase">What's Included</label>
                            <textarea name="includes" class="form-control border-0 bg-light" rows="6" placeholder="Flights, Hotels, Meals, etc..." style="border-radius: 15px;" required></textarea>
                        </div>
                    </div>
                </div>

                <!-- Media Uploads -->
                <div class="mb-5">
                    <h5 class="fw-bold text-primary mb-4 border-bottom pb-2">4. Media & Documents</h5>
                    <div class="row g-4">
                        <div class="col-md-4">
                            <label class="form-label fw-bold text-muted small text-uppercase">Main Cover Image</label>
                            <input type="file" name="main_image" class="form-control border-0 bg-light py-3" style="border-radius: 12px;" accept="image/*" required>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label fw-bold text-muted small text-uppercase">Add Gallery Images</label>
                            <input type="file" name="gallery[]" class="form-control border-0 bg-light py-3" style="border-radius: 12px;" accept="image/*" multiple>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label fw-bold text-muted small text-uppercase">Tour Brochure (PDF)</label>
                            <input type="file" name="brochure_pdf" class="form-control border-0 bg-light py-3" style="border-radius: 12px;" accept="application/pdf">
                        </div>
                    </div>
                </div>

                <div class="text-end pt-4">
                    <button type="submit" class="btn btn-primary px-5 py-3 rounded-pill fw-bold shadow-lg">
                        <i class="fas fa-plus me-2"></i> CREATE PACKAGE
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>
