<?php
$u=current_user();
$attention=dashboard_attention_counts($u);
$nav=navigation_items($u['role']);
$quickKeys=dashboard_quick_action_keys($u);
$cartCount=cart_count((int)$u['id']);
$carWashLive=isset($nav['car_wash'])&&car_wash_has_open_availability(7);
$homeNotices=home_manual_notifications((int)$u['id'],4);
?>
<?php render_platform_banners('dashboard'); ?>
<div class="grid-2 dashboard-top-grid">
  <section class="panel dashboard-welcome-card">
    <div class="panel-header"><div><h2>Olá, <?= e(explode(' ',$u['name'])[0]) ?> 👋</h2><p>Acesse seus recursos e acompanhe o que precisa de atenção.</p></div></div>
    <div class="profile-hero"><?= avatar_html($u) ?><div><h3 style="margin-bottom:5px"><?= e(role_label($u['role'])) ?></h3><p class="muted" style="margin:0"><?= e($u['company_name']?:$u['email']) ?></p></div></div>
    <form method="post" action="actions.php" style="margin-top:20px"><?= csrf_field() ?><input type="hidden" name="action" value="update_work_status"><input type="hidden" name="return_page" value="home"><div class="form-grid"><div class="form-group"><label>Status de trabalho</label><select name="work_status"><option value="available"<?= selected($u['work_status'],'available') ?>>Disponível</option><option value="busy"<?= selected($u['work_status'],'busy') ?>>Ocupado</option><option value="on_route"<?= selected($u['work_status'],'on_route') ?>>Em rota</option><option value="offline"<?= selected($u['work_status'],'offline') ?>>Offline</option></select></div><div class="form-group" style="justify-content:end"><button class="btn btn-secondary" type="submit">Atualizar status</button></div></div></form>
  </section>

  <section class="panel dashboard-quick-panel dashboard-home-quick-actions">
    <div class="panel-header"><div><h3>Ações rápidas</h3><p>Atalhos definidos para o seu perfil.</p></div></div>
    <div class="quick-actions quick-actions-dock">
      <?php foreach($quickKeys as $key):if(!isset($nav[$key]))continue;$item=$nav[$key];$count=(int)($attention[$key]??0);if($key==='cart')$count=$cartCount;?>
        <a class="quick-action<?= $count>0?' attention-bounce':'' ?><?= ($key==='car_wash'&&$carWashLive)?' car-wash-live':'' ?>" href="dashboard.php?page=<?= e($key) ?>"><span><?= $item[0] ?></span><b><?= e($item[1]) ?></b><?php if($count>0):?><em><?= $count ?></em><?php endif;?></a>
      <?php endforeach; ?>
    </div>
  </section>
</div>

<?php if($homeNotices):?>
<section class="panel home-notification-panel">
  <div class="panel-header"><div><span class="mini-label">AVISOS</span><h2>Comunicados para você</h2><p>Notificações publicadas pelo administrador ou pela sua empresa.</p></div></div>
  <div class="home-notification-grid">
    <?php foreach($homeNotices as $notice):?>
      <article class="home-notification-card home-notification-<?= e($notice['type']?:'info') ?><?= empty($notice['image'])?' home-notification-no-image':'' ?>">
        <?php if(!empty($notice['image'])):?><div class="home-notification-image"><img src="<?= e(media_url($notice['image'])) ?>" alt=""></div><?php endif;?>
        <div class="home-notification-copy"><div class="home-notification-title"><strong><?= e($notice['title']) ?></strong><span>🔔</span></div><p><?= e($notice['message']) ?></p><small><?= $notice['expires_at']?'Visível até '.e(date('d/m/Y H:i',strtotime($notice['expires_at']))):'Aviso sem prazo definido' ?></small></div>
      </article>
    <?php endforeach;?>
  </div>
</section>
<?php endif;?>

<?php
$dashboardNav = account_restricted($u) ? ['charges'=>['💳','Cobranças']] : $nav;
?>
<section class="panel mobile-dashboard-navigation">
  <div class="panel-header"><div><span class="mini-label">NAVEGAÇÃO</span><h2>Todos os recursos</h2><p>Escolha a área que deseja acessar.</p></div></div>
  <div class="mobile-nav-grid">
    <?php foreach($dashboardNav as $key=>$item): if($key==='home'||in_array($key,['cart','notifications','clinics','login_history','quick_actions','qr_scanner'],true))continue;$count=(int)($attention[$key]??0); ?>
      <a class="mobile-nav-card<?= $count>0?' attention-bounce':'' ?><?= ($key==='car_wash'&&$carWashLive)?' car-wash-live':'' ?>" href="dashboard.php?page=<?= e($key) ?>">
        <span class="mobile-nav-icon"><?= $item[0] ?></span>
        <strong><?= e($item[1]) ?></strong>
        <?php if($count>0):?><b><?= $count ?></b><?php endif;?>
      </a>
    <?php endforeach; ?>
        <a class="mobile-nav-card mobile-nav-card-danger" href="logout.php"><span class="mobile-nav-icon">↪</span><strong>Sair</strong></a>
  </div>
</section>
