<?php
/**
 * Admin - Fake Bildirim Yönetimi
 * @version 1.0.1
 */

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

// Demo mod kontrolü
$demoMode = $settings->get('demo_mode', 0) == 1;

// Ayarları çek
try {
    $settingsStmt = $db->query("SELECT * FROM fake_notification_settings WHERE id = 1");
    $fakeSettings = $settingsStmt->fetch(PDO::FETCH_ASSOC);
    
    if (!$fakeSettings) {
        $db->exec("INSERT INTO fake_notification_settings (id, is_active, interval_min, interval_max, display_duration, position, show_on_mobile) VALUES (1, 1, 5, 15, 5, 'bottom-left', 1)");
        $fakeSettings = [
            'is_active' => 1,
            'interval_min' => 5,
            'interval_max' => 15,
            'display_duration' => 5,
            'position' => 'bottom-left',
            'show_on_mobile' => 1
        ];
    }
} catch (PDOException $e) {
    $fakeSettings = [];
}

// İsimleri çek
try {
    $namesStmt = $db->query("SELECT * FROM fake_notification_names ORDER BY id DESC");
    $names = $namesStmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    $names = [];
}

// Şehirleri çek
try {
    $citiesStmt = $db->query("SELECT * FROM fake_notification_cities ORDER BY id DESC");
    $cities = $citiesStmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    $cities = [];
}

// Paketleri çek
try {
    $itemsStmt = $db->query("SELECT * FROM fake_notification_items ORDER BY id DESC");
    $items = $itemsStmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    $items = [];
}

// Form mesajları
$message = '';
$messageType = '';

// POST işlemleri
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    
    if ($demoMode && !isset($_POST['update_settings'])) {
        $message = 'Demo modda ekleme, düzenleme ve silme işlemleri yapılamaz!';
        $messageType = 'warning';
    } else {
        
        try {
            // Ayarları güncelle
            if (isset($_POST['update_settings'])) {
                $isActive = isset($_POST['is_active']) ? 1 : 0;
                $intervalMin = max(1, (int)$_POST['interval_min']);
                $intervalMax = max($intervalMin, (int)$_POST['interval_max']);
                $displayDuration = max(1, (int)$_POST['display_duration']);
                $position = in_array($_POST['position'], ['bottom-left', 'bottom-right', 'top-left', 'top-right']) ? $_POST['position'] : 'bottom-left';
                $showOnMobile = isset($_POST['show_on_mobile']) ? 1 : 0;
                
                $stmt = $db->prepare("UPDATE fake_notification_settings SET is_active = ?, interval_min = ?, interval_max = ?, display_duration = ?, position = ?, show_on_mobile = ? WHERE id = 1");
                $stmt->execute([$isActive, $intervalMin, $intervalMax, $displayDuration, $position, $showOnMobile]);
                
                $message = 'Ayarlar başarıyla güncellendi.';
                $messageType = 'success';
                
                $settingsStmt = $db->query("SELECT * FROM fake_notification_settings WHERE id = 1");
                $fakeSettings = $settingsStmt->fetch(PDO::FETCH_ASSOC);
            }
            
            // İsim ekle
            if (isset($_POST['add_name'])) {
                $name = trim($_POST['name']);
                if (!empty($name)) {
                    $stmt = $db->prepare("INSERT INTO fake_notification_names (name, is_active) VALUES (?, 1)");
                    $stmt->execute([$name]);
                    $message = 'İsim başarıyla eklendi.';
                    $messageType = 'success';
                    
                    $namesStmt = $db->query("SELECT * FROM fake_notification_names ORDER BY id DESC");
                    $names = $namesStmt->fetchAll(PDO::FETCH_ASSOC);
                } else {
                    $message = 'İsim boş olamaz.';
                    $messageType = 'danger';
                }
            }
            
            // Toplu isim ekle
            if (isset($_POST['add_names_bulk'])) {
                $namesList = trim($_POST['names_bulk']);
                if (!empty($namesList)) {
                    $namesArray = array_filter(array_map('trim', explode("\n", $namesList)));
                    $addedCount = 0;
                    foreach ($namesArray as $name) {
                        if (!empty($name)) {
                            $stmt = $db->prepare("INSERT INTO fake_notification_names (name, is_active) VALUES (?, 1)");
                            $stmt->execute([$name]);
                            $addedCount++;
                        }
                    }
                    $message = $addedCount . ' isim başarıyla eklendi.';
                    $messageType = 'success';
                    
                    $namesStmt = $db->query("SELECT * FROM fake_notification_names ORDER BY id DESC");
                    $names = $namesStmt->fetchAll(PDO::FETCH_ASSOC);
                }
            }
            
            // İsim sil
            if (isset($_POST['delete_name'])) {
                $id = (int)$_POST['name_id'];
                $stmt = $db->prepare("DELETE FROM fake_notification_names WHERE id = ?");
                $stmt->execute([$id]);
                $message = 'İsim silindi.';
                $messageType = 'success';
                
                $namesStmt = $db->query("SELECT * FROM fake_notification_names ORDER BY id DESC");
                $names = $namesStmt->fetchAll(PDO::FETCH_ASSOC);
            }
            
            // İsim durumu değiştir
            if (isset($_POST['toggle_name'])) {
                $id = (int)$_POST['name_id'];
                $stmt = $db->prepare("UPDATE fake_notification_names SET is_active = NOT is_active WHERE id = ?");
                $stmt->execute([$id]);
                
                $namesStmt = $db->query("SELECT * FROM fake_notification_names ORDER BY id DESC");
                $names = $namesStmt->fetchAll(PDO::FETCH_ASSOC);
            }
            
            // Şehir ekle
            if (isset($_POST['add_city'])) {
                $city = trim($_POST['city']);
                if (!empty($city)) {
                    $stmt = $db->prepare("INSERT INTO fake_notification_cities (city, is_active) VALUES (?, 1)");
                    $stmt->execute([$city]);
                    $message = 'Şehir başarıyla eklendi.';
                    $messageType = 'success';
                    
                    $citiesStmt = $db->query("SELECT * FROM fake_notification_cities ORDER BY id DESC");
                    $cities = $citiesStmt->fetchAll(PDO::FETCH_ASSOC);
                } else {
                    $message = 'Şehir boş olamaz.';
                    $messageType = 'danger';
                }
            }
            
            // Toplu şehir ekle
            if (isset($_POST['add_cities_bulk'])) {
                $citiesList = trim($_POST['cities_bulk']);
                if (!empty($citiesList)) {
                    $citiesArray = array_filter(array_map('trim', explode("\n", $citiesList)));
                    $addedCount = 0;
                    foreach ($citiesArray as $city) {
                        if (!empty($city)) {
                            $stmt = $db->prepare("INSERT INTO fake_notification_cities (city, is_active) VALUES (?, 1)");
                            $stmt->execute([$city]);
                            $addedCount++;
                        }
                    }
                    $message = $addedCount . ' şehir başarıyla eklendi.';
                    $messageType = 'success';
                    
                    $citiesStmt = $db->query("SELECT * FROM fake_notification_cities ORDER BY id DESC");
                    $cities = $citiesStmt->fetchAll(PDO::FETCH_ASSOC);
                }
            }
            
            // Şehir sil
            if (isset($_POST['delete_city'])) {
                $id = (int)$_POST['city_id'];
                $stmt = $db->prepare("DELETE FROM fake_notification_cities WHERE id = ?");
                $stmt->execute([$id]);
                $message = 'Şehir silindi.';
                $messageType = 'success';
                
                $citiesStmt = $db->query("SELECT * FROM fake_notification_cities ORDER BY id DESC");
                $cities = $citiesStmt->fetchAll(PDO::FETCH_ASSOC);
            }
            
            // Şehir durumu değiştir
            if (isset($_POST['toggle_city'])) {
                $id = (int)$_POST['city_id'];
                $stmt = $db->prepare("UPDATE fake_notification_cities SET is_active = NOT is_active WHERE id = ?");
                $stmt->execute([$id]);
                
                $citiesStmt = $db->query("SELECT * FROM fake_notification_cities ORDER BY id DESC");
                $cities = $citiesStmt->fetchAll(PDO::FETCH_ASSOC);
            }
            
            // Paket ekle
            if (isset($_POST['add_item'])) {
                $platform = trim($_POST['platform']);
                $platformIcon = trim($_POST['platform_icon']);
                $platformColor = trim($_POST['platform_color']);
                $packageName = trim($_POST['package_name']);
                $quantityMin = max(1, (int)$_POST['quantity_min']);
                $quantityMax = max($quantityMin, (int)$_POST['quantity_max']);
                
                if (!empty($platform) && !empty($packageName)) {
                    $stmt = $db->prepare("INSERT INTO fake_notification_items (platform, platform_icon, platform_color, package_name, quantity_min, quantity_max, is_active) VALUES (?, ?, ?, ?, ?, ?, 1)");
                    $stmt->execute([$platform, $platformIcon ?: 'fas fa-shopping-cart', $platformColor ?: '#8b5cf6', $packageName, $quantityMin, $quantityMax]);
                    $message = 'Paket başarıyla eklendi.';
                    $messageType = 'success';
                    
                    $itemsStmt = $db->query("SELECT * FROM fake_notification_items ORDER BY id DESC");
                    $items = $itemsStmt->fetchAll(PDO::FETCH_ASSOC);
                } else {
                    $message = 'Platform ve paket adı boş olamaz.';
                    $messageType = 'danger';
                }
            }
            
            // Paket sil
            if (isset($_POST['delete_item'])) {
                $id = (int)$_POST['item_id'];
                $stmt = $db->prepare("DELETE FROM fake_notification_items WHERE id = ?");
                $stmt->execute([$id]);
                $message = 'Paket silindi.';
                $messageType = 'success';
                
                $itemsStmt = $db->query("SELECT * FROM fake_notification_items ORDER BY id DESC");
                $items = $itemsStmt->fetchAll(PDO::FETCH_ASSOC);
            }
            
            // Paket durumu değiştir
            if (isset($_POST['toggle_item'])) {
                $id = (int)$_POST['item_id'];
                $stmt = $db->prepare("UPDATE fake_notification_items SET is_active = NOT is_active WHERE id = ?");
                $stmt->execute([$id]);
                
                $itemsStmt = $db->query("SELECT * FROM fake_notification_items ORDER BY id DESC");
                $items = $itemsStmt->fetchAll(PDO::FETCH_ASSOC);
            }
            
        } catch (PDOException $e) {
            $message = 'Veritabanı hatası: ' . $e->getMessage();
            $messageType = 'danger';
        }
    }
}

// Aktif sayıları
$activeNames = count(array_filter($names, fn($n) => $n['is_active']));
$activeCities = count(array_filter($cities, fn($c) => $c['is_active']));
$activeItems = count(array_filter($items, fn($i) => $i['is_active']));
?>

<style>
/* Sayfa özel stilleri */
.settings-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 20px;
}

.setting-item {
    background: var(--bg-hover);
    border-radius: 12px;
    padding: 20px;
}

.setting-item label {
    display: flex;
    align-items: center;
    gap: 12px;
    cursor: pointer;
    color: var(--text-primary);
    font-weight: 500;
    font-size: 14px;
}

.setting-item .form-check-input {
    width: 44px;
    height: 24px;
    margin: 0;
    cursor: pointer;
}

.setting-item .form-check-input:checked {
    background-color: var(--success);
    border-color: var(--success);
}

.input-group-custom {
    background: var(--bg-hover);
    border-radius: 12px;
    padding: 16px 20px;
}

.input-group-custom label {
    display: block;
    font-size: 13px;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 10px;
}

.input-group-custom .form-control,
.input-group-custom .form-select {
    background: var(--bg-dark);
}

.input-group-custom .form-text {
    color: var(--text-muted);
    font-size: 11px;
    margin-top: 6px;
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 16px;
}

@media (max-width: 768px) {
    .stats-grid {
        grid-template-columns: 1fr;
    }
}

.stat-card {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 24px;
    text-align: center;
}

.stat-card .stat-value {
    font-size: 32px;
    font-weight: 800;
    margin-bottom: 4px;
}

.stat-card .stat-label {
    font-size: 13px;
    color: var(--text-muted);
}

.stat-card.primary .stat-value { color: var(--primary); }
.stat-card.success .stat-value { color: var(--success); }
.stat-card.info .stat-value { color: var(--info); }

/* Tab stilleri */
.custom-tabs {
    display: flex;
    gap: 8px;
    padding: 8px;
    background: var(--bg-card);
    border-radius: 12px;
    margin-bottom: 20px;
    flex-wrap: wrap;
}

.custom-tabs .tab-btn {
    flex: 1;
    min-width: 120px;
    padding: 12px 20px;
    background: transparent;
    border: none;
    border-radius: 8px;
    color: var(--text-secondary);
    font-weight: 600;
    font-size: 14px;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

.custom-tabs .tab-btn:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.custom-tabs .tab-btn.active {
    background: var(--primary);
    color: #fff;
}

.custom-tabs .tab-btn .badge {
    background: rgba(255,255,255,0.2);
    padding: 2px 8px;
    border-radius: 10px;
    font-size: 11px;
}

.custom-tabs .tab-btn.active .badge {
    background: rgba(255,255,255,0.3);
}

/* Liste item */
.list-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding: 16px 20px;
    background: var(--bg-hover);
    border-radius: 10px;
    margin-bottom: 8px;
    flex-wrap: wrap;
}

.list-item:last-child {
    margin-bottom: 0;
}

.list-item-info {
    display: flex;
    align-items: center;
    gap: 12px;
    flex: 1;
    min-width: 0;
}

.list-item-name {
    font-weight: 600;
    color: var(--text-primary);
    font-size: 14px;
}

.list-item-meta {
    font-size: 12px;
    color: var(--text-muted);
}

.list-item-actions {
    display: flex;
    gap: 8px;
    flex-shrink: 0;
}

.list-item-actions .btn {
    padding: 8px 12px;
}

/* Platform item */
.platform-item {
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 16px 20px;
    background: var(--bg-hover);
    border-radius: 10px;
    margin-bottom: 8px;
}

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

.platform-info {
    flex: 1;
    min-width: 0;
}

.platform-name {
    font-weight: 700;
    color: var(--text-primary);
    font-size: 14px;
    margin-bottom: 2px;
}

.platform-package {
    font-size: 13px;
    color: var(--text-secondary);
}

.platform-quantity {
    font-size: 12px;
    color: var(--text-muted);
    margin-top: 2px;
}

.platform-actions {
    display: flex;
    gap: 8px;
    flex-shrink: 0;
}

@media (max-width: 576px) {
    .list-item {
        flex-direction: column;
        align-items: flex-start;
    }
    
    .list-item-actions {
        width: 100%;
        justify-content: flex-end;
    }
    
    .platform-item {
        flex-wrap: wrap;
    }
    
    .platform-actions {
        width: 100%;
        justify-content: flex-end;
        margin-top: 12px;
    }
}

/* Liste container */
.list-container {
    max-height: 400px;
    overflow-y: auto;
    padding: 4px;
}

/* Modal içi tab */
.modal-tabs {
    display: flex;
    gap: 4px;
    margin-bottom: 20px;
    background: var(--bg-dark);
    padding: 4px;
    border-radius: 8px;
}

.modal-tabs .modal-tab-btn {
    flex: 1;
    padding: 10px 16px;
    background: transparent;
    border: none;
    border-radius: 6px;
    color: var(--text-secondary);
    font-weight: 500;
    font-size: 13px;
    cursor: pointer;
    transition: all 0.2s;
}

.modal-tabs .modal-tab-btn:hover {
    color: var(--text-primary);
}

.modal-tabs .modal-tab-btn.active {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.modal-tab-content {
    display: none;
}

.modal-tab-content.active {
    display: block;
}
</style>

<?php if ($demoMode): ?>
<div class="alert alert-warning mb-4">
    <i class="fas fa-exclamation-triangle me-2"></i>
    <strong>Demo Mod Aktif!</strong> Ekleme, düzenleme ve silme işlemleri devre dışı bırakıldı. Sadece ayarları değiştirebilirsiniz.
</div>
<?php endif; ?>

<?php if ($message): ?>
<div class="alert alert-<?php echo $messageType; ?> alert-dismissible fade show mb-4">
    <i class="fas fa-<?php echo $messageType === 'success' ? 'check-circle' : ($messageType === 'warning' ? 'exclamation-triangle' : 'exclamation-circle'); ?> me-2"></i>
    <?php echo $message; ?>
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>

<!-- Genel Ayarlar -->
<div class="card mb-4">
    <div class="card-header">
        <h5 class="card-title"><i class="fas fa-cog"></i> Genel Ayarlar</h5>
        <?php if ($fakeSettings['is_active']): ?>
            <span class="status-badge status-completed"><i class="fas fa-check-circle me-1"></i> Sistem Aktif</span>
        <?php else: ?>
            <span class="status-badge status-cancelled"><i class="fas fa-times-circle me-1"></i> Sistem Pasif</span>
        <?php endif; ?>
    </div>
    <div class="card-body">
        <form method="POST">
            <!-- Toggle Ayarları -->
            <div class="row g-3 mb-4">
                <div class="col-md-6">
                    <div class="setting-item">
                        <label>
                            <input class="form-check-input" type="checkbox" name="is_active" <?php echo $fakeSettings['is_active'] ? 'checked' : ''; ?>>
                            <div>
                                <div>Fake Bildirimleri Aktif Et</div>
                                <small class="text-muted">Ziyaretçilere sahte satın alma bildirimleri göster</small>
                            </div>
                        </label>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="setting-item">
                        <label>
                            <input class="form-check-input" type="checkbox" name="show_on_mobile" <?php echo $fakeSettings['show_on_mobile'] ? 'checked' : ''; ?>>
                            <div>
                                <div>Mobilde Göster</div>
                                <small class="text-muted">Mobil cihazlarda bildirimleri göster</small>
                            </div>
                        </label>
                    </div>
                </div>
            </div>
            
            <!-- Sayısal Ayarlar -->
            <div class="row g-3">
                <div class="col-sm-6 col-lg-3">
                    <div class="input-group-custom">
                        <label>Minimum Aralık (sn)</label>
                        <input type="number" class="form-control" name="interval_min" value="<?php echo $fakeSettings['interval_min']; ?>" min="1" max="120">
                        <div class="form-text">Bildirimler arası min. süre</div>
                    </div>
                </div>
                <div class="col-sm-6 col-lg-3">
                    <div class="input-group-custom">
                        <label>Maksimum Aralık (sn)</label>
                        <input type="number" class="form-control" name="interval_max" value="<?php echo $fakeSettings['interval_max']; ?>" min="1" max="300">
                        <div class="form-text">Bildirimler arası max. süre</div>
                    </div>
                </div>
                <div class="col-sm-6 col-lg-3">
                    <div class="input-group-custom">
                        <label>Görüntülenme Süresi (sn)</label>
                        <input type="number" class="form-control" name="display_duration" value="<?php echo $fakeSettings['display_duration']; ?>" min="1" max="30">
                        <div class="form-text">Bildirimin ekranda kalma süresi</div>
                    </div>
                </div>
                <div class="col-sm-6 col-lg-3">
                    <div class="input-group-custom">
                        <label>Konum</label>
                        <select class="form-select" name="position">
                            <option value="bottom-left" <?php echo $fakeSettings['position'] === 'bottom-left' ? 'selected' : ''; ?>>Sol Alt</option>
                            <option value="bottom-right" <?php echo $fakeSettings['position'] === 'bottom-right' ? 'selected' : ''; ?>>Sağ Alt</option>
                            <option value="top-left" <?php echo $fakeSettings['position'] === 'top-left' ? 'selected' : ''; ?>>Sol Üst</option>
                            <option value="top-right" <?php echo $fakeSettings['position'] === 'top-right' ? 'selected' : ''; ?>>Sağ Üst</option>
                        </select>
                        <div class="form-text">Bildirimin gösterileceği köşe</div>
                    </div>
                </div>
            </div>
            
            <div class="mt-4">
                <button type="submit" name="update_settings" class="btn btn-primary-custom">
                    <i class="fas fa-save me-2"></i> Ayarları Kaydet
                </button>
            </div>
        </form>
    </div>
</div>

<!-- İstatistikler -->
<div class="stats-grid mb-4">
    <div class="stat-card primary">
        <div class="stat-value"><?php echo $activeNames; ?><span style="font-size: 18px; opacity: 0.5;">/<?php echo count($names); ?></span></div>
        <div class="stat-label"><i class="fas fa-user me-1"></i> Aktif İsim</div>
    </div>
    <div class="stat-card success">
        <div class="stat-value"><?php echo $activeCities; ?><span style="font-size: 18px; opacity: 0.5;">/<?php echo count($cities); ?></span></div>
        <div class="stat-label"><i class="fas fa-map-marker-alt me-1"></i> Aktif Şehir</div>
    </div>
    <div class="stat-card info">
        <div class="stat-value"><?php echo $activeItems; ?><span style="font-size: 18px; opacity: 0.5;">/<?php echo count($items); ?></span></div>
        <div class="stat-label"><i class="fas fa-box me-1"></i> Aktif Paket</div>
    </div>
</div>

<!-- Sekmeler -->
<div class="custom-tabs" id="mainTabs">
    <button type="button" class="tab-btn active" data-tab="names">
        <i class="fas fa-user"></i>
        <span class="d-none d-sm-inline">İsimler</span>
        <span class="badge"><?php echo count($names); ?></span>
    </button>
    <button type="button" class="tab-btn" data-tab="cities">
        <i class="fas fa-map-marker-alt"></i>
        <span class="d-none d-sm-inline">Şehirler</span>
        <span class="badge"><?php echo count($cities); ?></span>
    </button>
    <button type="button" class="tab-btn" data-tab="items">
        <i class="fas fa-box"></i>
        <span class="d-none d-sm-inline">Paketler</span>
        <span class="badge"><?php echo count($items); ?></span>
    </button>
</div>

<!-- Tab İçerikleri -->
<div id="tabContents">
    <!-- İsimler -->
    <div class="tab-content active" id="tab-names">
        <div class="card">
            <div class="card-header">
                <h5 class="card-title"><i class="fas fa-user"></i> Müşteri İsimleri</h5>
                <?php if (!$demoMode): ?>
                <button type="button" class="btn btn-primary-custom btn-sm" data-bs-toggle="modal" data-bs-target="#addNameModal">
                    <i class="fas fa-plus me-1"></i> <span class="d-none d-sm-inline">İsim Ekle</span>
                </button>
                <?php endif; ?>
            </div>
            <div class="card-body">
                <?php if (empty($names)): ?>
                <div class="empty-state">
                    <i class="fas fa-user-slash"></i>
                    <h4>Henüz isim eklenmemiş</h4>
                    <p>Fake bildirimlerde gösterilecek müşteri isimleri ekleyin.</p>
                </div>
                <?php else: ?>
                <div class="list-container">
                    <?php foreach ($names as $name): ?>
                    <div class="list-item">
                        <div class="list-item-info">
                            <div>
                                <div class="list-item-name"><?php echo clean($name['name']); ?></div>
                                <div class="list-item-meta"><?php echo date('d.m.Y H:i', strtotime($name['created_at'])); ?></div>
                            </div>
                        </div>
                        <?php if ($name['is_active']): ?>
                            <span class="status-badge status-completed">Aktif</span>
                        <?php else: ?>
                            <span class="status-badge status-cancelled">Pasif</span>
                        <?php endif; ?>
                        <div class="list-item-actions">
                            <?php if (!$demoMode): ?>
                            <form method="POST" class="d-inline">
                                <input type="hidden" name="name_id" value="<?php echo $name['id']; ?>">
                                <button type="submit" name="toggle_name" class="btn btn-sm <?php echo $name['is_active'] ? 'btn-warning-custom' : 'btn-success-custom'; ?>" title="<?php echo $name['is_active'] ? 'Pasif Yap' : 'Aktif Yap'; ?>">
                                    <i class="fas fa-<?php echo $name['is_active'] ? 'pause' : 'play'; ?>"></i>
                                </button>
                            </form>
                            <form method="POST" class="d-inline" onsubmit="return confirm('Bu ismi silmek istediğinize emin misiniz?')">
                                <input type="hidden" name="name_id" value="<?php echo $name['id']; ?>">
                                <button type="submit" name="delete_name" class="btn btn-sm btn-danger-custom" title="Sil">
                                    <i class="fas fa-trash"></i>
                                </button>
                            </form>
                            <?php else: ?>
                            <span class="text-muted"><i class="fas fa-lock"></i></span>
                            <?php endif; ?>
                        </div>
                    </div>
                    <?php endforeach; ?>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
    
    <!-- Şehirler -->
    <div class="tab-content" id="tab-cities">
        <div class="card">
            <div class="card-header">
                <h5 class="card-title"><i class="fas fa-map-marker-alt"></i> Şehirler</h5>
                <?php if (!$demoMode): ?>
                <button type="button" class="btn btn-primary-custom btn-sm" data-bs-toggle="modal" data-bs-target="#addCityModal">
                    <i class="fas fa-plus me-1"></i> <span class="d-none d-sm-inline">Şehir Ekle</span>
                </button>
                <?php endif; ?>
            </div>
            <div class="card-body">
                <?php if (empty($cities)): ?>
                <div class="empty-state">
                    <i class="fas fa-map-marker-alt"></i>
                    <h4>Henüz şehir eklenmemiş</h4>
                    <p>Fake bildirimlerde gösterilecek şehirleri ekleyin.</p>
                </div>
                <?php else: ?>
                <div class="list-container">
                    <?php foreach ($cities as $city): ?>
                    <div class="list-item">
                        <div class="list-item-info">
                            <div>
                                <div class="list-item-name"><?php echo clean($city['city']); ?></div>
                                <div class="list-item-meta"><?php echo date('d.m.Y H:i', strtotime($city['created_at'])); ?></div>
                            </div>
                        </div>
                        <?php if ($city['is_active']): ?>
                            <span class="status-badge status-completed">Aktif</span>
                        <?php else: ?>
                            <span class="status-badge status-cancelled">Pasif</span>
                        <?php endif; ?>
                        <div class="list-item-actions">
                            <?php if (!$demoMode): ?>
                            <form method="POST" class="d-inline">
                                <input type="hidden" name="city_id" value="<?php echo $city['id']; ?>">
                                <button type="submit" name="toggle_city" class="btn btn-sm <?php echo $city['is_active'] ? 'btn-warning-custom' : 'btn-success-custom'; ?>">
                                    <i class="fas fa-<?php echo $city['is_active'] ? 'pause' : 'play'; ?>"></i>
                                </button>
                            </form>
                            <form method="POST" class="d-inline" onsubmit="return confirm('Bu şehri silmek istediğinize emin misiniz?')">
                                <input type="hidden" name="city_id" value="<?php echo $city['id']; ?>">
                                <button type="submit" name="delete_city" class="btn btn-sm btn-danger-custom">
                                    <i class="fas fa-trash"></i>
                                </button>
                            </form>
                            <?php else: ?>
                            <span class="text-muted"><i class="fas fa-lock"></i></span>
                            <?php endif; ?>
                        </div>
                    </div>
                    <?php endforeach; ?>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
    
    <!-- Paketler -->
    <div class="tab-content" id="tab-items">
        <div class="card">
            <div class="card-header">
                <h5 class="card-title"><i class="fas fa-box"></i> Paketler</h5>
                <?php if (!$demoMode): ?>
                <button type="button" class="btn btn-primary-custom btn-sm" data-bs-toggle="modal" data-bs-target="#addItemModal">
                    <i class="fas fa-plus me-1"></i> <span class="d-none d-sm-inline">Paket Ekle</span>
                </button>
                <?php endif; ?>
            </div>
            <div class="card-body">
                <?php if (empty($items)): ?>
                <div class="empty-state">
                    <i class="fas fa-box-open"></i>
                    <h4>Henüz paket eklenmemiş</h4>
                    <p>Fake bildirimlerde gösterilecek paketleri ekleyin.</p>
                </div>
                <?php else: ?>
                <div class="list-container">
                    <?php foreach ($items as $item): ?>
                    <div class="platform-item">
                        <div class="platform-icon" style="background: <?php echo clean($item['platform_color']); ?>22; color: <?php echo clean($item['platform_color']); ?>;">
                            <i class="<?php echo clean($item['platform_icon']); ?>"></i>
                        </div>
                        <div class="platform-info">
                            <div class="platform-name"><?php echo clean($item['platform']); ?></div>
                            <div class="platform-package"><?php echo clean($item['package_name']); ?></div>
                            <div class="platform-quantity">
                                <i class="fas fa-chart-bar me-1"></i>
                                <?php echo number_format($item['quantity_min'], 0, ',', '.'); ?> - <?php echo number_format($item['quantity_max'], 0, ',', '.'); ?>
                            </div>
                        </div>
                        <?php if ($item['is_active']): ?>
                            <span class="status-badge status-completed">Aktif</span>
                        <?php else: ?>
                            <span class="status-badge status-cancelled">Pasif</span>
                        <?php endif; ?>
                        <div class="platform-actions">
                            <?php if (!$demoMode): ?>
                            <form method="POST" class="d-inline">
                                <input type="hidden" name="item_id" value="<?php echo $item['id']; ?>">
                                <button type="submit" name="toggle_item" class="btn btn-sm <?php echo $item['is_active'] ? 'btn-warning-custom' : 'btn-success-custom'; ?>">
                                    <i class="fas fa-<?php echo $item['is_active'] ? 'pause' : 'play'; ?>"></i>
                                </button>
                            </form>
                            <form method="POST" class="d-inline" onsubmit="return confirm('Bu paketi silmek istediğinize emin misiniz?')">
                                <input type="hidden" name="item_id" value="<?php echo $item['id']; ?>">
                                <button type="submit" name="delete_item" class="btn btn-sm btn-danger-custom">
                                    <i class="fas fa-trash"></i>
                                </button>
                            </form>
                            <?php else: ?>
                            <span class="text-muted"><i class="fas fa-lock"></i></span>
                            <?php endif; ?>
                        </div>
                    </div>
                    <?php endforeach; ?>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

<!-- İsim Ekle Modal -->
<div class="modal fade" id="addNameModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title"><i class="fas fa-user-plus"></i> İsim Ekle</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST">
                <div class="modal-body">
                    <div class="modal-tabs">
                        <button type="button" class="modal-tab-btn active" data-modal-tab="singleName">Tekli Ekle</button>
                        <button type="button" class="modal-tab-btn" data-modal-tab="bulkNames">Toplu Ekle</button>
                    </div>
                    <div class="modal-tab-content active" id="modal-singleName">
                        <div class="mb-3">
                            <label class="form-label">İsim</label>
                            <input type="text" class="form-control" name="name" placeholder="Örn: Ahmet Y.">
                            <div class="form-text">Gizlilik için soyismin sadece baş harfini kullanın.</div>
                        </div>
                        <button type="submit" name="add_name" class="btn btn-primary-custom w-100">
                            <i class="fas fa-plus me-2"></i> Ekle
                        </button>
                    </div>
                    <div class="modal-tab-content" id="modal-bulkNames">
                        <div class="mb-3">
                            <label class="form-label">İsimler (her satıra bir isim)</label>
                            <textarea class="form-control" name="names_bulk" rows="8" placeholder="Ahmet Y.&#10;Mehmet K.&#10;Ayşe S."></textarea>
                        </div>
                        <button type="submit" name="add_names_bulk" class="btn btn-primary-custom w-100">
                            <i class="fas fa-plus me-2"></i> Toplu Ekle
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Şehir Ekle Modal -->
<div class="modal fade" id="addCityModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title"><i class="fas fa-map-marker-alt"></i> Şehir Ekle</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST">
                <div class="modal-body">
                    <div class="modal-tabs">
                        <button type="button" class="modal-tab-btn active" data-modal-tab="singleCity">Tekli Ekle</button>
                        <button type="button" class="modal-tab-btn" data-modal-tab="bulkCities">Toplu Ekle</button>
                    </div>
                    <div class="modal-tab-content active" id="modal-singleCity">
                        <div class="mb-3">
                            <label class="form-label">Şehir</label>
                            <input type="text" class="form-control" name="city" placeholder="Örn: İstanbul">
                        </div>
                        <button type="submit" name="add_city" class="btn btn-primary-custom w-100">
                            <i class="fas fa-plus me-2"></i> Ekle
                        </button>
                    </div>
                    <div class="modal-tab-content" id="modal-bulkCities">
                        <div class="mb-3">
                            <label class="form-label">Şehirler (her satıra bir şehir)</label>
                            <textarea class="form-control" name="cities_bulk" rows="8" placeholder="İstanbul&#10;Ankara&#10;İzmir"></textarea>
                        </div>
                        <button type="submit" name="add_cities_bulk" class="btn btn-primary-custom w-100">
                            <i class="fas fa-plus me-2"></i> Toplu Ekle
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Paket Ekle Modal -->
<div class="modal fade" id="addItemModal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title"><i class="fas fa-box"></i> Paket Ekle</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST">
                <div class="modal-body">
                    <div class="row g-3">
                        <div class="col-12 col-md-6">
                            <label class="form-label">Platform</label>
                            <select class="form-select" name="platform" id="platformSelect" onchange="updatePlatformIcon()">
                                <option value="">Seçin...</option>
                                <option value="Instagram" data-icon="fab fa-instagram" data-color="#E4405F">Instagram</option>
                                <option value="TikTok" data-icon="fab fa-tiktok" data-color="#000000">TikTok</option>
                                <option value="YouTube" data-icon="fab fa-youtube" data-color="#FF0000">YouTube</option>
                                <option value="Twitter" data-icon="fab fa-twitter" data-color="#1DA1F2">Twitter</option>
                                <option value="Facebook" data-icon="fab fa-facebook" data-color="#1877F2">Facebook</option>
                                <option value="Spotify" data-icon="fab fa-spotify" data-color="#1DB954">Spotify</option>
                                <option value="Telegram" data-icon="fab fa-telegram" data-color="#0088cc">Telegram</option>
                                <option value="Discord" data-icon="fab fa-discord" data-color="#5865F2">Discord</option>
                                <option value="Twitch" data-icon="fab fa-twitch" data-color="#9146FF">Twitch</option>
                                <option value="LinkedIn" data-icon="fab fa-linkedin" data-color="#0A66C2">LinkedIn</option>
                                <option value="Pinterest" data-icon="fab fa-pinterest" data-color="#BD081C">Pinterest</option>
                            </select>
                        </div>
                        <div class="col-12 col-md-6">
                            <label class="form-label">Paket Adı</label>
                            <input type="text" class="form-control" name="package_name" placeholder="Örn: Takipçi, Beğeni" required>
                        </div>
                        <div class="col-6 col-md-4">
                            <label class="form-label">Platform İkonu</label>
                            <input type="text" class="form-control" name="platform_icon" id="platformIcon" placeholder="fab fa-instagram">
                        </div>
                        <div class="col-6 col-md-4">
                            <label class="form-label">Platform Rengi</label>
                            <input type="color" class="form-control form-control-color w-100" name="platform_color" id="platformColor" value="#8b5cf6" style="height: 46px;">
                        </div>
                        <div class="col-12 col-md-4">
                            <label class="form-label">Önizleme</label>
                            <div class="d-flex align-items-center gap-3 p-2 rounded" style="background: var(--bg-dark);">
                                <span class="avatar-md" id="platformPreview" style="background: #8b5cf622; color: #8b5cf6;">
                                    <i class="fas fa-shopping-cart"></i>
                                </span>
                                <span id="platformNamePreview" style="font-weight: 600;">Platform</span>
                            </div>
                        </div>
                        <div class="col-6">
                            <label class="form-label">Minimum Miktar</label>
                            <input type="number" class="form-control" name="quantity_min" value="500" min="1">
                        </div>
                        <div class="col-6">
                            <label class="form-label">Maksimum Miktar</label>
                            <input type="number" class="form-control" name="quantity_max" value="10000" min="1">
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary-custom" data-bs-dismiss="modal">İptal</button>
                    <button type="submit" name="add_item" class="btn btn-primary-custom">
                        <i class="fas fa-plus me-2"></i> Paket Ekle
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
// Ana Tab Sistemi
document.querySelectorAll('.custom-tabs .tab-btn').forEach(function(btn) {
    btn.addEventListener('click', function() {
        var tab = this.getAttribute('data-tab');
        
        // Tüm butonlardan active kaldır
        document.querySelectorAll('.custom-tabs .tab-btn').forEach(function(b) {
            b.classList.remove('active');
        });
        this.classList.add('active');
        
        // Tüm içerikleri gizle
        document.querySelectorAll('#tabContents .tab-content').forEach(function(c) {
            c.classList.remove('active');
        });
        
        // İlgili içeriği göster
        document.getElementById('tab-' + tab).classList.add('active');
    });
});

// Modal Tab Sistemi
document.querySelectorAll('.modal-tab-btn').forEach(function(btn) {
    btn.addEventListener('click', function() {
        var tab = this.getAttribute('data-modal-tab');
        var modal = this.closest('.modal');
        
        // Bu modaldaki tüm butonlardan active kaldır
        modal.querySelectorAll('.modal-tab-btn').forEach(function(b) {
            b.classList.remove('active');
        });
        this.classList.add('active');
        
        // Bu modaldaki tüm içerikleri gizle
        modal.querySelectorAll('.modal-tab-content').forEach(function(c) {
            c.classList.remove('active');
        });
        
        // İlgili içeriği göster
        document.getElementById('modal-' + tab).classList.add('active');
    });
});

// Platform seçimi
function updatePlatformIcon() {
    var select = document.getElementById('platformSelect');
    var option = select.options[select.selectedIndex];
    var icon = option.getAttribute('data-icon') || 'fas fa-shopping-cart';
    var color = option.getAttribute('data-color') || '#8b5cf6';
    var name = option.value || 'Platform';
    
    document.getElementById('platformIcon').value = icon;
    document.getElementById('platformColor').value = color;
    
    var preview = document.getElementById('platformPreview');
    preview.innerHTML = '<i class="' + icon + '"></i>';
    preview.style.background = color + '22';
    preview.style.color = color;
    
    document.getElementById('platformNamePreview').textContent = name;
}

// Renk değiştiğinde önizlemeyi güncelle
document.getElementById('platformColor').addEventListener('input', function() {
    var color = this.value;
    var preview = document.getElementById('platformPreview');
    preview.style.background = color + '22';
    preview.style.color = color;
});

// İkon değiştiğinde önizlemeyi güncelle
document.getElementById('platformIcon').addEventListener('input', function() {
    var icon = this.value || 'fas fa-shopping-cart';
    var preview = document.getElementById('platformPreview');
    preview.innerHTML = '<i class="' + icon + '"></i>';
});
</script>

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