<?php
// delete_books.php
include('include/dbcon.php');
include('session.php'); // if you use session-based auth, optional but recommended

$ids = [];

// Single delete (via GET)
if (isset($_GET['book_id'])) {
    $ids[] = (int) $_GET['book_id'];
}

// Bulk delete (via POST)
if (!empty($_POST['book_ids']) && is_array($_POST['book_ids'])) {
    foreach ($_POST['book_ids'] as $bid) {
        $bid = (int) $bid;
        if ($bid > 0) {
            $ids[] = $bid;
        }
    }
}

// Remove duplicates
$ids = array_values(array_unique($ids));

// If nothing to delete, just go back
if (empty($ids)) {
    header('Location: book.php');
    exit;
}

// Build IN() list
$idList = implode(',', $ids);

// IMPORTANT: In production, you might also want to:
// - check if books are issued
// - restrict delete to admins

// Delete from `book` table
$query = "DELETE FROM book WHERE book_id IN ($idList)";
if (!mysqli_query($con, $query)) {
    die('Error deleting books: ' . mysqli_error($con));
}

// Redirect back to list
header('Location: book.php');
exit;
