<?php
$page_title = 'Email Logs - Admin';

if (!Admin::isLoggedIn()) {
    Utils::redirect('/routex/admin/login');
}

require_once __DIR__ . '/../../includes/trackheader.php';

$page = $_GET['page'] ?? 1;
$page = max(1, intval($page));
$limit = 50;
$offset = ($page - 1) * $limit;

$stmt = $pdo->prepare("
    SELECT * FROM email_logs 
    ORDER BY created_at DESC 
    LIMIT :limit OFFSET :offset
");
$stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();
$logs = $stmt->fetchAll();

$stmt = $pdo->query("SELECT COUNT(*) as count FROM email_logs");
$total = $stmt->fetch()['count'];
$total_pages = ceil($total / $limit);
?>

<section class="min-h-screen bg-gray-50">
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
        <!-- Header -->
        <div class="flex flex-col md:flex-row justify-between items-center mb-8 gap-4">
            <div>
                <h1 class="text-4xl font-bold text-gray-900">Email Logs</h1>
                <p class="text-gray-600 mt-2">Total: <?php echo $total; ?> emails</p>
            </div>
            <a href="/routex/admin/dashboard" class="px-6 py-3 bg-white text-gray-700 border-2 border-gray-300 rounded-lg font-semibold hover:bg-gray-50 transition-all duration-300">
                <i class="fas fa-arrow-left mr-2"></i>Dashboard
            </a>
        </div>

        <!-- Stats -->
        <div class="grid grid-cols-1 md:grid-cols-3 gap-6 my-8">
            <?php
            $stmt = $pdo->query("SELECT COUNT(*) as count FROM email_logs WHERE status = 'sent'");
            $sent = $stmt->fetch()['count'];
            
            $stmt = $pdo->query("SELECT COUNT(*) as count FROM email_logs WHERE status = 'failed'");
            $failed = $stmt->fetch()['count'];
            
            $stmt = $pdo->query("SELECT COUNT(*) as count FROM email_logs WHERE status = 'pending'");
            $pending = $stmt->fetch()['count'];
            ?>
            <div class="bg-white rounded-xl shadow-md p-6">
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-gray-600 text-sm">Sent</p>
                        <p class="text-3xl font-bold text-green-600"><?php echo $sent; ?></p>
                    </div>
                    <div class="w-12 h-12 bg-green-100 rounded-lg flex items-center justify-center">
                        <i class="fas fa-check-circle text-green-600 text-xl"></i>
                    </div>
                </div>
            </div>

            <div class="bg-white rounded-xl shadow-md p-6">
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-gray-600 text-sm">Failed</p>
                        <p class="text-3xl font-bold text-red-600"><?php echo $failed; ?></p>
                    </div>
                    <div class="w-12 h-12 bg-red-100 rounded-lg flex items-center justify-center">
                        <i class="fas fa-times-circle text-red-600 text-xl"></i>
                    </div>
                </div>
            </div>

            <div class="bg-white rounded-xl shadow-md p-6">
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-gray-600 text-sm">Pending</p>
                        <p class="text-3xl font-bold text-yellow-600"><?php echo $pending; ?></p>
                    </div>
                    <div class="w-12 h-12 bg-yellow-100 rounded-lg flex items-center justify-center">
                        <i class="fas fa-clock text-yellow-600 text-xl"></i>
                    </div>
                </div>
            </div>
        </div>

        <!-- Logs Table -->
        <div class="bg-white rounded-xl shadow-md overflow-hidden">
            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead class="bg-gray-100 border-b">
                        <tr>
                            <th class="px-6 py-4 text-left text-sm font-semibold text-gray-900">Recipient</th>
                            <th class="px-6 py-4 text-left text-sm font-semibold text-gray-900">Subject</th>
                            <th class="px-6 py-4 text-left text-sm font-semibold text-gray-900">Status</th>
                            <th class="px-6 py-4 text-left text-sm font-semibold text-gray-900">Sent At</th>
                            <th class="px-6 py-4 text-left text-sm font-semibold text-gray-900">Details</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($logs as $log): ?>
                        <tr class="border-b hover:bg-gray-50">
                            <td class="px-6 py-4">
                                <p class="font-semibold text-gray-900"><?php echo htmlspecialchars($log['recipient_email']); ?></p>
                            </td>
                            <td class="px-6 py-4 text-sm text-gray-600">
                                <?php echo htmlspecialchars($log['subject']); ?>
                            </td>
                            <td class="px-6 py-4">
                                <?php
                                $status_colors = [
                                    'sent' => 'bg-green-100 text-green-800',
                                    'failed' => 'bg-red-100 text-red-800',
                                    'pending' => 'bg-yellow-100 text-yellow-800'
                                ];
                                $color = $status_colors[$log['status']] ?? 'bg-gray-100 text-gray-800';
                                ?>
                                <span class="<?php echo $color; ?> px-3 py-1 rounded-full text-sm font-semibold">
                                    <?php echo ucfirst($log['status']); ?>
                                </span>
                            </td>
                            <td class="px-6 py-4 text-sm text-gray-600">
                                <?php echo Utils::formatDateTime($log['created_at']); ?>
                            </td>
                            <td class="px-6 py-4 text-sm">
                                <?php if ($log['error_message']): ?>
                                <span class="text-red-600 cursor-help" title="<?php echo htmlspecialchars($log['error_message']); ?>">
                                    <i class="fas fa-exclamation-circle"></i>
                                </span>
                                <?php endif; ?>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>

            <!-- Pagination -->
            <?php if ($total_pages > 1): ?>
            <div class="px-6 py-4 border-t flex justify-center space-x-2">
                <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                <a href="/routex/admin/email-logs?page=<?php echo $i; ?>" class="<?php echo $i === $page ? 'btn-primary' : 'px-4 py-2 border-2 border-gray-300 rounded-lg hover:border-purple-600'; ?>">
                    <?php echo $i; ?>
                </a>
                <?php endfor; ?>
            </div>
            <?php endif; ?>
        </div>
    </div>
</section>

<?php require_once __DIR__ . '/../../includes/footer.php'; ?>
