<?php
include("../includes/db_connection.php");

$number = $_POST['number'] ?? '';
$id = intval($_POST['id'] ?? 0);

if($number == ''){
    echo "empty";
    exit;
}

if($id > 0){
    // Edit mode — exclude current record
    $stmt = $conn->prepare("SELECT id FROM certificates WHERE number=? AND id!=?");
    $stmt->bind_param("si", $number, $id);
}else{
    $stmt = $conn->prepare("SELECT id FROM certificates WHERE number=?");
    $stmt->bind_param("s", $number);
}

$stmt->execute();
$result = $stmt->get_result();

if($result->num_rows > 0){
    echo "exists";
}else{
    echo "available";
}
?>