<?php $base=array_filter(['token'=>$token]); $professionalPhoto=trim((string)($professional['profile_photo_path']??'')); ?>
<main class="container py-4 py-md-5"><div class="d-flex flex-column flex-md-row justify-content-between align-items-md-center gap-3 mb-4"><div class="d-flex align-items-center gap-3"><?php if($professionalPhoto!==''): ?><img src="<?= e(asset($professionalPhoto)) ?>" class="professional-profile-photo" alt="Foto de <?= e($professional['name']) ?>"><?php else: ?><div class="professional-profile-photo profile-placeholder"><i class="bi bi-person-fill"></i></div><?php endif; ?><div><span class="badge text-bg-warning text-dark rounded-pill px-3 py-2 mb-2">Painel do Instrutor</span><h1 class="h3 fw-bold text-cfc-dark mb-1"><?= e($professional['name']) ?></h1><p class="text-muted mb-0">Código de mensagens: <strong><?= e($professional['internal_code']??'---------') ?></strong></p></div></div><?php if($token===''): ?><a class="btn btn-outline-danger rounded-pill" href="<?= e(url('professional/logout')) ?>"><i class="bi bi-box-arrow-right"></i> Sair</a><?php endif; ?></div>
<?php if(!empty($daysOff)): ?>
<div class="alert alert-warning rounded-4 shadow-soft border-0 mb-4"><div class="d-flex gap-3 align-items-start"><i class="bi bi-calendar-x-fill fs-4"></i><div><strong>Suas próximas folgas</strong><div class="d-flex flex-wrap gap-2 mt-2"><?php foreach(array_slice($daysOff,0,8) as $dayOff): ?><span class="badge rounded-pill text-bg-light border text-dark"><?= e(date('d/m/Y',strtotime($dayOff['occurrence_date']))) ?> · <?= e(weekday_pt_full($dayOff['occurrence_date'])) ?><?= (int)($dayOff['recurrence_days']??0)>0 ? ' · recorrência '.(int)$dayOff['recurrence_days'].' dias' : '' ?><?= !empty($dayOff['note']) ? ' · '.e($dayOff['note']) : '' ?></span><?php endforeach; ?></div><small class="d-block mt-2">Nessas datas o sistema bloqueia novos agendamentos para você.</small></div></div></div>
<?php endif; ?>
<?php $nextWeekHolidays=[]; $today=new DateTimeImmutable('today'); foreach(Holiday::activeAll() as $holiday){for($i=0;$i<7;$i++){ $d=$today->modify('+'.$i.' days')->format('Y-m-d'); if(Holiday::appliesToDate($holiday,$d)){ $nextWeekHolidays[$d]=$holiday; }}} ksort($nextWeekHolidays); ?>
<?php if(!empty($nextWeekHolidays)): ?><div class="alert alert-info rounded-4 shadow-soft border-0 mb-4"><div class="d-flex gap-3 align-items-start"><i class="bi bi-calendar-heart-fill fs-4"></i><div><strong>Atenção: feriado nesta semana</strong><div class="d-flex flex-wrap gap-2 mt-2"><?php foreach($nextWeekHolidays as $date=>$holiday): ?><span class="badge rounded-pill text-bg-light border text-dark"><?= e(date('d/m/Y',strtotime($date))) ?> · <?= e(weekday_pt_full($date)) ?> · <?= e($holiday['name']) ?></span><?php endforeach; ?></div><small class="d-block mt-2">Confira sua agenda: novos agendamentos ficam bloqueados em feriados cadastrados.</small></div></div></div><?php endif; ?>
<div class="row g-4 mb-4">
<div class="col-sm-6 col-xl-3"><a class="dashboard-action-card <?= !empty($overdueConfirmationCount)?'lesson-confirmation-pulse':'' ?>" href="<?= e(url('professional/appointments',array_merge($base,['month'=>date('Y-m')]))) ?>"><span class="action-icon text-bg-primary"><i class="bi bi-folder2-open"></i></span><strong>Quantidade de aulas</strong><small><?= (int)($counters['done_count']??0) ?> concluída(s) · <?= (int)($counters['future_count']??0) ?> futura(s) · <?= (int)($counters['total_count']??0) ?> no histórico<?php if(!empty($overdueConfirmationCount)): ?><span class="d-block text-danger fw-bold mt-1"><i class="bi bi-exclamation-triangle-fill"></i> <?= (int)$overdueConfirmationCount ?> aguardando confirmação</span><?php endif; ?></small></a></div>
<div class="col-sm-6 col-xl-3"><a class="dashboard-action-card <?= !empty($portalUnreadAudioMessages)?'audio-notification-pulse':'' ?>" href="<?= e(url('portal/messages',$base)) ?>"><span class="action-icon text-bg-danger"><i class="bi bi-chat-dots"></i></span><strong>Mensagens</strong><small><?= (int)($portalUnreadMessages??0) ?> nova(s)<?php if(!empty($portalUnreadAudioMessages)): ?> · <?= (int)$portalUnreadAudioMessages ?> áudio(s) novo(s)<?php endif; ?></small></a></div>
<?php if(($videoCount??0)>0): ?><div class="col-sm-6 col-xl-3"><a class="dashboard-action-card" href="<?= e(url('portal/videos',$base)) ?>"><span class="action-icon text-bg-success"><i class="bi bi-play-btn"></i></span><strong>Videoaulas</strong><small><?= (int)$videoCount ?> conteúdo(s) liberado(s)</small></a></div><?php endif; ?>
<div class="col-sm-6 col-xl-3"><a class="dashboard-action-card" href="<?= e(url('professional/contacts',$base)) ?>"><span class="action-icon text-bg-success"><i class="bi bi-whatsapp"></i></span><strong>Contatos WhatsApp</strong><small><?= array_sum($contactCounts??[]) ?> contato(s) dos seus alunos</small></a></div>
<div class="col-sm-6 col-xl-3"><a class="dashboard-action-card" href="<?= e(url('portal/contacts',$base)) ?>"><span class="action-icon text-bg-info"><i class="bi bi-person-lines-fill"></i></span><strong>Contatos do sistema</strong><small>Converse com alunos e outros instrutores</small></a></div>
<div class="col-sm-6 col-xl-3"><a class="dashboard-action-card" href="<?= e(url('professional/chat-groups',$base)) ?>"><span class="action-icon text-bg-dark"><i class="bi bi-people-fill"></i></span><strong>Grupos de instrutores</strong><small><?= (int)($professionalGroupCount??0) ?> grupo(s) privado(s) · não visível ao administrador</small></a></div>
<?php if(($groupCount??0)>0): ?><div class="col-sm-6 col-xl-3"><a class="dashboard-action-card" href="<?= e(url('portal/messages',$base)) ?>"><span class="action-icon text-bg-dark"><i class="bi bi-people"></i></span><strong>Turmas teóricas</strong><small><?= (int)$groupCount ?> grupo(s) sob sua responsabilidade</small></a></div><?php endif; ?>
<?php if(!empty($professional['can_manage_theory_groups'])): ?><div class="col-sm-6 col-xl-3"><a class="dashboard-action-card" href="<?= e(url('professional/theory-groups',$base)) ?>"><span class="action-icon text-bg-warning"><i class="bi bi-people-fill"></i></span><strong>Criar e editar turmas</strong><small>Função liberada pela administração</small></a></div><?php endif; ?>
<?php if(!empty($professional['can_manage_theory_videos'])): ?><div class="col-sm-6 col-xl-3"><a class="dashboard-action-card" href="<?= e(url('professional/theory-videos',$base)) ?>"><span class="action-icon text-bg-success"><i class="bi bi-camera-video-fill"></i></span><strong>Vídeos das turmas</strong><small>Adicionar vídeos às turmas autorizadas</small></a></div><?php endif; ?>
</div>
<div class="card border-0 shadow-soft rounded-4 mb-4"><div class="card-body p-4"><div class="row align-items-center g-4"><div class="col-md-8"><h2 class="h5 fw-bold mb-2"><i class="bi bi-qr-code"></i> QR Code para cadastro de aluno</h2><p class="text-muted mb-2">Mostre este QR Code para o candidato. Ao apontar a câmera do celular, ele será levado diretamente para a área de cadastro.</p><span class="badge text-bg-light border text-dark">Link: <?= e(url('register')) ?></span></div><div class="col-md-4 text-center"><img src="https://api.qrserver.com/v1/create-qr-code/?size=220x220&data=<?= urlencode(url('register')) ?>" alt="QR Code para cadastro de aluno" width="220" height="220" class="img-fluid rounded-3 border"><small class="d-block text-muted mt-2">Aponte a câmera do celular</small></div></div></div></div>
<div class="card border-0 shadow-soft rounded-4 mb-4"><div class="card-body p-4 text-center"><h2 class="h5 fw-bold mb-2"><i class="bi bi-camera"></i> Minha foto de perfil</h2><p class="text-muted small">A foto poderá ser vista pelo administrador e pelos alunos vinculados às suas aulas. Imagens grandes são reduzidas automaticamente antes do envio.</p><form method="post" action="<?= e(url('professional/profile/photo')) ?>" enctype="multipart/form-data" id="professionalPhotoForm" data-image-compression="manual"><?= CSRF::input() ?><input type="hidden" name="token" value="<?= e($token) ?>"><input type="file" name="profile_photo" id="professionalPhotoInput" class="d-none" accept="image/jpeg,image/png,image/webp" required><button type="button" class="btn btn-primary rounded-pill px-4" id="chooseProfessionalPhoto"><i class="bi bi-camera-fill"></i> Escolher e atualizar foto</button><small id="professionalPhotoStatus" class="d-block text-muted mt-2"></small></form></div></div>
<div class="card border-0 shadow-soft rounded-4"><div class="card-body p-4"><h2 class="h5 fw-bold mb-3"><i class="bi bi-file-earmark-pdf"></i> Materiais do instrutor</h2><?php if(empty($materials)): ?><div class="alert alert-light border rounded-4 mb-0">Nenhum material liberado.</div><?php else: ?><div class="row g-3"><?php foreach($materials as $m): ?><div class="col-md-6"><div class="border rounded-4 p-3 d-flex justify-content-between gap-3 align-items-center"><div><strong><?= e($m['title']) ?></strong><small class="d-block text-muted"><?= e($m['description']??'') ?></small></div><a target="_blank" class="btn btn-sm btn-primary rounded-pill" href="<?= e(url('professional/material-download',array_merge($base,['id'=>(int)$m['id']]))) ?>">Abrir</a></div></div><?php endforeach; ?></div><?php endif; ?></div></div></main>

<script>
(() => {
    const button=document.getElementById('chooseProfessionalPhoto');
    const input=document.getElementById('professionalPhotoInput');
    const form=document.getElementById('professionalPhotoForm');
    const status=document.getElementById('professionalPhotoStatus');
    button?.addEventListener('click',()=>input?.click());
    input?.addEventListener('change',async function(){
        if(!this.files?.length)return;
        button.disabled=true;
        status.textContent='Reduzindo e preparando a foto...';
        try{
            if(window.CFCImageTools)await window.CFCImageTools.prepareInput(this,{maxDimension:1200,targetBytes:450*1024});
            status.textContent='Enviando foto...';
            HTMLFormElement.prototype.submit.call(form);
        }catch(error){
            button.disabled=false;
            status.textContent=error?.message||'Não foi possível preparar a foto.';
        }
    });
})();
</script>
