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

function offerCardImage(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 offerLabel(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';
}

$offers = [];
$offerSql = "SELECT id, title, description, discount_type, discount_value, start_date, end_date, is_active
             FROM special_offers
             ORDER BY is_active DESC, start_date ASC, id ASC";
$offerResult = $conn->query($offerSql);
if ($offerResult instanceof mysqli_result) {
    while ($row = $offerResult->fetch_assoc()) {
        $offers[] = $row;
    }
    $offerResult->free();
}

if (empty($offers)) {
    $offers = [
        ['id' => 1, 'title' => 'Weekend Getaway', 'description' => 'Save more on weekend bookings with premium stay perks.', 'discount_type' => 'percentage', 'discount_value' => 20, 'start_date' => date('Y-m-d'), 'end_date' => date('Y-m-d', strtotime('+30 days')), 'is_active' => 1],
        ['id' => 2, 'title' => 'Family Fun Package', 'description' => 'Kid-friendly amenities and breakfast included for family trips.', 'discount_type' => 'fixed_amount', 'discount_value' => 50, 'start_date' => date('Y-m-d'), 'end_date' => date('Y-m-d', strtotime('+45 days')), 'is_active' => 1],
        ['id' => 3, 'title' => 'Wellness Retreat', 'description' => 'Spa and wellness focused package with relaxing facilities.', 'discount_type' => 'percentage', 'discount_value' => 15, 'start_date' => date('Y-m-d'), 'end_date' => date('Y-m-d', strtotime('+60 days')), 'is_active' => 1],
    ];
}
?>
<!DOCTYPE html>
<html lang="zxx">

<head>
    <title>Offers - 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="Exclusive hotel deals and special offers at 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>
        .offers-page-section {
            padding: 50px 0;
        }

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

        .offer-page-card {
            border: 1px solid #e8eef5;
            border-radius: 14px;
            overflow: hidden;
            background: #fff;
            box-shadow: 0 10px 30px rgba(16, 36, 61, 0.07);
            display: flex;
            flex-direction: column;
            height: 100%;
        }

        .offer-page-card img {
            width: 100%;
            height: 220px;
            object-fit: cover;
        }

        .offer-page-body {
            padding: 16px;
            display: flex;
            flex-direction: column;
            gap: 10px;
            flex: 1 1 auto;
        }

        .offer-badge-row {
            display: flex;
            justify-content: space-between;
            align-items: center;
            gap: 8px;
            flex-wrap: wrap;
        }

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

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

        .offer-page-body h3 {
            margin: 0;
            font-size: 22px;
            line-height: 1.25;
            color: #10243d;
        }

        .offer-page-body p {
            margin: 0;
            color: #4f6278;
        }

        .offer-page-meta {
            font-size: 13px;
            color: #607286;
            margin-top: auto;
        }

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

        @media (max-width: 991px) {
            .offers-grid {
                grid-template-columns: repeat(2, minmax(0, 1fr));
            }
        }

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

            .offers-grid {
                grid-template-columns: 1fr;
                gap: 14px;
            }

            .offer-page-card img {
                height: 200px;
            }
        }
    </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 class="page-breadcrumb-hero" style="--page-hero-bg: url('images/background/8.webp');">
                <div class="container">
                    <div class="page-breadcrumb-content text-center">
                        <span class="page-breadcrumb-kicker">Exclusive Deals</span>
                        <h1>Special Offers</h1>
                        <p>Unlock seasonal discounts and premium package benefits for your next hotel booking.</p>
                        <ul class="page-breadcrumb-list">
                            <li><a href="index.php">Home</a></li>
                            <li class="active">Offers</li>
                        </ul>
                    </div>
                </div>
            </section>

            <section class="offers-page-section">
                <div class="container">
                    <div class="offers-grid">
                        <?php foreach ($offers as $offer): ?>
                            <?php
                            $offerId = (int)($offer['id'] ?? 0);
                            $title = trim((string)($offer['title'] ?? 'Special Offer'));
                            $description = trim((string)($offer['description'] ?? 'Enjoy a premium stay offer with exclusive value.'));
                            $discountType = (string)($offer['discount_type'] ?? 'percentage');
                            $discountValue = (float)($offer['discount_value'] ?? 0);
                            $startDate = (string)($offer['start_date'] ?? '');
                            $endDate = (string)($offer['end_date'] ?? '');
                            $isActive = (int)($offer['is_active'] ?? 0) === 1;
                            ?>
                            <article class="offer-page-card">
                                <img src="<?php echo htmlspecialchars(offerCardImage($offerId), ENT_QUOTES, 'UTF-8'); ?>" alt="<?php echo htmlspecialchars($title, ENT_QUOTES, 'UTF-8'); ?>">
                                <div class="offer-page-body">
                                    <div class="offer-badge-row">
                                        <span class="offer-discount-badge"><?php echo htmlspecialchars(offerLabel($discountType, $discountValue), ENT_QUOTES, 'UTF-8'); ?></span>
                                        <span class="offer-status"><?php echo $isActive ? 'Active' : 'Inactive'; ?></span>
                                    </div>
                                    <h3><?php echo htmlspecialchars($title, ENT_QUOTES, 'UTF-8'); ?></h3>
                                    <p><?php echo htmlspecialchars($description, ENT_QUOTES, 'UTF-8'); ?></p>
                                    <div class="offer-page-meta">
                                        Valid: <?php echo htmlspecialchars($startDate !== '' ? date('d M Y', strtotime($startDate)) : '-', ENT_QUOTES, 'UTF-8'); ?>
                                        to <?php echo htmlspecialchars($endDate !== '' ? date('d M Y', strtotime($endDate)) : '-', ENT_QUOTES, 'UTF-8'); ?>
                                    </div>
                                    <div class="offer-page-actions">
                                        <a href="offer-details.php?id=<?php echo urlencode((string)$offerId); ?>" class="btn-main">View Offer</a>
                                        <a href="reservation.php?offer_id=<?php echo urlencode((string)$offerId); ?>" class="btn-main">Book Now</a>
                                    </div>
                                </div>
                            </article>
                        <?php endforeach; ?>
                    </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>
