<?php
session_start();
include "koneksi.php";

$q = isset($_GET['q']) ? trim($_GET['q']) : '';

$where = "1=1";
if ($q !== '') {
    $q = $koneksi->real_escape_string($q);
    $where .= " AND (nama_mitra LIKE '%$q%' OR whatsapp LIKE '%$q%' OR alamat LIKE '%$q%')";
}

$sql = "SELECT * FROM kontak_mitra WHERE $where ORDER BY id DESC";
$res = mysqli_query($koneksi, $sql);

$data = [];
while($row = mysqli_fetch_assoc($res)){
    $data[] = $row;
}

header("Content-Type: application/json");
echo json_encode($data);
