<?php
/**
 * Test AJAX Search Members
 * Access: http://localhost/koperasi/simpanan/test_ajax_search.php?q=efendy
 */

require_once __DIR__ . '/../config/database.php';
$db = getDB();

$searchTerm = $_GET['q'] ?? '';
echo "<h2>Search Term: " . htmlspecialchars($searchTerm) . "</h2>";

$searchWhere = "WHERE a.status = 'aktif'";
$searchParams = [];

if ($searchTerm) {
    $searchWhere .= " AND (a.nama LIKE ? OR a.no_anggota LIKE ?)";
    $searchParams[] = "%$searchTerm%";
    $searchParams[] = "%$searchTerm%";
}

$searchQuery = "
    SELECT a.id, a.no_anggota, a.nama
    FROM anggota a
    $searchWhere
    ORDER BY a.nama ASC
    LIMIT 20
";

echo "<h3>Query:</h3><pre>" . htmlspecialchars($searchQuery) . "</pre>";
echo "<h3>Parameters:</h3><pre>" . print_r($searchParams, true) . "</pre>";

$stmt = $db->prepare($searchQuery);
$stmt->execute($searchParams);
$results = $stmt->fetchAll();

echo "<h3>Results (" . count($results) . "):</h3>";
echo "<pre>" . json_encode($results, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . "</pre>";

echo "<h3>Formatted Results:</h3>";
echo "<ul>";
foreach ($results as $r) {
    echo "<li>{$r['nama']} - {$r['no_anggota']}</li>";
}
echo "</ul>";
