<?php 
include 'includes/header.php';
include 'includes/navbar.php';

$package_id = isset($_GET['package_id']) ? (int)$_GET['package_id'] : null;
$destination = isset($_GET['destination']) ? $_GET['destination'] : '';
$date = isset($_GET['date']) ? $_GET['date'] : '';
$persons = isset($_GET['persons']) ? (int)$_GET['persons'] : 1;

$package_title = '';
if($package_id) {
    $stmt = $conn->prepare("SELECT title FROM packages WHERE id = ?");
    $stmt->bind_param("i", $package_id);
    $stmt->execute();
    $result = $stmt->get_result();
    if($row = $result->fetch_assoc()) {
        $package_title = $row['title'];
    }
}

if($_SERVER['REQUEST_METHOD'] == 'POST') {
    $name = sanitize($conn, $_POST['name']);
    $phone = sanitize($conn, $_POST['phone']);
    $dest = sanitize($conn, $_POST['destination']);
    $travel_date = sanitize($conn, $_POST['travel_date']);
    $qty = (int)$_POST['persons'];
    $pkg_id = !empty($_POST['package_id']) ? (int)$_POST['package_id'] : NULL;
    
    $stmt = $conn->prepare("INSERT INTO bookings (customer_name, phone_number, destination, travel_date, persons, package_id) VALUES (?, ?, ?, ?, ?, ?)");
    $stmt->bind_param("ssssii", $name, $phone, $dest, $travel_date, $qty, $pkg_id);
    
    if($stmt->execute()) {
        // Send to whatsapp
        $wa_num = !empty($settings['whatsapp_number']) ? $settings['whatsapp_number'] : '94771957420';
        $msg = "Hello, I need booking details:%0A";
        $msg .= "Name: $name%0A";
        $msg .= "Phone: $phone%0A";
        $msg .= "Destination/Package: " . ($package_title ?: $dest) . "%0A";
        $msg .= "Date: $travel_date%0A";
        $msg .= "Persons: $qty";
        
        echo "<script>
            alert('Booking submitted successfully!');
            window.location.href = 'https://wa.me/{$wa_num}?text={$msg}';
        </script>";
    } else {
        $error = "Failed to submit booking.";
    }
}
?>

<div class="bg-primary text-white py-5 text-center">
    <div class="container">
        <h1 class="display-4 fw-bold">Book Your Trip</h1>
        <p class="lead mb-0">Fill out the form below to secure your booking</p>
    </div>
</div>

<section class="py-5">
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card shadow-sm border-0">
                    <div class="card-body p-4 p-md-5">
                        <?php if(isset($error)): ?>
                            <div class="alert alert-danger"><?= $error ?></div>
                        <?php endif; ?>
                        
                        <form method="POST">
                            <?php if($package_id): ?>
                                <input type="hidden" name="package_id" value="<?= $package_id ?>">
                                <div class="alert alert-info">
                                    <strong>Selected Package:</strong> <?= htmlspecialchars($package_title) ?>
                                </div>
                            <?php endif; ?>
                            
                            <div class="row g-3">
                                <div class="col-md-6">
                                    <label class="form-label">Full Name *</label>
                                    <input type="text" name="name" class="form-control" required>
                                </div>
                                <div class="col-md-6">
                                    <label class="form-label">Phone Number *</label>
                                    <input type="tel" name="phone" class="form-control" required>
                                </div>
                                <div class="col-md-12">
                                    <label class="form-label">Destination *</label>
                                    <input type="text" name="destination" class="form-control" value="<?= htmlspecialchars($package_title ?: $destination) ?>" required>
                                </div>
                                <div class="col-md-6">
                                    <label class="form-label">Travel Date *</label>
                                    <input type="date" name="travel_date" class="form-control" value="<?= htmlspecialchars($date) ?>" required>
                                </div>
                                <div class="col-md-6">
                                    <label class="form-label">Number of Persons *</label>
                                    <input type="number" name="persons" class="form-control" min="1" value="<?= htmlspecialchars($persons) ?>" required>
                                </div>
                                <div class="col-12 mt-4">
                                    <button type="submit" class="btn btn-primary w-100 py-3 fw-bold fs-5">Confirm Booking & Continue to WhatsApp</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

<?php include 'includes/footer.php'; ?>
