Files
maria_uslugi/public/praticien/clients/ajax_list.php
T
2026-03-05 10:25:39 +01:00

109 lines
3.6 KiB
PHP

<?php
require_once __DIR__ . '/../../includes/init_praticien.php';
header('Content-Type: application/json; charset=utf-8');
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
header('Pragma: no-cache');
$praticien_id = (int)($_SESSION['reference_id'] ?? 0);
if ($praticien_id <= 0) {
http_response_code(401);
echo json_encode(['ok'=>false,'message'=>'Unauthorized']); exit;
}
$qRaw = trim((string)($_GET['q'] ?? ''));
$hasQ = ($qRaw !== '');
$qLike = '%'.$qRaw.'%';
$page = max(1, (int)($_GET['page'] ?? 1));
$limit = 10;
$offset= ($page-1)*$limit;
try {
// ----- TOTAL
$sqlTotal = "SELECT COUNT(*) FROM client_dossiers d
JOIN client c ON c.id=d.client_id
WHERE d.praticien_id=:p AND d.archived_at IS NULL";
$paramsT = [':p'=>$praticien_id];
if ($hasQ) {
$sqlTotal .= " AND (
c.nom LIKE :q_nom
OR c.prenom LIKE :q_prenom
OR c.email LIKE :q_email
OR c.telephone LIKE :q_tel
OR d.tags LIKE :q_tags
)";
$paramsT[':q_nom'] = $qLike;
$paramsT[':q_prenom'] = $qLike;
$paramsT[':q_email'] = $qLike;
$paramsT[':q_tel'] = $qLike;
$paramsT[':q_tags'] = $qLike;
}
$stT = $pdo->prepare($sqlTotal);
foreach ($paramsT as $k=>$v) {
$stT->bindValue($k, $v, $k===':p' ? PDO::PARAM_INT : PDO::PARAM_STR);
}
$stT->execute();
$total = (int)$stT->fetchColumn();
// ----- PAGE
$sql = "SELECT d.id AS dossier_id, d.tags,
c.nom, c.prenom, c.email, c.telephone,
(SELECT MAX(rv.date_rdv) FROM rendez_vous rv
WHERE rv.client_id=d.client_id AND rv.praticien_id=d.praticien_id
AND rv.statut IN ('confirmé','terminé')) AS last_rdv,
(SELECT LEFT(s.prochaine_action, 120) FROM client_suivi s
WHERE s.dossier_id=d.id ORDER BY s.date_rdv DESC, s.id DESC LIMIT 1) AS last_note
FROM client_dossiers d
JOIN client c ON c.id=d.client_id
WHERE d.praticien_id=:p AND d.archived_at IS NULL";
$params = [':p'=>$praticien_id];
if ($hasQ) {
$sql .= " AND (
c.nom LIKE :q_nom
OR c.prenom LIKE :q_prenom
OR c.email LIKE :q_email
OR c.telephone LIKE :q_tel
OR d.tags LIKE :q_tags
)";
$params[':q_nom'] = $qLike;
$params[':q_prenom'] = $qLike;
$params[':q_email'] = $qLike;
$params[':q_tel'] = $qLike;
$params[':q_tags'] = $qLike;
}
// tri: d'abord ceux qui ont un last_rdv, puis par date desc, puis maj dossier
$sql .= " ORDER BY (last_rdv IS NULL) ASC, last_rdv DESC, d.updated_at DESC
LIMIT :lim OFFSET :off";
$st = $pdo->prepare($sql);
foreach ($params as $k=>$v) {
if ($k === ':p') $st->bindValue($k, $v, PDO::PARAM_INT);
else $st->bindValue($k, $v, PDO::PARAM_STR);
}
$st->bindValue(':lim', $limit, PDO::PARAM_INT);
$st->bindValue(':off', $offset, PDO::PARAM_INT);
$st->execute();
$items=[];
while($r=$st->fetch(PDO::FETCH_ASSOC)){
$items[]=[
'dossier_id' => (int)$r['dossier_id'],
'tags' => $r['tags'],
'client_name' => trim(($r['prenom'] ?? '').' '.($r['nom'] ?? '')),
'email' => $r['email'] ?? '',
'telephone' => $r['telephone'] ?? '',
'last_rdv_fmt' => !empty($r['last_rdv']) ? (new DateTime($r['last_rdv']))->format('d/m/Y H:i') : '',
'last_note' => $r['last_note'] ?? ''
];
}
echo json_encode(['ok'=>true,'items'=>$items,'total'=>$total,'limit'=>$limit]);
} catch (Throwable $e) {
error_log('clients/ajax_list error: '.$e->getMessage());
http_response_code(500);
echo json_encode(['ok'=>false,'message'=>'Erreur serveur']);
}