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

$newsItems = [];

$newsSql = "SELECT n.id, n.title, n.slug, n.summary, n.featured_image, n.status, n.created_at, n.published_at,
                   COALESCE(CONCAT(u.first_name, ' ', u.last_name), 'Zemaraim Team') AS author_name
            FROM news n
            LEFT JOIN users u ON u.id = n.author_id
            WHERE n.status IN ('published', 'draft')
            ORDER BY (n.status = 'published') DESC, COALESCE(n.published_at, n.created_at) DESC
            LIMIT 12";

$newsResult = $conn->query($newsSql);
if ($newsResult instanceof mysqli_result) {
    while ($row = $newsResult->fetch_assoc()) {
        $title = trim((string)($row['title'] ?? ''));
        if ($title === '') {
            continue;
        }

        $image = trim((string)($row['featured_image'] ?? ''));
        if ($image === '') {
            $image = 'images/news/1.webp';
        }

        $summary = trim((string)($row['summary'] ?? ''));
        if ($summary === '') {
            $summary = 'Read the latest hotel updates, guest stories, and travel insights from our team.';
        }

        $slug = trim((string)($row['slug'] ?? ''));
        $detailUrl = 'news-details.php';
        if ($slug !== '') {
            $detailUrl .= '?slug=' . urlencode($slug);
        } elseif (!empty($row['id'])) {
            $detailUrl .= '?id=' . urlencode((string)$row['id']);
        }

        $dateValue = (string)($row['published_at'] ?: $row['created_at']);
        $dateText = $dateValue !== '' ? date('d M Y', strtotime($dateValue)) : date('d M Y');

        $newsItems[] = [
            'title' => $title,
            'summary' => $summary,
            'image' => str_replace('\\', '/', $image),
            'status' => ucfirst((string)($row['status'] ?? 'Update')),
            'date' => $dateText,
            'author' => trim((string)($row['author_name'] ?? 'Zemaraim Team')),
            'url' => $detailUrl,
        ];
    }
    $newsResult->free();
}

if (empty($newsItems)) {
    $newsItems = [
        ['title' => 'How Smart Technology is Transforming Hotel Experiences', 'summary' => 'Discover how modern hotel tech improves comfort, convenience, and personalization for every guest.', 'image' => 'images/news/1.webp', 'status' => 'Tips & Tricks', 'date' => '18 Mar 2024', 'author' => 'Brunilda Doniger', 'url' => 'news-details.php'],
        ['title' => 'Exploring the Rise of Unique and Lifestyle Hotels', 'summary' => 'See why travelers choose experience-first hotels that blend comfort with memorable design.', 'image' => 'images/news/2.webp', 'status' => 'Hospitality', 'date' => '18 Mar 2024', 'author' => 'Brunilda Doniger', 'url' => 'news-details.php'],
        ['title' => 'Hotels are Adapting to Modern Traveler Preferences', 'summary' => 'From flexible check-in to curated amenities, hotels are redesigning stays for today\'s guests.', 'image' => 'images/news/3.webp', 'status' => 'Trends', 'date' => '18 Mar 2024', 'author' => 'Brunilda Doniger', 'url' => 'news-details.php'],
        ['title' => 'Innovative Marketing Strategies for Boutique Hotels', 'summary' => 'Learn proven approaches boutique hotels use to increase direct bookings and guest loyalty.', 'image' => 'images/news/4.webp', 'status' => 'Marketing', 'date' => '18 Mar 2024', 'author' => 'Brunilda Doniger', 'url' => 'news-details.php'],
        ['title' => 'Transforming Common Spaces in Modern Hotels', 'summary' => 'Explore design ideas that turn common areas into inviting spaces for connection and comfort.', 'image' => 'images/news/5.webp', 'status' => 'Design', 'date' => '18 Mar 2024', 'author' => 'Brunilda Doniger', 'url' => 'news-details.php'],
        ['title' => 'Enhancing Hotel Operations and Guest Services', 'summary' => 'See how operational improvements create smoother service and a better guest journey.', 'image' => 'images/news/6.webp', 'status' => 'Operations', 'date' => '18 Mar 2024', 'author' => 'Brunilda Doniger', 'url' => 'news-details.php'],
    ];
}
?>
<!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/1.webp');">
                <div class="container">
                    <div class="page-breadcrumb-content text-center">
                        <span class="page-breadcrumb-kicker">Hotel Updates</span>
                        <h1>News</h1>
                        <p>Read the latest hotel updates, guest stories, travel ideas, and booking insights from our team.</p>
                        <ul class="page-breadcrumb-list">
                            <li><a href="index.php">Home</a></li>
                            <li class="active">News</li>
                        </ul>
                    </div>
                </div>
            </section>

            <!-- news section -->
            <section class="news-modern-section">
                <div class="container">
                    <div class="news-modern-head text-center wow fadeInUp">
                        <span class="news-kicker">Latest Updates</span>
                        <h2 class="news-title">Travel insights, hotel tips, and guest-inspired stories.</h2>
                        <p class="news-subtitle">Discover ideas and updates to help you plan a better stay and book with confidence at zemaraim Hotel.</p>
                    </div>

                    <div class="row g-3 g-md-4">
                        <?php foreach ($newsItems as $index => $item): ?>
                            <?php $delay = number_format(($index % 10) * 0.1, 1); ?>
                            <div class="col-lg-4 col-md-6 wow fadeInUp" data-wow-delay="<?php echo htmlspecialchars($delay, ENT_QUOTES, 'UTF-8'); ?>s">
                                <article class="news-modern-card h-100">
                                    <a href="<?php echo htmlspecialchars($item['url'], ENT_QUOTES, 'UTF-8'); ?>" class="news-modern-image-wrap">
                                        <img src="<?php echo htmlspecialchars($item['image'], ENT_QUOTES, 'UTF-8'); ?>" alt="<?php echo htmlspecialchars($item['title'], ENT_QUOTES, 'UTF-8'); ?>">
                                        <span class="news-modern-tag"><?php echo htmlspecialchars($item['status'], ENT_QUOTES, 'UTF-8'); ?></span>
                                    </a>
                                    <div class="news-modern-body">
                                        <h4><a href="<?php echo htmlspecialchars($item['url'], ENT_QUOTES, 'UTF-8'); ?>"><?php echo htmlspecialchars($item['title'], ENT_QUOTES, 'UTF-8'); ?></a></h4>
                                        <p><?php echo htmlspecialchars($item['summary'], ENT_QUOTES, 'UTF-8'); ?></p>
                                        <div class="news-modern-actions">
                                            <a href="<?php echo htmlspecialchars($item['url'], ENT_QUOTES, 'UTF-8'); ?>" class="btn-line">View Details</a>
                                        </div>
                                        <div class="news-modern-meta">
                                            <span><i class="icofont-ui-calendar"></i><?php echo htmlspecialchars($item['date'], ENT_QUOTES, 'UTF-8'); ?></span>
                                            <span><i class="icofont-user"></i><?php echo htmlspecialchars($item['author'], ENT_QUOTES, 'UTF-8'); ?></span>
                                        </div>
                                    </div>
                                </article>
                            </div>
                        <?php endforeach; ?>
                    </div>

                    <div class="news-modern-cta wow fadeInUp" data-wow-delay="0.6s">
                        <div class="news-cta-copy">
                            <h3>Ready for your next stay?</h3>
                            <p>Apply these travel tips and reserve your preferred room with direct booking benefits.</p>
                        </div>
                        <div class="news-cta-actions">
                            <a href="reservation.php" class="btn-main">Book Your Stay</a>
                            <a href="rooms.php" class="btn-main btn-line">View Rooms</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>
