   <?php include('partials/template.php'); 
   include('partials/sidebar.php');
   $search = isset($_GET['search']) ? $_GET['search'] :'';

   if (!empty($search)) {
      $query = "SELECT * FROM buku WHERE judul_buku LIKE '%$search%' OR penulis LIKE '%$search%' OR tahun_terbit LIKE '%$search%'";
   } else {
      $query = "SELECT * FROM buku";
   }
   $result = mysqli_query($koneksi, $query);
   ?>

   <div class="p-4 sm:ml-64">
      <div class="p-4 border-gray-200">
         <div class="container grid gap-4">
            <div class="flex justify-between items-center">
               <a href="index.php"><img class="h-8" src="assets/BOOKRENT.svg" alt="BOOKRENT"></a>
               <img src="assets/user.png" alt="User Photo" class="w-10 h-10 rounded-full border border-gray-300 shadow-sm">
            </div> 
            <div class="grid grid-cols-6 gap-4 mt-5">
               <?php include('partials/buku.php'); ?>
            </div>
         </div>
      </div>
   </div>

   <?php
   if (isset($_GET['alert'])) {
      $alert = $_GET['alert'];
      
      echo "<script>";
      switch ($alert) {
         case "tambah_berhasil":
               echo "Swal.fire('Berhasil!', 'Buku berhasil ditambahkan!', 'success');";
               break;
         case "tambah_gagal":
               echo "Swal.fire('Gagal!', 'Gagal menambahkan buku!', 'error');";
               break;
         case "update_berhasil":
               echo "Swal.fire('Berhasil!', 'Buku berhasil diperbarui!', 'success');";
               break;
         case "update_gagal":
               echo "Swal.fire('Gagal!', 'Gagal memperbarui buku!', 'error');";
               break;
         case "hapus_berhasil_buku":
               echo "Swal.fire('Berhasil!', 'Buku berhasil dihapus!', 'success');";
               break;
         case "hapus_gagal_buku":
               echo "Swal.fire('Gagal!', 'Gagal menghapus buku!', 'error');";
               break;

         case "akses_tidak_sah":
               echo "Swal.fire('Peringatan!', 'Akses tidak sah!', 'warning');";
               break;
      }
      echo "</script>";
   }
   ?>

   <script>
   function confirmDeleteBook(id) {
      Swal.fire({
         title: "Apakah Anda yakin?",
         text: "Buku yang dihapus tidak bisa dikembalikan!",
         icon: "warning",
         showCancelButton: true,
         confirmButtonColor: "#d33",
         cancelButtonColor: "#3085d6",
         confirmButtonText: "Ya, hapus!",
         cancelButtonText: "Batal"
      }).then((result) => {
         if (result.isConfirmed) {
               window.location.href = "hapus_buku.php?id=" + id;
         }
      });
   }
   </script>
