<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>

<div class="d-flex justify-content-between align-items-start mb-3">
    <div>
        <h1 class="h3 mb-1"><?= esc($campaign->subject) ?></h1>
        <p class="text-muted mb-0">
            <span class="badge bg-<?= $campaign->status === 'sent' ? 'success' : 'secondary' ?>"><?= esc(ucfirst($campaign->status)) ?></span>
            <?php if ($campaign->status === 'sent'): ?>
                · Sent <?= esc(date('d M Y, g:i a', strtotime($campaign->sent_at))) ?> to <?= (int) $campaign->sent_count ?> guests
            <?php endif; ?>
        </p>
    </div>
    <a href="<?= site_url('campaigns') ?>" class="btn btn-outline-secondary">Back to Campaigns</a>
</div>

<div class="row">
    <div class="col-lg-7 mb-3">
        <div class="card shadow-sm">
            <div class="card-header">Message</div>
            <div class="card-body">
                <p style="white-space: pre-wrap;"><?= esc($campaign->body) ?></p>
            </div>
        </div>

        <?php if (! empty($recipients)): ?>
        <div class="card shadow-sm mt-3">
            <div class="card-header">Recipients</div>
            <div class="table-scroll">
                <table class="table table-sm mb-0 align-middle">
                    <thead class="table-light"><tr><th>Guest</th><th>Email</th><th>Status</th><th>Error</th></tr></thead>
                    <tbody>
                        <?php foreach ($recipients as $r): ?>
                            <tr>
                                <td><a href="<?= site_url('guests/' . $r->guest_id) ?>" class="text-decoration-none"><?= esc($r->guest_name) ?></a></td>
                                <td class="small text-muted"><?= esc($r->guest_email) ?></td>
                                <td><span class="badge bg-<?= $r->status === 'sent' ? 'success' : 'danger' ?>"><?= esc(ucfirst($r->status)) ?></span></td>
                                <td class="small text-danger"><?= esc($r->error) ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
        <?php endif; ?>
    </div>

    <div class="col-lg-5">
        <div class="card shadow-sm">
            <div class="card-header">Audience</div>
            <div class="card-body">
                <dl class="row mb-0 small">
                    <dt class="col-5">Segment</dt><dd class="col-7"><?= esc($lookups->guestSegments[$filter['segment'] ?? '']['label'] ?? 'All segments') ?></dd>
                    <dt class="col-5">Nationality</dt><dd class="col-7"><?= esc($filter['nationality'] ?? 'All nationalities') ?></dd>
                    <dt class="col-5">Matches now</dt><dd class="col-7 fw-semibold"><?= (int) $audienceCount ?> guests</dd>
                </dl>
                <p class="text-muted small mt-2 mb-0">Blacklisted guests and guests with no email on file are always excluded.</p>

                <?php if ($campaign->status === 'draft'): ?>
                <hr>
                <form action="<?= site_url('campaigns/' . $campaign->id . '/send') ?>" method="post" onsubmit="return confirm('Send this campaign to ' + <?= (int) $audienceCount ?> + ' guests now? This cannot be undone.');">
                    <?= csrf_field() ?>
                    <button type="submit" class="btn btn-primary w-100"><i class="bi bi-send me-1"></i>Send to <?= (int) $audienceCount ?> Guests Now</button>
                </form>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

<?= $this->endSection() ?>
