<?php
$counts = $counts ?? ['all' => 0, 'campaign' => 0, 'manual' => 0, 'import' => 0, 'month' => 0];
$source = $source ?? 'all';
$q = $q ?? '';
$rows = $rows ?? [];
$canCampaigns = can_see('campaigns');
$canLists = can_see('lists');
$filterUrl = static function (string $src) use ($q): string {
    $qs = [];
    if ($src !== 'all') {
        $qs['source'] = $src;
    }
    if ($q !== '') {
        $qs['q'] = $q;
    }
    return url('unsubscribes') . ($qs ? '?' . http_build_query($qs) : '');
};
$sourceLabel = [
    'campaign' => 'Campaign',
    'manual' => 'Added here',
    'import' => 'Import',
];
$filters = [
    ['all', 'All', (int) $counts['all']],
    ['campaign', 'Campaign', (int) $counts['campaign']],
    ['manual', 'Added here', (int) $counts['manual']],
    ['import', 'Import', (int) $counts['import']],
];
$kpis = [
    [(string) (int) $counts['all'], 'Blocked', 'bi-slash-circle-fill', '#dc2626', '#fef2f2', 'all'],
    [(string) (int) $counts['month'], 'This month', 'bi-calendar3', '#2563eb', '#eff6ff', null],
    [(string) (int) $counts['campaign'], 'From a campaign', 'bi-megaphone-fill', '#7c3aed', '#f3e8ff', 'campaign'],
    [(string) (int) ($counts['manual'] + $counts['import']), 'Added or imported', 'bi-person-dash-fill', '#ea580c', '#fff7ed', null],
];
?>
<div class="unsub-page">
  <div class="d-flex flex-wrap justify-content-between align-items-start gap-3 mb-3">
    <div>
      <h1 class="page-title">Unsubscribes</h1>
      <p class="page-sub mb-0">People who said do not email. This list is kept forever. A data sync cannot put them back on a campaign.</p>
    </div>
    <form class="task-search" method="get">
      <?php if ($source !== 'all'): ?><input type="hidden" name="source" value="<?= e($source) ?>"><?php endif; ?>
      <i class="bi bi-search"></i>
      <input class="form-control" name="q" value="<?= e($q) ?>" placeholder="Search email">
    </form>
  </div>

  <div class="task-summary mb-3">
    <?php foreach ($kpis as $k): ?>
      <<?= $k[5] ? 'a' : 'div' ?> class="task-summary-card <?= $k[5] && $source === $k[5] ? 'is-active' : '' ?>" <?php if ($k[5]): ?>href="<?= e($filterUrl($k[5])) ?>"<?php endif; ?> style="--task-accent: <?= e($k[3]) ?>; --task-tint: <?= e($k[4]) ?>">
        <div class="task-summary-icon"><i class="bi <?= e($k[2]) ?>"></i></div>
        <div>
          <div class="task-summary-n"><?= e($k[0]) ?></div>
          <div class="task-summary-l"><?= e($k[1]) ?></div>
        </div>
      </<?= $k[5] ? 'a' : 'div' ?>>
    <?php endforeach; ?>
  </div>

  <div class="camp-compose-note mb-3">
    These addresses never go out in a campaign<?php if ($canLists): ?> and never show in <a href="<?= e(url('lists')) ?>">list</a> previews<?php endif; ?>. One-to-one sales email is separate.
  </div>

  <div class="task-toolbar mb-3">
    <div class="task-chips">
      <?php foreach ($filters as $f): ?>
        <a class="task-chip <?= $source === $f[0] ? 'active' : '' ?>" href="<?= e($filterUrl($f[0])) ?>">
          <?= e($f[1]) ?>
          <span><?= (int) $f[2] ?></span>
        </a>
      <?php endforeach; ?>
    </div>
  </div>

  <?php if (!$rows): ?>
    <div class="card-soft task-empty">
      <div class="task-empty-icon"><i class="bi bi-slash-circle"></i></div>
      <h2><?= $q !== '' || $source !== 'all' ? 'No matches in this view' : 'No unsubscribes yet' ?></h2>
      <p><?= $q !== '' || $source !== 'all' ? 'Try another filter or clear the search.' : 'When someone opts out of a campaign, they stay here for good.' ?></p>
    </div>
  <?php else: ?>
    <?php foreach ($rows as $r):
        $email = (string) ($r['email'] ?? '');
        $src = (string) ($r['source'] ?? 'campaign');
        $local = (string) (strstr($email, '@', true) ?: $email);
        $initials = name_initials(str_replace(['.', '_', '-', '+'], ' ', $local));
        $campName = trim((string) ($r['campaign_name'] ?? ''));
        $campId = (int) ($r['campaign_id'] ?? 0);
    ?>
      <div class="unsub-card unsub-tone-<?= e($src) ?>">
        <div class="lead-avatar"><?= e($initials) ?></div>
        <div class="flex-grow-1 min-w-0">
          <div class="lead-name text-truncate"><?= e($email) ?></div>
          <div class="lead-sub">
            <span class="lst-chip unsub-src-<?= e($src) ?>"><?= e($sourceLabel[$src] ?? $src) ?></span>
            <?php if ($campName !== ''): ?>
              <?php if ($canCampaigns && $campId): ?>
                <a href="<?= e(url('campaigns/' . $campId)) ?>"><?= e($campName) ?></a>
              <?php else: ?>
                <span><?= e($campName) ?></span>
              <?php endif; ?>
            <?php endif; ?>
          </div>
        </div>
        <div class="task-meta">
          <span class="lead-date" title="<?= e(dt_fmt($r['created_at'], 'M j, Y g:i A')) ?>"><?= e(relative_when($r['created_at'] ?? null)) ?></span>
        </div>
      </div>
    <?php endforeach; ?>
  <?php endif; ?>

  <?php \App\Core\View::partial('pager', compact('page', 'pages')); ?>
</div>
