<?= $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">Email Campaigns</h1>
        <p class="text-muted mb-0">Compose, target an audience, and send — manual send only, no automated drips.</p>
    </div>
    <a href="<?= site_url('campaigns/new') ?>" class="btn btn-primary"><i class="bi bi-plus-lg me-1"></i>New Campaign</a>
</div>

<div class="card shadow-sm">
    <div class="table-scroll">
        <table class="table table-sm align-middle mb-0">
            <thead class="table-light">
                <tr><th>Subject</th><th>Status</th><th class="text-end">Sent</th><th>Created By</th><th>Date</th><th></th></tr>
            </thead>
            <tbody>
                <?php if (empty($campaigns)): ?>
                    <tr><td colspan="6" class="text-center text-muted py-4">No campaigns yet.</td></tr>
                <?php endif; ?>
                <?php foreach ($campaigns as $c): ?>
                    <tr>
                        <td><a href="<?= site_url('campaigns/' . $c->id) ?>" class="text-decoration-none fw-semibold"><?= esc($c->subject) ?></a></td>
                        <td><span class="badge bg-<?= $c->status === 'sent' ? 'success' : 'secondary' ?>"><?= esc(ucfirst($c->status)) ?></span></td>
                        <td class="text-end"><?= $c->status === 'sent' ? (int) $c->sent_count : '—' ?></td>
                        <td class="small"><?= esc($c->created_by_name ?? '—') ?></td>
                        <td class="small text-muted"><?= esc(date('d M Y', strtotime($c->created_at))) ?></td>
                        <td class="text-end"><a href="<?= site_url('campaigns/' . $c->id) ?>" class="btn btn-sm btn-outline-secondary">View</a></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

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