<main id="main" class="main">
   <div class="pagetitle">
      <h1>Voucher</h1>
      <nav>
         <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="index.php">Home</a></li>
            <li class="breadcrumb-item active">Voucher</li>
         </ol>
      </nav>
   </div>
   <!-- End Page Title -->
   <section class="section dashboard">
      <div class="card recent-sales overflow-auto">
         <div class="filter">
            <!-- Modal Dialog Scrollable -->
            <a href="#" class="icon" data-bs-toggle="modal" data-bs-target="#tambah_data">
               <i class="bi bi-plus-lg"></i>
            </a>
         </div>
         <div class="card-body">
            <h5 class="card-title">Voucher</h5>
            <!-- Notifikasi -->
            <?php if (isset($_GET['notif'])): ?>
               <div class="alert alert-success" role="alert">
                  <?php echo htmlspecialchars($_GET['notif']); ?>
               </div>
            <?php endif; ?>
            <table class="table table-borderless datatable">
               <thead>
                  <tr>
                     <th scope="col">#</th>
                     <th scope="col">Judul</th>
                     <th scope="col">Slug</th>
                     <th scope="col">Status</th>
                     <th scope="col">Tanggal Dibuat</th>
                     <th scope="col">Aksi</th>
                  </tr>
               </thead>
               <tbody>
                  <?php
                  $no = 1;
                  $qry_posts = mysqli_query($koneksi, "SELECT * FROM posts ORDER BY created_at DESC");
                  while ($post = mysqli_fetch_array($qry_posts)):
                     ?>
                     <tr>
                        <th><?php echo $no++; ?></th>
                        <td><?php echo htmlspecialchars($post['title']); ?></td>
                        <td><?php echo htmlspecialchars($post['slug']); ?></td>
                        <td><?php echo ucfirst(htmlspecialchars($post['status'])); ?></td>
                        <td><?php echo date('d-m-Y H:i', strtotime($post['created_at'])); ?></td>
                        <td>
                           <a href="index.php?page=edit_post&id=<?php echo $post['id']; ?>" class="btn btn-sm btn-primary">
                              <i class="bi bi-pencil-square"></i>
                           </a>


                           <!-- Tombol Hapus -->
                           <button type="button" class="btn btn-sm btn-danger" data-bs-toggle="modal"
                              data-bs-target="#modalHapus_<?php echo $post['id']; ?>">
                              <i class="bi bi-trash"></i>
                           </button>
                           <!-- Modal Konfirmasi Hapus -->
                           <div class="modal fade" id="modalHapus_<?php echo $post['id']; ?>" tabindex="-1"
                              aria-labelledby="hapusLabel_<?php echo $post['id']; ?>" aria-hidden="true">
                              <div class="modal-dialog">
                                 <div class="modal-content">
                                    <div class="modal-header">
                                       <h5 class="modal-title" id="hapusLabel_<?php echo $post['id']; ?>">Konfirmasi Hapus
                                       </h5>
                                       <button type="button" class="btn-close" data-bs-dismiss="modal"
                                          aria-label="Close"></button>
                                    </div>
                                    <div class="modal-body">
                                       Apakah Anda yakin ingin menghapus postingan
                                       <strong><?php echo htmlspecialchars($post['title']); ?></strong>?
                                    </div>
                                    <div class="modal-footer">
                                       <button type="button" class="btn btn-secondary"
                                          data-bs-dismiss="modal">Batal</button>
                                       <form action="delete_post.php" method="post">
                                          <input type="hidden" name="id" value="<?php echo $post['id']; ?>">
                                          <button type="submit" class="btn btn-danger">Hapus</button>
                                       </form>
                                    </div>
                                 </div>
                              </div>
                           </div>


                        </td>
                     </tr>
                  <?php endwhile; ?>
               </tbody>
            </table>
         </div>
      </div>
   </section>
</main>