<?php
require_once '../config/db.php';
header('Content-Type: application/json');

$date = trim($_GET['date'] ?? '');
$invoice = trim($_GET['invoice_number'] ?? '');
$company = trim($_GET['company'] ?? '');

$sql = "SELECT purchase_date, invoice_number, supplier, total_amount, paid_amount, pending_amount
        FROM credit_puraching
        WHERE 1=1";
$params = [];

if ($date !== '') {
    $sql .= " AND DATE(purchase_date) = ?";
    $params[] = $date;
}
if ($invoice !== '') {
    $sql .= " AND invoice_number LIKE ?";
    $params[] = "%$invoice%";
}
if ($company !== '') {
    $sql .= " AND supplier LIKE ?";
    $params[] = "%$company%";
}

$sql .= " ORDER BY purchase_date DESC";
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
echo json_encode($stmt->fetchAll());