
        <?php include 'include/navbar.php';?>
        <?php include 'include/sidebar.php';?>
     
            <div id="layoutSidenav_content">
                <main>
                    <div class="container-fluid px-4">
                        <h1 class="mt-4">Dashboard</h1>
                        <ol class="breadcrumb mb-4">
                            <li class="breadcrumb-item active">Total Blog</li>
                        </ol>

                        <div class=" mt-4">
<?php
if (isset($_GET['delete'])) {
    $deleteId = $_GET['delete'];

    // First delete image file if needed (optional)
    $getImage = mysqli_query($conn, "SELECT image FROM blogs WHERE id = $deleteId");
    $imgRow = mysqli_fetch_assoc($getImage);
    if ($imgRow && file_exists("uploads/" . $imgRow['image'])) {
        unlink("uploads/" . $imgRow['image']); // delete file from uploads
    }

    // Delete blog post
    mysqli_query($conn, "DELETE FROM blogs WHERE id = $deleteId");
    echo "<script>alert('Blog deleted successfully'); window.location='".$_SERVER['PHP_SELF']."';</script>";
}
$result = mysqli_query($conn, "SELECT * FROM blogs");
?>

<div class="container mt-5">
  <h2 class="mb-4">All Blog Posts</h2>
  <table class="table table-bordered table-striped example">
    <thead class="">
      <tr>
        <th>ID</th>
        <th>Title</th>
        <th>Date</th>
        <th>Image</th>
        <th>Short Description</th>
        <th>Long Description</th>
        <th>Actions</th>
      </tr>
    </thead>
    <tbody>
      <?php while ($row = mysqli_fetch_assoc($result)) { ?>
        <tr>
          <td><?= $row['id']; ?></td>
          <td><?= htmlspecialchars($row['title']); ?></td>
          <td><?= $row['date']; ?></td>
          <td><img src="../uploads/<?= $row['image']; ?>" alt="" width="100"></td>
          <td><?= htmlspecialchars(substr($row['short_description'], 0, 100)); ?>...</td>
          <td><?= htmlspecialchars(substr($row['long_description'], 0, 100)); ?>...</td>
           <td>
            <a href="edit-blog.php?id=<?= $row['id']; ?>" class="btn btn-sm btn-primary mb-1">Edit</a>
            <a href="?delete=<?= $row['id']; ?>" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure?')">Delete</a>
          </td>
        </tr>
        </tr>
      <?php } ?>
    </tbody>
  </table>
</div>


                    </div>
                </main>
                <?php include "include/footer.php";?>
            </div>
        </div>
<!-- JS TextEditor -->
<script src="//cdn.ckeditor.com/4.13.1/standard/ckeditor.js"></script>
         <script>
        CKEDITOR.replace('arContent');
    </script>
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
        <script src="js/scripts.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.js" crossorigin="anonymous"></script>
        <script src="assets/demo/chart-area-demo.js"></script>
        <script src="assets/demo/chart-bar-demo.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/simple-datatables@latest" crossorigin="anonymous"></script>
        <script src="js/datatables-simple-demo.js"></script>
    </body>
</html>
