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

$pdo = db();
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

$group = getCustomerGroupById($pdo, $id);
if (!$group) {
    flash('Müşteri grubu bulunamadı.', 'error');
    redirect('customer_groups.php');
}

$emails = getCustomerGroupEmails($pdo, $id);
?>
<!doctype html>
<html lang="tr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Grup Detayı</title>
<style>
* { box-sizing: border-box; }
body { margin: 0; font-family: Arial, Helvetica, sans-serif; background: #f5f7fb; color: #111827; }
.container { max-width: 950px; 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; background: #e5e7eb; color: #111827; }
.card { background: #fff; border-radius: 18px; box-shadow: 0 10px 30px rgba(0,0,0,0.06); padding: 20px; margin-bottom: 20px; }
.meta { color: #6b7280; font-size: 14px; margin-top: 6px; }
.email-list { list-style: none; padding: 0; margin: 0; }
.email-list li { padding: 12px 14px; border-bottom: 1px solid #e5e7eb; font-size: 14px; word-break: break-word; }
.email-list li:last-child { border-bottom: 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>Grup Detayı</h1>
        <div class="actions">
            <a href="customer_groups.php" class="btn">Gruplara Dön</a>
            <a href="compose.php" class="btn">Mail Oluştur</a>
        </div>
    </div>

    <div class="card">
        <h2 style="margin-top:0;"><?= e($group['name']) ?></h2>
        <div class="meta">
            Toplam Benzersiz Mail: <strong><?= count($emails) ?></strong><br>
            Oluşturulma Tarihi: <strong><?= e($group['created_at']) ?></strong>
        </div>
    </div>

    <div class="card">
        <h3 style="margin-top:0;">Mail Adresleri</h3>
        <?php if (!$emails): ?>
            <p>Bu grupta kayıtlı mail adresi yok.</p>
        <?php else: ?>
            <ul class="email-list">
                <?php foreach ($emails as $email): ?>
                    <li><?= e($email) ?></li>
                <?php endforeach; ?>
            </ul>
        <?php endif; ?>
    </div>
</div>
</body>
</html>
