<?php
$d = $data;
$hour = (int) date('G');
$hi = $hour < 12 ? 'Good morning' : ($hour < 17 ? 'Good afternoon' : 'Good evening');
$mkt = $d['marketing'] ?? [];
$kpis = [
    ['Today’s Tasks', (int) ($d['tasks_today'] ?? 0), 'bi-check2-square', '#22c55e', url('tasks'), 'tasks', 'View Tasks →'],
    ['Today’s Meetings', (int) ($d['meetings_today'] ?? 0), 'bi-calendar-event', '#3b82f6', url('calendar'), 'calendar', 'View Calendar →'],
    ['Open ' . opportunity_label(), (int) $d['open_opportunities'], 'bi-currency-dollar', '#8b5cf6', url('opportunities'), 'opportunities', 'View Pipeline →'],
    ['Revenue This Month', money_fmt($d['revenue_month']), 'bi-bullseye', '#f97316', url('opportunities'), 'opportunities', 'View Report →'],
    ['Emails (This Month)', (int) $d['emails_month'], 'bi-envelope', '#14b8a6', url('emails'), 'emails', 'View Emails →'],
];
?>
<div class="d-flex justify-content-between align-items-start mb-3 dash-head">
  <div>
    <h1 class="page-title"><?= e($hi) ?>, <?= e(current_user()['first_name']) ?>! <span class="dash-wave">👋</span></h1>
    <p class="page-sub mb-0">Here’s what’s happening today.</p>
  </div>
  <div class="d-flex flex-wrap align-items-center gap-2">
    <?php \App\Core\View::partial('smart_tips_jump', ['tips' => $tips ?? []]); ?>
    <div class="dash-date"><i class="bi bi-calendar3"></i> <?= date('M j, Y') ?></div>
  </div>
</div>
<?php if (dash_on('kpis')): ?>
<div class="kpi-row kpi-row-5 mb-3">
  <?php foreach ($kpis as $k):
    if (!can_see($k[5])) continue;
  ?>
    <div class="card-soft kpi kpi-compact">
      <div class="kpi-icon" style="background:<?= e($k[3]) ?>"><i class="bi <?= e($k[2]) ?>"></i></div>
      <div class="kpi-body">
        <span><?= e($k[0]) ?></span>
        <h3><?= is_string($k[1]) ? e($k[1]) : (int) $k[1] ?></h3>
        <a class="kpi-link" href="<?= e($k[4]) ?>"><?= e($k[6]) ?></a>
      </div>
    </div>
    <?php endforeach; ?>
</div>
<?php endif; ?>
<?php if (dash_on('tasks')): ?>
<div class="row g-3 mb-3">
  <div class="col-lg-6">
    <?php \App\Core\View::partial('home_tasks', ['tasks' => $d['tasks'] ?? [], 'mode' => 'today']); ?>
  </div>
  <div class="col-lg-6">
    <?php \App\Core\View::partial('home_tasks', ['tasks' => $d['tasks'] ?? [], 'mode' => 'overdue']); ?>
  </div>
</div>
<?php endif; ?>
<?php if (dash_on('upcoming') || dash_on('recent')): ?>
<div class="row g-3 mb-3">
  <?php if (dash_on('upcoming')): ?>
  <div class="col-lg-6">
    <?php \App\Core\View::partial('home_upcoming', ['meetings' => $d['meetings'] ?? []]); ?>
  </div>
  <?php endif; ?>
  <?php if (dash_on('recent')): ?>
  <div class="col-lg-6">
    <?php \App\Core\View::partial('home_recent', ['recent' => $d['recent'] ?? []]); ?>
  </div>
  <?php endif; ?>
</div>
<?php endif; ?>
<?php \App\Core\View::partial('home_work_lists', [
    'newLeads' => $d['new_lead_rows'] ?? [],
    'needsAction' => $d['needs_action'] ?? [],
    'riskDeals' => $d['risk_deals'] ?? [],
    'riskAccounts' => $d['risk_accounts'] ?? [],
    'mine' => true,
]); ?>
<div class="row g-3">
  <?php if (dash_on('marketing')): ?>
  <div class="col-lg-6">
    <?php \App\Core\View::partial('home_marketing', ['mkt' => $mkt]); ?>
  </div>
  <?php endif; ?>
  <?php if (can_see('service_visits') && !empty($d['visits'])): ?>
  <div class="col-lg-6">
    <div class="card-soft p-3">
      <div class="d-flex justify-content-between"><h2 class="h6 mb-3">Upcoming visits</h2><a href="<?= e(url('service')) ?>">View all</a></div>
      <?php foreach (array_slice($d['visits'], 0, 6) as $v): ?>
        <a class="d-flex justify-content-between py-2 text-decoration-none border-bottom gap-2" href="<?= e(url('service/' . (int) $v['id'])) ?>">
          <span class="text-truncate"><?= e(visit_subject($v)) ?></span>
          <span class="small text-muted text-nowrap"><?= !empty($v['next_visit_at']) ? e(dt_fmt($v['next_visit_at'], 'M j, g:i A')) : e(visit_status_label($v)['text']) ?></span>
        </a>
      <?php endforeach; ?>
    </div>
  </div>
  <?php endif; ?>
</div>
<?php \App\Core\View::partial('smart_tips', ['tips' => $tips ?? [], 'kind' => 'sales']); ?>
