<?php if ($nearingExpiry || $expiry['expired']): ?>
<div class="alert alert-<?= $expiry['expired'] ? 'danger' : 'warning' ?> d-flex align-items-center gap-2 mb-4">
  <i class="bi bi-exclamation-triangle"></i>
  <div>
    <?= $expiry['expired']
      ? 'Your password has expired. Please change it from your Profile page.'
      : "Your password expires in {$expiry['expires_in_days']} day(s). Consider changing it soon." ?>
  </div>
  <a href="<?= base_url('profile') ?>" class="btn btn-sm btn-light-soft ms-auto">Go to Profile</a>
</div>
<?php endif; ?>

<div class="ab-card mb-4">
  <div class="ab-card-body d-flex align-items-center gap-3">
    <div class="ab-avatar" style="width:56px; height:56px; font-size:1.2rem; border-radius:16px;">
      <?= e(mb_strtoupper(mb_substr($user['first_name'],0,1).mb_substr($user['last_name'] ?? '',0,1))) ?>
    </div>
    <div>
      <h2 class="mb-0">Welcome back, <?= e($user['first_name']) ?>.</h2>
      <div class="ab-text-muted small"><?= e($user['job_title'] ?? $user['role_name'] ?? '') ?></div>
    </div>
  </div>
</div>

<?php if ($overview !== null): ?>
<div class="row g-3 mb-3">
  <div class="col-md-3"><div class="ab-stat-card"><div class="ab-stat-label">Revenue (30 days)</div><div class="ab-stat-value"><?= e(money($overview['revenue']['total_revenue'])) ?></div></div></div>
  <div class="col-md-3"><div class="ab-stat-card"><div class="ab-stat-label">New Leads (30 days)</div><div class="ab-stat-value"><?= array_sum(array_column($overview['leads_pipeline']['stages'], 'count')) ?></div></div></div>
  <div class="col-md-3"><div class="ab-stat-card"><div class="ab-stat-label">Appointments (30 days)</div><div class="ab-stat-value"><?= array_sum(array_column($overview['appointments']['by_status'], 'count')) ?></div></div></div>
  <div class="col-md-3"><div class="ab-stat-card"><div class="ab-stat-label">Conversations (30 days)</div><div class="ab-stat-value"><?= array_sum(array_column($overview['conversations']['by_channel'], 'total')) ?></div></div></div>
</div>

<div class="row g-3 mb-3">
  <div class="col-md-7">
    <div class="ab-card h-100">
      <div class="ab-card-header"><h2>Lead Pipeline Snapshot</h2><a href="<?= base_url('reports?type=leads') ?>" class="small fw-semibold">Full report</a></div>
      <div class="ab-card-body">
        <?php $maxCount = max(1, max(array_column($overview['leads_pipeline']['stages'], 'count'))); ?>
        <?php foreach ($overview['leads_pipeline']['stages'] as $stage): ?>
        <div class="d-flex align-items-center gap-2 mb-2">
          <div class="small" style="width:100px;"><?= e($stage['stage']) ?></div>
          <div class="flex-fill bg-light rounded-pill" style="height:10px;"><div class="bg-primary rounded-pill" style="height:10px; width:<?= (int) round($stage['count'] / $maxCount * 100) ?>%;"></div></div>
          <div class="small fw-semibold" style="width:30px; text-align:right;"><?= (int) $stage['count'] ?></div>
        </div>
        <?php endforeach; ?>
      </div>
    </div>
  </div>
  <div class="col-md-5">
    <div class="ab-card h-100">
      <div class="ab-card-header"><h2>Upcoming Appointments</h2><a href="<?= base_url('appointments') ?>" class="small fw-semibold">View all</a></div>
      <div class="ab-card-body">
        <?php foreach ($upcomingAppointments as $appt): ?>
        <div class="d-flex justify-content-between align-items-center py-2 border-bottom ab-hairline">
          <div><div class="small fw-semibold"><?= e(trim($appt['contact_name']) ?: 'Unnamed') ?></div><div class="ab-cell-sub"><?= e($appt['business_line_name']) ?></div></div>
          <div class="text-end"><div class="small"><?= e(date('M j, g:i A', strtotime($appt['scheduled_start']))) ?></div></div>
        </div>
        <?php endforeach; ?>
        <?php if (empty($upcomingAppointments)): ?><div class="ab-empty small py-3">Nothing in the next 7 days.</div><?php endif; ?>
      </div>
    </div>
  </div>
</div>

<?php if (!empty($recentActivity)): ?>
<div class="ab-card mb-3">
  <div class="ab-card-header"><h2>Recent Activity</h2><a href="<?= base_url('activity-logs') ?>" class="small fw-semibold">View all</a></div>
  <div class="ab-card-body">
    <?php foreach ($recentActivity as $a): ?>
    <div class="d-flex justify-content-between py-2 border-bottom ab-hairline small">
      <div><?= e($a['action']) ?><?= $a['user_name'] ? ' — ' . e($a['user_name']) : '' ?></div>
      <div class="ab-cell-sub"><?= e(date('M j, g:i A', strtotime($a['created_at']))) ?></div>
    </div>
    <?php endforeach; ?>
  </div>
</div>
<?php endif; ?>
<?php endif; ?>

<div class="row g-3">
  <?php if (\App\Core\Rbac::can('user_management.view')): ?>
  <div class="col-md-4">
    <a href="<?= base_url('users') ?>" class="text-decoration-none">
      <div class="ab-stat-card">
        <div class="ab-stat-icon bg-primary-soft ab-badge"><i class="bi bi-people-fill text-primary"></i></div>
        <div class="ab-stat-label">User Management</div>
        <div class="ab-stat-value" style="font-size:1.1rem;">Manage Users &amp; Access</div>
      </div>
    </a>
  </div>
  <div class="col-md-4">
    <a href="<?= base_url('roles') ?>" class="text-decoration-none">
      <div class="ab-stat-card">
        <div class="ab-stat-icon bg-secondary-soft ab-badge"><i class="bi bi-shield-lock-fill text-secondary"></i></div>
        <div class="ab-stat-label">Roles &amp; Permissions</div>
        <div class="ab-stat-value" style="font-size:1.1rem;">Configure Access Control</div>
      </div>
    </a>
  </div>
  <div class="col-md-4">
    <a href="<?= base_url('activity-logs') ?>" class="text-decoration-none">
      <div class="ab-stat-card">
        <div class="ab-stat-icon bg-warning-soft ab-badge"><i class="bi bi-clock-history text-warning"></i></div>
        <div class="ab-stat-label">Activity &amp; Security</div>
        <div class="ab-stat-value" style="font-size:1.1rem;">Audit Trail &amp; Login History</div>
      </div>
    </a>
  </div>
  <?php endif; ?>
  <?php if (\App\Core\Rbac::can('email_campaigns.view')): ?>
  <div class="col-md-4">
    <a href="<?= base_url('campaigns') ?>" class="text-decoration-none">
      <div class="ab-stat-card">
        <div class="ab-stat-icon bg-primary-soft ab-badge"><i class="bi bi-megaphone-fill text-primary"></i></div>
        <div class="ab-stat-label">Marketing</div>
        <div class="ab-stat-value" style="font-size:1.1rem;">Campaigns &amp; Automation</div>
      </div>
    </a>
  </div>
  <?php endif; ?>
  <div class="col-md-4">
    <a href="<?= base_url('profile') ?>" class="text-decoration-none">
      <div class="ab-stat-card">
        <div class="ab-stat-icon bg-success-soft ab-badge"><i class="bi bi-person-circle text-success"></i></div>
        <div class="ab-stat-label">My Profile</div>
        <div class="ab-stat-value" style="font-size:1.1rem;">Security &amp; Preferences</div>
      </div>
    </a>
  </div>
</div>

<?php if ($campaignStats !== null): ?>
<div class="ab-card mt-4">
  <div class="ab-card-header"><h2>Campaign Performance</h2><a href="<?= base_url('campaigns') ?>" class="small fw-semibold">View all campaigns</a></div>
  <div class="ab-card-body">
    <div class="row g-3 mb-3">
      <div class="col-md-3"><div class="ab-stat-card"><div class="ab-stat-label">Total Campaigns</div><div class="ab-stat-value"><?= (int) $campaignStats['totals']['total_campaigns'] ?></div></div></div>
      <div class="col-md-3"><div class="ab-stat-card"><div class="ab-stat-label">Completed</div><div class="ab-stat-value"><?= (int) $campaignStats['totals']['sent_campaigns'] ?></div></div></div>
      <div class="col-md-3"><div class="ab-stat-card"><div class="ab-stat-label">Total Recipients</div><div class="ab-stat-value"><?= (int) $campaignStats['totals']['total_recipients'] ?></div></div></div>
      <div class="col-md-3"><div class="ab-stat-card"><div class="ab-stat-label">Total Opens</div><div class="ab-stat-value"><?= (int) $campaignStats['totals']['total_opens'] ?></div></div></div>
    </div>
    <?php if (!empty($campaignStats['recent'])): ?>
    <div class="table-responsive">
      <table class="table ab-table">
        <thead><tr><th>Campaign</th><th>Recipients</th><th>Opens</th><th>Clicks</th><th>Sent</th></tr></thead>
        <tbody>
        <?php foreach ($campaignStats['recent'] as $c): ?>
          <tr>
            <td><a href="<?= base_url('campaigns/' . $c['id']) ?>" class="ab-cell-primary"><?= e($c['name']) ?></a></td>
            <td><?= (int) $c['recipients_count'] ?></td>
            <td><?= (int) $c['opens_count'] ?></td>
            <td><?= (int) $c['clicks_count'] ?></td>
            <td class="ab-cell-sub"><?= e(date('M j, Y', strtotime($c['sent_at']))) ?></td>
          </tr>
        <?php endforeach; ?>
        </tbody>
      </table>
    </div>
    <?php else: ?>
    <div class="ab-empty small py-3">No campaigns sent yet.</div>
    <?php endif; ?>
  </div>
</div>
<?php endif; ?>
