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

$facilities = [];
$fallbackMedia = [
    'images/facilities/1.webp',
    'images/facilities/2.webp',
    'images/facilities/3.webp',
    'images/facilities/4.webp',
    'images/facilities/5.webp',
    'images/facilities/6.webp',
];

$facilitySql = "SELECT id, name, description, icon, image_url
                FROM facilities
                WHERE is_active = 1
                ORDER BY id ASC";
$facilityResult = $conn->query($facilitySql);
if ($facilityResult instanceof mysqli_result) {
    $idx = 0;
    while ($row = $facilityResult->fetch_assoc()) {
        $name = trim((string)($row['name'] ?? 'Facility'));
        $description = trim((string)($row['description'] ?? ''));
        $iconRaw = trim((string)($row['icon'] ?? ''));
        $mediaRaw = trim((string)($row['image_url'] ?? ''));

        $looksLikeIconClass = preg_match('/^(icofont-|fa[srlbd]?\s|fa[srlbd]?-)/i', $iconRaw) === 1;
        $looksLikeIconImage = preg_match('/(\.svg(\?.*)?$|\/svg\/|icon)/i', $iconRaw) === 1;
        $looksLikePhoto = preg_match('/\.(webp|png|jpe?g|gif)(\?.*)?$/i', $mediaRaw) === 1 && preg_match('/\/svg\//i', $mediaRaw) !== 1;
        $mediaLooksLikeIcon = preg_match('/(\.svg(\?.*)?$|\/svg\/|icon)/i', $mediaRaw) === 1 || preg_match('/^(icofont-|fa[srlbd]?\s|fa[srlbd]?-)/i', $mediaRaw) === 1;
        $iconLooksLikePhoto = preg_match('/\.(webp|png|jpe?g|gif)(\?.*)?$/i', $iconRaw) === 1 && preg_match('/\/svg\//i', $iconRaw) !== 1;

        // Auto-correct rows where icon/image values were saved in swapped columns.
        if ($mediaLooksLikeIcon && $iconLooksLikePhoto) {
            $tmp = $mediaRaw;
            $mediaRaw = $iconRaw;
            $iconRaw = $tmp;
            $looksLikeIconClass = preg_match('/^(icofont-|fa[srlbd]?\s|fa[srlbd]?-)/i', $iconRaw) === 1;
            $looksLikeIconImage = preg_match('/(\.svg(\?.*)?$|\/svg\/|icon)/i', $iconRaw) === 1;
            $looksLikePhoto = preg_match('/\.(webp|png|jpe?g|gif)(\?.*)?$/i', $mediaRaw) === 1 && preg_match('/\/svg\//i', $mediaRaw) !== 1;
        }

        if ($description === '') {
            $description = 'Comfort-focused facility designed to improve your overall stay experience.';
        }

        $media = str_replace('\\', '/', $mediaRaw);
        if ($media === '' || !$looksLikePhoto) {
            $media = $fallbackMedia[$idx % count($fallbackMedia)];
        }

        $iconMode = 'class';
        $iconValue = 'icofont-star';
        if ($iconRaw !== '') {
            if ($looksLikeIconClass) {
                $iconMode = 'class';
                $iconValue = $iconRaw;
            } elseif ($looksLikeIconImage) {
                $iconMode = 'image';
                $iconValue = str_replace('\\', '/', $iconRaw);
                if (strpos($iconValue, '/') === false) {
                    $iconValue = 'images/svg/' . $iconValue;
                }
            }
        }

        $facilities[] = [
            'name' => $name,
            'description' => $description,
            'media' => $media,
            'icon_mode' => $iconMode,
            'icon_value' => $iconValue,
        ];
        $idx++;
    }
    $facilityResult->free();
}

if (empty($facilities)) {
    $facilities = [
        ['name' => 'Cafe and Restaurant', 'description' => 'Chef-crafted menus, local flavors, and all-day dining in a refined atmosphere.', 'media' => 'images/facilities/1.webp', 'icon_mode' => 'class', 'icon_value' => 'icofont-spoon-and-fork'],
        ['name' => 'Swimming Pool', 'description' => 'Relax with temperature-controlled water, lounge seating, and attentive poolside service.', 'media' => 'images/facilities/2.webp', 'icon_mode' => 'class', 'icon_value' => 'icofont-water'],
        ['name' => 'Fitness Center', 'description' => 'Modern cardio and strength equipment with enough space for every workout routine.', 'media' => 'images/facilities/3.webp', 'icon_mode' => 'class', 'icon_value' => 'icofont-dumbbell'],
        ['name' => 'Meeting Rooms', 'description' => 'Business-ready rooms with fast Wi-Fi, display screens, and flexible event setups.', 'media' => 'images/facilities/5.webp', 'icon_mode' => 'class', 'icon_value' => 'icofont-conference'],
        ['name' => 'Spa and Massage', 'description' => 'Recharge with signature treatments and wellness therapies by trained specialists.', 'media' => 'images/facilities/4.webp', 'icon_mode' => 'class', 'icon_value' => 'icofont-heart'],
        ['name' => 'Laundry Service', 'description' => 'Reliable same-day laundry support, ideal for long stays and business schedules.', 'media' => 'images/facilities/6.webp', 'icon_mode' => 'class', 'icon_value' => 'icofont-laundry'],
    ];
}
?>
<!DOCTYPE html>
<html lang="zxx">

<head>
    <title>zemaraim — 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="zemaraim — Hotel Website Template" name="description" >
    <meta content="" name="keywords" >
    <meta content="" name="author" >
    <!-- CSS Files
    ================================================== -->
    <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" >
    <!-- color scheme -->
    <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>
        
        <!-- page preloader begin -->
        <div id="de-loader"></div>
        <!-- page preloader close -->

        <!-- header begin -->
        <?php include 'header.php'; ?>
        <!-- header close -->
        <!-- content begin -->
        <div class="no-bottom no-top" id="content">

            <div id="top"></div>

            <section class="page-breadcrumb-hero" style="--page-hero-bg: url('images/background/mixed-1.webp');">
                <div class="container">
                    <div class="page-breadcrumb-content text-center">
                        <span class="page-breadcrumb-kicker">Premium Amenities</span>
                        <h1>Exceptional Facilities</h1>
                        <p>Explore wellness, dining, fitness, and service facilities designed to upgrade every stay.</p>
                        <ul class="page-breadcrumb-list">
                            <li><a href="index.php">Home</a></li>
                            <li class="active">Facilities</li>
                        </ul>
                    </div>
                </div>
            </section>
            <section class="facilities-intro-section">
                <div class="container">
                    <div class="row align-items-center g-4 g-lg-5">
                        <div class="col-12 col-lg-6 wow fadeInLeft" data-wow-delay=".1s">
                            <div class="facilities-intro-media">
                                <img src="images/facilities/1.webp" alt="Hotel facilities and services">
                            </div>
                        </div>
                        <div class="col-12 col-lg-6 wow fadeInRight" data-wow-delay=".2s">
                            <span class="facilities-kicker">Guest Experience First</span>
                            <h2 class="facilities-title">Facilities designed to make every stay worth booking</h2>
                            <p class="facilities-subtitle">From wellness to dining and business support, every amenity is planned to give your trip more comfort, more convenience, and more reasons to return.</p>
                            <div class="facilities-intro-stats">
                                <div class="facilities-intro-stat">
                                    <strong>24/7</strong>
                                    <span>Service Access</span>
                                </div>
                                <div class="facilities-intro-stat">
                                    <strong>6+</strong>
                                    <span>Premium Amenities</span>
                                </div>
                                <div class="facilities-intro-stat">
                                    <strong>Top Rated</strong>
                                    <span>Guest Satisfaction</span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </section>

            <section class="facilities-modern-section">
                <div class="container">
                    <div class="facilities-modern-head text-center">
                        <span class="facilities-kicker">Our Facilities</span>
                        <h2 class="facilities-title">Everything you need for a comfortable stay</h2>
                        <p class="facilities-subtitle">Explore our most requested amenities built for leisure and business travelers. Each facility is optimized for convenience across all devices and booking journeys.</p>
                    </div>
                    <div class="row g-4">
                        <?php foreach ($facilities as $index => $facility): ?>
                            <?php $delay = number_format((($index % 6) + 1) * 0.1, 1); ?>
                            <div class="col-12 col-sm-6 col-lg-4">
                                <article class="facility-modern-card wow fadeInUp" data-wow-delay=".<?php echo htmlspecialchars(substr($delay, 2), ENT_QUOTES, 'UTF-8'); ?>s">
                                    <div class="facility-modern-media">
                                        <img src="<?php echo htmlspecialchars($facility['media'], ENT_QUOTES, 'UTF-8'); ?>" alt="<?php echo htmlspecialchars($facility['name'], ENT_QUOTES, 'UTF-8'); ?>">
                                    </div>
                                    <div class="facility-modern-icon">
                                        <?php if ($facility['icon_mode'] === 'image'): ?>
                                            <img src="<?php echo htmlspecialchars($facility['icon_value'], ENT_QUOTES, 'UTF-8'); ?>" alt="<?php echo htmlspecialchars($facility['name'], ENT_QUOTES, 'UTF-8'); ?>" style="width:24px;height:24px;">
                                        <?php else: ?>
                                            <i class="<?php echo htmlspecialchars($facility['icon_value'], ENT_QUOTES, 'UTF-8'); ?>"></i>
                                        <?php endif; ?>
                                    </div>
                                    <h4><?php echo htmlspecialchars($facility['name'], ENT_QUOTES, 'UTF-8'); ?></h4>
                                    <p><?php echo htmlspecialchars($facility['description'], ENT_QUOTES, 'UTF-8'); ?></p>
                                </article>
                            </div>
                        <?php endforeach; ?>
                    </div>
                    <div class="facilities-modern-cta wow fadeInUp" data-wow-delay=".2s">
                        <div class="facilities-cta-copy">
                            <h3>Ready to enjoy these premium facilities?</h3>
                            <p>Reserve your room now and get a complete hotel experience designed for comfort, style, and convenience.</p>
                        </div>
                        <div class="facilities-cta-actions">
                            <a href="rooms.php" class="btn-main btn-line">Explore Rooms</a>
                            <a href="reservation.php" class="btn-main">Book Your Stay</a>
                        </div>
                    </div>
                </div>
            </section>

        </div>
        <!-- content close -->
        
        <!-- footer begin -->
        <?php include 'footer.php'; ?>
        <!-- footer close -->
    </div>

    <!-- Javascript Files
    ================================================== -->
    <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>
