<?php
require_once __DIR__ . '/../src/bootstrap.php';
require_once __DIR__ . '/../src/functions.php';

$pdo = db();
$flash = getFlash();

$stats = getDashboardStats($pdo);
$campaigns = getCampaigns($pdo, 50);

$statusMap = [
    'queued' => 'Kuyrukta',
    'sending' => 'Gönderiliyor',
    'completed' => 'Tamamlandı',
];
?>
<!doctype html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Mail Paneli</title>
    <style>
        * { box-sizing: border-box; }
        body { margin: 0; font-family: Arial, Helvetica, sans-serif; background: #f5f7fb; color: #111827; }
        .container { max-width: 1280px; margin: 30px auto; padding: 0 16px; }
        .topbar { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 20px; }
        .topbar h1 { margin: 0; font-size: 28px; }
        .actions { display: flex; gap: 10px; flex-wrap: wrap; }
        .btn { display: inline-flex; align-items: center; justify-content: center; text-decoration: none; border: 0; border-radius: 12px; padding: 12px 18px; cursor: pointer; font-weight: 600; font-size: 14px; min-height: 44px; }
        .btn-primary { background: #2563eb; color: #fff; }
        .btn-secondary { background: #e5e7eb; color: #111827; }
        .btn-danger { background: #dc2626; color: #fff; }
        .flash { padding: 14px 16px; border-radius: 12px; margin-bottom: 18px; font-size: 14px; }
        .flash.success { background: #dcfce7; color: #166534; }
        .flash.error { background: #fee2e2; color: #991b1b; }
        .grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 16px; margin-bottom: 24px; }
        .card { background: #fff; border-radius: 18px; box-shadow: 0 10px 30px rgba(0,0,0,0.06); padding: 20px; }
        .stat-title { font-size: 13px; color: #6b7280; margin-bottom: 8px; }
        .stat-value { font-size: 28px; font-weight: 700; }
        .panel-title { margin: 0 0 16px; font-size: 22px; }
        .table-wrap { overflow-x: auto; }
        table { width: 100%; border-collapse: collapse; min-width: 1150px; }
        th, td { text-align: left; padding: 14px 16px; border-bottom: 1px solid #e5e7eb; font-size: 14px; vertical-align: top; }
        th { background: #f9fafb; color: #374151; font-weight: 700; }
        .badge { display: inline-block; padding: 7px 12px; border-radius: 999px; font-size: 12px; font-weight: 700; }
        .badge-success { background: #dcfce7; color: #166534; }
        .badge-failed { background: #fee2e2; color: #991b1b; }
        .badge-open { background: #dbeafe; color: #1d4ed8; }
        .badge-status { background: #ede9fe; color: #6d28d9; }
        .badge-status-completed { background: #dcfce7; color: #166534; }
        .badge-status-sending { background: #fef3c7; color: #92400e; }
        .empty { color: #6b7280; font-size: 14px; padding: 12px 0; }
        .row-actions { display: flex; gap: 8px; flex-wrap: wrap; }
        .date-cell { white-space: nowrap; }
        @media (max-width: 1100px) { .grid { grid-template-columns: repeat(2, 1fr); } }
        @media (max-width: 640px) {
            .grid { grid-template-columns: 1fr; }
            .topbar h1 { font-size: 22px; }
            .actions { width: 100%; }
            .btn { width: 100%; }
        }
    </style>
</head>
<body>
<div class="container">
    <div class="topbar">
        <h1>Mail Paneli</h1>
        <div class="actions">
            <a href="customer_groups.php" class="btn btn-secondary">Müşteri Grupları</a>
            <a href="analytics.php" class="btn btn-secondary">Grafikler</a>
            <a href="compose.php" class="btn btn-primary">Yeni Mail Oluştur</a>
            <form method="post" action="delete_all_campaigns.php" onsubmit="return confirm('Tüm kampanyaları silmek istediğine emin misin? Bu işlem geri alınamaz.');" style="display:inline;">
                <button type="submit" class="btn btn-danger">Tüm Kayıtları Sil</button>
            </form>
        </div>
    </div>

    <?php if ($flash): ?>
        <div class="flash <?= e($flash['type']) ?>"><?= e($flash['message']) ?></div>
    <?php endif; ?>

    <div class="grid">
        <div class="card"><div class="stat-title">Kampanyalar</div><div class="stat-value"><?= (int)$stats['campaigns'] ?></div></div>
        <div class="card"><div class="stat-title">Toplam Gönderim</div><div class="stat-value"><?= (int)$stats['total_sent'] ?></div></div>
        <div class="card"><div class="stat-title">Başarılı</div><div class="stat-value"><?= (int)$stats['success'] ?></div></div>
        <div class="card"><div class="stat-title">Başarısız</div><div class="stat-value"><?= (int)$stats['failed'] ?></div></div>
        <div class="card"><div class="stat-title">Açılan</div><div class="stat-value"><?= (int)$stats['opened'] ?></div></div>
    </div>

    <div class="card">
        <h2 class="panel-title">Son Kampanyalar</h2>
        <?php if (!$campaigns): ?>
            <div class="empty">Henüz kampanya yok.</div>
        <?php else: ?>
            <div class="table-wrap">
                <table>
                    <thead>
                        <tr>
                            <th>ID</th><th>Konu</th><th>Durum</th><th>Toplam</th><th>Başarılı</th><th>Başarısız</th><th>Açılan</th><th>Başarı Oranı</th><th>Açılma Oranı</th><th>Tarih</th><th>Detay</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($campaigns as $campaign): ?>
                        <?php
                        $total = (int)$campaign['total_count'];
                        $success = (int)$campaign['success_count'];
                        $failed = (int)$campaign['failed_count'];
                        $opened = (int)$campaign['opened_count'];
                        $statusRaw = $campaign['status'] ?? 'queued';
                        $statusText = $statusMap[$statusRaw] ?? $statusRaw;
                        $statusClass = 'badge-status';
                        if ($statusRaw === 'completed') $statusClass = 'badge-status-completed';
                        elseif ($statusRaw === 'sending') $statusClass = 'badge-status-sending';
                        ?>
                        <tr>
                            <td>#<?= (int)$campaign['id'] ?></td>
                            <td><?= e($campaign['subject']) ?></td>
                            <td><span class="badge <?= e($statusClass) ?>"><?= e($statusText) ?></span></td>
                            <td><?= $total ?></td>
                            <td><span class="badge badge-success"><?= $success ?></span></td>
                            <td><span class="badge badge-failed"><?= $failed ?></span></td>
                            <td><span class="badge badge-open"><?= $opened ?></span></td>
                            <td>%<?= successRate($success, $total) ?></td>
                            <td>%<?= openedRate($opened, $success) ?></td>
                            <td class="date-cell"><?= e($campaign['created_at']) ?></td>
                            <td>
                                <div class="row-actions">
                                    <a href="campaign.php?id=<?= (int)$campaign['id'] ?>" class="btn btn-secondary">Görüntüle</a>
                                    <form method="post" action="delete_campaign.php" onsubmit="return confirm('Bu kampanyayı silmek istediğine emin misin?');" style="display:inline;">
                                        <input type="hidden" name="id" value="<?= (int)$campaign['id'] ?>">
                                        <button type="submit" class="btn btn-danger">Sil</button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>
</div>
</body>
</html>
