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

241 lines
9.3 KiB
PHP

<?php
require_once __DIR__ . '/../includes/init_praticien.php';
$praticien_id = $_SESSION['reference_id'];
// --- Today en Europe/Paris ---
date_default_timezone_set('Europe/Paris');
$tz = new DateTimeZone('Europe/Paris');
$todayStart = (new DateTime('today', $tz))->setTime(0, 0, 0);
$todayEnd = (clone $todayStart)->setTime(23, 59, 59);
$today_start = $todayStart->format('Y-m-d H:i:s');
$today_end = $todayEnd->format('Y-m-d H:i:s');
$stmt = $pdo->prepare("
SELECT rv.*, c.nom AS nom_client, c.prenom AS prenom_client, c.email
FROM rendez_vous rv
JOIN client c ON rv.client_id = c.id
WHERE rv.praticien_id = ?
AND rv.date_rdv BETWEEN ? AND ?
ORDER BY rv.date_rdv ASC
");
$stmt->execute([$praticien_id, $today_start, $today_end]);
$rdvs = $stmt->fetchAll();
// Map statuts (valeurs BDD -> libellés traduits)
$statutMap = [
'prévu' => $translations['statut_prevu'] ?? 'Prévu',
'confirmé' => $translations['statut_confirme'] ?? 'Confirmé',
'annulé' => $translations['statut_annule'] ?? 'Annulé',
'terminé' => $translations['statut_termine'] ?? 'Terminé',
];
$PAGE_TITLE = $translations['titre_rdv_du_jour'] ?? 'Rendez-vous du jour';
$HEAD_EXTRA = <<<HTML
<script src="/js/sweetalert2@11.js"></script>
HTML;
require_once '../includes/header.php';
?>
<div class="main1">
<div class="container py-5">
<div class="shadow p-4 rounded bg-light">
<h2 class="mb-4 text-center"><?= htmlspecialchars($translations['titre_rdv_du_jour'] ?? 'Rendez-vous du jour') ?></h2>
<a href="/public/praticien/rendez_vous.php" class="btn btn-outline-secondary mb-4">
<?= htmlspecialchars($translations['retour_liste_complete'] ?? 'Retour à la liste complète') ?>
</a>
<div class="table-responsive">
<table class="table table-bordered table-hover align-middle text-center mobile-cards" id="table_rdv">
<thead class="table-light">
<tr>
<th><?= htmlspecialchars($translations['col_heure'] ?? 'Heure') ?></th>
<th><?= htmlspecialchars($translations['col_client'] ?? 'Client') ?></th>
<th><?= htmlspecialchars($translations['col_duree'] ?? 'Durée') ?></th>
<th><?= htmlspecialchars($translations['col_statut'] ?? 'Statut') ?></th>
<th><?= htmlspecialchars($translations['btn_chat'] ?? 'Chat') ?></th>
<th><?= htmlspecialchars($translations['status_online'] ?? 'Visio') ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($rdvs as $r):
$dt = new DateTime($r['date_rdv'], $tz);
$id = (int)$r['id'];
?>
<tr data-statut="<?= htmlspecialchars($r['statut']) ?>">
<td><?= htmlspecialchars($dt->format('H:i')) ?></td>
<td><?= htmlspecialchars($r['prenom_client'] . ' ' . $r['nom_client']) ?></td>
<td><?= (int)$r['duree_minutes'] . ' ' . htmlspecialchars($translations['min_abbr'] ?? 'min') ?></td>
<td>
<select class="form-select form-select-sm" onchange="changerStatut(<?= $id ?>, this.value, this)">
<?php foreach (['prévu','confirmé','annulé','terminé'] as $s): ?>
<option value="<?= $s ?>" <?= ($r['statut'] === $s ? 'selected' : '') ?>>
<?= htmlspecialchars($statutMap[$s] ?? ucfirst($s)) ?>
</option>
<?php endforeach; ?>
</select>
</td>
<!-- Chat -->
<td>
<a class="btn btn-outline-secondary btn-sm btn-table" href="/messages/start.php?client_id=<?= (int)$r['client_id'] ?>">
<?= htmlspecialchars($translations['chat_praticien'] ?? 'Ouvrir le chat') ?>
</a>
</td>
<!-- Visio / Cabinet -->
<td>
<?php
$lieu = strtolower(trim((string)($r['lieu'] ?? '')));
if ($lieu === 'online') {
// LOGIQUE VISIO (inchangée)
$statut = $r['statut']; // 'confirmé' | 'prévu' | 'annulé' | 'terminé'
$now = new DateTime('now', $tz);
$start = new DateTime($r['date_rdv'], $tz);
$open = (clone $start)->modify('-15 minutes');
$close = (clone $start)->modify('+' . (int)($r['duree_minutes'] ?? 120) . ' minutes');
if ($statut === 'confirmé') {
if ($now >= $open && $now <= $close) {
?>
<a href="/visio/join.php?rdv=<?= (int)$r['id'] ?>&start=1" class="btn btn-outline-secondary btn-sm btn-table">
<?= htmlspecialchars($translations['visio_nachalo'] ?? 'Démarrer la visio') ?>
</a>
<?php
} else {
?>
<small class="text-muted d-block mt-1">
<?php
if ($now < $open) echo htmlspecialchars(($translations['ouvert_a'] ?? 'Ouvert à') . ' ' . $open->format('H:i'));
elseif ($now > $close) echo htmlspecialchars(($translations['ferme_a'] ?? 'Fermé à') . ' ' . $close->format('H:i'));
else echo htmlspecialchars(($translations['prev_a'] ?? 'Prévu le') . ' ' . $start->format('d/m/Y \à H:i') . '.');
?>
</small>
<?php
}
} elseif ($statut === 'prévu') {
?>
<small class="text-danger d-block mt-1">
<?= htmlspecialchars($translations['votre_rdv_jdet'] ?? 'En attente de confirmation') ?>
</small>
<?php
} else {
?>
<small class="text-muted d-block mt-1">
<?= htmlspecialchars(($translations['zaverchen_a'] ?? 'Terminé') . ' ' . $close->format('H:i')) ?>
</small>
<?php
}
} else {
// OFFLINE : afficher juste le libellé "Очно"
echo '<span class="text-nowrap">'.htmlspecialchars($translations['option_cabinet'] ?? 'Очно').'</span>';
}
?>
</td>
</tr>
<?php endforeach; ?>
<?php if (empty($rdvs)): ?>
<tr>
<td colspan="6" class="text-muted">
<?= htmlspecialchars($translations['aucun_rdv_aujourdhui'] ?? "Aucun rendez-vous aujourd'hui") ?>
</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<script>
// --- Mise à jour du statut avec SweetAlert2 ---
// On gère JSON *et* fallback texte "OK" si l'endpoint n'est pas encore JSON.
function changerStatut(id, statut, selEl) {
fetch('/public/praticien/changer_statut.php', {
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
body: new URLSearchParams({ id: String(id), statut: String(statut) })
})
.then(async (r) => {
const raw = (await r.text()).trim();
let ok = false, message = '';
try {
const data = JSON.parse(raw);
ok = !!data.ok;
message = data.message || '';
} catch (_) {
// Fallback: si l'API renvoie juste "OK" ou un message en clair
if (raw.toUpperCase().includes('OK')) {
ok = true;
message = raw;
} else {
throw new Error(raw.slice(0, 180));
}
}
if (!ok) throw new Error(message || 'Erreur');
Swal.fire({
icon: 'success',
title: message || '<?= addslashes($translations['maj_ok'] ?? "Mise à jour réussie") ?>',
timer: 1800,
showConfirmButton: false
});
try {
const tr = selEl && selEl.closest('tr');
if (tr) tr.dataset.statut = statut;
} catch(e){}
})
.catch((e) => {
Swal.fire({
icon: 'error',
title: '<?= addslashes($translations['erreur_mise_a_jour'] ?? "Erreur de mise à jour") ?>',
text: e.message || '<?= addslashes($translations['erreur_mise_a_jour'] ?? "Erreur de mise à jour.") ?>',
confirmButtonText: 'OK'
});
});
}
</script>
<!--*********Adaptation des tables pour un affichage mobile***************-->
<script defer>
(function(){
function initMobileCards(tbl){
if (!tbl) return;
const heads = Array.from(tbl.querySelectorAll('thead th')).map(th => th.textContent.trim());
tbl.querySelectorAll('tbody tr').forEach(tr => {
Array.from(tr.children).forEach((td, i) => {
if (!td.hasAttribute('data-label')) {
td.setAttribute('data-label', heads[i] || '');
}
});
});
document.documentElement.classList.add('mc-ready');
}
function ready(fn){
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', fn);
else fn();
}
ready(function(){
const tbl = document.getElementById('table_rdv') || document.querySelector('table.mobile-cards');
initMobileCards(tbl);
});
})();
</script>
<!--*****************************FIN Affichage mobile************************-->
<footer class="site-footer">
<div class="container">
<p><?= $translations['copyr'] ?? '&copy; '.date('Y') ?></p>
</div>
</footer>
</body>
</html>