<?php $e=static fn($v)=>htmlspecialchars((string)$v,ENT_QUOTES,'UTF-8');$date=$filters['date']??date('Y-m-d');$view=$filters['view']??'week';$grouped=[];foreach($appointments as $a)$grouped[$a['appointment_date']][]=$a; ?>
<style>.appt-toolbar{display:flex;gap:10px;flex-wrap:wrap;align-items:center;margin:16px 0}.appt-tabs{display:flex;background:#f3f5f8;border-radius:14px;padding:4px}.appt-tabs a{padding:9px 14px;border-radius:10px;text-decoration:none;color:#46515f;font-weight:700}.appt-tabs a.active{background:#fff;box-shadow:0 2px 8px rgba(0,0,0,.08);color:#17212b}.appt-day{margin:18px 0}.appt-day-title{display:flex;justify-content:space-between;align-items:center;margin-bottom:8px}.appt-card{display:grid;grid-template-columns:92px 1fr auto;gap:16px;align-items:center;padding:16px;margin-bottom:10px}.appt-time{font-weight:800;font-size:1.05rem}.appt-person strong{display:block;font-size:1rem}.appt-person small{display:block;color:#687585}.appt-status{font-size:.78rem;font-weight:800;padding:6px 9px;border-radius:999px;background:#eef2f5}.appt-status.confirmed{background:#e9f7ef}.appt-status.cancelled,.appt-status.no_show{background:#fbecec}.appt-status.completed{background:#eaf0fb}.appt-actions{display:flex;gap:7px}@media(max-width:700px){.appt-card{grid-template-columns:70px 1fr}.appt-actions{grid-column:2}.appt-toolbar{align-items:stretch}.appt-tabs{width:100%}.appt-tabs a{flex:1;text-align:center}.appt-card{padding:14px;gap:10px}}</style>
<section class="page-header"><div><p class="eyebrow">Schedule</p><h1>Appointments</h1><p><?= $e($pageDescription??'') ?></p></div><?php if(\App\Core\Auth::hasPermission('appointments.create')): ?><a class="btn btn-primary" href="/crm/public/appointments/create">+ New Appointment</a><?php endif; ?></section>
<?php if(!empty($message)): ?><div class="alert alert-success"><?= $e($message) ?></div><?php endif; ?><?php if(!empty($error)): ?><div class="alert alert-danger"><?= $e($error) ?></div><?php endif; ?>
<div class="appt-toolbar"><div class="appt-tabs"><?php foreach(['day'=>'Day','week'=>'Week','month'=>'Month'] as $k=>$label): ?><a class="<?= $view===$k?'active':'' ?>" href="/crm/public/appointments?view=<?= $k ?>&date=<?= $e($date) ?>"><?= $label ?></a><?php endforeach; ?></div><form method="get" class="inline-form"><input type="hidden" name="view" value="<?= $e($view) ?>"><input type="date" name="date" value="<?= $e($date) ?>"><select name="status"><option value="">All statuses</option><?php foreach(['scheduled','confirmed','completed','cancelled','no_show'] as $s): ?><option value="<?= $s ?>" <?= ($filters['status']??'')===$s?'selected':'' ?>><?= $e(ucwords(str_replace('_',' ',$s))) ?></option><?php endforeach; ?></select><button class="btn btn-secondary" type="submit">Apply</button></form></div>
<?php if(!$grouped): ?><div class="card" style="padding:30px;text-align:center;color:#667085">No appointments in this view.</div><?php else: foreach($grouped as $day=>$rows): ?><section class="appt-day"><div class="appt-day-title"><h2><?= $e(date('D, M j, Y',strtotime($day))) ?></h2><span><?= count($rows) ?> appointment<?= count($rows)!==1?'s':'' ?></span></div><?php foreach($rows as $a): ?><article class="card appt-card"><div class="appt-time"><?= $e(date('g:i A',strtotime($a['start_time']))) ?><br><small><?= $e(date('g:i A',strtotime($a['end_time']))) ?></small></div><div class="appt-person"><strong><?= $e($a['first_name'].' '.$a['last_name']) ?></strong><small><?= $e($a['patient_reference']) ?> · <?= $e($a['service_name']??'Clinical appointment') ?></small><small>Therapist: <?= $e($a['therapist_name']) ?></small></div><div><span class="appt-status <?= $e($a['status']) ?>"><?= $e(ucwords(str_replace('_',' ',$a['status']))) ?></span><div class="appt-actions" style="margin-top:8px"><?php if(\App\Core\Auth::hasRole('THERAPIST')): ?><?php if(in_array($a['status'],['scheduled','confirmed'],true)): ?><a class="btn btn-small" href="/crm/public/sessions/create?appointment_id=<?= (int)$a['id'] ?>">Session</a><form method="post" action="/crm/public/appointments/<?= (int)$a['id'] ?>/status" class="inline-form"><input type="hidden" name="_csrf" value="<?= $e($csrf) ?>"><input type="hidden" name="status" value="completed"><button class="btn btn-small" type="submit">Complete</button></form><form method="post" action="/crm/public/appointments/<?= (int)$a['id'] ?>/status" class="inline-form"><input type="hidden" name="_csrf" value="<?= $e($csrf) ?>"><input type="hidden" name="status" value="no_show"><button class="btn btn-small" type="submit">No-show</button></form><?php endif; ?><?php else: ?><a class="btn btn-secondary btn-small" href="/crm/public/appointments/<?= (int)$a['id'] ?>/edit">Open</a><?php if(in_array($a['status'],['scheduled','confirmed'],true)): ?><a class="btn btn-small" href="/crm/public/sessions/create?appointment_id=<?= (int)$a['id'] ?>">Session</a><form method="post" action="/crm/public/appointments/<?= (int)$a['id'] ?>/cancel" class="inline-form"><input type="hidden" name="_csrf" value="<?= $e($csrf) ?>"><button class="btn btn-small" type="submit">Cancel</button></form><?php endif; ?><?php endif; ?></div></div></article><?php endforeach; ?></section><?php endforeach; endif; ?>
