<?php
$c = $campaign;
$s = $stats ?? [];
$st = (string) ($c['status'] ?? 'draft');
$chip = commerce_status_label($st === 'sent' ? 'sent' : 'draft', $st === 'sent' ? 'Sent' : 'Draft');
$subject = trim(merge_readable((string) ($c['subject'] ?? '')));
$list = trim((string) ($c['list_name'] ?? ''));
$delivered = (int) ($s['delivered'] ?? 0);
$opened = (int) ($s['opened'] ?? 0);
$clicked = (int) ($s['clicked'] ?? 0);
$bounced = (int) ($s['bounced'] ?? 0);
$unsub = (int) ($s['unsubscribed'] ?? 0);
?>
<div class="rec-page vis-page">
  <a class="rec-back" href="<?= e(url('campaigns')) ?>"><i class="bi bi-arrow-left"></i> All campaigns</a>

  <div class="rec-hero rec-tone-<?= e($chip['rec']) ?>">
    <div class="rec-hero-main">
      <div class="rec-hero-avatar doc-hero-<?= e($chip['rec']) ?>"><i class="bi bi-megaphone-fill"></i></div>
      <div class="min-w-0">
        <p class="rec-kicker">Campaign<?= $list !== '' ? ' · ' . $list : '' ?></p>
        <div class="d-flex flex-wrap align-items-center gap-2 mb-1">
          <h1 class="page-title mb-0"><?= e($c['name']) ?></h1>
          <span class="due-pill <?= e($chip['class']) ?>"><?= e($chip['text']) ?></span>
        </div>
        <div class="rec-hero-sub">
          <?php if ($subject !== ''): ?><span><i class="bi bi-envelope"></i> <?= e($subject) ?></span><?php endif; ?>
          <?php if (!empty($c['sent_at'])): ?><span><i class="bi bi-calendar3"></i> <?= e(dt_fmt($c['sent_at'])) ?></span><?php endif; ?>
        </div>
      </div>
    </div>
    <div class="rec-hero-actions">
      <p class="rec-action-hint"><?= $st === 'draft' ? 'Send when the list and message are ready.' : 'This campaign already went out.' ?></p>
      <?php if ($st === 'draft'): ?>
        <form class="ajax-form" action="<?= e(url('campaigns/' . $c['id'] . '/send')) ?>" method="post">
          <?= csrf_field() ?>
          <button class="btn btn-primary" type="submit"><i class="bi bi-send"></i> Send to list</button>
        </form>
      <?php endif; ?>
      <?php if (can_see('campaign_reports')): ?>
        <a class="btn btn-outline-primary" href="<?= e(url('campaigns/reports')) ?>"><i class="bi bi-graph-up"></i> Reports</a>
      <?php endif; ?>
    </div>
  </div>

  <div class="task-summary is-five mb-3">
    <?php foreach ([
        [$delivered, 'Delivered', 'bi-envelope-check-fill', '#16a34a', '#f0fdf4'],
        [$opened, 'Opened', 'bi-eye-fill', '#7c3aed', '#f5f3ff'],
        [$clicked, 'Clicked', 'bi-cursor-fill', '#ea580c', '#fff7ed'],
        [$bounced, 'Bounced', 'bi-exclamation-triangle-fill', '#dc2626', '#fef2f2'],
        [$unsub, 'Unsubscribed', 'bi-person-dash-fill', '#db2777', '#fce7f3'],
    ] as $k): ?>
      <div class="task-summary-card" 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"><?= (int) $k[0] ?></div><div class="task-summary-l"><?= e($k[1]) ?></div></div>
      </div>
    <?php endforeach; ?>
  </div>

  <div class="row g-3">
    <div class="col-lg-8">
      <div class="card-soft rec-card p-3">
        <?php \App\Core\View::partial('record_section_head', [
            'icon' => 'bi-chat-text',
            'title' => 'Message',
            'hint' => 'When this is sent, each person sees their own name and company — not these sample words.',
        ]); ?>
        <div class="vis-need-detail"><?= nl2br(e(strip_tags(merge_readable((string) $c['body'])))) ?></div>
      </div>
    </div>
    <div class="col-lg-4">
      <?php \App\Core\View::partial('record_facts', [
          'title' => 'About this campaign',
          'hint' => 'Who it goes to.',
          'icon' => 'bi-megaphone',
          'facts' => [
              ['label' => 'Status', 'value' => $chip['text']],
              ['label' => 'List', 'value' => $list !== '' ? $list : ''],
              ['label' => 'Template', 'value' => (string) ($c['template_name'] ?? '')],
              ['label' => 'Subject', 'value' => $subject],
              ['label' => 'Sent', 'value' => !empty($c['sent_at']) ? dt_fmt($c['sent_at']) : ''],
          ],
      ]); ?>
    </div>
  </div>
</div>
