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

$customer_id = isset($_GET['customer_id']) ? intval($_GET['customer_id']) : 0;

if ($customer_id <= 0) {
    echo json_encode([]);
    exit;
}

$stmt = $conn->prepare("SELECT id, invoice, customer_id, type, name, father_name, email, phone1, phone2, address, cnic, note, granted, cnic_front, cnic_back, create_on 
                        FROM sale_guarantors WHERE customer_id = :customer_id");
$stmt->bindParam(':customer_id', $customer_id, PDO::PARAM_INT);
$stmt->execute();
$guarantors = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Prefix CNIC images path
foreach($guarantors as &$g) {
    if(!empty($g['cnic_front'])) $g['cnic_front'] = $g['cnic_front'];
    if(!empty($g['cnic_back']))  $g['cnic_back']  = $g['cnic_back'];
}

echo json_encode($guarantors);
?>
