<div class="row g-3 mb-3">
  <div class="col-md-3"><div class="ab-stat-card"><div class="ab-stat-label">Recipients</div><div class="ab-stat-value"><?= (int) $campaign['recipients_count'] ?></div></div></div>
  <div class="col-md-3"><div class="ab-stat-card"><div class="ab-stat-label">Delivery Rate</div><div class="ab-stat-value"><?= $rates['delivery_rate'] ?>%</div></div></div>
  <div class="col-md-3"><div class="ab-stat-card"><div class="ab-stat-label">Open Rate</div><div class="ab-stat-value"><?= $rates['open_rate'] ?>%</div></div></div>
  <div class="col-md-3"><div class="ab-stat-card"><div class="ab-stat-label">Click Rate</div><div class="ab-stat-value"><?= $rates['click_rate'] ?>%</div></div></div>
</div>
<div class="row g-3 mb-3">
  <div class="col-md-4"><div class="ab-stat-card"><div class="ab-stat-label">Bounce Rate</div><div class="ab-stat-value"><?= $rates['bounce_rate'] ?>%</div></div></div>
  <div class="col-md-4"><div class="ab-stat-card"><div class="ab-stat-label">Unsubscribe Rate</div><div class="ab-stat-value"><?= $rates['unsubscribe_rate'] ?>%</div></div></div>
  <div class="col-md-4"><div class="ab-stat-card"><div class="ab-stat-label">Status</div><div class="ab-stat-value" style="font-size:1.1rem;">
    <span class="ab-badge <?= match($campaign['status']){'sent'=>'bg-success-soft','sending'=>'bg-primary-soft','scheduled'=>'bg-secondary-soft','cancelled'=>'bg-danger-soft',default=>'bg-secondary-soft'} ?>"><?= e($campaign['status']==='sent'?'Completed':ucfirst($campaign['status'])) ?></span>
  </div></div></div>
</div>

<div class="ab-card mb-3">
  <div class="ab-card-header">
    <h2><?= e($campaign['subject']) ?></h2>
    <div class="d-flex gap-2">
      <?php if ($campaign['status'] === 'draft'): ?>
      <button class="btn btn-light-soft btn-sm" id="abScheduleBtn"><i class="bi bi-clock me-1"></i>Schedule</button>
      <button class="btn btn-primary btn-sm" id="abSendNowBtn"><i class="bi bi-send me-1"></i>Send Now</button>
      <?php elseif (in_array($campaign['status'], ['scheduled','sending'])): ?>
      <button class="btn btn-outline-danger btn-sm" id="abCancelBtn"><i class="bi bi-x-circle me-1"></i>Cancel</button>
      <?php endif; ?>
    </div>
  </div>
  <div class="ab-card-body"><div class="border rounded-3 p-3" style="max-width:640px; background:var(--ab-canvas);"><?= $campaign['body_html'] ?></div></div>
</div>

<div class="ab-card">
  <div class="ab-card-header"><h2>Recipients</h2></div>
  <div class="table-responsive">
    <table class="table ab-table">
      <thead><tr><th>Email</th><th>Status</th><th>Sent</th><th>Opened</th><th>Clicked</th></tr></thead>
      <tbody>
      <?php foreach ($recipients as $r): ?>
        <tr>
          <td><?= e($r['email_address']) ?></td>
          <td><span class="ab-badge <?= match($r['status']){'clicked'=>'bg-success-soft','opened'=>'bg-primary-soft','bounced'=>'bg-danger-soft','unsubscribed'=>'bg-secondary-soft',default=>'bg-secondary-soft'} ?>"><?= e(ucfirst($r['status'])) ?></span></td>
          <td class="ab-cell-sub"><?= $r['sent_at'] ? e(date('M j, g:i A', strtotime($r['sent_at']))) : '—' ?></td>
          <td class="ab-cell-sub"><?= $r['opened_at'] ? e(date('M j, g:i A', strtotime($r['opened_at']))) : '—' ?></td>
          <td class="ab-cell-sub"><?= $r['clicked_at'] ? e(date('M j, g:i A', strtotime($r['clicked_at']))) : '—' ?></td>
        </tr>
      <?php endforeach; ?>
      <?php if (empty($recipients)): ?><tr><td colspan="5" class="text-center p-4 ab-text-muted">No recipients yet — send or schedule this campaign to build the recipient list.</td></tr><?php endif; ?>
      </tbody>
    </table>
  </div>
</div>

<!-- Schedule modal -->
<div class="modal fade" id="abScheduleModal" tabindex="-1"><div class="modal-dialog modal-dialog-centered"><div class="modal-content">
  <div class="modal-header"><h5 class="modal-title">Schedule Campaign</h5><button class="btn-close" data-bs-dismiss="modal"></button></div>
  <div class="modal-body"><label class="form-label ab-req">Send At</label><input type="datetime-local" class="form-control" id="abScheduleAt" required></div>
  <div class="modal-footer"><button class="btn btn-light-soft" data-bs-dismiss="modal">Cancel</button><button class="btn btn-primary" id="abConfirmScheduleBtn">Schedule</button></div>
</div></div></div>

<script>
document.getElementById('abSendNowBtn')?.addEventListener('click', function () {
  if (!confirm('Send this campaign now to all matching recipients?')) return;
  abFetch('<?= base_url('campaigns/' . $campaign['id'] . '/send-now') ?>', { method: 'POST' })
    .then(function (res) { abToast(res.message); setTimeout(function () { window.location.reload(); }, 800); })
    .catch(function (err) { abToast(err.message, 'danger'); });
});

document.getElementById('abScheduleBtn')?.addEventListener('click', function () {
  new bootstrap.Modal(document.getElementById('abScheduleModal')).show();
});
document.getElementById('abConfirmScheduleBtn')?.addEventListener('click', function () {
  var sendAt = document.getElementById('abScheduleAt').value.replace('T', ' ') + ':00';
  abFetch('<?= base_url('campaigns/' . $campaign['id'] . '/schedule') ?>', { method: 'POST', body: { send_at: sendAt } })
    .then(function (res) { abToast(res.message); setTimeout(function () { window.location.reload(); }, 800); })
    .catch(function (err) { abToast(err.message, 'danger'); });
});

document.getElementById('abCancelBtn')?.addEventListener('click', function () {
  if (!confirm('Cancel this campaign? Any not-yet-sent emails will be removed from the queue.')) return;
  abFetch('<?= base_url('campaigns/' . $campaign['id'] . '/cancel') ?>', { method: 'POST' })
    .then(function (res) { abToast(res.message); setTimeout(function () { window.location.reload(); }, 800); })
    .catch(function (err) { abToast(err.message, 'danger'); });
});
</script>
