<?php
session_start();

error_reporting(0);
// ini_set('display_errors', 1);
// ini_set('display_errors', 1);


include('includes/config.php');

if (strlen($_SESSION['alogin']) == 0) {
  header('location:index.php');
  exit;
}

// ✅ Delete Book + Its Copies + QR Codes
if (isset($_GET['del'])) {
  $id = intval($_GET['del']);

  try {
    // STEP 1: Fetch all QR codes for this book
    $sqlFetch = "SELECT QRCode FROM tblbookcopies WHERE book_id = :bookid";
    $queryFetch = $dbh->prepare($sqlFetch);
    $queryFetch->bindParam(':bookid', $id, PDO::PARAM_INT);
    $queryFetch->execute();
    $qrResults = $queryFetch->fetchAll(PDO::FETCH_ASSOC);

    // STEP 2: Delete QR code image files (if they exist)
    if ($qrResults) {
      foreach ($qrResults as $qr) {
        if (!empty($qr['QRCode']) && file_exists($qr['QRCode'])) {
          unlink($qr['QRCode']);
        }
      }
    }

    // STEP 3: Delete all book copies from tblbookcopies
    $sqlCopies = "DELETE FROM tblbookcopies WHERE book_id = :bookid";
    $queryCopies = $dbh->prepare($sqlCopies);
    $queryCopies->bindParam(':bookid', $id, PDO::PARAM_INT);
    $queryCopies->execute();

    // STEP 4: Delete book from tblbooks
    $sqlBook = "DELETE FROM tblbooks WHERE id = :id";
    $queryBook = $dbh->prepare($sqlBook);
    $queryBook->bindParam(':id', $id, PDO::PARAM_INT);
    $queryBook->execute();

    $_SESSION['delmsg'] = "Book and its copies deleted successfully.";
  } catch (Exception $e) {
    $_SESSION['error'] = "Error deleting book: " . $e->getMessage();
  }

  header('location:manage-books.php');
  exit;
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <title>Manage Books | Online Library</title>
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <link rel="icon" type="image/x-icon" href="../<?php echo $_SESSION['logo']; ?>">

  <!-- Bootstrap -->
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" rel="stylesheet">

  <!-- FontAwesome -->
  <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">

  <!-- DataTables -->
  <link href="https://cdn.datatables.net/1.13.6/css/dataTables.bootstrap4.min.css" rel="stylesheet">

  <style>
    .square-card {
      width: 100%;
      background-color: #f8f9fa;
      border: 1px solid #dee2e6;
      box-shadow: 0 4px 8px rgba(0, 0, 0, 0.05);
      border-radius: 5px;
    }

    .flash-message {
      max-width: 600px;
      margin: 0 auto 15px;
    }

    .table-wrapper {
      overflow-x: auto;
      padding: 10px;
    }

    .col-md-12 h5 {
      color: #0d6efd;
      font-size: 20px;
    }

    .book-copies {
      font-size: 0.9rem;
      line-height: 1.4;
    }

    .book-copies span {
      display: inline-block;
      background: #e9ecef;
      padding: 3px 6px;
      margin: 2px;
      border-radius: 3px;
    }

    .author-badge {
      display: inline-block;
      background-color: #e2e3e5;
      color: #000;
      padding: 3px 6px;
      margin: 1px;
      border-radius: 4px;
      font-size: 0.85rem;
    }

    .dot {
      height: 10px;
      width: 10px;
      border-radius: 50%;
      display: inline-block;
      margin-right: 5px;
    }

    .dot.green {
      background-color: #28a745;
      /* Available */
    }

    .dot.orange {
      background-color: #ffc107;
      /* Issued */
    }

    .dot.red {
      background-color: #dc3545;
      /* Damaged */
    }

    .dot.grey {
      background-color: grey;
      /* Lost */
    }

    .dot.black {
      background-color: black;
      /* NA */
    }

    /* --- Compact number columns --- */
    .table td.copies-count {
      text-align: center;
      font-weight: 600;
      width: 60px;
    }

    /* --- Book Name & Authors readable --- */
    .table td.book-name,
    .table td.authors {
      white-space: normal !important;
      max-width: 220px;
      word-break: break-word;
    }

    /* --- Accession Nos and Book IDs --- */
    /* --- Accession Nos and Book Copy IDs: balanced layout --- */
    .table td.book-copies-scroll {
      white-space: normal;
      word-break: break-word;
      line-height: 1.4;
      vertical-align: top;
    }

    /* Table layout fix to auto-balance wide text cells */
    #booksTable {
      table-layout: auto;
      width: 100%;
    }

    #booksTable th:nth-child(4),
    /* Acc No */
    #booksTable td:nth-child(4) {
      width: 15%;
      min-width: 150px;
    }

    #booksTable th:nth-child(7),
    /* Book Copy IDs */
    #booksTable td:nth-child(7) {
      width: 28%;
      min-width: 250px;
    }

    .book-copies-scroll span {
      display: inline-block;
      background: #f1f3f4;
      padding: 3px 6px;
      margin: 2px;
      border-radius: 4px;
      font-size: 0.8rem;
    }

    /* --- Dot alignment --- */
    .dot {
      height: 10px;
      width: 10px;
      border-radius: 50%;
      display: inline-block;
      margin-right: 4px;
      vertical-align: middle;
    }

    /* --- Action buttons: 2×2 layout --- */
    .actions-grid {
      display: grid;
      grid-template-columns: repeat(2, 1fr);
      gap: 4px;
    }

    .actions-grid .btn {
      font-size: 0.75rem;
      padding: 3px 5px;
    }
  </style>
</head>

<body>
  <div class="d-flex flex-column min-vh-100">
    <?php include('includes/header.php'); ?>

    <div class="main-content flex-grow-1">
      <div class="row pad-botm mb-4">
        <div class="col-md-12">
          <h5 class="header-line" style="color:#0d6efd;">MANAGE BOOKS</h5>
        </div>
      </div>

      <!-- Flash Messages -->
      <div class="row justify-content-center">
        <div class="col-md-8">
          <?php foreach (['error', 'msg', 'updatemsg', 'delmsg'] as $type): ?>
            <?php if (!empty($_SESSION[$type])): ?>
              <div class="alert alert-<?php echo ($type == 'error') ? 'danger' : 'success'; ?> flash-message">
                <strong><?php echo ucfirst($type); ?> :</strong> <?php echo htmlentities($_SESSION[$type]); ?>
                <?php $_SESSION[$type] = ""; ?>
              </div>
            <?php endif; ?>
          <?php endforeach; ?>
        </div>
      </div>

      <!-- Book Table -->
      <div class="card square-card mb-5">
        <div class="card-header bg-secondary text-white d-flex justify-content-between align-items-center">
          <div>
            <i class="fa fa-book mr-2"></i> Book List
          </div>
          <a href="add-book.php" class="btn btn-light btn-sm text-dark shadow-sm rounded-pill px-3">
            <i class="fa fa-plus-circle"></i> <b> ADD</b>
          </a>
        </div>



        <!-- ✅ Status Hint / Legend -->
        <div class="p-2 bg-light border-bottom">
          <small>
            <span class="badge badge-light border px-2 py-1 mr-2 text-dark">
              <span class="dot"
                style="background-color: green; height:12px; width:12px; display:inline-block; border-radius:50%;"></span>
              Available
            </span>
            <span class="badge badge-light border px-2 py-1 mr-2 text-dark">
              <span class="dot"
                style="background-color: orange; height:12px; width:12px; display:inline-block; border-radius:50%;"></span>
              Issued
            </span>
            <span class="badge badge-light border px-2 py-1 text-dark">
              <span class="dot"
                style="background-color: red; height:12px; width:12px; display:inline-block; border-radius:50%;"></span>
              Damaged
            </span>
            <span class="badge badge-light border px-2 py-1 text-dark">
              <span class="dot"
                style="background-color: grey; height:12px; width:12px; display:inline-block; border-radius:50%;"></span>
              Lost
            </span>
            <span class="badge badge-light border px-2 py-1 text-dark">
              <span class="dot"
                style="background-color: black; height:12px; width:12px; display:inline-block; border-radius:50%;"></span>
              NA
            </span>
          </small>
        </div>
        <!--  End of Hint -->

        <!-- FILTER SECTION -->
        <div class="px-3 pt-3 pb-1 bg-white border-bottom">
          <form method="GET" class="form-row">
            <div class="form-group col-md-3">
              <label>Search by Acc No</label>
              <input type="text" name="search_accno" value="<?= $_GET['search_accno'] ?? '' ?>" class="form-control">
            </div>

            <div class="form-group col-md-3">
              <label>Search by Book Name</label>
              <input type="text" name="search_bookname" value="<?= $_GET['search_bookname'] ?? '' ?>"
                class="form-control">
            </div>

            <div class="form-group col-md-2">
              <label>Department</label>
              <select name="department" class="form-control">
                <option value="">All</option>
                <?php
                $d = $dbh->query("SELECT department_id, department_name FROM tbldepartment ORDER BY department_name");
                while ($row = $d->fetch(PDO::FETCH_ASSOC)) {
                  $sel = ($_GET['department'] ?? '') == $row['department_id'] ? "selected" : "";
                  echo "<option value='{$row['department_id']}' $sel>{$row['department_name']}</option>";
                }
                ?>
              </select>
            </div>

            <div class="form-group col-md-2">
              <label>Degree</label>
              <select name="degree" class="form-control">
                <option value="">All</option>
                <?php
                $deg = $dbh->query("SELECT degree_id, degree_name FROM tbldegree ORDER BY degree_name");
                while ($row = $deg->fetch(PDO::FETCH_ASSOC)) {
                  $sel = ($_GET['degree'] ?? '') == $row['degree_id'] ? "selected" : "";
                  echo "<option value='{$row['degree_id']}' $sel>{$row['degree_name']}</option>";
                }
                ?>
              </select>
            </div>

            <div class="form-group col-md-2">
              <label>Shift</label>
              <select name="shift" class="form-control">
                <option value="">All</option>
                <option value="1" <?= (($_GET['shift'] ?? '') == "1" ? "selected" : "") ?>>I</option>
                <option value="2" <?= (($_GET['shift'] ?? '') == "2" ? "selected" : "") ?>>II</option>
              </select>
            </div>

            <div class="form-group col-md-2">
              <label>Status</label>
              <select name="status" class="form-control">
                <option value="">All</option>
                <option value="available" <?= (($_GET['status'] ?? '') == "available" ? "selected" : "") ?>>Available
                </option>
                <option value="issued" <?= (($_GET['status'] ?? '') == "issued" ? "selected" : "") ?>>Issued</option>
                <option value="damaged" <?= (($_GET['status'] ?? '') == "damaged" ? "selected" : "") ?>>Damaged</option>
                <option value="lost" <?= (($_GET['status'] ?? '') == "lost" ? "selected" : "") ?>>Lost</option>
                <option value="NA" <?= (($_GET['status'] ?? '') == "NA" ? "selected" : "") ?>>Condemned/NA</option>
              </select>
            </div>

            <div class="form-group col-md-2">
              <label>Category</label>
              <select name="category" class="form-control">
                <option value="">All</option>
                <?php
                $cat = $dbh->query("SELECT id, CategoryName FROM tblcategory ORDER BY CategoryName");
                while ($row = $cat->fetch(PDO::FETCH_ASSOC)) {
                  $sel = ($_GET['category'] ?? '') == $row['id'] ? "selected" : "";
                  echo "<option value='{$row['id']}' $sel>{$row['CategoryName']}</option>";
                }
                ?>
              </select>
            </div>

            <div class="form-group col-md-2">
              <label>Cupboard</label>
              <select name="cupboard" class="form-control">
                <option value="">All</option>
                <?php
                $rack = $dbh->query("SELECT DISTINCT rack_no FROM tbl_racks ORDER BY rack_no");
                while ($row = $rack->fetch(PDO::FETCH_ASSOC)) {
                  $sel = ($_GET['cupboard'] ?? '') == $row['rack_no'] ? "selected" : "";
                  echo "<option value='{$row['rack_no']}' $sel>Cupboard {$row['rack_no']}</option>";
                }
                ?>
              </select>
            </div>


            <div class="col-12">
              <div class="d-flex flex-column flex-md-row align-items-start align-items-md-center gap-2">
                <button class="btn btn-primary btn-sm">Apply Filters</button>
                <a href="manage-books.php" class="btn btn-secondary btn-sm">Reset</a>
                <?php 
$countSql = "SELECT COUNT(*) AS totalCopies
             FROM tblbookcopies c
             JOIN tblbooks b ON b.id = c.book_id
             WHERE 1=1";

$countParams = [];

// Department
if (!empty($_GET['department'])) {
  $countSql .= " AND b.department_id = :department";
  $countParams[':department'] = $_GET['department'];
}

// Degree
if (!empty($_GET['degree'])) {
  $countSql .= " AND b.degree_id = :degree";
  $countParams[':degree'] = $_GET['degree'];
}

// Category
if (!empty($_GET['category'])) {
  $countSql .= " AND b.Catid = :category";
  $countParams[':category'] = $_GET['category'];
}

// Cupboard (Rack)
if (!empty($_GET['cupboard'])) {
  $countSql .= " AND c.rack_id IN (SELECT id FROM tbl_racks WHERE rack_no = :cupboard)";
  $countParams[':cupboard'] = $_GET['cupboard'];
}

// Status
if (!empty($_GET['status'])) {
  $countSql .= " AND c.book_status = :status";
  $countParams[':status'] = $_GET['status'];
}

// Shift
if (!empty($_GET['shift'])) {
  $countSql .= " AND c.shift = :shift";
  $countParams[':shift'] = $_GET['shift'];
}

// --- NEW: Search Filters ---
$searchAcc = trim($_GET['search_accno'] ?? '');
$searchBook = trim($_GET['search_bookname'] ?? '');

if ($searchAcc !== "") {
    $countSql .= " AND c.accession_no LIKE :acc";
    $countParams[':acc'] = "%$searchAcc%";
}

if ($searchBook !== "") {
    $countSql .= " AND b.BookName LIKE :bname";
    $countParams[':bname'] = "%$searchBook%";
}

// Execute
$countQuery = $dbh->prepare($countSql);
$countQuery->execute($countParams);
$totalFilteredCopies = $countQuery->fetchColumn();
?>



                <span class="alert alert-info py-1 px-2 mb-0">
                  Total Book Copies Filtered: <strong><?= $totalFilteredCopies ?></strong>
                </span>

              </div>
            </div>

          </form>
        </div>

        <div class="card-body table-wrapper pt-1">

          <table id="booksTable" class="table table-bordered table-hover table-sm text-dark">
            <thead class="table-theme-header">
              <tr>
                <th>#</th>
                <th>Book Name</th>
                <th>Authors</th>
                <th>Acc No</th>
                <th>Total Copies</th>
                <th>Available Copies</th>
                <th>Book Copies IDs</th>
                <th>Action</th>
              </tr>
            </thead>
            <tbody>
              <?php
              $sql = "SELECT 
  b.id AS bookid,
  b.BookName,
  b.AuthorIds,
  b.current_count,
  b.total_count,
  GROUP_CONCAT(c.book_no ORDER BY c.book_no SEPARATOR ', ') AS bookCopies,
  GROUP_CONCAT(c.accession_no ORDER BY c.accession_no SEPARATOR ', ') AS bookCopiesAcc_no
FROM tblbooks b
LEFT JOIN tblbookcopies c ON c.book_id = b.id
WHERE 1=1";

              $params = [];


              // -------------------------
              // SEARCH CONDITIONS
              // -------------------------
              $searchAcc = trim($_GET['search_accno'] ?? '');
              $searchBook = trim($_GET['search_bookname'] ?? '');

              $searchMode = false;

              if ($searchAcc !== "") {
                $sql .= " AND c.accession_no LIKE :acc";
                $params[':acc'] = "%$searchAcc%";
                $searchMode = true;
              }

              if ($searchBook !== "") {
                $sql .= " AND b.BookName LIKE :bname";
                $params[':bname'] = "%$searchBook%";
                $searchMode = true;
              }


              // -------------------------
              // OTHER FILTERS
              // -------------------------
              if (!empty($_GET['department'])) {
                $sql .= " AND b.department_id = :department";
                $params[':department'] = $_GET['department'];
              }

              if (!empty($_GET['degree'])) {
                $sql .= " AND b.degree_id = :degree";
                $params[':degree'] = $_GET['degree'];
              }

              if (!empty($_GET['category'])) {
                $sql .= " AND b.Catid = :category";
                $params[':category'] = $_GET['category'];
              }

              if (!empty($_GET['cupboard'])) {
                $sql .= " AND c.rack_id IN (SELECT id FROM tbl_racks WHERE rack_no = :cupboard)";
                $params[':cupboard'] = $_GET['cupboard'];
              }

              if (!empty($_GET['status'])) {
                $sql .= " AND c.book_status = :status";
                $params[':status'] = $_GET['status'];
              }

              if (!empty($_GET['shift'])) {
                $sql .= " AND c.shift = :shift";
                $params[':shift'] = $_GET['shift'];
              }


              // -------------------------
// GROUPING
// -------------------------
              $sql .= " GROUP BY b.id";


              // -------------------------
// LIMIT ONLY IF NO SEARCH
// -------------------------
              if (!$searchMode) {
                $sql .= " LIMIT 100";
              }

              // EXECUTE
              $query = $dbh->prepare($sql);
              $query->execute($params);
              $results = $query->fetchAll(PDO::FETCH_OBJ);


              $cnt = 1;



              // if ($query->rowCount() > 0):
              foreach ($results as $r):
                // Fetch author names based on stored IDs
                $authorNames = [];
                if (!empty($r->AuthorIds)) {
                  $authorSql = "SELECT AuthorName FROM tblauthors WHERE FIND_IN_SET(id, :ids)";
                  $authorQuery = $dbh->prepare($authorSql);
                  $authorQuery->bindParam(':ids', $r->AuthorIds, PDO::PARAM_STR);
                  $authorQuery->execute();
                  $authorResults = $authorQuery->fetchAll(PDO::FETCH_OBJ);
                  foreach ($authorResults as $a) {
                    $authorNames[] = $a->AuthorName;
                  }
                }
                ?>
                <tr>
                  <td><?= htmlentities($cnt++) ?></td>
                  <td class="book-name"><?= htmlentities($r->BookName) ?></td>
                  <td class="authors">
                    <?php
                    if (!empty($authorNames)) {
                      foreach ($authorNames as $author) {
                        echo "<span class='author-badge'>" . htmlentities($author) . "</span> ";
                      }
                    } else {
                      echo "<span class='text-muted'>No Authors</span>";
                    }
                    ?>
                  </td>
                  <td class="book-copies-scroll">
                    <?php
                    if (!empty($r->bookCopiesAcc_no)) {
                      $copies = explode(", ", $r->bookCopiesAcc_no);

                      // Fetch all statuses for these copies in a single query
                      $in = str_repeat('?,', count($copies) - 1) . '?';
                      $statusSql = "SELECT accession_no, book_status FROM tblbookcopies WHERE accession_no IN ($in)";
                      $statusQuery = $dbh->prepare($statusSql);
                      $statusQuery->execute($copies);
                      $statusResults = $statusQuery->fetchAll(PDO::FETCH_KEY_PAIR);
                      // FETCH_KEY_PAIR → returns [book_no => book_status]
                  
                      foreach ($copies as $copy) {
                        $status = strtolower($statusResults[$copy] ?? 'available'); // Default available if missing
                  
                        // Choose dot color & label based on status
                        if ($status === "issued") {
                          $dotColor = "orange";
                          $statusLabel = "Issued";
                        } elseif ($status === "damaged") {
                          $dotColor = "red";
                          $statusLabel = "Damaged";
                        } elseif ($status === "lost") {
                          $dotColor = "grey";
                          $statusLabel = "Lost";
                        } elseif ($status === "na") {
                          $dotColor = "black";
                          $statusLabel = "NA";
                        } else {
                          $dotColor = "green";
                          $statusLabel = "Available";
                        }

                        echo "<span style='white-space: nowrap; display: inline-block; margin-right: 6px;'>"
                          . "<span class='dot $dotColor' title='$statusLabel'></span> "
                          . htmlentities($copy)
                          . "</span>";

                      }
                    } else {
                      echo "<span class='text-muted'>No Copies</span>";
                    }
                    ?>
                  </td>
                  <td class="copies-count"><?= htmlentities($r->total_count) ?></td>
                  <td class="copies-count"><?= htmlentities($r->current_count) ?></td>


                  <td class="book-copies-scroll">
                    <?php
                    if (!empty($r->bookCopies)) {
                      $copies = explode(", ", $r->bookCopies);

                      // Fetch all statuses for these copies in a single query
                      $in = str_repeat('?,', count($copies) - 1) . '?';
                      $statusSql = "SELECT book_no, book_status FROM tblbookcopies WHERE book_no IN ($in)";
                      $statusQuery = $dbh->prepare($statusSql);
                      $statusQuery->execute($copies);
                      $statusResults = $statusQuery->fetchAll(PDO::FETCH_KEY_PAIR);
                      // FETCH_KEY_PAIR → returns [book_no => book_status]
                  
                      foreach ($copies as $copy) {
                        $status = strtolower($statusResults[$copy] ?? 'available'); // Default available if missing
                  
                        // Choose dot color & label based on status
                        if ($status === "issued") {
                          $dotColor = "orange";
                          $statusLabel = "Issued";
                        } elseif ($status === "damaged") {
                          $dotColor = "red";
                          $statusLabel = "Damaged";
                        } elseif ($status === "lost") {
                          $dotColor = "grey";
                          $statusLabel = "Lost";
                        } elseif ($status === "na") {
                          $dotColor = "black";
                          $statusLabel = "NA";
                        } else {
                          $dotColor = "green";
                          $statusLabel = "Available";
                        }

                        echo "<span style='white-space: nowrap; display: inline-block; margin-right: 6px;'>"
                          . "<span class='dot $dotColor' title='$statusLabel'></span> "
                          . htmlentities($copy)
                          . "</span>";

                      }
                    } else {
                      echo "<span class='text-muted'>No Copies</span>";
                    }
                    ?>
                  </td>






                  <!-- <td class="book-copies" data-search="<?= htmlentities($r->bookCopies) ?>">
                  <?php
                  // if (!empty($r->bookCopies)) {
                  //   $copies = explode(", ", $r->bookCopies);
                  //   foreach ($copies as $copy) {
                  //     echo "<span>".htmlentities($copy)."</span>";
                  //   }
                  // } else {
                  //   echo "<span class='text-muted'>No Copies</span>";
                  // }
                  ?> -->
                  <span style="display:none;"><?= htmlentities($r->bookCopies) ?></span>
                  </td>
                  <td class="actions-grid">
                    <a href="view-book-details.php?bookid=<?= (int) $r->bookid ?>" class="btn btn-sm btn-success mb-1">
                      <i class="fa fa-eye"></i> View
                    </a>
                    <a href="edit-book.php?bookid=<?= (int) $r->bookid ?>" class="btn btn-sm btn-primary mb-1">
                      <i class="fa fa-edit"></i> Edit
                    </a>
                    <a href="book-copies.php?bookid=<?= (int) $r->bookid ?>" class="btn btn-sm btn-info mb-1">
                      <i class="fa fa-layer-group"></i> Copies
                    </a>
                    <button class="btn btn-warning btn-sm" onclick="openPrintAllModal(<?= (int) $r->bookid ?>)">
                      <i class="fa fa-qrcode"></i> Print All QR
                    </button>


                    <!-- <a href="manage-books.php?del=<?= (int) $r->bookid ?>" onclick="return confirm('Delete this book?');"
                      class="btn btn-sm btn-danger">
                      <i class="fa fa-trash"></i> Delete
                    </a> -->
                  </td>
                </tr>
              <?php endforeach;


              // else:
// ?>
              <!-- <tr>
    <td colspan="8" class="text-center text-muted">No books found</td>
</tr> -->
              <?php
              // endif;
              ?>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>
  <div class="modal fade" id="printPositionModal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
      <form method="GET" action="print_qr_all_copies.php">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title">Select Label Position</h5>
            <button type="button" class="close" data-dismiss="modal">&times;</button>
          </div>
          <div class="modal-body">
            <input type="hidden" name="bookid" id="printAllBookId">
            <div class="form-group">
              <label>Row (1–8)</label>
              <input type="number" class="form-control" name="startRow" min="1" max="8" required>
            </div>
            <div class="form-group">
              <label>Column (1–3)</label>
              <input type="number" class="form-control" name="startCol" min="1" max="3" required>
            </div>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
            <button type="submit" class="btn btn-primary">Print</button>
          </div>
        </div>
      </form>
    </div>
  </div>

  <?php include('includes/footer.php'); ?>

  <!-- jQuery -->
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <!-- Bootstrap JS -->
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>
  <!-- DataTables -->
  <script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
  <script src="https://cdn.datatables.net/1.13.6/js/dataTables.bootstrap4.min.js"></script>

  <script>
    $(document).ready(function () {
      $('#booksTable').DataTable({
        pageLength: 10,
        lengthChange: true,
        ordering: true,
        responsive: true,
        language: {
          search: "🔍 Search books:"
        }
      });
    });
    function openPrintAllModal(bookId) {
      document.getElementById("printAllBookId").value = bookId;
      $("#printPositionModal").modal("show");
    }

  </script>

</body>

</html>