<?php
/**
 * SMM Paket Satış Scripti - Genel Ayarlar
 * KDV Desteği Eklendi
 * @version 1.0.5
 */

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

if (!isLoggedIn() || !isAdmin()) {
    redirect(SITE_URL . '/admin/');
    exit;
}

// CSRF Token
if (!isset($_SESSION['csrf_token'])) {
    $_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}
$csrfToken = $_SESSION['csrf_token'];

// Demo modu kontrolü
function isDemoMode() {
    global $db;
    try {
        $stmt = $db->prepare("SELECT setting_value FROM settings WHERE setting_key = 'demo_mode'");
        $stmt->execute();
        $value = $stmt->fetchColumn();
        return $value === '1' || $value === 1;
    } catch (PDOException $e) {
        return false;
    }
}

$demoMode = isDemoMode();

// Ayar sekmeleri
$tabs = [
    'general' => ['title' => 'Genel', 'icon' => 'fas fa-cog'],
    'contact' => ['title' => 'İletişim', 'icon' => 'fas fa-address-card'],
    'social' => ['title' => 'Sosyal Medya', 'icon' => 'fas fa-share-alt'],
    'footer' => ['title' => 'Footer', 'icon' => 'fas fa-shoe-prints'],
    'payment' => ['title' => 'Ödeme', 'icon' => 'fas fa-credit-card'],
    'seo' => ['title' => 'SEO & Analitik', 'icon' => 'fas fa-chart-line']
];

$activeTab = $_GET['tab'] ?? 'general';
if (!isset($tabs[$activeTab])) {
    $activeTab = 'general';
}

// Toggle/Checkbox alanları - HER SEKME İÇİN AYRI TANIMLA
$toggleFieldsByTab = [
    'general' => ['maintenance_mode', 'kdv_enabled', 'kdv_included'],
    'footer' => ['footer_whatsapp_enabled'],
    'contact' => [],
    'social' => [],
    'payment' => [],
    'seo' => []
];

// POST İşlemi
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Demo modunda işlem yapılamaz
    if ($demoMode) {
        setFlash('error', '🔒 Demo modunda ayarlar değiştirilemez.');
        redirect(SITE_URL . '/admin/ayarlar.php?tab=' . $activeTab);
        exit;
    }
    
    if (!hash_equals($csrfToken, $_POST['csrf_token'] ?? '')) {
        setFlash('error', 'Güvenlik hatası.');
        redirect(SITE_URL . '/admin/ayarlar.php?tab=' . $activeTab);
        exit;
    }
    
    try {
        // SADECE AKTİF SEKMENİN toggle alanlarını işle
        $currentTabToggles = $toggleFieldsByTab[$activeTab] ?? [];
        
        foreach ($currentTabToggles as $field) {
            $value = isset($_POST[$field]) ? '1' : '0';
            
            $checkStmt = $db->prepare("SELECT id FROM settings WHERE setting_key = ?");
            $checkStmt->execute([$field]);
            
            if ($checkStmt->fetch()) {
                $db->prepare("UPDATE settings SET setting_value = ?, updated_at = NOW() WHERE setting_key = ?")->execute([$value, $field]);
            } else {
                $group = 'general';
                if (strpos($field, 'footer_') === 0) $group = 'footer';
                if (strpos($field, 'kdv_') === 0) $group = 'finance';
                
                $db->prepare("INSERT INTO settings (setting_key, setting_value, setting_type, setting_group, created_at) VALUES (?, ?, 'bool', ?, NOW())")->execute([$field, $value, $group]);
            }
        }
        
        // Diğer alanları işle
        foreach ($_POST as $key => $value) {
            if ($key === 'csrf_token') continue;
            if (is_array($value)) continue;
            if (in_array($key, $currentTabToggles)) continue; // Bu sekmenin toggle'ları zaten işlendi
            
            $value = is_string($value) ? trim($value) : '';
            
            $checkStmt = $db->prepare("SELECT id FROM settings WHERE setting_key = ?");
            $checkStmt->execute([$key]);
            
            if ($checkStmt->fetch()) {
                $db->prepare("UPDATE settings SET setting_value = ?, updated_at = NOW() WHERE setting_key = ?")->execute([$value, $key]);
            } else {
                $group = 'general';
                if (strpos($key, 'contact_') === 0) $group = 'contact';
                elseif (strpos($key, 'whatsapp') === 0 || strpos($key, 'instagram') === 0 || strpos($key, 'telegram') === 0 || strpos($key, 'twitter') === 0 || strpos($key, 'facebook') === 0 || strpos($key, 'youtube') === 0 || strpos($key, 'tiktok') === 0) $group = 'social';
                elseif (strpos($key, 'footer_') === 0) $group = 'footer';
                elseif (strpos($key, 'currency') === 0 || strpos($key, 'kdv_') === 0) $group = 'finance';
                elseif (strpos($key, 'google_') === 0 || strpos($key, 'facebook_pixel') === 0 || strpos($key, 'site_keywords') === 0) $group = 'seo';
                
                $db->prepare("INSERT INTO settings (setting_key, setting_value, setting_group, created_at) VALUES (?, ?, ?, NOW())")->execute([$key, $value, $group]);
            }
        }
        
        // Logo yükleme
        if (isset($_FILES['site_logo']) && $_FILES['site_logo']['error'] === UPLOAD_ERR_OK) {
            $uploadDir = __DIR__ . '/../uploads/';
            if (!is_dir($uploadDir)) mkdir($uploadDir, 0755, true);
            
            $ext = strtolower(pathinfo($_FILES['site_logo']['name'], PATHINFO_EXTENSION));
            if (in_array($ext, ['jpg', 'jpeg', 'png', 'gif', 'svg', 'webp'])) {
                $filename = 'logo_' . time() . '.' . $ext;
                if (move_uploaded_file($_FILES['site_logo']['tmp_name'], $uploadDir . $filename)) {
                    $logoPath = 'uploads/' . $filename;
                    $check = $db->prepare("SELECT id FROM settings WHERE setting_key = 'site_logo'");
                    $check->execute();
                    if ($check->fetch()) {
                        $db->prepare("UPDATE settings SET setting_value = ? WHERE setting_key = 'site_logo'")->execute([$logoPath]);
                    } else {
                        $db->prepare("INSERT INTO settings (setting_key, setting_value, setting_group, created_at) VALUES ('site_logo', ?, 'general', NOW())")->execute([$logoPath]);
                    }
                }
            }
        }
        
        // Favicon yükleme
        if (isset($_FILES['site_favicon']) && $_FILES['site_favicon']['error'] === UPLOAD_ERR_OK) {
            $uploadDir = __DIR__ . '/../uploads/';
            if (!is_dir($uploadDir)) mkdir($uploadDir, 0755, true);
            
            $ext = strtolower(pathinfo($_FILES['site_favicon']['name'], PATHINFO_EXTENSION));
            if (in_array($ext, ['ico', 'png', 'svg'])) {
                $filename = 'favicon_' . time() . '.' . $ext;
                if (move_uploaded_file($_FILES['site_favicon']['tmp_name'], $uploadDir . $filename)) {
                    $faviconPath = 'uploads/' . $filename;
                    $check = $db->prepare("SELECT id FROM settings WHERE setting_key = 'site_favicon'");
                    $check->execute();
                    if ($check->fetch()) {
                        $db->prepare("UPDATE settings SET setting_value = ? WHERE setting_key = 'site_favicon'")->execute([$faviconPath]);
                    } else {
                        $db->prepare("INSERT INTO settings (setting_key, setting_value, setting_group, created_at) VALUES ('site_favicon', ?, 'general', NOW())")->execute([$faviconPath]);
                    }
                }
            }
        }
        
        setFlash('success', 'Ayarlar başarıyla kaydedildi.');
        
    } catch (Exception $e) {
        setFlash('error', 'Hata: ' . $e->getMessage());
    }
    
    redirect(SITE_URL . '/admin/ayarlar.php?tab=' . $activeTab);
    exit;
}

// Footer sayfalarını çek
$footerPages = [];
try {
    $footerPages = $db->query("SELECT id, title, slug FROM pages WHERE show_in_footer = 1 ORDER BY sort_order ASC")->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {}

// Null-safe helper function
function safeGet($settings, $key, $default = '') {
    $value = $settings->get($key, $default);
    return $value !== null ? $value : $default;
}

function safeLtrim($value, $chars = " \t\n\r\0\x0B") {
    return $value !== null ? ltrim($value, $chars) : '';
}

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

<style>
/* Demo Banner */
.demo-banner {
    background: linear-gradient(135deg, rgba(245, 158, 11, 0.15), rgba(239, 68, 68, 0.15));
    border: 1px solid rgba(245, 158, 11, 0.3);
    border-radius: 12px;
    padding: 15px 20px;
    margin-bottom: 20px;
    display: flex;
    align-items: center;
    gap: 12px;
}
.demo-banner i { font-size: 24px; color: #f59e0b; }
.demo-banner-text h4 { margin: 0 0 4px; font-size: 14px; font-weight: 600; color: #f59e0b; }
.demo-banner-text p { margin: 0; font-size: 12px; color: var(--text-secondary); }

/* Tabs */
.tabs-card {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: 16px;
    margin-bottom: 25px;
    padding: 8px;
}

.tabs-nav {
    display: flex;
    gap: 6px;
    overflow-x: auto;
    scrollbar-width: none;
}

.tabs-nav::-webkit-scrollbar { display: none; }

.tab-btn {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 14px 24px;
    background: transparent;
    border: none;
    border-radius: 12px;
    color: var(--text-secondary);
    font-size: 14px;
    font-weight: 500;
    white-space: nowrap;
    cursor: pointer;
    transition: all 0.3s;
    text-decoration: none;
}

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

.tab-btn.active {
    background: var(--gradient);
    color: #fff;
    box-shadow: 0 4px 15px rgba(var(--primary-rgb), 0.3);
}

.tab-btn i { font-size: 15px; }

/* Content Card */
.settings-card {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: 16px;
    overflow: hidden;
}

.settings-header {
    padding: 24px 28px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 15px;
}

.settings-header h3 {
    font-size: 20px;
    font-weight: 700;
    margin: 0;
    display: flex;
    align-items: center;
    gap: 12px;
}

.settings-header h3 i {
    width: 44px;
    height: 44px;
    background: rgba(var(--primary-rgb), 0.15);
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--primary);
    font-size: 18px;
}

.settings-body { padding: 28px; }

/* Form */
.form-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 20px;
}

.form-group { margin-bottom: 0; }
.form-group.full { grid-column: span 2; }

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

.form-control {
    width: 100%;
    padding: 12px 16px;
    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 {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(var(--primary-rgb), 0.15);
    outline: none;
}

.form-control::placeholder { color: var(--text-secondary); }

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

.form-hint {
    font-size: 12px;
    color: var(--text-secondary);
    margin-top: 6px;
}

/* Input Group */
.input-group { display: flex; }

.input-prefix {
    padding: 12px 14px;
    background: var(--bg-hover);
    border: 1px solid var(--border-color);
    border-right: none;
    border-radius: 10px 0 0 10px;
    color: var(--text-secondary);
    font-size: 13px;
    white-space: nowrap;
    display: flex;
    align-items: center;
}

.input-group .form-control {
    border-radius: 0 10px 10px 0;
}

/* Upload */
.upload-box {
    display: flex;
    align-items: center;
    gap: 20px;
}

.upload-preview {
    width: 100px;
    height: 60px;
    background: var(--bg-dark);
    border: 2px dashed var(--border-color);
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    flex-shrink: 0;
}

.upload-preview.square {
    width: 60px;
    height: 60px;
}

.upload-preview img {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
}

.upload-preview i {
    font-size: 24px;
    color: var(--text-secondary);
    opacity: 0.5;
}

.upload-btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 10px 20px;
    background: var(--bg-hover);
    border: 1px dashed var(--border-color);
    border-radius: 10px;
    color: var(--text-secondary);
    font-size: 13px;
    cursor: pointer;
    transition: all 0.2s;
}

.upload-btn:hover {
    border-color: var(--primary);
    color: var(--primary);
}

.upload-btn input { display: none; }

/* Toggle */
.toggle-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    background: var(--bg-hover);
    border-radius: 12px;
    margin-bottom: 12px;
}

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

.toggle-info h5 {
    font-size: 14px;
    font-weight: 600;
    margin: 0 0 4px;
}

.toggle-info p {
    font-size: 12px;
    color: var(--text-secondary);
    margin: 0;
}

.toggle-switch {
    position: relative;
    width: 50px;
    height: 26px;
}

.toggle-switch input[type="checkbox"] { 
    opacity: 0;
    width: 0;
    height: 0;
    position: absolute;
}

.toggle-switch .slider {
    position: absolute;
    inset: 0;
    background: var(--bg-dark);
    border-radius: 13px;
    cursor: pointer;
    transition: all 0.3s;
}

.toggle-switch .slider::after {
    content: '';
    position: absolute;
    top: 3px;
    left: 3px;
    width: 20px;
    height: 20px;
    background: #fff;
    border-radius: 50%;
    transition: all 0.3s;
}

.toggle-switch input[type="checkbox"]:checked + .slider {
    background: var(--primary);
}

.toggle-switch input[type="checkbox"]:checked + .slider::after {
    left: 27px;
}

/* Section */
.section {
    margin-bottom: 35px;
    padding-bottom: 35px;
    border-bottom: 1px solid var(--border-color);
}

.section:last-child {
    margin-bottom: 0;
    padding-bottom: 0;
    border-bottom: none;
}

.section-title {
    font-size: 15px;
    font-weight: 600;
    color: var(--primary);
    margin-bottom: 20px;
    display: flex;
    align-items: center;
    gap: 10px;
}

.section-title i { font-size: 14px; }

/* Info Box */
.info-box {
    background: rgba(59, 130, 246, 0.1);
    border: 1px solid rgba(59, 130, 246, 0.2);
    border-radius: 10px;
    padding: 14px 18px;
    font-size: 13px;
    color: #3b82f6;
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 20px;
}

/* Footer Pages List */
.pages-list {
    background: var(--bg-hover);
    border-radius: 10px;
    padding: 5px;
}

.page-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 15px;
    background: var(--bg-dark);
    border-radius: 8px;
    margin-bottom: 5px;
    font-size: 13px;
}

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

.page-item .title { font-weight: 500; }

.page-item .slug {
    font-size: 11px;
    color: var(--text-secondary);
    font-family: monospace;
}

.page-item .edit-btn {
    width: 28px;
    height: 28px;
    background: rgba(59, 130, 246, 0.15);
    border: none;
    border-radius: 6px;
    color: #3b82f6;
    cursor: pointer;
    font-size: 11px;
    display: flex;
    align-items: center;
    justify-content: center;
    text-decoration: none;
}

/* Social Icons */
.social-icon {
    width: 20px;
    text-align: center;
    margin-right: 8px;
}

.social-icon.instagram { color: #E4405F; }
.social-icon.whatsapp { color: #25D366; }
.social-icon.telegram { color: #0088cc; }
.social-icon.twitter { color: #1DA1F2; }
.social-icon.facebook { color: #1877F2; }
.social-icon.youtube { color: #FF0000; }
.social-icon.tiktok { color: #000; }

/* Responsive */
@media (max-width: 768px) {
    .form-grid { grid-template-columns: 1fr; }
    .form-group.full { grid-column: span 1; }
    .upload-box { flex-direction: column; align-items: flex-start; }
    .tabs-nav { padding-bottom: 5px; }
}
</style>

<?php if ($demoMode): ?>
<div class="demo-banner">
    <i class="fas fa-shield-alt"></i>
    <div class="demo-banner-text">
        <h4>Demo Modu Aktif</h4>
        <p>Demo modunda ayarlar görüntülenebilir ancak değiştirilemez.</p>
    </div>
</div>
<?php endif; ?>

<!-- Tabs -->
<div class="tabs-card">
    <div class="tabs-nav">
        <?php foreach ($tabs as $key => $tab): ?>
        <a href="?tab=<?php echo $key; ?>" class="tab-btn <?php echo $activeTab === $key ? 'active' : ''; ?>">
            <i class="<?php echo $tab['icon']; ?>"></i>
            <?php echo $tab['title']; ?>
        </a>
        <?php endforeach; ?>
    </div>
</div>

<!-- Content -->
<form method="POST" enctype="multipart/form-data">
    <input type="hidden" name="csrf_token" value="<?php echo $csrfToken; ?>">
    
    <div class="settings-card">
        <div class="settings-header">
            <h3>
                <i class="<?php echo $tabs[$activeTab]['icon']; ?>"></i>
                <?php echo $tabs[$activeTab]['title']; ?> Ayarları
            </h3>
            <?php if ($demoMode): ?>
            <button type="button" class="btn-primary-custom" disabled style="opacity: 0.5; cursor: not-allowed;">
                <i class="fas fa-lock"></i> Demo Mod
            </button>
            <?php else: ?>
            <button type="submit" class="btn-primary-custom">
                <i class="fas fa-save"></i> Kaydet
            </button>
            <?php endif; ?>
        </div>
        
        <div class="settings-body">
            
            <?php if ($activeTab === 'general'): ?>
            <!-- GENEL AYARLAR -->
            <div class="section">
                <h4 class="section-title"><i class="fas fa-globe"></i> Site Bilgileri</h4>
                <div class="form-grid">
                    <div class="form-group">
                        <label class="form-label">Site Adı</label>
                        <input type="text" name="site_name" class="form-control" value="<?php echo clean(safeGet($settings, 'site_name')); ?>" placeholder="SocialBoost">
                    </div>
                    <div class="form-group">
                        <label class="form-label">Site Başlığı</label>
                        <input type="text" name="site_title" class="form-control" value="<?php echo clean(safeGet($settings, 'site_title')); ?>" placeholder="SocialBoost - Sosyal Medya Paketleri">
                    </div>
                    <div class="form-group full">
                        <label class="form-label">Site Açıklaması</label>
                        <textarea name="site_description" class="form-control" rows="2" placeholder="Site açıklaması..."><?php echo clean(safeGet($settings, 'site_description')); ?></textarea>
                    </div>
                    <div class="form-group full">
                        <label class="form-label">Site URL</label>
                        <input type="url" name="site_url" class="form-control" value="<?php echo clean(safeGet($settings, 'site_url')); ?>" placeholder="https://example.com">
                        <div class="form-hint">Sonunda / olmadan yazın</div>
                    </div>
                </div>
            </div>
            
            <div class="section">
                <h4 class="section-title"><i class="fas fa-image"></i> Logo & Favicon</h4>
                <div class="form-grid">
                    <div class="form-group">
                        <label class="form-label">Site Logosu</label>
                        <div class="upload-box">
                            <div class="upload-preview">
                                <?php $logo = safeGet($settings, 'site_logo'); if ($logo): ?>
                                <img src="<?php echo SITE_URL . '/' . $logo; ?>" alt="Logo">
                                <?php else: ?>
                                <i class="fas fa-image"></i>
                                <?php endif; ?>
                            </div>
                            <div>
                                <label class="upload-btn">
                                    <i class="fas fa-upload"></i> Logo Yükle
                                    <input type="file" name="site_logo" accept="image/*">
                                </label>
                                <div class="form-hint">PNG, SVG önerilir</div>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="form-label">Favicon</label>
                        <div class="upload-box">
                            <div class="upload-preview square">
                                <?php $favicon = safeGet($settings, 'site_favicon'); if ($favicon): ?>
                                <img src="<?php echo SITE_URL . '/' . $favicon; ?>" alt="Favicon">
                                <?php else: ?>
                                <i class="fas fa-star"></i>
                                <?php endif; ?>
                            </div>
                            <div>
                                <label class="upload-btn">
                                    <i class="fas fa-upload"></i> Favicon Yükle
                                    <input type="file" name="site_favicon" accept=".ico,.png,.svg">
                                </label>
                                <div class="form-hint">ICO veya PNG, 32x32px</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="section">
                <h4 class="section-title"><i class="fas fa-toggle-on"></i> Site Durumu</h4>
                <div class="toggle-item">
                    <div class="toggle-info">
                        <h5>Bakım Modu</h5>
                        <p>Aktifken site ziyaretçilere kapatılır</p>
                    </div>
                    <label class="toggle-switch">
                        <input type="checkbox" name="maintenance_mode" value="1" <?php echo safeGet($settings, 'maintenance_mode', '0') == '1' ? 'checked' : ''; ?>>
                        <span class="slider"></span>
                    </label>
                </div>
            </div>
            
            <div class="section">
                <h4 class="section-title"><i class="fas fa-percent"></i> KDV Ayarları</h4>
                <div class="toggle-item">
                    <div class="toggle-info">
                        <h5>KDV Uygula</h5>
                        <p>Aktifken fiyatlara KDV eklenir</p>
                    </div>
                    <label class="toggle-switch">
                        <input type="checkbox" name="kdv_enabled" value="1" <?php echo safeGet($settings, 'kdv_enabled', '0') == '1' ? 'checked' : ''; ?>>
                        <span class="slider"></span>
                    </label>
                </div>
                <div class="toggle-item">
                    <div class="toggle-info">
                        <h5>Fiyatlara KDV Dahil</h5>
                        <p>Aktifken paket fiyatları KDV dahil olarak kabul edilir</p>
                    </div>
                    <label class="toggle-switch">
                        <input type="checkbox" name="kdv_included" value="1" <?php echo safeGet($settings, 'kdv_included', '0') == '1' ? 'checked' : ''; ?>>
                        <span class="slider"></span>
                    </label>
                </div>
                <div class="form-grid" style="margin-top: 15px;">
                    <div class="form-group">
                        <label class="form-label">KDV Oranı (%)</label>
                        <input type="number" name="kdv_rate" class="form-control" value="<?php echo clean(safeGet($settings, 'kdv_rate', '20')); ?>" placeholder="20" min="0" max="100" step="0.01">
                        <div class="form-hint">Türkiye'de standart KDV oranı %20'dir</div>
                    </div>
                </div>
            </div>
            
            <?php elseif ($activeTab === 'contact'): ?>
            <!-- İLETİŞİM -->
            <div class="section">
                <h4 class="section-title"><i class="fas fa-envelope"></i> İletişim Bilgileri</h4>
                <div class="form-grid">
                    <div class="form-group">
                        <label class="form-label">E-posta Adresi</label>
                        <input type="email" name="contact_email" class="form-control" value="<?php echo clean(safeGet($settings, 'contact_email')); ?>" placeholder="info@example.com">
                    </div>
                    <div class="form-group">
                        <label class="form-label">Telefon Numarası</label>
                        <input type="text" name="contact_phone" class="form-control" value="<?php echo clean(safeGet($settings, 'contact_phone')); ?>" placeholder="+90 555 555 55 55">
                    </div>
                    <div class="form-group full">
                        <label class="form-label">Adres</label>
                        <textarea name="contact_address" class="form-control" rows="2" placeholder="Şirket adresi..."><?php echo clean(safeGet($settings, 'contact_address')); ?></textarea>
                    </div>
                </div>
            </div>
            
            <?php elseif ($activeTab === 'social'): ?>
            <!-- SOSYAL MEDYA -->
            <div class="section">
                <h4 class="section-title"><i class="fas fa-share-alt"></i> Sosyal Medya Hesapları</h4>
                <div class="info-box">
                    <i class="fas fa-info-circle"></i>
                    Bu bilgiler footer ve iletişim sayfasında gösterilir.
                </div>
                
                <div class="form-grid">
                    <div class="form-group">
                        <label class="form-label"><i class="fab fa-whatsapp social-icon whatsapp"></i> WhatsApp</label>
                        <input type="text" name="whatsapp_number" class="form-control" value="<?php echo clean(safeGet($settings, 'whatsapp_number')); ?>" placeholder="+905555555555">
                        <div class="form-hint">Ülke kodu ile, boşluksuz</div>
                    </div>
                    <div class="form-group">
                        <label class="form-label"><i class="fab fa-instagram social-icon instagram"></i> Instagram</label>
                        <div class="input-group">
                            <span class="input-prefix">instagram.com/</span>
                            <input type="text" name="instagram_username" class="form-control" value="<?php echo clean(safeLtrim(safeGet($settings, 'instagram_username'), '@')); ?>" placeholder="kullaniciadi">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="form-label"><i class="fab fa-telegram social-icon telegram"></i> Telegram</label>
                        <div class="input-group">
                            <span class="input-prefix">t.me/</span>
                            <input type="text" name="telegram_username" class="form-control" value="<?php echo clean(safeLtrim(safeGet($settings, 'telegram_username'), '@')); ?>" placeholder="kullaniciadi">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="form-label"><i class="fab fa-x-twitter social-icon twitter"></i> Twitter / X</label>
                        <div class="input-group">
                            <span class="input-prefix">twitter.com/</span>
                            <input type="text" name="twitter_username" class="form-control" value="<?php echo clean(safeLtrim(safeGet($settings, 'twitter_username'), '@')); ?>" placeholder="kullaniciadi">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="form-label"><i class="fab fa-facebook social-icon facebook"></i> Facebook</label>
                        <div class="input-group">
                            <span class="input-prefix">facebook.com/</span>
                            <input type="text" name="facebook_username" class="form-control" value="<?php echo clean(safeGet($settings, 'facebook_username')); ?>" placeholder="sayfaadi">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="form-label"><i class="fab fa-youtube social-icon youtube"></i> YouTube</label>
                        <div class="input-group">
                            <span class="input-prefix">youtube.com/</span>
                            <input type="text" name="youtube_channel" class="form-control" value="<?php echo clean(safeGet($settings, 'youtube_channel')); ?>" placeholder="@kanal veya channel/ID">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="form-label"><i class="fab fa-tiktok social-icon tiktok"></i> TikTok</label>
                        <div class="input-group">
                            <span class="input-prefix">tiktok.com/@</span>
                            <input type="text" name="tiktok_username" class="form-control" value="<?php echo clean(safeLtrim(safeGet($settings, 'tiktok_username'), '@')); ?>" placeholder="kullaniciadi">
                        </div>
                    </div>
                </div>
            </div>
            
            <?php elseif ($activeTab === 'footer'): ?>
            <!-- FOOTER -->
            <div class="section">
                <h4 class="section-title"><i class="fas fa-building"></i> Footer Bilgileri</h4>
                <div class="form-grid">
                    <div class="form-group full">
                        <label class="form-label">Hakkında Metni</label>
                        <textarea name="footer_about" class="form-control" rows="3" placeholder="Şirketiniz hakkında kısa açıklama..."><?php echo clean(safeGet($settings, 'footer_about')); ?></textarea>
                        <div class="form-hint">Footer'ın sol tarafında logo altında görünür</div>
                    </div>
                    <div class="form-group full">
                        <label class="form-label">Copyright Metni</label>
                        <input type="text" name="footer_text" class="form-control" value="<?php echo clean(safeGet($settings, 'footer_text')); ?>" placeholder="© 2024 Şirket Adı. Tüm hakları saklıdır.">
                    </div>
                </div>
            </div>
            
            <div class="section">
                <h4 class="section-title"><i class="fas fa-file-alt"></i> Footer'da Gösterilen Sayfalar</h4>
                <div class="info-box">
                    <i class="fas fa-info-circle"></i>
                    <a href="<?php echo SITE_URL; ?>/admin/sayfalar.php" style="color: inherit;">Sayfalar</a> bölümünden "Footer'da Göster" seçeneğini işaretleyerek yönetebilirsiniz.
                </div>
                
                <?php if (!empty($footerPages)): ?>
                <div class="pages-list">
                    <?php foreach ($footerPages as $fp): ?>
                    <div class="page-item">
                        <div>
                            <span class="title"><?php echo clean($fp['title']); ?></span>
                            <span class="slug">/sayfa/<?php echo clean($fp['slug']); ?></span>
                        </div>
                        <a href="<?php echo SITE_URL; ?>/admin/sayfalar.php?edit=<?php echo $fp['id']; ?>" class="edit-btn">
                            <i class="fas fa-edit"></i>
                        </a>
                    </div>
                    <?php endforeach; ?>
                </div>
                <?php else: ?>
                <p style="color: var(--text-secondary); font-size: 14px;">Footer'da gösterilecek sayfa yok.</p>
                <?php endif; ?>
            </div>
            
            <div class="section">
                <h4 class="section-title"><i class="fab fa-whatsapp"></i> WhatsApp Butonu</h4>
                <div class="toggle-item">
                    <div class="toggle-info">
                        <h5>Sabit WhatsApp Butonu</h5>
                        <p>Sol alt köşede yeşil WhatsApp butonu gösterilir</p>
                    </div>
                    <label class="toggle-switch">
                        <input type="checkbox" name="footer_whatsapp_enabled" value="1" <?php echo safeGet($settings, 'footer_whatsapp_enabled', '1') == '1' ? 'checked' : ''; ?>>
                        <span class="slider"></span>
                    </label>
                </div>
            </div>
            
            <?php elseif ($activeTab === 'payment'): ?>
            <!-- ÖDEME -->
            <div class="section">
                <h4 class="section-title"><i class="fas fa-money-bill"></i> Para Birimi</h4>
                <div class="form-grid">
                    <div class="form-group">
                        <label class="form-label">Para Birimi Kodu</label>
                        <select name="currency_code" class="form-control">
                            <option value="TRY" <?php echo safeGet($settings, 'currency_code') === 'TRY' ? 'selected' : ''; ?>>TRY - Türk Lirası</option>
                            <option value="USD" <?php echo safeGet($settings, 'currency_code') === 'USD' ? 'selected' : ''; ?>>USD - Amerikan Doları</option>
                            <option value="EUR" <?php echo safeGet($settings, 'currency_code') === 'EUR' ? 'selected' : ''; ?>>EUR - Euro</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label class="form-label">Para Birimi Sembolü</label>
                        <input type="text" name="currency_symbol" class="form-control" value="<?php echo clean(safeGet($settings, 'currency_symbol', '₺')); ?>" placeholder="₺">
                    </div>
                </div>
            </div>
            
            <?php elseif ($activeTab === 'seo'): ?>
            <!-- SEO & ANALİTİK -->
            <div class="section">
                <h4 class="section-title"><i class="fas fa-tags"></i> Meta Etiketleri</h4>
                <div class="form-grid">
                    <div class="form-group full">
                        <label class="form-label">Anahtar Kelimeler</label>
                        <input type="text" name="site_keywords" class="form-control" value="<?php echo clean(safeGet($settings, 'site_keywords')); ?>" placeholder="instagram takipçi, tiktok beğeni, youtube abone">
                        <div class="form-hint">Virgülle ayırarak yazın</div>
                    </div>
                </div>
            </div>
            
            <div class="section">
                <h4 class="section-title"><i class="fas fa-chart-bar"></i> Analitik & İzleme</h4>
                <div class="form-grid">
                    <div class="form-group">
                        <label class="form-label"><i class="fab fa-google" style="color: #4285F4; margin-right: 8px;"></i>Google Analytics ID</label>
                        <input type="text" name="google_analytics_id" class="form-control" value="<?php echo clean(safeGet($settings, 'google_analytics_id')); ?>" placeholder="G-XXXXXXXXXX">
                        <div class="form-hint">Google Analytics 4 ölçüm kimliği</div>
                    </div>
                    <div class="form-group">
                        <label class="form-label"><i class="fab fa-facebook" style="color: #1877F2; margin-right: 8px;"></i>Facebook Pixel ID</label>
                        <input type="text" name="facebook_pixel_id" class="form-control" value="<?php echo clean(safeGet($settings, 'facebook_pixel_id')); ?>" placeholder="123456789012345">
                        <div class="form-hint">Facebook reklam pikseli</div>
                    </div>
                </div>
            </div>
            
            <?php endif; ?>
            
        </div>
    </div>
</form>

<script>
// Logo/Favicon önizleme
document.querySelectorAll('input[type="file"]').forEach(function(input) {
    input.addEventListener('change', function(e) {
        var file = e.target.files[0];
        if (file) {
            var reader = new FileReader();
            reader.onload = function(e) {
                var preview = input.closest('.upload-box').querySelector('.upload-preview');
                if (preview) {
                    preview.innerHTML = '<img src="' + e.target.result + '" alt="Preview">';
                }
            };
            reader.readAsDataURL(file);
        }
    });
});
</script>

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