<?php
require_once __DIR__ . '/../_pdo_db_connect.php';
require_once __DIR__ . '/../_response.php';
require_once __DIR__ . '/../middleware/auth_check.php';
$payload = require_auth();
$q = $_GET['q'] ?? null;
$status = $_GET['status'] ?? null;
$page = max(1,(int)($_GET['page']??1));
$per = min(100,(int)($_GET['per']??20));
$offset = ($page-1)*$per;
$sql = "SELECT id,name,phone,email,customer_status,created_at FROM sd_customers WHERE 1=1";
$params = [];
if ($q){ $sql .= " AND (name LIKE :q OR phone LIKE :q OR email LIKE :q)"; $params[':q']="%$q%"; }
if ($status){ $sql .= " AND customer_status = :s"; $params[':s']=$status; }
$sql .= " ORDER BY created_at DESC LIMIT :off,:per";
$pdo = get_db();
$stmt = $pdo->prepare($sql);
$stmt->bindValue(':off', (int)$offset, PDO::PARAM_INT);
$stmt->bindValue(':per', (int)$per, PDO::PARAM_INT);
foreach($params as $k=>$v) $stmt->bindValue($k,$v);
$stmt->execute();
$rows = $stmt->fetchAll();
json_res(['data'=>$rows]);
