<?php
require_once 'databaseConnection.php';
$mysqli = db_connect();

if ($_SERVER["REQUEST_METHOD"] === "POST") {
    $proformaNumber = trim($_POST['proformaNumber']);
    $jobNumber = trim($_POST['jobNumber'] ?? '');

    if (empty($proformaNumber) || empty($jobNumber)) {
        echo "empty";
        exit;
    }

    // Check for duplicate
    $stmt = $mysqli->prepare("SELECT id FROM proformaRegistry WHERE proformaNumber = ?");
    $stmt->bind_param("s", $proformaNumber);
    $stmt->execute();
    $stmt->store_result();

    if ($stmt->num_rows > 0) {
        echo "duplicate";
        exit;
    }
    $stmt->close();

    // Insert with both proformaNumber and jobNumber
    $stmt = $mysqli->prepare("INSERT INTO proformaRegistry (proformaNumber, jobNumber) VALUES (?, ?)");
    $stmt->bind_param("ss", $proformaNumber, $jobNumber);
    if ($stmt->execute()) {
        echo "success";
    } else {
        echo "error";
    }

    $stmt->close();
    $mysqli->close();
}
?>
