<?php
include('includes/config.php');

if (isset($_POST['isbn'])) {
    $isbn = $_POST['isbn'];
    $bookid = isset($_POST['bookid']) ? intval($_POST['bookid']) : 0;

    // Exclude current book if editing
    $sql = "SELECT id FROM tblbooks WHERE ISBNNumber = :isbn AND id != :bookid";
    $query = $dbh->prepare($sql);
    $query->bindParam(':isbn', $isbn, PDO::PARAM_STR);
    $query->bindParam(':bookid', $bookid, PDO::PARAM_INT);
    $query->execute();

    if ($query->rowCount() > 0) {
        echo "<span style='color:red;'>ISBN already exists ❌</span>";
    } else {
        echo "<span style='color:green;'>ISBN is available ✅</span>";
    }
}
?>
