<?php
session_start(); // ✅ ต้องมี
require 'backend/config.php';
require 'backend/visit_log.php';

// นับจำนวนผู้เข้าชม
$result = $conn->query("SELECT COUNT(*) AS total FROM visit_logs");
$data = $result->fetch_assoc();
$totalVisitors = $data['total'];
?>
<!DOCTYPE html>
<html lang="th">
<head>
  <meta charset="UTF-8">
  <title>วัดปราสาท เชียงใหม่</title>
  <link rel="stylesheet" href="css/style.css">
</head>
<body>

<header>
  <h1>วัดปราสาท อำเภอเมืองเชียงใหม่</h1>
  <nav>
    <a href="index.php">หน้าแรก</a>
    <a href="about.html">ประวัติวัด</a>

    <?php if (isset($_SESSION['user_id'])): ?>
        <span style="color:white; margin-right:10px;">
          สวัสดี, <?php echo $_SESSION['fullname']; ?>
        </span>
        <a href="logout.php">ออกจากระบบ</a>
    <?php else: ?>
        <a href="login.html">เข้าสู่ระบบ</a>
        <a href="register.html">สมัครสมาชิก</a>
    <?php endif; ?>
  </nav>
</header>

<!-- 🔥 Hero รูปหน้าวัด -->
<section class="hero"
  style="
    background-image:
      linear-gradient(rgba(0,0,0,0.45), rgba(0,0,0,0.45)),
      url('images/wat-prasat.jpg');
  ">
  <h2>ยินดีต้อนรับสู่วัดปราสาท</h2>
  <p>แหล่งเรียนรู้ทางพระพุทธศาสนา</p>
</section>

<div class="container">
  <h3>ข่าวประชาสัมพันธ์</h3>

<?php
$news = $conn->query("SELECT * FROM news ORDER BY created_at DESC LIMIT 3");
while ($n = $news->fetch_assoc()) {
?>
  <div class="news">
    <h4><?php echo $n['title']; ?></h4>
    <p><?php echo $n['content']; ?></p>
  </div>
<?php } ?>

</div>

<footer>
  <p>© 2026 วัดปราสาท เชียงใหม่</p>
  <p>จำนวนผู้เข้าชมทั้งหมด: <?php echo $totalVisitors; ?> ครั้ง</p>
</footer>

</body>
</html>
