<?php
/**
 * Admin - Bildirimler Yönetimi
 * Site genelinde gösterilecek bildirim/duyuru yönetimi
 * @version 1.0.0
 */

require_once __DIR__ . '/../config.php';

// Admin kontrolü
if (!isLoggedIn() || !isAdmin()) {
    setFlash('error', 'Bu sayfaya erişim yetkiniz yok.');
    header('Location: ' . SITE_URL . '/giris.php');
    exit;
}

// Hedef kitle seçenekleri
$targetOptions = [
    'all' => 'Herkese',
    'users' => 'Sadece Üyelere',
    'guests' => 'Sadece Ziyaretçilere'
];

// Popüler ikonlar
$popularIcons = [
    'fas fa-bell' => 'Zil',
    'fas fa-info-circle' => 'Bilgi',
    'fas fa-exclamation-triangle' => 'Uyarı',
    'fas fa-check-circle' => 'Başarı',
    'fas fa-times-circle' => 'Hata',
    'fas fa-gift' => 'Hediye',
    'fas fa-percent' => 'İndirim',
    'fas fa-bullhorn' => 'Duyuru',
    'fas fa-star' => 'Yıldız',
    'fas fa-fire' => 'Ateş',
    'fas fa-bolt' => 'Şimşek',
    'fas fa-heart' => 'Kalp',
    'fas fa-rocket' => 'Roket',
    'fas fa-crown' => 'Taç',
    'fas fa-tag' => 'Etiket',
    'fas fa-clock' => 'Saat',
    'fas fa-calendar' => 'Takvim',
    'fas fa-envelope' => 'Zarf',
    'fas fa-shield-alt' => 'Kalkan',
    'fas fa-cog' => 'Ayar'
];

// Popüler renkler
$popularColors = [
    '#6366f1' => 'Mor',
    '#3b82f6' => 'Mavi',
    '#10b981' => 'Yeşil',
    '#f59e0b' => 'Turuncu',
    '#ef4444' => 'Kırmızı',
    '#8b5cf6' => 'Lila',
    '#ec4899' => 'Pembe',
    '#06b6d4' => 'Cyan',
    '#84cc16' => 'Lime',
    '#f97316' => 'Orange'
];

// Bildirim sil
if (isset($_GET['delete'])) {
    $deleteId = (int)$_GET['delete'];
    try {
        $db->prepare("DELETE FROM notifications WHERE id = ?")->execute([$deleteId]);
        setFlash('success', 'Bildirim silindi.');
    } catch (PDOException $e) {
        setFlash('error', 'Bildirim silinemedi.');
    }
    header('Location: ' . SITE_URL . '/admin/bildirimler.php');
    exit;
}

// Durum değiştir
if (isset($_GET['toggle'])) {
    $toggleId = (int)$_GET['toggle'];
    try {
        $db->prepare("UPDATE notifications SET is_active = NOT is_active WHERE id = ?")->execute([$toggleId]);
        setFlash('success', 'Bildirim durumu güncellendi.');
    } catch (PDOException $e) {
        setFlash('error', 'Durum güncellenemedi.');
    }
    header('Location: ' . SITE_URL . '/admin/bildirimler.php');
    exit;
}

// Bildirim düzenleme
$editMode = false;
$editNotification = null;
$errors = [];

if (isset($_GET['edit'])) {
    $editId = (int)$_GET['edit'];
    $stmt = $db->prepare("SELECT * FROM notifications WHERE id = ?");
    $stmt->execute([$editId]);
    $editNotification = $stmt->fetch(PDO::FETCH_ASSOC);
    if ($editNotification) {
        $editMode = true;
    }
}

// Form gönderildi
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $title = trim($_POST['title'] ?? '');
    $message = trim($_POST['message'] ?? '');
    $icon = trim($_POST['icon'] ?? 'fas fa-bell');
    $iconColor = trim($_POST['icon_color'] ?? '#6366f1');
    $link = trim($_POST['link'] ?? '');
    $linkText = trim($_POST['link_text'] ?? '');
    $target = $_POST['target'] ?? 'all';
    $startDate = $_POST['start_date'] ?? null;
    $endDate = $_POST['end_date'] ?? null;
    $isActive = isset($_POST['is_active']) ? 1 : 0;
    $sortOrder = (int)($_POST['sort_order'] ?? 0);
    
    // Validasyon
    if (empty($title)) {
        $errors[] = 'Başlık gereklidir.';
    }
    if (empty($message)) {
        $errors[] = 'Mesaj gereklidir.';
    }
    if (!array_key_exists($target, $targetOptions)) {
        $target = 'all';
    }
    
    // Renk validasyonu
    if (!preg_match('/^#[a-fA-F0-9]{6}$/', $iconColor)) {
        $iconColor = '#6366f1';
    }
    
    // Tarih kontrolü
    $startDate = !empty($startDate) ? $startDate : null;
    $endDate = !empty($endDate) ? $endDate : null;
    
    if (empty($errors)) {
        try {
            if (isset($_POST['notification_id']) && $_POST['notification_id'] > 0) {
                // Güncelle
                $stmt = $db->prepare("
                    UPDATE notifications SET 
                        title = ?, message = ?, icon = ?, icon_color = ?,
                        link = ?, link_text = ?, target = ?,
                        start_date = ?, end_date = ?, is_active = ?, sort_order = ?
                    WHERE id = ?
                ");
                $stmt->execute([
                    $title, $message, $icon, $iconColor,
                    $link ?: null, $linkText ?: null, $target,
                    $startDate, $endDate, $isActive, $sortOrder,
                    $_POST['notification_id']
                ]);
                setFlash('success', 'Bildirim güncellendi.');
            } else {
                // Yeni ekle
                $stmt = $db->prepare("
                    INSERT INTO notifications 
                    (title, message, icon, icon_color, link, link_text, target, start_date, end_date, is_active, sort_order, created_at)
                    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())
                ");
                $stmt->execute([
                    $title, $message, $icon, $iconColor,
                    $link ?: null, $linkText ?: null, $target,
                    $startDate, $endDate, $isActive, $sortOrder
                ]);
                setFlash('success', 'Bildirim eklendi.');
            }
            
            header('Location: ' . SITE_URL . '/admin/bildirimler.php');
            exit;
            
        } catch (PDOException $e) {
            $errors[] = 'Veritabanı hatası: ' . $e->getMessage();
        }
    }
}

// Bildirimleri çek
try {
    $notifications = $db->query("
        SELECT * FROM notifications 
        ORDER BY sort_order DESC, created_at DESC
    ")->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    $notifications = [];
}

// İstatistikler
$stats = [
    'total' => count($notifications),
    'active' => 0,
    'scheduled' => 0,
    'expired' => 0
];

$now = date('Y-m-d H:i:s');
foreach ($notifications as $n) {
    if ($n['is_active']) {
        if ($n['end_date'] && $n['end_date'] < $now) {
            $stats['expired']++;
        } elseif ($n['start_date'] && $n['start_date'] > $now) {
            $stats['scheduled']++;
        } else {
            $stats['active']++;
        }
    }
}

$adminPageTitle = 'Bildirimler';
require_once __DIR__ . '/header.php';
?>

<style>
.stats-row {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 15px;
    margin-bottom: 25px;
}
.stat-card {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 20px;
    text-align: center;
}
.stat-card h3 {
    font-size: 32px;
    font-weight: 800;
    margin: 0 0 5px;
}
.stat-card p {
    font-size: 12px;
    color: var(--text-muted);
    margin: 0;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.content-grid {
    display: grid;
    grid-template-columns: 1fr 420px;
    gap: 25px;
}

/* Bildirim Listesi */
.notification-list {
    display: flex;
    flex-direction: column;
    gap: 15px;
}

.notification-item {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: 14px;
    padding: 20px;
    transition: all 0.2s;
}
.notification-item:hover {
    border-color: rgba(var(--primary-rgb), 0.3);
}
.notification-item.inactive {
    opacity: 0.6;
}

.notification-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 12px;
    gap: 15px;
}

.notification-title {
    display: flex;
    align-items: center;
    gap: 14px;
    flex: 1;
}

.notification-icon {
    width: 48px;
    height: 48px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 22px;
    flex-shrink: 0;
}

.notification-title-text h4 {
    font-size: 16px;
    font-weight: 600;
    margin: 0 0 4px;
}
.notification-title-text span {
    font-size: 12px;
    color: var(--text-muted);
}

.notification-badges {
    display: flex;
    gap: 8px;
    flex-shrink: 0;
    flex-wrap: wrap;
}

.badge-status {
    padding: 5px 12px;
    border-radius: 6px;
    font-size: 11px;
    font-weight: 600;
    display: inline-flex;
    align-items: center;
    gap: 5px;
}
.badge-active { background: rgba(16, 185, 129, 0.15); color: #10b981; }
.badge-inactive { background: rgba(107, 114, 128, 0.15); color: #6b7280; }
.badge-scheduled { background: rgba(59, 130, 246, 0.15); color: #3b82f6; }
.badge-expired { background: rgba(239, 68, 68, 0.15); color: #ef4444; }

.notification-content {
    font-size: 14px;
    color: var(--text-secondary);
    line-height: 1.6;
    margin-bottom: 15px;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.notification-link {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
    color: var(--primary);
    margin-bottom: 15px;
}
.notification-link i { font-size: 11px; }

.notification-meta {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-top: 15px;
    border-top: 1px solid var(--border-color);
    flex-wrap: wrap;
    gap: 10px;
}

.notification-info {
    display: flex;
    gap: 15px;
    font-size: 12px;
    color: var(--text-muted);
    flex-wrap: wrap;
}
.notification-info span {
    display: flex;
    align-items: center;
    gap: 5px;
}
.notification-info i { font-size: 11px; }

.notification-actions {
    display: flex;
    gap: 8px;
}

.action-btn {
    width: 34px;
    height: 34px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    border: none;
    cursor: pointer;
    transition: all 0.2s;
}
.action-btn.edit { background: rgba(59, 130, 246, 0.15); color: #3b82f6; }
.action-btn.edit:hover { background: #3b82f6; color: #fff; }
.action-btn.toggle { background: rgba(245, 158, 11, 0.15); color: #f59e0b; }
.action-btn.toggle:hover { background: #f59e0b; color: #fff; }
.action-btn.delete { background: rgba(239, 68, 68, 0.15); color: #ef4444; }
.action-btn.delete:hover { background: #ef4444; color: #fff; }

/* Form Card */
.form-card {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: 16px;
    position: sticky;
    top: 85px;
}

.form-card-header {
    padding: 20px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    gap: 12px;
}
.form-card-header .icon {
    width: 48px;
    height: 48px;
    background: rgba(var(--primary-rgb), 0.15);
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 22px;
    color: var(--primary);
}
.form-card-header h3 {
    font-size: 17px;
    font-weight: 600;
    margin: 0;
}
.form-card-header p {
    font-size: 13px;
    color: var(--text-muted);
    margin: 4px 0 0;
}

.form-card-body {
    padding: 20px;
}

.form-group {
    margin-bottom: 18px;
}
.form-group:last-child {
    margin-bottom: 0;
}

.form-label {
    display: block;
    font-size: 13px;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 8px;
}
.form-label .required {
    color: var(--danger);
}

.form-hint {
    font-size: 11px;
    color: var(--text-muted);
    margin-top: 5px;
}

.form-control, .form-select {
    width: 100%;
    padding: 11px 14px;
    background: var(--bg-dark);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    color: var(--text-primary);
    font-size: 14px;
    transition: all 0.2s;
}
.form-control:focus, .form-select:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(var(--primary-rgb), 0.15);
    outline: none;
}

textarea.form-control {
    min-height: 100px;
    resize: vertical;
}

.form-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
}

/* İkon Seçici */
.icon-selector {
    display: grid;
    grid-template-columns: repeat(10, 1fr);
    gap: 6px;
    padding: 12px;
    background: var(--bg-dark);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    max-height: 150px;
    overflow-y: auto;
}

.icon-option {
    width: 36px;
    height: 36px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-card);
    border: 2px solid transparent;
    cursor: pointer;
    font-size: 16px;
    color: var(--text-secondary);
    transition: all 0.2s;
}
.icon-option:hover {
    border-color: rgba(var(--primary-rgb), 0.3);
    color: var(--text-primary);
}
.icon-option.selected {
    border-color: var(--primary);
    background: rgba(var(--primary-rgb), 0.15);
    color: var(--primary);
}

/* Renk Seçici */
.color-selector {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.color-option {
    width: 36px;
    height: 36px;
    border-radius: 8px;
    cursor: pointer;
    border: 3px solid transparent;
    transition: all 0.2s;
}
.color-option:hover {
    transform: scale(1.1);
}
.color-option.selected {
    border-color: #fff;
    box-shadow: 0 0 0 2px var(--primary);
}

.custom-color-input {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 10px;
}
.custom-color-input input[type="color"] {
    width: 40px;
    height: 36px;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    background: transparent;
}
.custom-color-input input[type="text"] {
    flex: 1;
}

/* Checkbox */
.form-check {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px;
    background: var(--bg-dark);
    border-radius: 10px;
    cursor: pointer;
    margin-bottom: 10px;
}
.form-check:last-child {
    margin-bottom: 0;
}
.form-check input[type="checkbox"] {
    width: 18px;
    height: 18px;
    accent-color: var(--primary);
    cursor: pointer;
}
.form-check-label {
    font-size: 13px;
    color: var(--text-primary);
    cursor: pointer;
    flex: 1;
}
.form-check-label small {
    display: block;
    font-size: 11px;
    color: var(--text-muted);
    margin-top: 2px;
}

/* Preview */
.preview-box {
    background: var(--bg-dark);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    padding: 15px;
    margin-top: 18px;
}

.preview-label {
    font-size: 11px;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 10px;
}

.preview-notification {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 15px;
    display: flex;
    align-items: flex-start;
    gap: 14px;
}

.preview-icon {
    width: 42px;
    height: 42px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    flex-shrink: 0;
}

.preview-content {
    flex: 1;
}
.preview-content .title {
    font-weight: 600;
    font-size: 14px;
    margin-bottom: 4px;
    color: var(--text-primary);
}
.preview-content .message {
    font-size: 13px;
    color: var(--text-secondary);
    line-height: 1.5;
}
.preview-content .link {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    margin-top: 8px;
    font-size: 12px;
    font-weight: 600;
}

/* Form Actions */
.form-actions {
    display: flex;
    gap: 10px;
    padding-top: 18px;
    border-top: 1px solid var(--border-color);
    margin-top: 18px;
}

.btn-submit {
    flex: 1;
    background: var(--gradient);
    border: none;
    color: #fff;
    padding: 14px 20px;
    border-radius: 10px;
    font-weight: 600;
    font-size: 14px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    transition: all 0.3s;
}
.btn-submit:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 25px rgba(var(--primary-rgb), 0.4);
}

.btn-cancel {
    background: var(--bg-hover);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    padding: 14px 20px;
    border-radius: 10px;
    font-weight: 600;
    font-size: 14px;
    cursor: pointer;
    transition: all 0.2s;
    text-decoration: none;
    display: flex;
    align-items: center;
    justify-content: center;
}
.btn-cancel:hover {
    background: var(--bg-card);
    color: var(--text-primary);
}

/* Empty State */
.empty-state {
    text-align: center;
    padding: 60px 20px;
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: 14px;
}
.empty-state i {
    font-size: 50px;
    color: var(--text-muted);
    opacity: 0.3;
    margin-bottom: 15px;
}
.empty-state h4 {
    font-size: 18px;
    margin-bottom: 8px;
}
.empty-state p {
    color: var(--text-secondary);
    margin-bottom: 0;
}

/* Responsive */
@media (max-width: 1199px) {
    .content-grid {
        grid-template-columns: 1fr;
    }
    .form-card {
        position: static;
        order: -1;
    }
}

@media (max-width: 768px) {
    .stats-row {
        grid-template-columns: repeat(2, 1fr);
    }
    .form-row {
        grid-template-columns: 1fr;
    }
    .icon-selector {
        grid-template-columns: repeat(8, 1fr);
    }
}

@media (max-width: 576px) {
    .stats-row {
        grid-template-columns: 1fr;
    }
    .notification-header {
        flex-direction: column;
    }
    .notification-badges {
        align-self: flex-start;
    }
    .icon-selector {
        grid-template-columns: repeat(6, 1fr);
    }
}
</style>

<?php if (!empty($errors)): ?>
<div class="alert alert-danger">
    <i class="fas fa-exclamation-circle"></i>
    <?php echo implode('<br>', $errors); ?>
</div>
<?php endif; ?>

<!-- İstatistikler -->
<div class="stats-row">
    <div class="stat-card">
        <h3><?php echo $stats['total']; ?></h3>
        <p>Toplam</p>
    </div>
    <div class="stat-card">
        <h3 style="color: #10b981;"><?php echo $stats['active']; ?></h3>
        <p>Aktif</p>
    </div>
    <div class="stat-card">
        <h3 style="color: #3b82f6;"><?php echo $stats['scheduled']; ?></h3>
        <p>Planlanmış</p>
    </div>
    <div class="stat-card">
        <h3 style="color: #ef4444;"><?php echo $stats['expired']; ?></h3>
        <p>Süresi Dolmuş</p>
    </div>
</div>

<div class="content-grid">
    <!-- Sol: Bildirim Listesi -->
    <div>
        <?php if (empty($notifications)): ?>
        <div class="empty-state">
            <i class="fas fa-bell-slash"></i>
            <h4>Henüz Bildirim Yok</h4>
            <p>Sağdaki formu kullanarak yeni bir bildirim ekleyebilirsiniz.</p>
        </div>
        <?php else: ?>
        <div class="notification-list">
            <?php foreach ($notifications as $notification): 
                $now = date('Y-m-d H:i:s');
                
                // Durum belirleme
                $statusClass = 'badge-inactive';
                $statusText = 'Pasif';
                $statusIcon = 'fas fa-pause';
                
                if ($notification['is_active']) {
                    if ($notification['end_date'] && $notification['end_date'] < $now) {
                        $statusClass = 'badge-expired';
                        $statusText = 'Süresi Dolmuş';
                        $statusIcon = 'fas fa-clock';
                    } elseif ($notification['start_date'] && $notification['start_date'] > $now) {
                        $statusClass = 'badge-scheduled';
                        $statusText = 'Planlanmış';
                        $statusIcon = 'fas fa-calendar-alt';
                    } else {
                        $statusClass = 'badge-active';
                        $statusText = 'Aktif';
                        $statusIcon = 'fas fa-check';
                    }
                }
                
                $iconColor = $notification['icon_color'] ?? '#6366f1';
                $icon = $notification['icon'] ?? 'fas fa-bell';
            ?>
            <div class="notification-item <?php echo !$notification['is_active'] ? 'inactive' : ''; ?>">
                <div class="notification-header">
                    <div class="notification-title">
                        <div class="notification-icon" style="background: <?php echo $iconColor; ?>22; color: <?php echo $iconColor; ?>;">
                            <i class="<?php echo clean($icon); ?>"></i>
                        </div>
                        <div class="notification-title-text">
                            <h4><?php echo clean($notification['title']); ?></h4>
                            <span>
                                <?php echo $targetOptions[$notification['target']] ?? 'Herkese'; ?>
                                <?php if ($notification['sort_order'] > 0): ?>
                                 • Sıra: <?php echo $notification['sort_order']; ?>
                                <?php endif; ?>
                            </span>
                        </div>
                    </div>
                    <div class="notification-badges">
                        <span class="badge-status <?php echo $statusClass; ?>">
                            <i class="<?php echo $statusIcon; ?>"></i> <?php echo $statusText; ?>
                        </span>
                    </div>
                </div>
                
                <div class="notification-content">
                    <?php echo clean($notification['message']); ?>
                </div>
                
                <?php if (!empty($notification['link']) && !empty($notification['link_text'])): ?>
                <div class="notification-link">
                    <i class="fas fa-external-link-alt"></i>
                    <?php echo clean($notification['link_text']); ?>
                </div>
                <?php endif; ?>
                
                <div class="notification-meta">
                    <div class="notification-info">
                        <span><i class="fas fa-calendar-plus"></i> <?php echo date('d.m.Y', strtotime($notification['created_at'])); ?></span>
                        <?php if ($notification['start_date']): ?>
                        <span><i class="fas fa-play"></i> <?php echo date('d.m.Y H:i', strtotime($notification['start_date'])); ?></span>
                        <?php endif; ?>
                        <?php if ($notification['end_date']): ?>
                        <span><i class="fas fa-stop"></i> <?php echo date('d.m.Y H:i', strtotime($notification['end_date'])); ?></span>
                        <?php endif; ?>
                    </div>
                    <div class="notification-actions">
                        <a href="?edit=<?php echo $notification['id']; ?>" class="action-btn edit" title="Düzenle">
                            <i class="fas fa-edit"></i>
                        </a>
                        <a href="?toggle=<?php echo $notification['id']; ?>" class="action-btn toggle" title="<?php echo $notification['is_active'] ? 'Pasif Yap' : 'Aktif Yap'; ?>">
                            <i class="fas fa-<?php echo $notification['is_active'] ? 'pause' : 'play'; ?>"></i>
                        </a>
                        <a href="?delete=<?php echo $notification['id']; ?>" class="action-btn delete" title="Sil" onclick="return confirm('Bu bildirimi silmek istediğinize emin misiniz?')">
                            <i class="fas fa-trash"></i>
                        </a>
                    </div>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
        <?php endif; ?>
    </div>
    
    <!-- Sağ: Form -->
    <div>
        <div class="form-card">
            <div class="form-card-header">
                <div class="icon">
                    <i class="fas fa-<?php echo $editMode ? 'edit' : 'plus'; ?>"></i>
                </div>
                <div>
                    <h3><?php echo $editMode ? 'Bildirimi Düzenle' : 'Yeni Bildirim'; ?></h3>
                    <p><?php echo $editMode ? 'Mevcut bildirimi güncelleyin' : 'Yeni bir bildirim oluşturun'; ?></p>
                </div>
            </div>
            
            <div class="form-card-body">
                <form method="POST" id="notificationForm">
                    <?php if ($editMode): ?>
                    <input type="hidden" name="notification_id" value="<?php echo $editNotification['id']; ?>">
                    <?php endif; ?>
                    
                    <div class="form-group">
                        <label class="form-label">Başlık <span class="required">*</span></label>
                        <input type="text" name="title" class="form-control" placeholder="Bildirim başlığı" 
                               value="<?php echo clean($editMode ? $editNotification['title'] : ($_POST['title'] ?? '')); ?>" required>
                    </div>
                    
                    <div class="form-group">
                        <label class="form-label">Mesaj <span class="required">*</span></label>
                        <textarea name="message" class="form-control" placeholder="Bildirim mesajı..." required><?php echo clean($editMode ? $editNotification['message'] : ($_POST['message'] ?? '')); ?></textarea>
                    </div>
                    
                    <div class="form-group">
                        <label class="form-label">İkon</label>
                        <input type="hidden" name="icon" id="selectedIcon" value="<?php echo clean($editMode ? ($editNotification['icon'] ?? 'fas fa-bell') : 'fas fa-bell'); ?>">
                        <div class="icon-selector">
                            <?php foreach ($popularIcons as $iconClass => $iconName): 
                                $isSelected = $editMode ? ($editNotification['icon'] === $iconClass) : ($iconClass === 'fas fa-bell');
                            ?>
                            <div class="icon-option <?php echo $isSelected ? 'selected' : ''; ?>" data-icon="<?php echo $iconClass; ?>" title="<?php echo $iconName; ?>">
                                <i class="<?php echo $iconClass; ?>"></i>
                            </div>
                            <?php endforeach; ?>
                        </div>
                    </div>
                    
                    <div class="form-group">
                        <label class="form-label">İkon Rengi</label>
                        <div class="color-selector">
                            <?php foreach ($popularColors as $colorCode => $colorName): 
                                $isSelected = $editMode ? ($editNotification['icon_color'] === $colorCode) : ($colorCode === '#6366f1');
                            ?>
                            <div class="color-option <?php echo $isSelected ? 'selected' : ''; ?>" 
                                 style="background: <?php echo $colorCode; ?>;" 
                                 data-color="<?php echo $colorCode; ?>" 
                                 title="<?php echo $colorName; ?>"></div>
                            <?php endforeach; ?>
                        </div>
                        <div class="custom-color-input">
                            <input type="color" id="customColorPicker" value="<?php echo $editMode ? ($editNotification['icon_color'] ?? '#6366f1') : '#6366f1'; ?>">
                            <input type="text" name="icon_color" id="iconColorInput" class="form-control" placeholder="#6366f1" 
                                   value="<?php echo clean($editMode ? ($editNotification['icon_color'] ?? '#6366f1') : '#6366f1'); ?>">
                        </div>
                    </div>
                    
                    <div class="form-row">
                        <div class="form-group">
                            <label class="form-label">Link Metni</label>
                            <input type="text" name="link_text" class="form-control" placeholder="Örn: Detaylar" 
                                   value="<?php echo clean($editMode ? ($editNotification['link_text'] ?? '') : ''); ?>">
                        </div>
                        <div class="form-group">
                            <label class="form-label">Link URL</label>
                            <input type="url" name="link" class="form-control" placeholder="https://..." 
                                   value="<?php echo clean($editMode ? ($editNotification['link'] ?? '') : ''); ?>">
                        </div>
                    </div>
                    
                    <div class="form-group">
                        <label class="form-label">Hedef Kitle</label>
                        <select name="target" class="form-select">
                            <?php foreach ($targetOptions as $key => $label): ?>
                            <option value="<?php echo $key; ?>" <?php echo ($editMode && $editNotification['target'] === $key) ? 'selected' : ''; ?>>
                                <?php echo $label; ?>
                            </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    
                    <div class="form-row">
                        <div class="form-group">
                            <label class="form-label">Başlangıç Tarihi</label>
                            <input type="datetime-local" name="start_date" class="form-control" 
                                   value="<?php echo $editMode && $editNotification['start_date'] ? date('Y-m-d\TH:i', strtotime($editNotification['start_date'])) : ''; ?>">
                            <div class="form-hint">Boş = Hemen başlar</div>
                        </div>
                        <div class="form-group">
                            <label class="form-label">Bitiş Tarihi</label>
                            <input type="datetime-local" name="end_date" class="form-control" 
                                   value="<?php echo $editMode && $editNotification['end_date'] ? date('Y-m-d\TH:i', strtotime($editNotification['end_date'])) : ''; ?>">
                            <div class="form-hint">Boş = Süresiz</div>
                        </div>
                    </div>
                    
                    <div class="form-group">
                        <label class="form-label">Sıralama</label>
                        <input type="number" name="sort_order" class="form-control" min="0" max="999" 
                               value="<?php echo $editMode ? $editNotification['sort_order'] : 0; ?>">
                        <div class="form-hint">Yüksek değer = daha üstte gösterilir</div>
                    </div>
                    
                    <div class="form-group">
                        <label class="form-check">
                            <input type="checkbox" name="is_active" value="1" 
                                   <?php echo (!$editMode || $editNotification['is_active']) ? 'checked' : ''; ?>>
                            <span class="form-check-label">
                                Aktif
                                <small>Bildirimi yayınla</small>
                            </span>
                        </label>
                    </div>
                    
                    <!-- Önizleme -->
                    <div class="preview-box">
                        <div class="preview-label">Önizleme</div>
                        <div class="preview-notification">
                            <div class="preview-icon" id="previewIcon" style="background: #6366f122; color: #6366f1;">
                                <i class="fas fa-bell"></i>
                            </div>
                            <div class="preview-content">
                                <div class="title" id="previewTitle">Bildirim Başlığı</div>
                                <div class="message" id="previewMessage">Bildirim mesajı burada görünecek...</div>
                                <div class="link" id="previewLink" style="display: none; color: #6366f1;">
                                    <i class="fas fa-external-link-alt"></i>
                                    <span></span>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="form-actions">
                        <?php if ($editMode): ?>
                        <a href="?" class="btn-cancel">İptal</a>
                        <?php endif; ?>
                        <button type="submit" class="btn-submit">
                            <i class="fas fa-<?php echo $editMode ? 'save' : 'plus'; ?>"></i>
                            <?php echo $editMode ? 'Güncelle' : 'Bildirim Ekle'; ?>
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<script>
// Değişkenler
var currentIcon = document.getElementById('selectedIcon').value;
var currentColor = document.getElementById('iconColorInput').value;

// İkon seçimi
document.querySelectorAll('.icon-option').forEach(function(option) {
    option.addEventListener('click', function() {
        document.querySelectorAll('.icon-option').forEach(function(o) {
            o.classList.remove('selected');
        });
        this.classList.add('selected');
        currentIcon = this.dataset.icon;
        document.getElementById('selectedIcon').value = currentIcon;
        updatePreview();
    });
});

// Renk seçimi
document.querySelectorAll('.color-option').forEach(function(option) {
    option.addEventListener('click', function() {
        document.querySelectorAll('.color-option').forEach(function(o) {
            o.classList.remove('selected');
        });
        this.classList.add('selected');
        currentColor = this.dataset.color;
        document.getElementById('iconColorInput').value = currentColor;
        document.getElementById('customColorPicker').value = currentColor;
        updatePreview();
    });
});

// Renk picker
document.getElementById('customColorPicker').addEventListener('input', function() {
    currentColor = this.value;
    document.getElementById('iconColorInput').value = currentColor;
    document.querySelectorAll('.color-option').forEach(function(o) {
        o.classList.remove('selected');
    });
    updatePreview();
});

// Renk input
document.getElementById('iconColorInput').addEventListener('input', function() {
    if (/^#[a-fA-F0-9]{6}$/.test(this.value)) {
        currentColor = this.value;
        document.getElementById('customColorPicker').value = currentColor;
        document.querySelectorAll('.color-option').forEach(function(o) {
            o.classList.remove('selected');
            if (o.dataset.color === currentColor) {
                o.classList.add('selected');
            }
        });
        updatePreview();
    }
});

// Önizleme güncelle
function updatePreview() {
    var title = document.querySelector('input[name="title"]').value || 'Bildirim Başlığı';
    var message = document.querySelector('textarea[name="message"]').value || 'Bildirim mesajı burada görünecek...';
    var linkText = document.querySelector('input[name="link_text"]').value;
    var link = document.querySelector('input[name="link"]').value;
    
    // İkon
    var previewIcon = document.getElementById('previewIcon');
    previewIcon.style.background = currentColor + '22';
    previewIcon.style.color = currentColor;
    previewIcon.innerHTML = '<i class="' + currentIcon + '"></i>';
    
    // İçerik
    document.getElementById('previewTitle').textContent = title;
    document.getElementById('previewMessage').textContent = message.substring(0, 150) + (message.length > 150 ? '...' : '');
    
    // Link
    var previewLink = document.getElementById('previewLink');
    if (linkText && link) {
        previewLink.style.display = 'inline-flex';
        previewLink.style.color = currentColor;
        previewLink.querySelector('span').textContent = linkText;
    } else {
        previewLink.style.display = 'none';
    }
}

// Input eventleri
document.querySelector('input[name="title"]').addEventListener('input', updatePreview);
document.querySelector('textarea[name="message"]').addEventListener('input', updatePreview);
document.querySelector('input[name="link_text"]').addEventListener('input', updatePreview);
document.querySelector('input[name="link"]').addEventListener('input', updatePreview);

// Sayfa yüklendiğinde önizleme güncelle
updatePreview();
</script>

<?php require_once __DIR__ . '/footer.php'; ?>