<?php
$base=array_filter(['token'=>$token]);$monthDt=DateTime::createFromFormat('Y-m',$month)?:new DateTime('first day of this month');$prev=(clone $monthDt)->modify('-1 month')->format('Y-m');$next=(clone $monthDt)->modify('+1 month')->format('Y-m');
$counters=$counters??['done_count'=>0,'not_done_count'=>0,'future_count'=>0,'total_count'=>0];$names=[1=>'Janeiro',2=>'Fevereiro',3=>'Março',4=>'Abril',5=>'Maio',6=>'Junho',7=>'Julho',8=>'Agosto',9=>'Setembro',10=>'Outubro',11=>'Novembro',12=>'Dezembro'];$title=($names[(int)$monthDt->format('n')]??$monthDt->format('m')).' de '.$monthDt->format('Y');
?>
<main class="container py-4 py-md-5"><span class="badge text-bg-warning text-dark rounded-pill px-3 py-2 mb-3">Pasta de aulas agendadas</span><h1 class="h3 fw-bold"><?= e($title) ?></h1><p class="text-muted">Confirme 1 ou 2 aulas ministradas e veja foto, CPF e contato do aluno.</p><div class="d-flex flex-wrap gap-2 mb-4"><a class="btn btn-outline-primary" href="<?= e(url('professional/appointments',array_merge($base,['month'=>$prev]))) ?>"><i class="bi bi-chevron-left"></i> Mês anterior</a><a class="btn btn-outline-primary" href="<?= e(url('professional/appointments',array_merge($base,['month'=>$next]))) ?>">Próximo mês <i class="bi bi-chevron-right"></i></a><a class="btn btn-primary" href="<?= e(url('professional/panel',$base)) ?>">Painel</a></div>
<div class="row g-3 mb-4"><?php foreach([['Concluídas','done_count','success'],['Não feitas','not_done_count','danger'],['Futuras','future_count','primary'],['Total do mês','total_count','dark']] as $c): ?><div class="col-6 col-lg-3"><div class="card border-0 shadow-soft rounded-4 h-100"><div class="card-body p-4 text-center"><span class="text-muted"><?= e($c[0]) ?></span><strong class="d-block fs-2 text-<?= e($c[2]) ?>"><?= (int)$counters[$c[1]] ?></strong></div></div></div><?php endforeach; ?></div>
<?php if(empty($appointments)): ?><div class="alert alert-info rounded-4">Nenhuma aula nesta pasta mensal.</div><?php else: ?><div class="row g-4">
<?php foreach($appointments as $a): $absence=!empty($a['absence_notified_at']);$noShow=!empty($a['student_no_show_at']);$profAbsence=!empty($a['professional_absence_notified_at']);$ended=Appointment::canConfirmAfterScheduledEnd($a['end_at']??null);$scheduled=max(1,(int)($a['lesson_units']?:Appointment::lessonUnitsFromRange($a['start_at'],$a['end_at'])));$maxConfirm=min(2,$scheduled);$canConfirm=$a['status']==='booked'&&!$absence&&!$noShow&&!$profAbsence&&$ended&&!empty($a['lesson_category'])&&!empty($a['lesson_subject']);$levels=$learningLevelMap[(int)$a['student_id']]??[]; ?>
<div class="col-12"><div class="card border-0 shadow-soft rounded-4"><div class="card-body p-4"><div class="row g-4">
<div class="col-lg-3"><div class="d-flex gap-3 align-items-center"><?php if(!empty($a['profile_photo_path'])): ?><img src="<?= e(asset($a['profile_photo_path'])) ?>" class="profile-avatar" alt="Foto do aluno"><?php else: ?><div class="profile-avatar profile-placeholder"><i class="bi bi-person-fill"></i></div><?php endif; ?><div><h2 class="h5 fw-bold mb-1"><?= e($a['student_name']) ?></h2><small class="text-muted d-block">CPF: <strong><?= e(format_cpf($a['student_cpf']??'')) ?></strong></small><small class="text-muted d-block">Código: <?= e($a['student_internal_code']??'---------') ?></small><a target="_blank" class="btn btn-sm btn-success rounded-pill mt-2" href="<?= e(whatsapp_url($a['student_whatsapp']??'','Olá '.short_person_name($a['student_name']).', sobre sua aula agendada.')) ?>"><i class="bi bi-whatsapp"></i> WhatsApp</a></div></div></div>
<div class="col-lg-3"><strong class="d-block fs-5"><?= e(date('d/m/Y',strtotime($a['start_at']))) ?></strong><span><?= e(date('H:i',strtotime($a['start_at']))) ?>–<?= e(date('H:i',strtotime($a['end_at']))) ?></span><small class="d-block text-muted mt-2">Categoria <?= e($a['lesson_category']??'-') ?> · <?= e($a['lesson_subject']??'Conteúdo não informado') ?></small><span class="badge <?= $a['status']==='completed'?'text-bg-success':($a['status']==='booked'?'text-bg-primary':'text-bg-secondary') ?> mt-2"><?= e($a['status']==='completed'?'Concluída':($a['status']==='booked'?'Agendada':'Cancelada')) ?></span></div>
<div class="col-lg-3"><h3 class="h6 fw-bold">Nível por categoria</h3><?php if($levels): ?><div class="mb-2"><?php foreach($levels as $l): ?><span class="badge text-bg-light border d-block text-start mb-1"><?= e(student_learning_category_label($l['category_key'])) ?>: <?= e(student_level_label($l['level_value'])) ?> <small>(<?= e(short_person_name($l['professional_name'])) ?>)</small></span><?php endforeach; ?></div><?php endif; ?><form method="post" action="<?= e(url('professional/student-level')) ?>"><?= CSRF::input() ?><input type="hidden" name="student_id" value="<?= (int)$a['student_id'] ?>"><input type="hidden" name="token" value="<?= e($token) ?>"><input type="hidden" name="week" value="<?= e(date('Y-m-d',strtotime($a['start_at']))) ?>"><select name="learning_category" class="form-select form-select-sm mb-2" required><option value="">Categoria de aprendizado...</option><?php foreach(student_learning_category_options() as $k=>$label): ?><option value="<?= e($k) ?>"><?= e($label) ?></option><?php endforeach; ?></select><select name="student_level" class="form-select form-select-sm mb-2" required><option value="">Classificação...</option><?php foreach(student_level_options() as $k=>$label): if($k==='')continue; ?><option value="<?= e($k) ?>"><?= e($label) ?></option><?php endforeach; ?></select><textarea name="student_level_note" rows="2" class="form-control form-control-sm mb-2" placeholder="Observação"></textarea><button class="btn btn-sm btn-outline-primary rounded-pill">Salvar classificação</button></form></div>
<div class="col-lg-3"><h3 class="h6 fw-bold">Confirmação da aula</h3><?php if($a['status']==='completed'): ?><div class="alert alert-success rounded-3 py-2">Foram confirmadas <?= (int)$a['lesson_units'] ?> aula(s).</div><?php elseif($canConfirm): ?><form method="post" action="<?= e(url('professional/confirm-lesson')) ?>" onsubmit="return confirm('Confirmar a quantidade de aulas ministradas e descontar do saldo?');"><?= CSRF::input() ?><input type="hidden" name="appointment_id" value="<?= (int)$a['id'] ?>"><input type="hidden" name="token" value="<?= e($token) ?>"><input type="hidden" name="week" value="<?= e(date('Y-m-d',strtotime($a['start_at']))) ?>"><label class="form-label">Quantas aulas foram ministradas?</label><select name="confirmed_units" class="form-select mb-2" required><option value="1">1 aula (50 minutos)</option><?php if($maxConfirm>=2): ?><option value="2">2 aulas (100 minutos)</option><?php endif; ?></select><button class="btn btn-success rounded-pill w-100"><i class="bi bi-check2-circle"></i> Confirmar aula</button></form><?php elseif($a['status']==='booked'&&!$ended): ?><div class="alert alert-info rounded-3 py-2">Confirmação disponível após <?= e(date('d/m/Y H:i',strtotime($a['end_at']))) ?>.</div><?php elseif($a['status']==='booked'): ?><div class="alert alert-warning rounded-3 py-2">Verifique categoria, conteúdo ou ocorrência de ausência.</div><?php endif; ?></div>
</div>
<?php if($a['status']==='booked'): ?><hr><div class="row g-3"><div class="col-md-6"><?php if(!$profAbsence&&!$noShow): ?><form method="post" action="<?= e(url('professional/notify-absence')) ?>" class="d-flex gap-2" onsubmit="return confirm('Registrar apenas o aviso de ausência? A aula continuará agendada.');"><?= CSRF::input() ?><input type="hidden" name="appointment_id" value="<?= (int)$a['id'] ?>"><input type="hidden" name="token" value="<?= e($token) ?>"><input type="hidden" name="week" value="<?= e(date('Y-m-d',strtotime($a['start_at']))) ?>"><input name="reason" class="form-control form-control-sm" placeholder="Motivo opcional"><button class="btn btn-sm btn-warning">Não vou comparecer</button></form><?php endif; ?></div><div class="col-md-6"><?php if(!$absence&&!$noShow&&!$profAbsence&&Appointment::canRateByDate($a['start_at'])): ?><form method="post" action="<?= e(url('professional/mark-student-absence')) ?>" class="d-flex gap-2" onsubmit="return confirm('Marcar falta do aluno?');"><?= CSRF::input() ?><input type="hidden" name="appointment_id" value="<?= (int)$a['id'] ?>"><input type="hidden" name="token" value="<?= e($token) ?>"><input type="hidden" name="week" value="<?= e(date('Y-m-d',strtotime($a['start_at']))) ?>"><input name="reason" class="form-control form-control-sm" placeholder="Observação opcional"><button class="btn btn-sm btn-outline-danger">Aluno não veio</button></form><?php endif; ?></div></div><?php endif; ?>
</div></div></div><?php endforeach; ?></div><?php endif; ?></main>
