<?php
require_once __DIR__ . '/config/database.php';

$offerId = isset($_GET['id']) ? (int)$_GET['id'] : 0;

function offerImageById(int $id): string
{
    $images = [
        'images/offers/1.webp',
        'images/offers/2.webp',
        'images/offers/3.webp',
        'images/offers/4.webp',
        'images/offers/5.webp',
        'images/offers/6.webp',
    ];
    return $images[$id % count($images)];
}

function offerDiscountLabel(string $type, float $value): string
{
    if ($type === 'percentage') {
        return 'Save ' . rtrim(rtrim(number_format($value, 2), '0'), '.') . '%';
    }
    return '$' . rtrim(rtrim(number_format($value, 2), '0'), '.') . ' OFF';
}

$currentOffer = null;
if ($offerId > 0) {
    $stmt = $conn->prepare("SELECT * FROM special_offers WHERE id = ? LIMIT 1");
    if ($stmt) {
        $stmt->bind_param('i', $offerId);
        $stmt->execute();
        $result = $stmt->get_result();
        $currentOffer = $result ? $result->fetch_assoc() : null;
        $stmt->close();
    }
}

if (!$currentOffer) {
    $result = $conn->query("SELECT * FROM special_offers WHERE is_active = 1 ORDER BY start_date ASC, id ASC LIMIT 1");
    if ($result instanceof mysqli_result) {
        $currentOffer = $result->fetch_assoc();
        $result->free();
    }
}

if (!$currentOffer) {
    $currentOffer = [
        'id' => 0,
        'title' => 'Romantic Escape Package',
        'description' => 'Enjoy a premium couple stay with curated dining, spa relaxation, and exclusive in-room touches made for unforgettable moments.',
        'discount_type' => 'percentage',
        'discount_value' => 20,
        'start_date' => date('Y-m-d'),
        'end_date' => date('Y-m-d', strtotime('+30 days')),
        'is_active' => 1,
    ];
}

$offerTitle = trim((string)($currentOffer['title'] ?? 'Special Offer'));
$offerDescription = trim((string)($currentOffer['description'] ?? 'Exclusive hotel package for your next stay.'));
$offerDiscountType = (string)($currentOffer['discount_type'] ?? 'percentage');
$offerDiscountValue = (float)($currentOffer['discount_value'] ?? 0);
$offerStartDate = (string)($currentOffer['start_date'] ?? '');
$offerEndDate = (string)($currentOffer['end_date'] ?? '');
$offerActive = (int)($currentOffer['is_active'] ?? 0) === 1;
$offerImage = offerImageById((int)($currentOffer['id'] ?? 0));
$offerBadge = offerDiscountLabel($offerDiscountType, $offerDiscountValue);

$durationText = 'Limited time';
if ($offerStartDate !== '' && $offerEndDate !== '') {
    $days = (int)((strtotime($offerEndDate) - strtotime($offerStartDate)) / 86400) + 1;
    if ($days > 0) {
        $durationText = $days . ' Day' . ($days > 1 ? 's' : '');
    }
}

$availabilityText = $offerActive ? 'Active Now' : 'Unavailable';

$relatedOffers = [];
$relatedSql = "SELECT id, title, description, discount_type, discount_value
               FROM special_offers
               WHERE is_active = 1 AND id <> ?
               ORDER BY start_date ASC, id ASC
               LIMIT 3";
$relatedStmt = $conn->prepare($relatedSql);
if ($relatedStmt) {
    $currentId = (int)($currentOffer['id'] ?? 0);
    $relatedStmt->bind_param('i', $currentId);
    $relatedStmt->execute();
    $relatedResult = $relatedStmt->get_result();
    while ($relatedResult && ($row = $relatedResult->fetch_assoc())) {
        $relatedOffers[] = $row;
    }
    $relatedStmt->close();
}
?>
<!DOCTYPE html>
<html lang="zxx">

<head>
    <title>Almaris - Hotel Website Template</title>
    <link rel="icon" href="images/icon.webp" type="image/gif" sizes="16x16">
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
    <meta content="width=device-width, initial-scale=1.0" name="viewport">
    <meta content="Almaris - Hotel Website Template" name="description">
    <meta content="" name="keywords">
    <meta content="" name="author">
    <link href="css/bootstrap.min.css" rel="stylesheet" type="text/css" id="bootstrap">
    <link href="css/plugins.css" rel="stylesheet" type="text/css">
    <link href="css/swiper.css" rel="stylesheet" type="text/css">
    <link href="css/style.css" rel="stylesheet" type="text/css">
    <link href="css/coloring.css" rel="stylesheet" type="text/css">
    <link id="colors" href="css/colors/scheme-01.css" rel="stylesheet" type="text/css">
</head>

<body>
    <div id="wrapper">
        <a href="#" id="back-to-top"></a>
        <div id="de-loader"></div>

        <?php include 'header.php'; ?>

        <div class="no-bottom no-top" id="content">
            <div id="top"></div>

            <section id="subheader" class="relative jarallax text-light">
                <img src="images/background/8.webp" class="jarallax-img" alt="">
                <div class="container relative z-index-1000">
                    <div class="row justify-content-center">
                        <div class="col-12 col-lg-8 text-center px-3 px-sm-4">
                            <h1 class="fs-36 fs-sm-42 fs-md-48 fs-lg-56 fw-bold"><?php echo htmlspecialchars($offerTitle, ENT_QUOTES, 'UTF-8'); ?></h1>
                            <p class="fs-16 fs-sm-17 fs-md-18 fs-lg-20 mt-3 mt-md-4" style="line-height: 1.6;"><?php echo htmlspecialchars($offerDescription, ENT_QUOTES, 'UTF-8'); ?></p>
                            <ul class="crumb mt-4">
                                <li><a href="index.php">Home</a></li>
                                <li class="active">Special Offers</li>
                            </ul>
                        </div>
                    </div>
                </div>
                <div class="de-overlay"></div>
            </section>

            <section class="offer-single-modern-section">
                <div class="container">
                    <div class="offer-single-modern-wrap">
                        <div class="row g-4 g-lg-5 align-items-center">
                            <div class="col-12 col-lg-6">
                                <div class="offer-single-media wow fadeInLeft" data-wow-delay=".1s">
                                    <img src="<?php echo htmlspecialchars($offerImage, ENT_QUOTES, 'UTF-8'); ?>" alt="<?php echo htmlspecialchars($offerTitle, ENT_QUOTES, 'UTF-8'); ?>">
                                </div>
                            </div>
                            <div class="col-12 col-lg-6">
                                <div class="offer-single-main-card wow fadeInRight" data-wow-delay=".2s">
                                    <span class="offer-single-kicker">Limited Time Offer</span>
                                    <h2><?php echo htmlspecialchars($offerTitle, ENT_QUOTES, 'UTF-8'); ?></h2>
                                    <p><?php echo htmlspecialchars($offerDescription, ENT_QUOTES, 'UTF-8'); ?></p>
                                    <div class="offer-single-meta">
                                        <div><span>Offer</span><strong><?php echo htmlspecialchars($offerBadge, ENT_QUOTES, 'UTF-8'); ?></strong></div>
                                        <div><span>Duration</span><strong><?php echo htmlspecialchars($durationText, ENT_QUOTES, 'UTF-8'); ?></strong></div>
                                        <div><span>Availability</span><strong><?php echo htmlspecialchars($availabilityText, ENT_QUOTES, 'UTF-8'); ?></strong></div>
                                    </div>
                                    <div class="offer-single-actions">
                                        <a href="reservation.php?offer_id=<?php echo urlencode((string)($currentOffer['id'] ?? 0)); ?>" class="btn-main">Book This Offer</a>
                                        <a href="rooms.php" class="btn-main btn-line">View Rooms</a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </section>

            <section class="offer-includes-section">
                <div class="container">
                    <div class="offer-section-head text-center">
                        <span class="offer-single-kicker">Package Includes</span>
                        <h2>Everything included in your stay</h2>
                        <p>Each inclusion is designed to increase guest comfort and make booking this offer an easy decision.</p>
                    </div>
                    <div class="row g-3 g-md-4">
                        <div class="col-12 col-sm-6 col-lg-4"><article class="offer-include-card"><i class="icofont-bed"></i><h4>Luxury Accommodation</h4><p>Premium room category with elegant interiors and modern amenities.</p></article></div>
                        <div class="col-12 col-sm-6 col-lg-4"><article class="offer-include-card"><i class="icofont-fork-and-knife"></i><h4>Romantic Dinner</h4><p>Candlelit dinner experience with curated couple-friendly menu.</p></article></div>
                        <div class="col-12 col-sm-6 col-lg-4"><article class="offer-include-card"><i class="icofont-wine"></i><h4>Champagne Toast</h4><p>Complimentary premium bottle prepared in-room upon arrival.</p></article></div>
                        <div class="col-12 col-sm-6 col-lg-4"><article class="offer-include-card"><i class="icofont-heart"></i><h4>Rose and Chocolate Setup</h4><p>Romantic room styling with rose petals and artisan chocolates.</p></article></div>
                        <div class="col-12 col-sm-6 col-lg-4"><article class="offer-include-card"><i class="icofont-spa"></i><h4>Couples Spa</h4><p>Relaxing signature treatment designed for two guests.</p></article></div>
                        <div class="col-12 col-sm-6 col-lg-4"><article class="offer-include-card"><i class="icofont-plate"></i><h4>Breakfast in Bed</h4><p>Fresh breakfast delivered directly to your room at preferred time.</p></article></div>
                    </div>
                </div>
            </section>

            <section class="offer-mid-cta-section">
                <div class="container">
                    <div class="offer-mid-cta">
                        <div class="offer-mid-cta-copy">
                            <h3>Secure this offer before it ends</h3>
                            <p>Limited slots per day. Reserve now to lock in your preferred dates and best available package price.</p>
                        </div>
                        <div class="offer-mid-cta-actions">
                            <a href="reservation.php" class="btn-main">Confirm Reservation</a>
                            <a href="contact.php" class="btn-main btn-line">Ask a Question</a>
                        </div>
                    </div>
                </div>
            </section>

            <section class="offer-related-section">
                <div class="container">
                    <div class="offer-section-head text-center">
                        <span class="offer-single-kicker">More Offers</span>
                        <h2>Guests also book these deals</h2>
                        <p>Explore other packages that combine savings with premium hotel experiences.</p>
                    </div>
                    <div class="row g-3 g-md-4">
                        <?php if (!empty($relatedOffers)): ?>
                            <?php foreach ($relatedOffers as $related): ?>
                                <div class="col-12 col-sm-6 col-lg-4">
                                    <a href="special-offer-single.php?id=<?php echo urlencode((string)$related['id']); ?>" class="offer-related-card">
                                        <img src="<?php echo htmlspecialchars(offerImageById((int)$related['id']), ENT_QUOTES, 'UTF-8'); ?>" alt="<?php echo htmlspecialchars((string)$related['title'], ENT_QUOTES, 'UTF-8'); ?>">
                                        <div class="offer-related-body">
                                            <span><?php echo htmlspecialchars(offerDiscountLabel((string)$related['discount_type'], (float)$related['discount_value']), ENT_QUOTES, 'UTF-8'); ?></span>
                                            <h4><?php echo htmlspecialchars((string)$related['title'], ENT_QUOTES, 'UTF-8'); ?></h4>
                                            <p><?php echo htmlspecialchars(trim((string)$related['description']) ?: 'Special package offer available for a limited time.', ENT_QUOTES, 'UTF-8'); ?></p>
                                        </div>
                                    </a>
                                </div>
                            <?php endforeach; ?>
                        <?php else: ?>
                            <div class="col-12"><div class="text-center">More offers will appear here soon.</div></div>
                        <?php endif; ?>
                    </div>
                </div>
            </section>
        </div>

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

    <script src="js/plugins.js"></script>
    <script src="js/designesia.js"></script>
    <script src="js/swiper.js"></script>
    <script src="js/custom-marquee.js"></script>
    <script src="js/custom-swiper-1.js"></script>
</body>

</html>