<?php
function nt_h($v) {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}

$messages = $messages ?? [];
$stats = $stats ?? [];
?>

<div class="container-fluid py-3">

    <div class="card border-0 shadow-sm mb-4" style="border-radius:24px;background:linear-gradient(135deg,#0f172a,#1d4ed8);color:#fff;">
        <div class="card-body p-4">
            <h3 class="fw-bold mb-1"><i class="fa fa-paper-plane"></i> الإشعارات والبريد</h3>
            <div class="opacity-75">Queued per-conference email notifications</div>
        </div>
    </div>

    <div class="row g-3 mb-4">
        <?php foreach (['queued'=>'Queued','sent'=>'Sent','failed'=>'Failed','cancelled'=>'Cancelled'] as $key=>$label): ?>
            <div class="col-md-3 col-6">
                <div class="card border-0 shadow-sm h-100" style="border-radius:20px;">
                    <div class="card-body">
                        <div class="text-muted small fw-bold"><?= nt_h($label) ?></div>
                        <div class="display-6 fw-bold"><?= (int)($stats[$key] ?? 0) ?></div>
                    </div>
                </div>
            </div>
        <?php endforeach; ?>
    </div>

    <div class="card border-0 shadow-sm" style="border-radius:22px;">
        <div class="card-body">
            <h5 class="fw-bold mb-3">Outbox</h5>

            <div class="table-responsive">
                <table class="table table-bordered align-middle">
                    <thead class="table-dark">
                    <tr>
                        <th>ID</th>
                        <th>Conference</th>
                        <th>Template</th>
                        <th>Recipient</th>
                        <th>Subject</th>
                        <th>Status</th>
                        <th>Created</th>
                        <th>Action</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($messages as $m): ?>
                        <tr>
                            <td><?= (int)$m['id'] ?></td>
                            <td><?= nt_h($m['short_name'] ?? ('#'.$m['conference_id'])) ?></td>
                            <td><span class="badge bg-primary"><?= nt_h($m['template_key']) ?></span></td>
                            <td>
                                <strong><?= nt_h($m['recipient_name'] ?: $m['recipient_email']) ?></strong>
                                <div class="text-muted small"><?= nt_h($m['recipient_email']) ?></div>
                            </td>
                            <td style="min-width:260px;">
                                <?= nt_h($m['subject']) ?>
                                <?php if (!empty($m['submission_no'])): ?>
                                    <div class="text-muted small"><?= nt_h($m['submission_no']) ?></div>
                                <?php endif; ?>
                            </td>
                            <td>
                                <?php
                                $class = match($m['status']) {
                                    'sent' => 'bg-success',
                                    'failed' => 'bg-danger',
                                    'cancelled' => 'bg-secondary',
                                    default => 'bg-warning text-dark'
                                };
                                ?>
                                <span class="badge <?= $class ?>"><?= nt_h($m['status']) ?></span>
                            </td>
                            <td><?= nt_h($m['created_at']) ?></td>
                            <td>
                                <?php if ($m['status'] === 'queued'): ?>
                                    <a class="btn btn-sm btn-success" href="/easychair/admin/notifications/mark-sent?id=<?= (int)$m['id'] ?>">Mark sent</a>
                                    <a class="btn btn-sm btn-outline-danger" href="/easychair/admin/notifications/cancel?id=<?= (int)$m['id'] ?>">Cancel</a>
                                <?php else: ?>
                                    <span class="text-muted small">No action</span>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>

                    <?php if (!$messages): ?>
                        <tr>
                            <td colspan="8" class="text-center text-muted py-4">No notifications found.</td>
                        </tr>
                    <?php endif; ?>
                    </tbody>
                </table>
            </div>

        </div>
    </div>
</div>
