<?php
require_once "db.php";

$invoice = trim($_GET['invoice'] ?? '');
$result = [];

if($invoice){
    $stmt = $conn->prepare("SELECT s.sale_id, s.invoice_no, s.due, s.customer_id, cu.customer_name 
                            FROM sales s 
                            JOIN customers cu ON s.customer_id = cu.customer_id
                            WHERE s.invoice_no LIKE ?");
    $likeInvoice = "%$invoice%";
    $stmt->bind_param("s", $likeInvoice);
    $stmt->execute();
    $res = $stmt->get_result();

    while($row = $res->fetch_assoc()){
        $result[] = $row;
    }
}

header('Content-Type: application/json');
echo json_encode($result);
