<?php
session_start();
require_once '../config/database.php';
require_once '../includes/email_service.php';

if (!isset($_SESSION['admin_id'])) {
    header('Location: login.php');
    exit;
}

emailEnsureDefaults($pdo);

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

$stmt = $pdo->query("SELECT COUNT(*) FROM email_logs");
$total = (int)$stmt->fetchColumn();
$totalPages = max(1, (int)ceil($total / $limit));

$stmt = $pdo->prepare("SELECT * FROM email_logs ORDER BY id DESC LIMIT " . (int)$limit . " OFFSET " . (int)$offset);
$stmt->execute();
$logs = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>لاگ ایمیل - پنل مدیریت</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.rtl.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Vazirmatn:wght@400;600;700&display=swap" rel="stylesheet">
    <link href="assets/css/admin.css" rel="stylesheet">
</head>
<body class="admin-app">
    <?php include 'includes/menu.php'; ?>
    <?php include 'includes/header_bar.php'; ?>

    <main class="main">
        <div class="main-content">
            <?php
            $admin_page_title = 'لاگ ارسال ایمیل';
            $admin_page_icon = 'fas fa-list';
            $admin_page_actions = '<a href="email_settings.php" class="btn btn-outline-primary btn-sm"><i class="fas fa-envelope ms-1"></i>تنظیمات ایمیل</a>';
            include 'includes/topbar.php';
            ?>

                <div class="card">
                    <div class="card-body p-0">
                        <div class="table-responsive">
                            <table class="table table-bordered mb-0">
                                <thead>
                                    <tr>
                                        <th>ID</th>
                                        <th>رویداد</th>
                                        <th>گیرنده</th>
                                        <th>موضوع</th>
                                        <th>وضعیت</th>
                                        <th>خطا</th>
                                        <th>زمان</th>
                                    </tr>
                                </thead>
                                <tbody>
                                <?php if (empty($logs)): ?>
                                    <tr><td colspan="7" class="text-center py-4">لاگی ثبت نشده است.</td></tr>
                                <?php else: ?>
                                    <?php foreach ($logs as $log): ?>
                                    <tr>
                                        <td><?= (int)$log['id'] ?></td>
                                        <td><?= htmlspecialchars($log['event_key']) ?></td>
                                        <td><?= htmlspecialchars($log['recipient_email']) ?></td>
                                        <td><?= htmlspecialchars($log['subject']) ?></td>
                                        <td>
                                            <?php
                                            $status = $log['status'];
                                            $cls = 'secondary';
                                            if ($status === 'sent') $cls = 'success';
                                            if ($status === 'failed') $cls = 'danger';
                                            if ($status === 'skipped') $cls = 'warning';
                                            ?>
                                            <span class="badge bg-<?= $cls ?>"><?= htmlspecialchars($status) ?></span>
                                        </td>
                                        <td style="max-width:250px;white-space:normal;"><?= htmlspecialchars((string)$log['error_message']) ?></td>
                                        <td><?= htmlspecialchars($log['created_at']) ?></td>
                                    </tr>
                                    <?php endforeach; ?>
                                <?php endif; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>

                <?php if ($totalPages > 1): ?>
                <nav class="mt-4">
                    <ul class="pagination justify-content-center">
                        <?php for ($i = 1; $i <= $totalPages; $i++): ?>
                            <li class="page-item <?= $i === $page ? 'active' : '' ?>">
                                <a class="page-link" href="?page=<?= $i ?>"><?= $i ?></a>
                            </li>
                        <?php endfor; ?>
                    </ul>
                </nav>
                <?php endif; ?>
        </div>
    </main>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<?php include 'includes/admin_shell_scripts.php'; ?>
</body>
</html>
