<?php
// --- check_status.php ---
include "../../connect.php";

header('Content-Type: application/json');

try {
    $invoiceNumber = filterRequest("invoice_number");

    if (empty($invoiceNumber)) {
        echo json_encode(["status" => "failure", "message" => "Invoice number is required."]);
        exit;
    }

    $stmt = $con->prepare("SELECT status FROM click_invoices WHERE invoice_number = :invoice_number LIMIT 1");
    $stmt->execute([':invoice_number' => $invoiceNumber]);
    $invoice = $stmt->fetch(PDO::FETCH_ASSOC);

    if ($invoice) {
        echo json_encode([
            "status" => "success",
            "invoice_status" => $invoice['status']
        ]);
    } else {
        echo json_encode(["status" => "failure", "message" => "Invoice not found."]);
    }

} catch (Exception $e) {
    error_log("Error in check_status.php: " . $e->getMessage());
    echo json_encode(["status" => "error", "message" => "Server error."]);
}
?>
