<?php
$photo=trim($student['profile_photo_path']??'');$totalAppointments=count($appointments??[]);
$detranUrl=normalize_http_url($studentSettings['detran_simulado_url']??'');$detranButtonText=trim((string)($studentSettings['detran_simulado_button_text']??'Prova Teórica'))?:'Prova Teórica';
$pendingPortalCharges=$pendingPortalCharges??[];
$googleMapsUrl=normalize_http_url($studentSettings['google_maps_url']??'');
$wazeUrl=normalize_http_url($studentSettings['waze_url']??'');
$studentAnnouncements=$studentAnnouncements??[];
?>
<main class="container py-4 py-md-5">
<div class="d-flex flex-column flex-md-row align-items-md-center justify-content-between gap-3 mb-4"><div><span class="badge text-bg-warning text-dark rounded-pill px-3 py-2 mb-2">Área do Aluno</span><h1 class="h3 fw-bold text-cfc-dark mb-1">Olá, <?= e(short_person_name($student['name']??'Aluno')) ?></h1><p class="text-muted mb-0">Acesse rapidamente somente as áreas disponíveis para o seu cadastro.</p></div><div><?php if($photo!==''): ?><img src="<?= e(asset($photo)) ?>" 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></div>
<?php if($studentAnnouncements): ?>
<section class="student-notice-board mb-4" aria-label="Quadro de avisos">
    <div class="d-flex align-items-center gap-2 mb-3"><span class="notice-board-icon"><i class="bi bi-megaphone-fill"></i></span><div><h2 class="h5 fw-bold mb-0">Quadro de avisos</h2><small class="text-muted">Comunicados da administração</small></div></div>
    <div class="d-grid gap-3"><?php foreach($studentAnnouncements as $notice): ?><div class="alert alert-<?= e($notice['style'] ?: 'info') ?> rounded-4 border-0 shadow-sm mb-0"><strong class="d-block mb-1"><?= e($notice['title']) ?></strong><div><?= nl2br(e($notice['message'])) ?></div></div><?php endforeach; ?></div>
</section>
<?php endif; ?>
<?php if($pendingPortalCharges): $first=$pendingPortalCharges[0]; ?><div class="alert alert-warning border-0 shadow-soft rounded-4 d-flex flex-column flex-md-row justify-content-between align-items-md-center gap-3 mb-4"><div><strong><i class="bi bi-bell-fill"></i> Nova cobrança disponível</strong><span class="d-block mt-1"><?= e($first['title']??'Cobrança') ?> · <?= e(format_money_br($first['amount']??0)) ?></span></div><a class="btn btn-warning rounded-pill px-4" href="<?= e(url('portal/payments')) ?>"><i class="bi bi-bank"></i> Abrir cobranças</a></div><?php endif; ?>
<div class="row g-4 mb-4">
<div class="col-sm-6 col-xl-3"><a class="dashboard-action-card" href="<?= e(url('lessons')) ?>"><span class="action-icon text-bg-primary"><i class="bi bi-calendar2-check"></i></span><strong>Minhas aulas</strong><small><?= (int)$totalAppointments ?> agendamento(s), separados por mês</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')) ?>"><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 <?= !empty($unreadAudioMessages)?'audio-notification-pulse':'' ?>" href="<?= e(url('portal/messages')) ?>"><span class="action-icon text-bg-danger"><i class="bi bi-chat-dots"></i></span><strong>Mensagens</strong><small><?= (int)($unreadMessages??0) ?> nova(s)<?php if(!empty($unreadAudioMessages)): ?> · <?= (int)$unreadAudioMessages ?> áudio(s) novo(s)<?php endif; ?> · código <?= e($student['internal_code']??'---------') ?></small></a></div>
<?php if(!empty($portalCharges)): ?><div class="col-sm-6 col-xl-3"><a class="dashboard-action-card" href="<?= e(url('portal/payments')) ?>"><span class="action-icon text-bg-warning"><i class="bi bi-bank"></i></span><strong>Bancos / cobranças</strong><small><?= count($portalCharges) ?> cobrança(s)</small></a></div><?php endif; ?>
<?php if(($availableSlotCount??0)>0): ?><div class="col-sm-6 col-xl-3"><a class="dashboard-action-card" href="<?= e(url('calendar')) ?>"><span class="action-icon text-bg-info"><i class="bi bi-calendar-plus"></i></span><strong>Agendar aula</strong><small><?= (int)$availableSlotCount ?> horário(s) disponível(is)</small></a></div><?php endif; ?>
<div class="col-sm-6 col-xl-3"><a class="dashboard-action-card" href="<?= e($detranUrl!==''?$detranUrl:url('theory')) ?>"<?= $detranUrl!==''?' target="_blank" rel="noopener"':'' ?>><span class="action-icon text-bg-dark"><i class="bi bi-journal-check"></i></span><strong><?= e($detranButtonText) ?></strong><small><?= $detranUrl!==''?'Abrir o simulado do DETRAN':'Acessar a prova teórica' ?></small></a></div>
<?php if($googleMapsUrl!==''||$wazeUrl!==''): ?><div class="col-sm-6 col-xl-3"><div class="dashboard-action-card location-action-card"><span class="action-icon text-bg-primary"><i class="bi bi-geo-alt-fill"></i></span><strong>Como chegar</strong><small class="mb-2">Abra a localização do estabelecimento</small><div class="d-flex flex-wrap justify-content-center gap-2"><?php if($googleMapsUrl!==''): ?><a target="_blank" rel="noopener" class="btn btn-sm btn-primary rounded-pill" href="<?= e($googleMapsUrl) ?>"><i class="bi bi-map"></i> Google Maps</a><?php endif; ?><?php if($wazeUrl!==''): ?><a target="_blank" rel="noopener" class="btn btn-sm btn-info rounded-pill text-dark" href="<?= e($wazeUrl) ?>"><i class="bi bi-sign-turn-right-fill"></i> Waze</a><?php endif; ?></div></div></div><?php endif; ?>
<?php if(($ebookCount??0)>0): ?><div class="col-sm-6 col-xl-3"><a class="dashboard-action-card" href="<?= e(url('ebooks')) ?>"><span class="action-icon text-bg-secondary"><i class="bi bi-book"></i></span><strong>E-books / PDFs</strong><small><?= (int)$ebookCount ?> material(is) liberado(s)</small></a></div><?php endif; ?>
<div class="col-sm-6 col-xl-3"><a class="dashboard-action-card" href="<?= e(url('portal/contacts')) ?>"><span class="action-icon text-bg-info"><i class="bi bi-person-lines-fill"></i></span><strong>Contatos</strong><small>Abra uma conversa pelo sistema</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')) ?>"><span class="action-icon text-bg-success"><i class="bi bi-people"></i></span><strong>Grupo teórico</strong><small><?= (int)$groupCount ?> grupo(s) disponível(is)</small></a></div><?php endif; ?>
</div>
<div class="row g-4">
<div class="col-lg-5"><div class="card border-0 shadow-soft rounded-4 h-100"><div class="card-body p-4 text-center"><h2 class="h5 fw-bold mb-3"><i class="bi bi-person-circle"></i> Foto de perfil</h2><p class="text-muted small">Imagens grandes são reduzidas automaticamente para funcionar no celular e respeitar o limite da hospedagem.</p><form method="post" action="<?= e(url('profile/photo')) ?>" enctype="multipart/form-data" id="profilePhotoForm" data-image-compression="manual"><?= CSRF::input() ?><input type="file" name="profile_photo" id="profilePhotoInput" class="d-none" accept="image/jpeg,image/png,image/webp" required><button type="button" class="btn btn-primary rounded-pill px-4" id="chooseProfilePhoto"><i class="bi bi-camera"></i> Escolher e atualizar foto</button><small id="profilePhotoStatus" class="d-block text-muted mt-2"></small></form></div></div></div>
<div class="col-lg-7"><div class="card border-0 shadow-soft rounded-4 h-100"><div class="card-body p-4"><h2 class="h5 fw-bold mb-3"><i class="bi bi-speedometer2"></i> Meu nível de aprendizado por categoria</h2><?php if(empty($learningLevels)): ?><div class="alert alert-light border rounded-4 mb-0">Nenhuma classificação registrada ainda.</div><?php else: ?><div class="row g-3"><?php foreach($learningLevels as $level): ?><div class="col-md-6"><div class="border rounded-4 p-3 h-100"><strong class="d-block mb-1"><?= e(student_learning_category_label($level['category_key'])) ?></strong><span class="badge text-bg-primary mb-2"><?= e(student_level_label($level['level_value'])) ?></span><small class="d-block text-muted">Instrutor: <?= e(short_person_name($level['professional_name'])) ?></small><?php if(!empty($level['note'])): ?><p class="small text-muted mb-0 mt-2"><?= e($level['note']) ?></p><?php endif; ?></div></div><?php endforeach; ?></div><?php endif; ?></div></div></div>
</div>
</main>
<script>
(() => {
    const button=document.getElementById('chooseProfilePhoto');
    const input=document.getElementById('profilePhotoInput');
    const form=document.getElementById('profilePhotoForm');
    const status=document.getElementById('profilePhotoStatus');
    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>
