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

$pdo = db();
$flash = getFlash();
$groups = getCustomerGroups($pdo);
?>
<!doctype html>
<html lang="tr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Müşteri Grupları</title>
<style>
* { box-sizing: border-box; }
body { margin: 0; font-family: Arial, Helvetica, sans-serif; background: #f5f7fb; color: #111827; }
.container { max-width: 1200px; 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; }
.card { background: #fff; border-radius: 18px; box-shadow: 0 10px 30px rgba(0,0,0,0.06); padding: 20px; }
.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; }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; min-width: 760px; }
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; }
.row-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.empty { color: #6b7280; font-size: 14px; padding: 12px 0; }
@media (max-width: 640px) { .topbar h1 { font-size: 22px; } .actions { width: 100%; } .btn { width: 100%; } }
</style>
</head>
<body>
<div class="container">
    <div class="topbar">
        <h1>Müşteri Grupları</h1>
        <div class="actions">
            <a href="index.php" class="btn btn-secondary">Panele Dön</a>
            <a href="compose.php" class="btn btn-secondary">Mail Oluştur</a>
            <a href="customer_group_create.php" class="btn btn-primary">Yeni Müşteri Grubu</a>
        </div>
    </div>

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

    <div class="card">
        <?php if (!$groups): ?>
            <div class="empty">Henüz müşteri grubu yok.</div>
        <?php else: ?>
            <div class="table-wrap">
                <table>
                    <thead><tr><th>ID</th><th>Grup Adı</th><th>Mail Sayısı</th><th>Oluşturulma</th><th>İşlemler</th></tr></thead>
                    <tbody>
                    <?php foreach ($groups as $group): ?>
                        <tr>
                            <td>#<?= (int)$group['id'] ?></td>
                            <td><?= e($group['name']) ?></td>
                            <td><?= (int)$group['email_count'] ?></td>
                            <td><?= e($group['created_at']) ?></td>
                            <td>
                                <div class="row-actions">
                                    <a href="customer_group_view.php?id=<?= (int)$group['id'] ?>" class="btn btn-secondary">Görüntüle</a>
                                    <form method="post" action="customer_group_delete.php" onsubmit="return confirm('Bu müşteri grubunu silmek istediğine emin misin?');" style="display:inline;">
                                        <input type="hidden" name="id" value="<?= (int)$group['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>
