<?php include 'include/header.php';?>
<?php include 'include/navbar.php';?>
<?php

// Check if 'id' is passed in the URL
if (!isset($_GET['id']) || empty($_GET['id'])) {
    echo "<p class='text-center text-danger'>No blog selected.</p>";
    exit;
}

$id = intval($_GET['id']); // Sanitize input

// Fetch blog from database
$sql = "SELECT * FROM blogs WHERE id = $id";
$result = $conn->query($sql);

if ($result->num_rows == 0) {
    echo "<p class='text-center text-danger'>Blog not found.</p>";
    exit;
}

$row = $result->fetch_assoc();
?>
  <style>
    .blog-image {
      height: 300px !important;
      /*object-fit: cover;*/
      width: 100%;
    }
  </style>
 
  <style>
  
    .section-title {
      color: #C86A19;
    }
    .blog-card {
      border: 1px solid #eee;
      border-radius: 10px;
      transition: 0.3s;
    }
    .blog-card:hover {
      box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
    }
    .blog-image {
      border-top-left-radius: 10px;
      border-top-right-radius: 10px;
      height: 200px;
      /*object-fit: cover;*/

    }
    .blog-card h5{
        color: #c86a19;
    }
  </style>
<section class="py-5">
  <div class="container">
    <div class="row justify-content-center">
      <div class="col-md-10">

        <img src="admin/<?php echo $row['image']; ?>" alt="Blog Image"  class="blog-image mb-4">

        <h2 class="fw-bold"><?php echo $row['title']; ?></h2>
        <p class="text-muted"><?php echo $row['date']; ?></p>
        <hr>
        <p><?php echo $row['long_description']; ?></p>

        <a href="blog.php" class="btn btn-outline-secondary mt-4">← Back to Blogs</a>

      </div>
    </div>
  </div>
</section>

<section class="py-2 ">
  <div class="container">
    <h2></h2>
    <div class="row g-4 ">
      <?php
      $sql = "SELECT * FROM blogs ORDER BY id DESC";
      $result = $conn->query($sql);

      if ($result->num_rows > 0) {
          while ($row = $result->fetch_assoc()) {
              $title = $row['title'];
              $image = $row['image'];
              $date = $row['date'];
              $short_description = $row['short_description'];
              $id = $row['id'];
              ?>
              <div class="col-md-4 mt-3">
                <div class="blog-card">
                  <img src="admin/<?php echo $image; ?>" class="img-fluid blog-image" alt="Blog Image">
                  <div class="p-3">
                    <h5 class="fw-bold"><?php echo $title; ?></h5>
                    <p class="small text-muted"><?php echo $date; ?> · 3 min read</p>
                    <p><?php echo substr($short_description, 0, 100); ?>...</p>
                    <a href="blog-details.php?id=<?php echo $id; ?>" class="btn btn-sm btn-outline-warning text-dark">Read More</a>
                  </div>
                </div>
              </div>
              <?php
          }
      } else {
          echo "<p>No blog posts found.</p>";
      }
      ?>
    </div>
  </div>
</section>

</body>
</html>
