<?php
require_once 'config.php';

$id = (int)$_GET['id'];
$conn = getDB();

$campaign = $conn->query("SELECT * FROM bm_campaigns WHERE id=$id")->fetch_assoc();
$logs = $conn->query("SELECT * FROM bm_email_log WHERE campaign_id=$id ORDER BY id DESC");
?>

<!DOCTYPE html>
<html>
<head>
<title>Campaign Details</title>

<style>
body { font-family: Arial; background: #f5f7fb; padding: 20px; }

.card {
    background: white;
    padding: 20px;
    border-radius: 10px;
    margin-bottom: 20px;
}

table {
    width: 100%;
    border-collapse: collapse;
}

th, td {
    padding: 10px;
    border-bottom: 1px solid #eee;
}

.success { color: green; }
.failed { color: red; }
.skipped { color: orange; }
</style>
</head>

<body>

<div class="card">
    <h2><?= htmlspecialchars($campaign['subject']) ?></h2>
    <p><strong>From:</strong> <?= $campaign['from_email'] ?></p>
    <p><strong>Total Sent:</strong> <?= $campaign['sent'] ?></p>
    <p><strong>Failed:</strong> <?= $campaign['failed'] ?></p>
</div>

<div class="card">
    <h3>Email Logs</h3>

    <table>
        <tr>
            <th>Email</th>
            <th>Name</th>
            <th>Status</th>
            <th>Reason</th>
            <th>Time</th>
        </tr>

        <?php while($row = $logs->fetch_assoc()): ?>
        <tr>
            <td><?= $row['email'] ?></td>
            <td><?= $row['first_name'] . ' ' . $row['last_name'] ?></td>
            <td class="<?= $row['status'] ?>">
                <?= $row['status'] ?>
            </td>
            <td><?= $row['reason'] ?></td>
            <td><?= $row['sent_at'] ?></td>
        </tr>
        <?php endwhile; ?>
    </table>
</div>

</body>
</html>