<?php
require_once 'config.php'; // contains your DB code

$conn = getDB();
$result = $conn->query("SELECT * FROM bm_campaigns ORDER BY created_at DESC");
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Campaign Dashboard</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<style>
body {
    font-family: 'Segoe UI', sans-serif;
    background: #f5f7fb;
    margin: 0;
}

.container {
    padding: 20px;
}

h1 {
    margin-bottom: 20px;
}

.card {
    background: #fff;
    padding: 20px;
    border-radius: 12px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.05);
}

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

th, td {
    padding: 12px;
    text-align: left;
}

th {
    background: #f1f3f6;
}

tr:not(:last-child) {
    border-bottom: 1px solid #eee;
}

.status {
    padding: 5px 10px;
    border-radius: 20px;
    font-size: 12px;
    font-weight: bold;
}

.running { background: #e6f4ff; color: #007bff; }
.completed { background: #e6ffed; color: #28a745; }
.failed { background: #ffe6e6; color: #dc3545; }

.btn {
    padding: 6px 10px;
    border-radius: 6px;
    text-decoration: none;
    font-size: 13px;
    background: #007bff;
    color: white;
}
</style>
</head>

<body>

<div class="container">
    <h1>📧 Campaign Dashboard</h1>

    <div class="card">
        <table>
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Subject</th>
                    <th>From</th>
                    <th>Total</th>
                    <th>Sent</th>
                    <th>Failed</th>
                    <th>Status</th>
                    <th>Created</th>
                    <th>Action</th>
                </tr>
            </thead>

            <tbody>
            <?php while($row = $result->fetch_assoc()): ?>
                <tr>
                    <td><?= $row['id'] ?></td>
                    <td><?= htmlspecialchars($row['subject']) ?></td>
                    <td><?= htmlspecialchars($row['from_email']) ?></td>
                    <td><?= $row['total_clean'] ?></td>
                    <td><?= $row['sent'] ?></td>
                    <td><?= $row['failed'] ?></td>
                    <td>
                        <span class="status <?= $row['status'] ?>">
                            <?= ucfirst($row['status']) ?>
                        </span>
                    </td>
                    <td><?= $row['created_at'] ?></td>
                    <td>
                        <a class="btn" href="campaign_view.php?id=<?= $row['id'] ?>">
                            View
                        </a>
                    </td>
                </tr>
            <?php endwhile; ?>
            </tbody>
        </table>
    </div>
</div>
<link rel="stylesheet" href="https://cdn.datatables.net/1.13.6/css/jquery.dataTables.min.css">
<script src="https://code.jquery.com/jquery-3.7.0.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>

<script>
$(document).ready(function() {
    $('table').DataTable();
});
</script>
</body>
</html>