<?php
/**
 * Admin — Newsletter Email Logs
 *
 * @var array $emailLogs
 */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$csrf = htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8');
?>

<div class="admin-toolbar">
    <div class="admin-toolbar-left">
        <a href="<?= $bp ?>/admin/newsletter" class="admin-btn admin-btn-secondary">
            <svg class="admin-btn-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M17 21v-2a4 4 0 00-4-4H5a4 4 0 00-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M23 21v-2a4 4 0 00-3-3.87"/><path d="M16 3.13a4 4 0 010 7.75"/></svg>
            Back to Subscribers
        </a>
        <a href="<?= $bp ?>/admin/newsletter/compose" class="admin-btn admin-btn-primary">
            <svg class="admin-btn-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><line x1="22" y1="2" x2="11" y2="13"/><polygon points="22 2 15 22 11 13 2 9 22 2"/></svg>
            Compose New
        </a>
    </div>
</div>

<div class="admin-card">
    <div class="admin-card-header">
        <h3 class="admin-card-title">Newsletter Email Logs</h3>
    </div>
    <div class="admin-card-body">
        <?php if (empty($emailLogs)): ?>
        <div class="admin-empty-state">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" width="48" height="48" style="margin-bottom:1rem;opacity:0.4;"><path d="M14 2H6a2 2 0 00-2 2v16a2 2 0 002 2h12a2 2 0 002-2V8z"/><polyline points="14 2 14 8 20 8"/></svg>
            <p>No email logs found. Logs will appear here after you send newsletters.</p>
        </div>
        <?php else: ?>
        <div class="admin-table-responsive">
            <table class="admin-table">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Subject</th>
                        <th>Recipients</th>
                        <th>Sent</th>
                        <th>Failed</th>
                        <th>Status</th>
                        <th>Sent At</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($emailLogs as $i => $log): ?>
                    <tr>
                        <td><?= $i + 1 ?></td>
                        <td>
                            <strong><?= htmlspecialchars($log['subject'] ?? '', ENT_QUOTES, 'UTF-8') ?></strong>
                        </td>
                        <td><?= (int) ($log['recipient_count'] ?? 0) ?></td>
                        <td style="color:#10b981;"><?= (int) ($log['sent_count'] ?? 0) ?></td>
                        <td style="color:<?= ($log['failed_count'] ?? 0) > 0 ? '#ef4444' : 'inherit' ?>;">
                            <?= (int) ($log['failed_count'] ?? 0) ?>
                        </td>
                        <td>
                            <?php
                            $statusColors = [
                                'sent'    => 'admin-badge-success',
                                'partial' => 'admin-badge-warning',
                                'failed'  => 'admin-badge-danger',
                                'draft'   => 'admin-badge-secondary',
                                'sending' => 'admin-badge-info',
                            ];
                            $status = $log['status'] ?? 'draft';
                            $badgeClass = $statusColors[$status] ?? 'admin-badge-info';
                            ?>
                            <span class="admin-badge <?= $badgeClass ?>"><?= ucfirst($status) ?></span>
                        </td>
                        <td>
                            <?php if (!empty($log['sent_at'])): ?>
                                <?= date('M d, Y h:i A', strtotime($log['sent_at'])) ?>
                            <?php else: ?>
                                <span class="admin-text-muted">Not sent</span>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <?php endif; ?>
    </div>
</div>
