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

function offerDetailImage(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 offerDetailLabel(string $type, float $value): string
{
    if ($type === 'percentage') {
        return rtrim(rtrim(number_format($value, 2), '0'), '.') . '% OFF';
    }
    return '$' . rtrim(rtrim(number_format($value, 2), '0'), '.') . ' OFF';
}

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

if ($offerId > 0) {
    $stmt = $conn->prepare("SELECT id, title, description, discount_type, discount_value, applicable_rooms, start_date, end_date, is_active FROM special_offers WHERE id = ? LIMIT 1");
    if ($stmt) {
        $stmt->bind_param('i', $offerId);
        $stmt->execute();
        $result = $stmt->get_result();
        $offer = $result ? $result->fetch_assoc() : null;
        $stmt->close();
    }
}

if (!$offer) {
    $result = $conn->query("SELECT id, title, description, discount_type, discount_value, applicable_rooms, start_date, end_date, is_active FROM special_offers WHERE is_active = 1 ORDER BY start_date ASC, id ASC LIMIT 1");
    if ($result instanceof mysqli_result) {
        $offer = $result->fetch_assoc();
        $result->free();
    }
}

if (!$offer) {
    $offer = [
        'id' => 1,
        'title' => 'Weekend Escape Offer',
        'description' => 'Book now and enjoy premium comfort with exclusive savings for your next stay.',
        'discount_type' => 'percentage',
        'discount_value' => 20,
        'applicable_rooms' => 'All Rooms',
        'start_date' => date('Y-m-d'),
        'end_date' => date('Y-m-d', strtotime('+30 days')),
        'is_active' => 1,
    ];
}

$offerTitle = trim((string)($offer['title'] ?? 'Special Offer'));
$offerDescription = trim((string)($offer['description'] ?? ''));
$discountType = (string)($offer['discount_type'] ?? 'percentage');
$discountValue = (float)($offer['discount_value'] ?? 0);
$offerRooms = trim((string)($offer['applicable_rooms'] ?? 'All Rooms'));
$startDate = (string)($offer['start_date'] ?? '');
$endDate = (string)($offer['end_date'] ?? '');
$isActive = (int)($offer['is_active'] ?? 0) === 1;
$offerImage = offerDetailImage((int)($offer['id'] ?? 1));
$discountLabel = offerDetailLabel($discountType, $discountValue);

$relatedOffers = [];
$relatedStmt = $conn->prepare("SELECT id, title, description, discount_type, discount_value, start_date, end_date FROM special_offers WHERE is_active = 1 AND id <> ? ORDER BY start_date ASC, id ASC LIMIT 3");
if ($relatedStmt) {
    $currentId = (int)($offer['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><?php echo htmlspecialchars($offerTitle, ENT_QUOTES, 'UTF-8'); ?> - Zemaraim Hotel Booking</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="Offer details page for Zemaraim Hotel Booking." name="description">
    <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">
    <style>
        .offer-details-section {
            padding: 50px 0;
        }

        .offer-details-wrap {
            border: 1px solid #e7edf4;
            border-radius: 16px;
            background: #fff;
            box-shadow: 0 14px 40px rgba(15, 34, 58, 0.08);
            overflow: hidden;
        }

        .offer-details-media img {
            width: 100%;
            height: 100%;
            min-height: 280px;
            object-fit: cover;
            display: block;
        }

        .offer-details-body {
            padding: 26px;
            display: flex;
            flex-direction: column;
            gap: 14px;
            height: 100%;
        }

        .offer-details-top {
            display: flex;
            align-items: center;
            justify-content: space-between;
            gap: 10px;
            flex-wrap: wrap;
        }

        .offer-details-badge {
            background: #10243d;
            color: #fff;
            font-size: 12px;
            font-weight: 700;
            padding: 7px 12px;
            border-radius: 999px;
        }

        .offer-details-status {
            font-size: 12px;
            font-weight: 700;
            color: #2f7a46;
        }

        .offer-details-body h1 {
            margin: 0;
            color: #10243d;
            font-size: 34px;
            line-height: 1.2;
        }

        .offer-details-body p {
            margin: 0;
            color: #4e6074;
        }

        .offer-details-meta {
            display: grid;
            grid-template-columns: repeat(2, minmax(0, 1fr));
            gap: 12px;
            margin-top: 8px;
        }

        .offer-meta-item {
            border: 1px solid #e8eef5;
            border-radius: 10px;
            padding: 10px 12px;
            background: #f9fbfd;
        }

        .offer-meta-item span {
            display: block;
            color: #74879a;
            font-size: 12px;
            line-height: 1.2;
            margin-bottom: 3px;
        }

        .offer-meta-item strong {
            color: #1c3148;
            font-size: 14px;
            line-height: 1.4;
        }

        .offer-details-actions {
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
            margin-top: 10px;
        }

        .offer-related-section {
            padding: 20px 0 50px;
        }

        .offer-related-grid {
            display: grid;
            grid-template-columns: repeat(3, minmax(0, 1fr));
            gap: 16px;
        }

        .offer-related-card {
            border: 1px solid #e8eef5;
            border-radius: 12px;
            background: #fff;
            overflow: hidden;
            box-shadow: 0 8px 24px rgba(16, 36, 61, 0.06);
            display: block;
            height: 100%;
        }

        .offer-related-card img {
            width: 100%;
            height: 180px;
            object-fit: cover;
            display: block;
        }

        .offer-related-body {
            padding: 14px;
        }

        .offer-related-body span {
            color: #10243d;
            font-weight: 700;
            font-size: 12px;
        }

        .offer-related-body h3 {
            margin: 7px 0 6px;
            color: #10243d;
            font-size: 20px;
            line-height: 1.3;
        }

        .offer-related-body p {
            margin: 0;
            color: #5c7087;
            font-size: 14px;
            line-height: 1.55;
        }

        @media (max-width: 991px) {
            .offer-details-body h1 {
                font-size: 30px;
            }

            .offer-related-grid {
                grid-template-columns: repeat(2, minmax(0, 1fr));
            }
        }

        @media (max-width: 575px) {
            .offer-details-section {
                padding: 30px 0;
            }

            .offer-details-body {
                padding: 18px;
            }

            .offer-details-body h1 {
                font-size: 26px;
            }

            .offer-details-meta {
                grid-template-columns: 1fr;
            }

            .offer-related-section {
                padding: 10px 0 30px;
            }

            .offer-related-grid {
                grid-template-columns: 1fr;
            }
        }
    </style>
</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-lg-8 text-center">
                            <h1><?php echo htmlspecialchars($offerTitle, ENT_QUOTES, 'UTF-8'); ?></h1>
                            <p class="mt-3 lead"><?php echo htmlspecialchars($offerDescription, ENT_QUOTES, 'UTF-8'); ?></p>
                            <ul class="crumb">
                                <li><a href="index.php">Home</a></li>
                                <li><a href="offer.php">Offers</a></li>
                                <li class="active">Details</li>
                            </ul>
                        </div>
                    </div>
                </div>
                <div class="de-overlay"></div>
            </section>

            <section class="offer-details-section">
                <div class="container">
                    <div class="offer-details-wrap">
                        <div class="row g-0">
                            <div class="col-12 col-lg-6">
                                <div class="offer-details-media">
                                    <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-details-body">
                                    <div class="offer-details-top">
                                        <span class="offer-details-badge"><?php echo htmlspecialchars($discountLabel, ENT_QUOTES, 'UTF-8'); ?></span>
                                        <span class="offer-details-status"><?php echo $isActive ? 'Active' : 'Inactive'; ?></span>
                                    </div>
                                    <h1><?php echo htmlspecialchars($offerTitle, ENT_QUOTES, 'UTF-8'); ?></h1>
                                    <p><?php echo htmlspecialchars($offerDescription, ENT_QUOTES, 'UTF-8'); ?></p>
                                    <div class="offer-details-meta">
                                        <div class="offer-meta-item">
                                            <span>Valid From</span>
                                            <strong><?php echo htmlspecialchars($startDate !== '' ? date('d M Y', strtotime($startDate)) : '-', ENT_QUOTES, 'UTF-8'); ?></strong>
                                        </div>
                                        <div class="offer-meta-item">
                                            <span>Valid To</span>
                                            <strong><?php echo htmlspecialchars($endDate !== '' ? date('d M Y', strtotime($endDate)) : '-', ENT_QUOTES, 'UTF-8'); ?></strong>
                                        </div>
                                        <div class="offer-meta-item">
                                            <span>Applicable Rooms</span>
                                            <strong><?php echo htmlspecialchars($offerRooms !== '' ? $offerRooms : 'All Rooms', ENT_QUOTES, 'UTF-8'); ?></strong>
                                        </div>
                                        <div class="offer-meta-item">
                                            <span>Offer Type</span>
                                            <strong><?php echo htmlspecialchars($discountType === 'percentage' ? 'Percentage Discount' : 'Fixed Discount', ENT_QUOTES, 'UTF-8'); ?></strong>
                                        </div>
                                    </div>
                                    <div class="offer-details-actions">
                                        <a href="reservation.php?offer_id=<?php echo urlencode((string)($offer['id'] ?? 0)); ?>" class="btn-main">Book This Offer</a>
                                        <a href="rooms.php" class="btn-main btn-line">Browse Rooms</a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </section>

            <section class="offer-related-section">
                <div class="container">
                    <h2 class="mb-4">More Active Offers</h2>
                    <div class="offer-related-grid">
                        <?php if (!empty($relatedOffers)): ?>
                            <?php foreach ($relatedOffers as $related): ?>
                                <?php
                                $relatedId = (int)($related['id'] ?? 0);
                                $relatedTitle = trim((string)($related['title'] ?? 'Special Offer'));
                                $relatedDescription = trim((string)($related['description'] ?? ''));
                                ?>
                                <a href="offer-details.php?id=<?php echo urlencode((string)$relatedId); ?>" class="offer-related-card">
                                    <img src="<?php echo htmlspecialchars(offerDetailImage($relatedId), ENT_QUOTES, 'UTF-8'); ?>" alt="<?php echo htmlspecialchars($relatedTitle, ENT_QUOTES, 'UTF-8'); ?>">
                                    <div class="offer-related-body">
                                        <span><?php echo htmlspecialchars(offerDetailLabel((string)($related['discount_type'] ?? 'percentage'), (float)($related['discount_value'] ?? 0)), ENT_QUOTES, 'UTF-8'); ?></span>
                                        <h3><?php echo htmlspecialchars($relatedTitle, ENT_QUOTES, 'UTF-8'); ?></h3>
                                        <p><?php echo htmlspecialchars($relatedDescription !== '' ? $relatedDescription : 'Limited-time offer available for your stay.', ENT_QUOTES, 'UTF-8'); ?></p>
                                    </div>
                                </a>
                            <?php endforeach; ?>
                        <?php else: ?>
                            <div class="offer-meta-item">
                                <span>Info</span>
                                <strong>No additional active offers are available right now.</strong>
                            </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>
