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

$mails = $mails ?? [];
$stats = $stats ?? [];
$status = $_GET['status'] ?? '';
$q = $_GET['q'] ?? '';
$flash = $_SESSION['mail_flash'] ?? '';
unset($_SESSION['mail_flash']);
?>

<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-envelope-circle-check"></i>
                Conference Mail Queue
            </h3>
            <div class="opacity-75">
                View, resend, cancel, and inspect conference email messages.
            </div>
        </div>
    </div>

    <?php if ($flash): ?>
        <div class="alert alert-info"><?= cm_h($flash) ?></div>
    <?php endif; ?>

    <div class="row g-3 mb-4">
        <?php foreach (['queued','sent','failed','cancelled'] as $st): ?>
            <div class="col-md-3">
                <div class="card border-0 shadow-sm" style="border-radius:20px;">
                    <div class="card-body text-center">
                        <div class="fs-2 fw-bold"><?= (int)($stats[$st] ?? 0) ?></div>
                        <div class="text-muted"><?= cm_h($st) ?></div>
                    </div>
                </div>
            </div>
        <?php endforeach; ?>
    </div>

    <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
        <div class="card-body p-4">
            <form method="get" action="/easychair/admin/conference-mail" class="row g-2 align-items-end">
                <div class="col-md-5">
                    <label class="form-label fw-bold">Search</label>
                    <input name="q" class="form-control" value="<?= cm_h($q) ?>" placeholder="email, subject, template, submission no">
                </div>

                <div class="col-md-3">
                    <label class="form-label fw-bold">Status</label>
                    <select name="status" class="form-select">
                        <option value="">All</option>
                        <?php foreach (['queued','sent','failed','cancelled'] as $st): ?>
                            <option value="<?= cm_h($st) ?>" <?= $status === $st ? 'selected' : '' ?>>
                                <?= cm_h($st) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <div class="col-md-2">
                    <button class="btn btn-primary w-100">Filter</button>
                </div>

                <div class="col-md-2">
                    <a class="btn btn-success w-100"
                       onclick="return confirm('Send all queued/failed emails for this conference?')"
                       href="/easychair/admin/conference-mail/send-queued">
                        Send Queue
                    </a>
                </div>
            </form>
        </div>
    </div>

    <div class="card border-0 shadow-sm" style="border-radius:22px;">
        <div class="card-body p-4">
            <div class="table-responsive">
                <table class="table table-bordered align-middle">
                    <thead class="table-dark">
                    <tr>
                        <th>ID</th>
                        <th>Submission</th>
                        <th>Template</th>
                        <th>Recipient</th>
                        <th>Subject</th>
                        <th>Status</th>
                        <th>Retry</th>
                        <th>Created / Sent</th>
                        <th>Error</th>
                        <th>Actions</th>
                    </tr>
                    </thead>

                    <tbody>
                    <?php foreach ($mails as $m): ?>
                        <tr>
                            <td><?= (int)$m['id'] ?></td>

                            <td>
                                <?php if (!empty($m['submission_id'])): ?>
                                    <a href="/easychair/admin/submission-workflow?submission_id=<?= (int)$m['submission_id'] ?>">
                                        <?= cm_h($m['submission_no'] ?: ('#'.$m['submission_id'])) ?>
                                    </a>
                                    <div class="text-muted small"><?= cm_h($m['submission_title'] ?? '') ?></div>
                                <?php else: ?>
                                    <span class="text-muted">No submission</span>
                                <?php endif; ?>
                            </td>

                            <td>
                                <span class="badge bg-primary"><?= cm_h($m['template_key'] ?? '') ?></span>
                                <span class="badge bg-secondary"><?= cm_h($m['event_key'] ?? '') ?></span>
                            </td>

                            <td>
                                <?= cm_h($m['recipient_email'] ?? '') ?>
                                <div class="text-muted small"><?= cm_h($m['recipient_name'] ?? '') ?></div>
                            </td>

                            <td style="min-width:260px;"><?= cm_h($m['subject'] ?? '') ?></td>

                            <td>
                                <?php
                                $cls = match($m['status'] ?? '') {
                                    'sent' => 'bg-success',
                                    'failed' => 'bg-danger',
                                    'cancelled' => 'bg-secondary',
                                    default => 'bg-warning text-dark',
                                };
                                ?>
                                <span class="badge <?= $cls ?>"><?= cm_h($m['status'] ?? '') ?></span>
                            </td>

                            <td class="small">
                                <div>Retries: <?= (int)($m['retry_count'] ?? 0) ?></div>
                                <div>Last: <?= cm_h($m['last_attempt_at'] ?? '') ?></div>
                            </td>

                            <td class="small">
                                <div>Created: <?= cm_h($m['created_at'] ?? '') ?></div>
                                <div>Sent: <?= cm_h($m['sent_at'] ?? '') ?></div>
                            </td>

                            <td class="small text-danger" style="max-width:260px;">
                                <?= cm_h(mb_substr((string)($m['error_message'] ?? ''), 0, 220)) ?>
                            </td>

                            <td>
                                <div class="d-flex flex-wrap gap-1">
                                    <a class="btn btn-sm btn-outline-secondary"
                                       target="_blank"
                                       href="/easychair/admin/conference-mail/preview?id=<?= (int)$m['id'] ?>">
                                        Preview
                                    </a>

                                    <a class="btn btn-sm btn-primary"
                                       href="/easychair/admin/conference-mail/resend?id=<?= (int)$m['id'] ?>">
                                        Resend
                                    </a>

                                    <?php if (in_array($m['status'], ['queued','failed'], true)): ?>
                                        <a class="btn btn-sm btn-danger"
                                           onclick="return confirm('Cancel this email?')"
                                           href="/easychair/admin/conference-mail/cancel?id=<?= (int)$m['id'] ?>">
                                            Cancel
                                        </a>
                                    <?php endif; ?>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; ?>

                    <?php if (!$mails): ?>
                        <tr>
                            <td colspan="10" class="text-center text-muted py-4">
                                No conference emails found.
                            </td>
                        </tr>
                    <?php endif; ?>
                    </tbody>

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