<?php
/**
 * hotel-details.php
 * Dynamic Hotel Detail Page with WhatsApp Inquiry & Database Integration
 */

require_once __DIR__ . '/db_config.php';
require_once __DIR__ . '/header.php';

// URL Parameter se hotel fetch karna
$hotel_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
$slug     = isset($_GET['slug']) ? trim((string) $_GET['slug']) : '';

if ($hotel_id > 0) {
    $stmt =$conn->prepare("SELECT * FROM hotels WHERE id = ?");
    $stmt->bind_param("i", $hotel_id);
} else if (!empty($slug)) {
    $stmt =$conn->prepare("SELECT * FROM hotels WHERE slug = ?");
    $stmt->bind_param("s", $slug);
} else {
    $stmt =$conn->prepare("SELECT * FROM hotels ORDER BY id DESC LIMIT 1");
}

$stmt->execute();
$result =$stmt->get_result();
$hotel  =$result->fetch_assoc();

if (!$hotel) {
    echo "<div style='text-align:center; padding:100px;'><h2>Hotel Not Found!</h2><p style='color:#64748B; margin-top:10px;'>Aapne jo hotel maanga hai wo database mein nahi mila.</p></div>";
    require_once __DIR__ . '/footer.php';
    exit;
}

// Safe Fallback Keys
$hotel['hotel_name'] =$hotel['hotel_name'] ?? $hotel['title'] ?? 'Untitled Hotel';$hotel['price']      = $hotel['price'] ?? $hotel['amount'] ?? 0;
$hotel['category']   =$hotel['category'] ?? 'Standard';
$hotel['location']   =$hotel['location'] ?? 'Chardham Route';
$hotel['image']      =$hotel['image'] ?? $hotel['banner_image'] ?? '';$hotel['overview']   = $hotel['overview'] ?? $hotel['description'] ?? '';
$hotel['amenities']  =$hotel['amenities'] ?? '';
?>

<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">

<style>
:root {
    --pkg-navy: #0F172A;
    --pkg-gold: #D4A359;
    --brand-green: #12521f;
    --pkg-cream: #F8FAFC;
    --pkg-line: #E2E8F0;
    --text-color: #334155;
}

* { box-sizing: border-box; font-family: 'Poppins', sans-serif; }

.pkg-details-wrap {
    background: var(--pkg-cream);
    padding: 40px 0;
    color: var(--text-color);
}

.pkg-container {
    width: min(1200px, calc(100% - 30px));
    margin: 0 auto;
}

.pkg-grid {
    display: grid;
    grid-template-columns: 2fr 1fr;
    gap: 30px;
}

.pkg-main {
    background: #fff;
    padding: 35px;
    border-radius: 16px;
    border: 1px solid var(--pkg-line);
    box-shadow: 0 4px 20px rgba(0,0,0,0.03);
}

.pkg-sidebar {
    background: #fff;
    padding: 28px;
    border-radius: 16px;
    border: 1px solid var(--pkg-line);
    height: fit-content;
    position: sticky;
    top: 25px;
    box-shadow: 0 4px 20px rgba(0,0,0,0.03);
}

.pkg-badge {
    background: #FEF3C7;
    color: #D97706;
    padding: 6px 14px;
    border-radius: 20px;
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    display: inline-block;
    letter-spacing: 0.5px;
}

.pkg-price {
    font-size: 28px;
    color: var(--brand-green);
    font-weight: 700;
    margin: 12px 0;
}

.pkg-price span {
    font-size: 13.5px;
    color: #64748B;
    font-weight: 500;
}

.pkg-img {
    width: 100%;
    max-height: 420px;
    object-fit: cover;
    border-radius: 12px;
    margin: 20px 0;
    box-shadow: 0 4px 15px rgba(0,0,0,0.05);
}

.btn-book {
    width: 100%;
    background: var(--brand-green);
    color: #fff;
    border: none;
    padding: 13px;
    border-radius: 8px;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    margin-top: 15px;
    transition: opacity 0.2s;
}

.btn-book:hover {
    opacity: 0.9;
}

.section-box {
    margin-top: 30px;
    padding-top: 22px;
    border-top: 1px solid var(--pkg-line);
}

.section-box h3 {
    color: var(--pkg-navy);
    margin-bottom: 12px;
    font-size: 18px;
    font-weight: 600;
}

.section-box p {
    font-size: 14px;
    line-height: 1.7;
    color: var(--text-color);
}

.inquiry-modal {
    display: none;
    position: fixed;
    z-index: 9999;
    left: 0; top: 0;
    width: 100%; height: 100%;
    background: rgba(15, 23, 42, 0.6);
    backdrop-filter: blur(4px);
}

.modal-content {
    background: #fff;
    margin: 4% auto;
    padding: 35px 25px;
    border-radius: 16px;
    width: 90%; max-width: 480px;
    position: relative;
    box-shadow: 0 10px 30px rgba(0,0,0,0.15);
}

.close-modal {
    position: absolute;
    right: 20px; top: 15px;
    font-size: 26px;
    cursor: pointer;
    color: #64748B;
}

.form-group { margin-bottom: 14px; }
.form-group input, .form-group textarea {
    width: 100%; padding: 12px 14px;
    border: 1px solid var(--pkg-line); border-radius: 8px;
    font-size: 13.5px;
    outline: none;
    transition: border-color 0.2s;
}
.form-group input:focus, .form-group textarea:focus {
    border-color: var(--pkg-gold);
}

@media (max-width: 992px) { 
    .pkg-grid { grid-template-columns: 1fr; } 
    .pkg-sidebar { position: relative; top: 0; }
}
</style>

<main class="pkg-details-wrap">
    <div class="pkg-container">
        <div class="pkg-grid">
            
            <!-- Left Side: Hotel Image and Overview -->
            <div class="pkg-main">
                <?php if(!empty($hotel['category'])): ?>
                    <span class="pkg-badge"><?php echo htmlspecialchars($hotel['category']); ?></span>
                <?php endif; ?>

                <h1 style="margin-top: 12px; color: var(--pkg-navy); font-size: 24px; font-weight: 700;"><?php echo htmlspecialchars($hotel['hotel_name']); ?></h1>
                
                <p style="margin-top: 8px; font-size: 14px; color: #64748B;">
                    <i class="fas fa-map-marker-alt" style="color: var(--pkg-gold); margin-right: 6px;"></i> <strong>Location:</strong> <?php echo htmlspecialchars($hotel['location']); ?>
                </p>
                
                <!-- Hotel Image Display -->
                <?php if(!empty($hotel['image'])): ?>
                    <img src="images/<?php echo htmlspecialchars($hotel['image']); ?>" alt="<?php echo htmlspecialchars($hotel['hotel_name']); ?>" class="pkg-img">
                <?php else: ?>
                    <div style="background: #F1F5F9; padding: 30px; border-radius: 8px; text-align: center; color: #64748B; margin: 20px 0; font-size: 13px;">
                        <i class="fas fa-hotel" style="font-size: 28px; margin-bottom: 8px; display: block;"></i>
                        Hotel image not uploaded in database.
                    </div>
                <?php endif; ?>

                <?php if(!empty($hotel['overview'])): ?>
                    <div class="section-box">
                        <h3>Hotel Overview</h3>
                        <p><?php echo nl2br(htmlspecialchars($hotel['overview'])); ?></p>
                    </div>
                <?php endif; ?>

                <?php if(!empty($hotel['amenities'])): ?>
                    <div class="section-box">
                        <h3>Amenities & Facilities</h3>
                        <p><?php echo nl2br(htmlspecialchars($hotel['amenities'])); ?></p>
                    </div>
                <?php endif; ?>
            </div>

            <!-- Right Side: Price & WhatsApp Inquiry Button -->
            <div class="pkg-sidebar">
                <span style="color: #64748B; font-size: 13px; font-weight: 500;">Starting Price</span>
                <div class="pkg-price">
                    ₹<?php echo number_format((float)$hotel['price'], 2); ?>
                    <span>/ night</span>
                </div>

                <p style="font-size: 12.5px; color: #64748B; margin-bottom: 15px; line-height: 1.5;">Includes complimentary breakfast and all applicable taxes.</p>

                <button class="btn-book" id="openInquiryBtn"><i class="fab fa-whatsapp" style="margin-right: 6px; font-size: 16px;"></i> Book Hotel via WhatsApp</button>
            </div>

        </div>
    </div>
</main>

<!-- Popup Booking Form -->
<div id="inquiryModal" class="inquiry-modal">
    <div class="modal-content">
        <span class="close-modal" id="closeInquiryBtn">&times;</span>
        <h3 style="margin-bottom:15px; color: var(--pkg-navy); font-size: 18px; font-weight: 600;">Book <?php echo htmlspecialchars($hotel['hotel_name']); ?></h3>

        <form id="whatsappInquiryForm">
            <div class="form-group">
                <input type="text" id="inq_name" required placeholder="Full Name">
            </div>
            <div class="form-group">
                <input type="email" id="inq_email" required placeholder="Email Address">
            </div>
            <div class="form-group">
                <input type="text" id="inq_phone" required placeholder="Phone Number">
            </div>
            <div class="form-group">
                <input type="number" id="inq_rooms" required placeholder="Number of Rooms" min="1" value="1">
            </div>
            <div class="form-group">
                <input type="date" id="inq_date" required>
            </div>
            <div class="form-group">
                <textarea id="inq_message" required rows="3" placeholder="Special Request / Check-in Details"></textarea>
            </div>
            <button type="submit" class="btn-book" style="background: #25D366;"><i class="fab fa-whatsapp" style="margin-right: 6px;"></i> Send Booking to WhatsApp</button>
        </form>
    </div>
</div>

<script>
    const modal = document.getElementById("inquiryModal");
    const openBtn = document.getElementById("openInquiryBtn");
    const closeBtn = document.getElementById("closeInquiryBtn");

    if (openBtn) {
        openBtn.addEventListener("click", function() {
            modal.style.display = "block";
        });
    }

    if (closeBtn) {
        closeBtn.addEventListener("click", function() {
            modal.style.display = "none";
        });
    }

    window.addEventListener("click", function(event) {
        if (event.target === modal) {
            modal.style.display = "none";
        }
    });

    document.getElementById("whatsappInquiryForm").addEventListener("submit", function(e) {
        e.preventDefault();

        const name = document.getElementById("inq_name").value;
        const email = document.getElementById("inq_email").value;
        const phone = document.getElementById("inq_phone").value;
        const rooms = document.getElementById("inq_rooms").value;
        const date = document.getElementById("inq_date").value;
        const message = document.getElementById("inq_message").value;
        const hotelTitle = "<?php echo addslashes($hotel['hotel_name']); ?>";

        const whatsappNumber = "919876543210"; // Apna WhatsApp number yahan dalein

        const text = `*New Hotel Booking Inquiry*%0A` +
                     `*Hotel:* ${hotelTitle}%0A` +
                     `*Name:* ${name}%0A` +
                     `*Email:* ${email}%0A` +
                     `*Phone:* ${phone}%0A` +
                     `*Rooms:* ${rooms}%0A` +
                     `*Check-in Date:* ${date}%0A` +
                     `*Details:* ${message}`;

        const whatsappUrl = `https://api.whatsapp.com/send?phone=${whatsappNumber}&text=${text}`;
        window.open(whatsappUrl, '_blank');
        modal.style.display = "none";
    });
</script>

<?php
require_once __DIR__ . '/footer.php';
?>