<?php
/**
 * Admin Panel - iyzico Ayarları
 * @version 1.2.1
 */

// Config'i dahil et
if (!defined('SITE_VERSION')) {
    require_once __DIR__ . '/../config.php';
}

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

// iyzico helper'ı dahil et
$iyzicoHelperPath = __DIR__ . '/../includes/iyzico.php';
if (file_exists($iyzicoHelperPath)) {
    require_once $iyzicoHelperPath;
}

// SDK yüklü mü kontrol et
$sdkLoaded = function_exists('isIyzicoSDKLoaded') ? isIyzicoSDKLoaded() : false;

// Kart aileleri listesi
$cardFamilies = [
    'Bonus' => 'Bonus Card (Garanti)',
    'Axess' => 'Axess (Akbank)',
    'Maximum' => 'Maximum (İş Bankası)',
    'World' => 'World (Yapı Kredi)',
    'CardFinans' => 'CardFinans (Finansbank)',
    'Paraf' => 'Paraf (Halkbank)',
    'Advantage' => 'Advantage (HSBC)',
    'Bankkart' => 'Bankkart (Ziraat)',
    'Combo' => 'Combo (TEB)',
    'Miles&Smiles' => 'Miles&Smiles (THY)',
    'Diger' => 'Diğer Kartlar'
];

// Form gönderildi mi?
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    
    // CSRF kontrolü
    if (!validateCSRFToken($_POST['csrf_token'] ?? '')) {
        setFlash('error', 'Güvenlik doğrulaması başarısız.');
        redirect(SITE_URL . '/admin/iyzico-ayarlari.php');
        exit;
    }
    
    // Test bağlantısı
    if (isset($_POST['test_connection'])) {
        $testApiKey = trim($_POST['iyzico_api_key'] ?? '');
        $testSecretKey = trim($_POST['iyzico_secret_key'] ?? '');
        $testSandbox = (isset($_POST['iyzico_sandbox']) && $_POST['iyzico_sandbox'] === '1');
        
        if (empty($testApiKey) || empty($testSecretKey)) {
            setFlash('error', 'API Key ve Secret Key zorunludur.');
        } elseif (!$sdkLoaded) {
            setFlash('error', 'iyzico SDK yüklü değil.');
        } else {
            $tempSettings = [
                'iyzico_api_key' => $testApiKey,
                'iyzico_secret_key' => $testSecretKey,
                'iyzico_sandbox' => $testSandbox ? '1' : '0',
                'iyzico_enabled' => '1'
            ];
            
            $iyzico = new IyzicoPayment($db, $tempSettings);
            
            if (method_exists($iyzico, 'testConnection')) {
                $testResult = $iyzico->testConnection();
                
                if ($testResult['success']) {
                    setFlash('success', $testResult['message'] ?? 'Bağlantı başarılı!');
                } else {
                    setFlash('error', 'Bağlantı hatası: ' . ($testResult['error'] ?? 'Bilinmeyen hata'));
                }
            }
        }
        
        redirect(SITE_URL . '/admin/iyzico-ayarlari.php');
        exit;
    }
    
    // Ayarları kaydet
    if (isset($_POST['save_settings'])) {
        
        $iyzicoEnabled = isset($_POST['iyzico_enabled']) && $_POST['iyzico_enabled'] === '1' ? '1' : '0';
        $iyzicoSandbox = isset($_POST['iyzico_sandbox']) && $_POST['iyzico_sandbox'] === '1' ? '1' : '0';
        $iyzico3dEnabled = isset($_POST['iyzico_3d_enabled']) && $_POST['iyzico_3d_enabled'] === '1' ? '1' : '0';
        $iyzicoInstallment = isset($_POST['iyzico_installment_enabled']) && $_POST['iyzico_installment_enabled'] === '1' ? '1' : '0';
        
        $updateSettings = [
            'iyzico_enabled' => $iyzicoEnabled,
            'iyzico_sandbox' => $iyzicoSandbox,
            'iyzico_api_key' => trim($_POST['iyzico_api_key'] ?? ''),
            'iyzico_secret_key' => trim($_POST['iyzico_secret_key'] ?? ''),
            'iyzico_3d_enabled' => $iyzico3dEnabled,
            'iyzico_installment_enabled' => $iyzicoInstallment,
            'iyzico_max_installment' => (int)($_POST['iyzico_max_installment'] ?? 12),
        ];
        
        try {
            foreach ($updateSettings as $key => $value) {
                $check = $db->prepare("SELECT COUNT(*) FROM settings WHERE setting_key = ?");
                $check->execute([$key]);
                
                if ($check->fetchColumn() > 0) {
                    $stmt = $db->prepare("UPDATE settings SET setting_value = ?, updated_at = NOW() WHERE setting_key = ?");
                    $stmt->execute([$value, $key]);
                } else {
                    $stmt = $db->prepare("INSERT INTO settings (setting_key, setting_value, setting_type, setting_group, created_at) VALUES (?, ?, 'string', 'payment', NOW())");
                    $stmt->execute([$key, $value]);
                }
            }
            
            $iyzicoActiveInt = $iyzicoEnabled === '1' ? 1 : 0;
            $checkMethod = $db->prepare("SELECT COUNT(*) FROM payment_methods WHERE code = 'iyzico'");
            $checkMethod->execute();
            
            if ($checkMethod->fetchColumn() > 0) {
                $db->prepare("UPDATE payment_methods SET is_active = ? WHERE code = 'iyzico'")->execute([$iyzicoActiveInt]);
            } else {
                $db->prepare("INSERT INTO payment_methods (name, code, description, icon, is_active, sort_order) VALUES (?, ?, ?, ?, ?, ?)")
                   ->execute(['Kredi Kartı (iyzico)', 'iyzico', 'Kredi kartı ile güvenli ödeme', 'fas fa-credit-card', $iyzicoActiveInt, 2]);
            }
            
            $modeText = $iyzicoSandbox === '1' ? 'Sandbox' : 'Canlı';
            setFlash('success', "iyzico ayarları kaydedildi. Ortam: {$modeText}");
            
        } catch (PDOException $e) {
            setFlash('error', 'Hata: ' . $e->getMessage());
        }
        
        redirect(SITE_URL . '/admin/iyzico-ayarlari.php');
        exit;
    }
    
    // Taksit ekle
    if (isset($_POST['add_installment'])) {
        $cardFamily = trim($_POST['card_family'] ?? '');
        $installmentCount = (int)($_POST['installment_count'] ?? 0);
        $installmentRate = floatval($_POST['installment_rate'] ?? 0);
        
        if (empty($cardFamily) || $installmentCount < 2 || $installmentCount > 12) {
            setFlash('error', 'Geçersiz değerler.');
        } else {
            try {
                $checkStmt = $db->prepare("SELECT COUNT(*) FROM iyzico_installments WHERE card_family = ? AND installment_count = ?");
                $checkStmt->execute([$cardFamily, $installmentCount]);
                
                if ($checkStmt->fetchColumn() > 0) {
                    setFlash('error', 'Bu kayıt zaten var.');
                } else {
                    $stmt = $db->prepare("INSERT INTO iyzico_installments (card_family, installment_count, installment_rate, is_active, created_at) VALUES (?, ?, ?, 1, NOW())");
                    $stmt->execute([$cardFamily, $installmentCount, $installmentRate]);
                    setFlash('success', 'Taksit eklendi.');
                }
            } catch (PDOException $e) {
                setFlash('error', 'Hata: ' . $e->getMessage());
            }
        }
        
        redirect(SITE_URL . '/admin/iyzico-ayarlari.php#taksit-yonetimi');
        exit;
    }
    
    // Taksit güncelle
    if (isset($_POST['update_installment'])) {
        $installmentId = (int)($_POST['installment_id'] ?? 0);
        $installmentRate = floatval($_POST['installment_rate'] ?? 0);
        $isActive = isset($_POST['is_active']) ? 1 : 0;
        
        if ($installmentId > 0) {
            try {
                $stmt = $db->prepare("UPDATE iyzico_installments SET installment_rate = ?, is_active = ? WHERE id = ?");
                $stmt->execute([$installmentRate, $isActive, $installmentId]);
                setFlash('success', 'Taksit güncellendi.');
            } catch (PDOException $e) {
                setFlash('error', 'Hata: ' . $e->getMessage());
            }
        }
        
        redirect(SITE_URL . '/admin/iyzico-ayarlari.php#taksit-yonetimi');
        exit;
    }
    
    // Taksit sil
    if (isset($_POST['delete_installment'])) {
        $installmentId = (int)($_POST['installment_id'] ?? 0);
        
        if ($installmentId > 0) {
            try {
                $db->prepare("DELETE FROM iyzico_installments WHERE id = ?")->execute([$installmentId]);
                setFlash('success', 'Taksit silindi.');
            } catch (PDOException $e) {
                setFlash('error', 'Hata: ' . $e->getMessage());
            }
        }
        
        redirect(SITE_URL . '/admin/iyzico-ayarlari.php#taksit-yonetimi');
        exit;
    }
    
    // Varsayılan taksitleri oluştur
    if (isset($_POST['create_default_installments'])) {
        try {
            $db->query("DELETE FROM iyzico_installments");
            
            $defaultRates = [2 => 1.50, 3 => 2.50, 4 => 3.50, 5 => 4.50, 6 => 5.50, 7 => 6.50, 8 => 7.50, 9 => 8.50, 10 => 9.50, 11 => 10.50, 12 => 11.50];
            
            $stmt = $db->prepare("INSERT INTO iyzico_installments (card_family, installment_count, installment_rate, is_active, created_at) VALUES (?, ?, ?, 1, NOW())");
            
            foreach ($cardFamilies as $familyCode => $familyName) {
                foreach ($defaultRates as $count => $rate) {
                    $stmt->execute([$familyCode, $count, $rate]);
                }
            }
            
            setFlash('success', 'Varsayılan taksitler oluşturuldu.');
        } catch (PDOException $e) {
            setFlash('error', 'Hata: ' . $e->getMessage());
        }
        
        redirect(SITE_URL . '/admin/iyzico-ayarlari.php#taksit-yonetimi');
        exit;
    }
}

// Mevcut ayarları çek
$iyzicoSettings = [
    'enabled' => '0',
    'sandbox' => '1',
    'api_key' => '',
    'secret_key' => '',
    '3d_enabled' => '0',
    'installment_enabled' => '1',
    'max_installment' => 12,
];

try {
    $stmt = $db->query("SELECT setting_key, setting_value FROM settings WHERE setting_key LIKE 'iyzico_%'");
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    foreach ($rows as $row) {
        switch ($row['setting_key']) {
            case 'iyzico_enabled': $iyzicoSettings['enabled'] = $row['setting_value']; break;
            case 'iyzico_sandbox': $iyzicoSettings['sandbox'] = $row['setting_value']; break;
            case 'iyzico_api_key': $iyzicoSettings['api_key'] = $row['setting_value']; break;
            case 'iyzico_secret_key': $iyzicoSettings['secret_key'] = $row['setting_value']; break;
            case 'iyzico_3d_enabled': $iyzicoSettings['3d_enabled'] = $row['setting_value']; break;
            case 'iyzico_installment_enabled': $iyzicoSettings['installment_enabled'] = $row['setting_value']; break;
            case 'iyzico_max_installment': $iyzicoSettings['max_installment'] = (int)$row['setting_value']; break;
        }
    }
} catch (PDOException $e) {}

// Taksit ayarlarını çek
$installments = [];
$installmentsByFamily = [];
try {
    $installments = $db->query("SELECT * FROM iyzico_installments ORDER BY card_family, installment_count")->fetchAll(PDO::FETCH_ASSOC);
    foreach ($installments as $inst) {
        $installmentsByFamily[$inst['card_family']][] = $inst;
    }
} catch (PDOException $e) {}

// İstatistikler
$iyzicoStats = ['total_transactions' => 0, 'total_amount' => 0, 'success_count' => 0, 'today_count' => 0, 'today_amount' => 0];

try {
    $stmt = $db->query("SELECT COUNT(*) as total, COALESCE(SUM(amount), 0) as total_amount, SUM(CASE WHEN status IN ('paid', 'completed') THEN 1 ELSE 0 END) as success_count FROM orders WHERE payment_method = 'iyzico'");
    $stats = $stmt->fetch(PDO::FETCH_ASSOC);
    $iyzicoStats['total_transactions'] = $stats['total'] ?? 0;
    $iyzicoStats['total_amount'] = $stats['total_amount'] ?? 0;
    $iyzicoStats['success_count'] = $stats['success_count'] ?? 0;
    
    $stmt = $db->query("SELECT COUNT(*) as count, COALESCE(SUM(amount), 0) as amount FROM orders WHERE payment_method = 'iyzico' AND DATE(created_at) = CURDATE() AND status IN ('paid', 'completed')");
    $today = $stmt->fetch(PDO::FETCH_ASSOC);
    $iyzicoStats['today_count'] = $today['count'] ?? 0;
    $iyzicoStats['today_amount'] = $today['amount'] ?? 0;
} catch (PDOException $e) {}

// Son işlemler
$recentTransactions = [];
try {
    $recentTransactions = $db->query("SELECT o.*, p.name as package_name FROM orders o JOIN packages p ON o.package_id = p.id WHERE o.payment_method = 'iyzico' ORDER BY o.created_at DESC LIMIT 10")->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {}

// Header
$adminPageTitle = 'iyzico Ayarları';
require_once __DIR__ . '/header.php';

// Boolean değerler
$isEnabled = ($iyzicoSettings['enabled'] === '1' || $iyzicoSettings['enabled'] === 1);
$isSandbox = ($iyzicoSettings['sandbox'] === '1' || $iyzicoSettings['sandbox'] === 1);
$is3dEnabled = ($iyzicoSettings['3d_enabled'] === '1' || $iyzicoSettings['3d_enabled'] === 1);
$isInstallmentEnabled = ($iyzicoSettings['installment_enabled'] === '1' || $iyzicoSettings['installment_enabled'] === 1);
?>

<style>
/* ==================== GENEL ==================== */
.iyzico-page { color: #f1f5f9; }
.iyzico-page h1, .iyzico-page h2, .iyzico-page h3, .iyzico-page h4, .iyzico-page h5, .iyzico-page h6 { color: #ffffff; }
.iyzico-page p, .iyzico-page span, .iyzico-page div, .iyzico-page td, .iyzico-page th { color: #e2e8f0; }

/* ==================== HEADER ==================== */
.iyzico-header {
    display: flex;
    align-items: center;
    gap: 20px;
    margin-bottom: 30px;
    flex-wrap: wrap;
}

.iyzico-logo {
    display: flex;
    align-items: center;
    gap: 15px;
    padding: 20px 25px;
    background: linear-gradient(135deg, rgba(59, 130, 246, 0.15), rgba(139, 92, 246, 0.15));
    border: 1px solid rgba(59, 130, 246, 0.3);
    border-radius: 16px;
    flex: 1;
    min-width: 280px;
}

.iyzico-logo-icon {
    width: 60px;
    height: 60px;
    background: linear-gradient(135deg, #3b82f6, #8b5cf6);
    border-radius: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 28px;
    color: #fff;
}

.iyzico-logo-text h3 { margin: 0; font-size: 20px; font-weight: 700; color: #fff !important; }
.iyzico-logo-text span { font-size: 13px; color: #94a3b8 !important; }

.sdk-status {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 20px;
    border-radius: 12px;
    font-size: 14px;
    font-weight: 600;
}

.sdk-status.success { background: rgba(16, 185, 129, 0.2); border: 1px solid rgba(16, 185, 129, 0.4); color: #34d399 !important; }
.sdk-status.error { background: rgba(239, 68, 68, 0.2); border: 1px solid rgba(239, 68, 68, 0.4); color: #f87171 !important; }

/* ==================== STATUS PILLS ==================== */
.current-status { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 20px; }

.status-pill {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 14px;
    border-radius: 20px;
    font-size: 12px;
    font-weight: 600;
}

.status-pill.active { background: rgba(16, 185, 129, 0.2); border: 1px solid rgba(16, 185, 129, 0.4); color: #34d399 !important; }
.status-pill.inactive { background: rgba(239, 68, 68, 0.2); border: 1px solid rgba(239, 68, 68, 0.4); color: #f87171 !important; }
.status-pill.sandbox { background: rgba(245, 158, 11, 0.2); border: 1px solid rgba(245, 158, 11, 0.4); color: #fbbf24 !important; }
.status-pill.production { background: rgba(16, 185, 129, 0.2); border: 1px solid rgba(16, 185, 129, 0.4); color: #34d399 !important; }

/* ==================== İSTATİSTİKLER ==================== */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 20px;
    margin-bottom: 30px;
}

.stat-card {
    background: #1e293b;
    border: 1px solid rgba(255,255,255,0.1);
    border-radius: 16px;
    padding: 24px;
    text-align: center;
}

.stat-card-icon {
    width: 50px;
    height: 50px;
    margin: 0 auto 15px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 22px;
}

.stat-card-icon.primary { background: rgba(99, 102, 241, 0.2); color: #818cf8; }
.stat-card-icon.success { background: rgba(16, 185, 129, 0.2); color: #34d399; }
.stat-card-icon.warning { background: rgba(245, 158, 11, 0.2); color: #fbbf24; }

.stat-card-value { font-size: 24px; font-weight: 800; margin-bottom: 5px; color: #fff !important; }
.stat-card-label { font-size: 13px; color: #94a3b8 !important; }

/* ==================== TABS ==================== */
.iyzico-tabs {
    display: flex;
    gap: 5px;
    background: #1e293b;
    padding: 6px;
    border-radius: 14px;
    margin-bottom: 25px;
    flex-wrap: wrap;
}

.iyzico-tab {
    padding: 12px 24px;
    background: transparent;
    border: none;
    border-radius: 10px;
    color: #94a3b8;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s;
    display: flex;
    align-items: center;
    gap: 8px;
}

.iyzico-tab:hover { color: #fff; background: rgba(99, 102, 241, 0.1); }
.iyzico-tab.active { background: linear-gradient(135deg, #6366f1, #8b5cf6); color: #fff; }

.iyzico-tab-content { display: none; }
.iyzico-tab-content.active { display: block; }

/* ==================== ORTAM SEÇİMİ ==================== */
.environment-toggle {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 15px;
    margin-bottom: 25px;
}

.env-option {
    padding: 24px 20px;
    background: #0f172a;
    border: 3px solid rgba(255,255,255,0.1);
    border-radius: 14px;
    cursor: pointer;
    text-align: center;
    transition: all 0.3s;
    position: relative;
}

.env-option:hover { border-color: rgba(99, 102, 241, 0.5); }

.env-option.active.sandbox {
    border-color: #f59e0b !important;
    background: rgba(245, 158, 11, 0.15) !important;
    box-shadow: 0 0 25px rgba(245, 158, 11, 0.3);
}

.env-option.active.production {
    border-color: #10b981 !important;
    background: rgba(16, 185, 129, 0.15) !important;
    box-shadow: 0 0 25px rgba(16, 185, 129, 0.3);
}

.env-option.active::after {
    content: '✓';
    position: absolute;
    top: 10px;
    right: 10px;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    font-weight: bold;
}

.env-option.active.sandbox::after { background: #f59e0b; color: #000; }
.env-option.active.production::after { background: #10b981; color: #fff; }

.env-option i { font-size: 36px; margin-bottom: 14px; display: block; }
.env-option.sandbox i { color: #fbbf24 !important; }
.env-option.production i { color: #34d399 !important; }
.env-option h6 { margin: 0 0 8px; font-size: 17px; font-weight: 700; color: #fff !important; }
.env-option p { margin: 0; font-size: 13px; color: #94a3b8 !important; }

/* ==================== SETTING ITEM ==================== */
.setting-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 20px;
    padding: 20px;
    background: #0f172a;
    border: 1px solid rgba(255,255,255,0.1);
    border-radius: 12px;
    margin-bottom: 15px;
}

.setting-item.is-active { border-color: rgba(16, 185, 129, 0.4); background: rgba(16, 185, 129, 0.05); }

.setting-info h6 { margin: 0 0 6px; font-size: 15px; font-weight: 600; color: #fff !important; display: flex; align-items: center; gap: 8px; }
.setting-info p { margin: 0; font-size: 13px; color: #94a3b8 !important; }

.status-dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.status-dot.on { background: #10b981; box-shadow: 0 0 8px #10b981; }
.status-dot.off { background: #64748b; }

/* Toggle Switch */
.toggle-switch { position: relative; width: 60px; height: 32px; flex-shrink: 0; }
.toggle-switch input { opacity: 0; width: 0; height: 0; }

.toggle-slider {
    position: absolute;
    cursor: pointer;
    top: 0; left: 0; right: 0; bottom: 0;
    background-color: #334155;
    transition: 0.3s;
    border-radius: 32px;
    border: 2px solid #475569;
}

.toggle-slider:before {
    position: absolute;
    content: "";
    height: 24px;
    width: 24px;
    left: 2px;
    bottom: 2px;
    background-color: #94a3b8;
    transition: 0.3s;
    border-radius: 50%;
}

.toggle-switch input:checked + .toggle-slider { background-color: #10b981; border-color: #10b981; }
.toggle-switch input:checked + .toggle-slider:before { transform: translateX(28px); background-color: #fff; }

/* ==================== FORM ELEMENTS ==================== */
.form-group-custom { margin-bottom: 20px; }
.form-group-custom .form-label { display: block; margin-bottom: 8px; font-size: 14px; font-weight: 600; color: #fff !important; }

.api-key-input { position: relative; }
.api-key-input input {
    width: 100%;
    padding: 14px 50px 14px 16px;
    background: #0f172a;
    border: 1px solid rgba(255,255,255,0.15);
    border-radius: 10px;
    color: #fff !important;
    font-size: 14px;
}

.api-key-input input::placeholder { color: #64748b !important; }
.api-key-input input:focus { outline: none; border-color: #6366f1; box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.2); }

.api-key-toggle {
    position: absolute;
    right: 12px;
    top: 50%;
    transform: translateY(-50%);
    background: none;
    border: none;
    color: #64748b;
    cursor: pointer;
    font-size: 18px;
}

.api-key-toggle:hover { color: #6366f1; }
.form-hint { margin-top: 6px; font-size: 12px; color: #64748b !important; }

.form-select-custom, .form-input-custom {
    width: 100%;
    padding: 12px 16px;
    background: #0f172a;
    border: 1px solid rgba(255,255,255,0.15);
    border-radius: 10px;
    color: #fff !important;
    font-size: 14px;
}

.form-select-custom:focus, .form-input-custom:focus { outline: none; border-color: #6366f1; }
.form-select-custom option { background: #1e293b; color: #fff; }

/* ==================== CARD ==================== */
.iyzico-page .card { background: #1e293b; border: 1px solid rgba(255,255,255,0.1); border-radius: 16px; }
.iyzico-page .card-header { background: rgba(0,0,0,0.2); border-bottom: 1px solid rgba(255,255,255,0.1); padding: 18px 24px; }
.iyzico-page .card-title { margin: 0; font-size: 16px; font-weight: 600; color: #fff !important; display: flex; align-items: center; gap: 10px; }
.iyzico-page .card-title i { color: #6366f1 !important; }
.iyzico-page .card-body { padding: 24px; }

/* ==================== TABLO - KOYU ARKA PLAN ==================== */
.iyzico-page .card-body.p-0 {
    padding: 0 !important;
    background: #1e293b !important;
}

.iyzico-page .table-responsive {
    background: #1e293b !important;
    border-radius: 0 0 16px 16px;
    overflow: hidden;
}

.iyzico-page .table,
.iyzico-page table.table {
    margin: 0 !important;
    background: #1e293b !important;
    color: #e2e8f0 !important;
    border-collapse: collapse;
    width: 100%;
}

.iyzico-page .table thead,
.iyzico-page table.table thead {
    background: #0f172a !important;
}

.iyzico-page .table thead tr,
.iyzico-page table.table thead tr {
    background: #0f172a !important;
}

.iyzico-page .table thead th,
.iyzico-page table.table thead th {
    background: #0f172a !important;
    color: #94a3b8 !important;
    font-weight: 600;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    padding: 14px 16px !important;
    border: none !important;
    border-bottom: 1px solid rgba(255,255,255,0.1) !important;
}

.iyzico-page .table tbody,
.iyzico-page table.table tbody {
    background: #1e293b !important;
}

.iyzico-page .table tbody tr,
.iyzico-page table.table tbody tr {
    background: #1e293b !important;
    transition: background 0.2s;
}

.iyzico-page .table tbody tr:nth-child(even),
.iyzico-page table.table tbody tr:nth-child(even) {
    background: #172033 !important;
}

.iyzico-page .table tbody tr:hover,
.iyzico-page table.table tbody tr:hover {
    background: #263449 !important;
}

.iyzico-page .table tbody td,
.iyzico-page table.table tbody td {
    background: transparent !important;
    color: #e2e8f0 !important;
    padding: 16px !important;
    border: none !important;
    border-bottom: 1px solid rgba(255,255,255,0.05) !important;
    vertical-align: middle;
}

.iyzico-page .table tbody tr:last-child td,
.iyzico-page table.table tbody tr:last-child td {
    border-bottom: none !important;
}

/* Tablo içi link ve özel sınıflar */
.iyzico-page .table tbody td a {
    color: #60a5fa !important;
    font-weight: 600;
    text-decoration: none;
}

.iyzico-page .table tbody td a:hover {
    color: #93c5fd !important;
    text-decoration: underline;
}

.customer-name {
    color: #ffffff !important;
    font-weight: 600;
    font-size: 14px;
}

.customer-email {
    color: #64748b !important;
    font-size: 12px;
    margin-top: 2px;
}

.amount {
    color: #34d399 !important;
    font-weight: 700;
    font-size: 15px;
}

.date {
    color: #94a3b8 !important;
    font-size: 13px;
}

/* Status badge'ler */
.status-badge {
    display: inline-flex;
    align-items: center;
    padding: 5px 12px;
    border-radius: 20px;
    font-size: 11px;
    font-weight: 600;
}

.status-pending { background: rgba(245, 158, 11, 0.2) !important; color: #fbbf24 !important; }
.status-paid { background: rgba(59, 130, 246, 0.2) !important; color: #60a5fa !important; }
.status-processing { background: rgba(139, 92, 246, 0.2) !important; color: #a78bfa !important; }
.status-completed { background: rgba(16, 185, 129, 0.2) !important; color: #34d399 !important; }
.status-cancelled { background: rgba(239, 68, 68, 0.2) !important; color: #f87171 !important; }
.status-refunded { background: rgba(107, 114, 128, 0.2) !important; color: #9ca3af !important; }

/* ==================== TAKSİT TABLOSU ==================== */
.installment-section { margin-bottom: 30px; }

.installment-card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 15px 20px;
    background: #0f172a;
    border-radius: 12px 12px 0 0;
    border-bottom: 1px solid rgba(255,255,255,0.1);
}

.installment-card-header h5 { margin: 0; font-size: 15px; font-weight: 600; color: #fff !important; display: flex; align-items: center; gap: 10px; }

.installment-table { width: 100%; border-collapse: collapse; background: #1e293b !important; }
.installment-table th { padding: 12px 16px; background: #0f172a !important; color: #94a3b8 !important; font-size: 11px; font-weight: 600; text-transform: uppercase; border-bottom: 1px solid rgba(255,255,255,0.1); }
.installment-table td { padding: 14px 16px; border-bottom: 1px solid rgba(255,255,255,0.05); color: #e2e8f0 !important; background: transparent !important; }
.installment-table tr { background: #1e293b !important; }
.installment-table tr:nth-child(even) { background: #172033 !important; }
.installment-table tr:hover { background: #263449 !important; }

.installment-count { font-weight: 700; color: #fff !important; }

.rate-input {
    width: 80px;
    padding: 8px 12px;
    background: #0f172a !important;
    border: 1px solid rgba(255,255,255,0.1);
    border-radius: 8px;
    color: #fff !important;
    text-align: center;
}

.mini-toggle { position: relative; width: 44px; height: 24px; }
.mini-toggle input { opacity: 0; width: 0; height: 0; }
.mini-toggle .slider { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background-color: #334155; transition: 0.3s; border-radius: 24px; }
.mini-toggle .slider:before { position: absolute; content: ""; height: 18px; width: 18px; left: 3px; bottom: 3px; background-color: #94a3b8; transition: 0.3s; border-radius: 50%; }
.mini-toggle input:checked + .slider { background-color: #10b981; }
.mini-toggle input:checked + .slider:before { transform: translateX(20px); background-color: #fff; }

/* ==================== TAKSİT EKLEME FORMU ==================== */
.add-installment-form {
    background: #0f172a;
    border: 1px solid rgba(255,255,255,0.1);
    border-radius: 12px;
    padding: 20px;
    margin-bottom: 25px;
}

.add-installment-form h5 { margin: 0 0 20px; font-size: 15px; font-weight: 600; color: #fff !important; display: flex; align-items: center; gap: 10px; }
.add-installment-form h5 i { color: #10b981; }

.add-form-row { display: grid; grid-template-columns: 1fr 1fr 1fr auto; gap: 15px; align-items: end; }

/* ==================== BUTONLAR ==================== */
.btn-group-custom { display: flex; gap: 12px; flex-wrap: wrap; margin-top: 25px; padding-top: 20px; border-top: 1px solid rgba(255,255,255,0.1); }

.btn-primary-custom, .btn-info-custom, .btn-success-custom, .btn-warning-custom, .btn-danger-custom, .btn-secondary-custom {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 12px 24px;
    border: none;
    border-radius: 10px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s;
    text-decoration: none;
}

.btn-primary-custom { background: linear-gradient(135deg, #6366f1, #8b5cf6); color: #fff; }
.btn-primary-custom:hover { transform: translateY(-2px); box-shadow: 0 10px 30px rgba(99, 102, 241, 0.3); }

.btn-info-custom { background: rgba(59, 130, 246, 0.2); color: #60a5fa; border: 1px solid rgba(59, 130, 246, 0.3); }
.btn-info-custom:hover { background: #3b82f6; color: #fff; }

.btn-success-custom { background: rgba(16, 185, 129, 0.2); color: #34d399; border: 1px solid rgba(16, 185, 129, 0.3); }
.btn-success-custom:hover { background: #10b981; color: #fff; }

.btn-warning-custom { background: rgba(245, 158, 11, 0.2); color: #fbbf24; border: 1px solid rgba(245, 158, 11, 0.3); }
.btn-warning-custom:hover { background: #f59e0b; color: #000; }

.btn-danger-custom { background: rgba(239, 68, 68, 0.2); color: #f87171; border: 1px solid rgba(239, 68, 68, 0.3); }
.btn-danger-custom:hover { background: #ef4444; color: #fff; }

.btn-secondary-custom { background: rgba(100, 116, 139, 0.2); color: #94a3b8; border: 1px solid rgba(100, 116, 139, 0.3); }
.btn-secondary-custom:hover { background: #64748b; color: #fff; }

.btn-sm { padding: 8px 16px; font-size: 13px; }

/* ==================== BİLGİ KUTULARI ==================== */
.info-box { padding: 18px 20px; background: rgba(59, 130, 246, 0.1); border: 1px solid rgba(59, 130, 246, 0.3); border-radius: 12px; margin-bottom: 15px; }
.info-box p { margin: 0; font-size: 14px; color: #e2e8f0 !important; line-height: 1.6; }
.info-box i { color: #60a5fa !important; margin-right: 8px; }
.info-box a { color: #60a5fa !important; }
.info-box code { display: inline-block; background: rgba(0,0,0,0.4); padding: 6px 12px; border-radius: 6px; font-size: 12px; color: #34d399 !important; margin-top: 8px; }

/* ==================== TEST KARTLARI ==================== */
.test-cards { background: #0f172a; border: 1px solid rgba(255,255,255,0.1); border-radius: 12px; padding: 20px; margin-bottom: 15px; }
.test-cards h6 { margin: 0 0 15px; font-size: 14px; font-weight: 600; color: #fff !important; display: flex; align-items: center; gap: 10px; }
.test-card-item { display: flex; align-items: center; justify-content: space-between; padding: 12px 14px; background: #1e293b; border-radius: 10px; margin-bottom: 10px; }
.test-card-number { font-family: monospace; font-size: 14px; color: #fff !important; letter-spacing: 1px; font-weight: 600; }
.test-card-type { font-size: 11px; color: #94a3b8 !important; background: rgba(255,255,255,0.1); padding: 4px 8px; border-radius: 6px; }
.test-info { margin-top: 15px; padding: 12px 16px; background: rgba(59, 130, 246, 0.1); border-radius: 8px; font-size: 12px; color: #94a3b8 !important; }

/* ==================== EMPTY STATE ==================== */
.empty-state { text-align: center; padding: 60px 30px; background: #1e293b; }
.empty-state i { font-size: 60px; color: #475569; margin-bottom: 20px; }
.empty-state h5 { font-size: 18px; font-weight: 600; color: #fff !important; margin-bottom: 10px; }
.empty-state p { font-size: 14px; color: #94a3b8 !important; margin-bottom: 25px; }

/* ==================== RESPONSIVE ==================== */
@media (max-width: 1199px) {
    .stats-grid { grid-template-columns: repeat(2, 1fr); }
    .add-form-row { grid-template-columns: 1fr 1fr; }
}

@media (max-width: 767px) {
    .stats-grid { grid-template-columns: 1fr 1fr; gap: 15px; }
    .environment-toggle { grid-template-columns: 1fr; }
    .setting-item { flex-direction: column; align-items: flex-start; gap: 15px; }
    .toggle-switch { align-self: flex-end; }
    .add-form-row { grid-template-columns: 1fr; }
}

@media (max-width: 576px) {
    .stats-grid { grid-template-columns: 1fr; }
    .iyzico-tab span { display: none; }
}
</style>

<div class="iyzico-page">

<!-- Header -->
<div class="iyzico-header">
    <div class="iyzico-logo">
        <div class="iyzico-logo-icon"><i class="fas fa-credit-card"></i></div>
        <div class="iyzico-logo-text">
            <h3>iyzico Ödeme Sistemi</h3>
            <span>Kredi/Banka Kartı ile Güvenli Ödeme</span>
        </div>
    </div>
    <?php if ($sdkLoaded): ?>
    <div class="sdk-status success"><i class="fas fa-check-circle"></i> SDK Yüklü</div>
    <?php else: ?>
    <div class="sdk-status error"><i class="fas fa-times-circle"></i> SDK Yüklü Değil</div>
    <?php endif; ?>
</div>

<!-- Durum -->
<div class="current-status">
    <span class="status-pill <?php echo $isEnabled ? 'active' : 'inactive'; ?>">
        <i class="fas fa-<?php echo $isEnabled ? 'check-circle' : 'times-circle'; ?>"></i>
        <?php echo $isEnabled ? 'iyzico Aktif' : 'iyzico Pasif'; ?>
    </span>
    <span class="status-pill <?php echo $isSandbox ? 'sandbox' : 'production'; ?>">
        <i class="fas fa-<?php echo $isSandbox ? 'flask' : 'globe'; ?>"></i>
        <?php echo $isSandbox ? 'Sandbox Modu' : 'Canlı Mod'; ?>
    </span>
    <?php if ($is3dEnabled): ?><span class="status-pill active"><i class="fas fa-shield-alt"></i> 3D Secure</span><?php endif; ?>
    <?php if ($isInstallmentEnabled): ?><span class="status-pill active"><i class="fas fa-calendar-alt"></i> Taksit Aktif</span><?php endif; ?>
</div>

<!-- İstatistikler -->
<div class="stats-grid">
    <div class="stat-card">
        <div class="stat-card-icon primary"><i class="fas fa-credit-card"></i></div>
        <div class="stat-card-value"><?php echo number_format($iyzicoStats['total_transactions']); ?></div>
        <div class="stat-card-label">Toplam İşlem</div>
    </div>
    <div class="stat-card">
        <div class="stat-card-icon success"><i class="fas fa-check-circle"></i></div>
        <div class="stat-card-value"><?php echo number_format($iyzicoStats['success_count']); ?></div>
        <div class="stat-card-label">Başarılı</div>
    </div>
    <div class="stat-card">
        <div class="stat-card-icon warning"><i class="fas fa-coins"></i></div>
        <div class="stat-card-value"><?php echo formatMoney($iyzicoStats['total_amount']); ?></div>
        <div class="stat-card-label">Toplam Tutar</div>
    </div>
    <div class="stat-card">
        <div class="stat-card-icon primary"><i class="fas fa-calendar-day"></i></div>
        <div class="stat-card-value"><?php echo formatMoney($iyzicoStats['today_amount']); ?></div>
        <div class="stat-card-label">Bugün</div>
    </div>
</div>

<!-- Tabs -->
<div class="iyzico-tabs">
    <button class="iyzico-tab active" onclick="switchTab('genel')"><i class="fas fa-cog"></i> <span>Genel Ayarlar</span></button>
    <button class="iyzico-tab" onclick="switchTab('taksit')"><i class="fas fa-calendar-alt"></i> <span>Taksit Yönetimi</span></button>
    <button class="iyzico-tab" onclick="switchTab('islemler')"><i class="fas fa-history"></i> <span>Son İşlemler</span></button>
</div>

<!-- GENEL AYARLAR TAB -->
<div class="iyzico-tab-content active" id="tab-genel">
    <div class="row">
        <div class="col-lg-7 mb-4">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title"><i class="fas fa-cog"></i> iyzico Ayarları</h5>
                </div>
                <div class="card-body">
                    <form method="POST">
                        <input type="hidden" name="csrf_token" value="<?php echo generateCSRFToken(); ?>">
                        <input type="hidden" name="iyzico_sandbox" id="iyzicoSandboxInput" value="<?php echo $isSandbox ? '1' : '0'; ?>">
                        
                        <div class="form-group-custom">
                            <label class="form-label">Çalışma Ortamı</label>
                            <div class="environment-toggle">
                                <div class="env-option sandbox <?php echo $isSandbox ? 'active' : ''; ?>" onclick="selectEnvironment('sandbox')" id="envSandbox">
                                    <i class="fas fa-flask"></i>
                                    <h6>Sandbox (Test)</h6>
                                    <p>Test ortamı</p>
                                </div>
                                <div class="env-option production <?php echo !$isSandbox ? 'active' : ''; ?>" onclick="selectEnvironment('production')" id="envProduction">
                                    <i class="fas fa-globe"></i>
                                    <h6>Production (Canlı)</h6>
                                    <p>Gerçek ödeme</p>
                                </div>
                            </div>
                        </div>
                        
                        <div class="form-group-custom">
                            <label class="form-label">API Key</label>
                            <div class="api-key-input">
                                <input type="password" name="iyzico_api_key" id="apiKeyInput" value="<?php echo clean($iyzicoSettings['api_key']); ?>" placeholder="API Key">
                                <button type="button" class="api-key-toggle" onclick="toggleVisibility('apiKeyInput', this)"><i class="fas fa-eye"></i></button>
                            </div>
                        </div>
                        
                        <div class="form-group-custom">
                            <label class="form-label">Secret Key</label>
                            <div class="api-key-input">
                                <input type="password" name="iyzico_secret_key" id="secretKeyInput" value="<?php echo clean($iyzicoSettings['secret_key']); ?>" placeholder="Secret Key">
                                <button type="button" class="api-key-toggle" onclick="toggleVisibility('secretKeyInput', this)"><i class="fas fa-eye"></i></button>
                            </div>
                        </div>
                        
                        <div class="setting-item <?php echo $isEnabled ? 'is-active' : ''; ?>" id="settingEnabled">
                            <div class="setting-info">
                                <h6><span class="status-dot <?php echo $isEnabled ? 'on' : 'off'; ?>" id="dotEnabled"></span> iyzico Ödeme Aktif</h6>
                                <p>Kredi kartı ile ödeme almayı açar/kapatır</p>
                            </div>
                            <label class="toggle-switch">
                                <input type="checkbox" name="iyzico_enabled" value="1" <?php echo $isEnabled ? 'checked' : ''; ?> onchange="updateSettingStatus('settingEnabled', 'dotEnabled', this.checked)">
                                <span class="toggle-slider"></span>
                            </label>
                        </div>
                        
                        <div class="setting-item <?php echo $is3dEnabled ? 'is-active' : ''; ?>" id="setting3d">
                            <div class="setting-info">
                                <h6><span class="status-dot <?php echo $is3dEnabled ? 'on' : 'off'; ?>" id="dot3d"></span> 3D Secure Zorunlu</h6>
                                <p>Tüm ödemelerde 3D Secure doğrulaması</p>
                            </div>
                            <label class="toggle-switch">
                                <input type="checkbox" name="iyzico_3d_enabled" value="1" <?php echo $is3dEnabled ? 'checked' : ''; ?> onchange="updateSettingStatus('setting3d', 'dot3d', this.checked)">
                                <span class="toggle-slider"></span>
                            </label>
                        </div>
                        
                        <div class="setting-item <?php echo $isInstallmentEnabled ? 'is-active' : ''; ?>" id="settingInstallment">
                            <div class="setting-info">
                                <h6><span class="status-dot <?php echo $isInstallmentEnabled ? 'on' : 'off'; ?>" id="dotInstallment"></span> Taksit Seçeneği</h6>
                                <p>Müşterilerin taksitli ödeme yapmasına izin verir</p>
                            </div>
                            <label class="toggle-switch">
                                <input type="checkbox" name="iyzico_installment_enabled" value="1" <?php echo $isInstallmentEnabled ? 'checked' : ''; ?> onchange="updateSettingStatus('settingInstallment', 'dotInstallment', this.checked)">
                                <span class="toggle-slider"></span>
                            </label>
                        </div>
                        
                        <div class="form-group-custom">
                            <label class="form-label">Maksimum Taksit</label>
                            <select name="iyzico_max_installment" class="form-select-custom">
                                <?php for ($i = 1; $i <= 12; $i++): ?>
                                <option value="<?php echo $i; ?>" <?php echo $iyzicoSettings['max_installment'] == $i ? 'selected' : ''; ?>><?php echo $i == 1 ? 'Tek Çekim' : $i . ' Taksit'; ?></option>
                                <?php endfor; ?>
                            </select>
                        </div>
                        
                        <div class="btn-group-custom">
                            <button type="submit" name="save_settings" class="btn-primary-custom"><i class="fas fa-save"></i> Kaydet</button>
                            <button type="submit" name="test_connection" class="btn-info-custom"><i class="fas fa-plug"></i> Test Et</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        
        <div class="col-lg-5 mb-4">
            <div class="card mb-4">
                <div class="card-header"><h5 class="card-title"><i class="fas fa-info-circle"></i> Bilgi</h5></div>
                <div class="card-body">
                    <div class="info-box"><p><i class="fas fa-link"></i> <a href="https://merchant.iyzipay.com/" target="_blank">iyzico Merchant Panel</a>'den API bilgilerinizi alın.</p></div>
                    <div class="info-box"><p><i class="fas fa-code"></i> Callback URL:<br><code><?php echo SITE_URL; ?>/iyzico-callback.php</code></p></div>
                </div>
            </div>
            
            <?php if ($isSandbox): ?>
            <div class="card">
                <div class="card-header"><h5 class="card-title"><i class="fas fa-flask"></i> Test Kartları</h5></div>
                <div class="card-body">
                    <div class="test-cards">
                        <h6><i class="fas fa-credit-card" style="color:#fbbf24"></i> Başarılı Ödeme</h6>
                        <div class="test-card-item"><span class="test-card-number">5528 7900 0000 0008</span><span class="test-card-type">MasterCard</span></div>
                        <div class="test-card-item"><span class="test-card-number">4603 4500 0000 0000</span><span class="test-card-type">Visa</span></div>
                    </div>
                    <div class="test-info"><i class="fas fa-info-circle" style="color:#60a5fa"></i> CVV: 123 | Son Kullanma: Gelecekteki tarih</div>
                </div>
            </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<!-- TAKSİT YÖNETİMİ TAB -->
<div class="iyzico-tab-content" id="tab-taksit">
    <div class="card">
        <div class="card-header d-flex justify-content-between align-items-center flex-wrap gap-2">
            <h5 class="card-title"><i class="fas fa-calendar-alt"></i> Taksit Yönetimi</h5>
            <?php if (empty($installments)): ?>
            <form method="POST" style="display:inline;">
                <input type="hidden" name="csrf_token" value="<?php echo generateCSRFToken(); ?>">
                <button type="submit" name="create_default_installments" class="btn-success-custom btn-sm"><i class="fas fa-magic"></i> Varsayılanları Oluştur</button>
            </form>
            <?php endif; ?>
        </div>
        <div class="card-body">
            
            <!-- Taksit Ekleme Formu -->
            <div class="add-installment-form">
                <h5><i class="fas fa-plus-circle"></i> Yeni Taksit Ekle</h5>
                <form method="POST">
                    <input type="hidden" name="csrf_token" value="<?php echo generateCSRFToken(); ?>">
                    <div class="add-form-row">
                        <div>
                            <label class="form-label">Kart Ailesi</label>
                            <select name="card_family" class="form-select-custom" required>
                                <option value="">Seçin...</option>
                                <?php foreach ($cardFamilies as $code => $name): ?>
                                <option value="<?php echo $code; ?>"><?php echo $name; ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div>
                            <label class="form-label">Taksit Sayısı</label>
                            <select name="installment_count" class="form-select-custom" required>
                                <?php for ($i = 2; $i <= 12; $i++): ?>
                                <option value="<?php echo $i; ?>"><?php echo $i; ?> Taksit</option>
                                <?php endfor; ?>
                            </select>
                        </div>
                        <div>
                            <label class="form-label">Komisyon (%)</label>
                            <input type="number" name="installment_rate" class="form-input-custom" step="0.01" min="0" max="50" value="0" required>
                        </div>
                        <div>
                            <button type="submit" name="add_installment" class="btn-success-custom"><i class="fas fa-plus"></i> Ekle</button>
                        </div>
                    </div>
                </form>
            </div>
            
            <?php if (empty($installments)): ?>
            <div class="empty-state">
                <i class="fas fa-calendar-times"></i>
                <h5>Taksit Seçeneği Yok</h5>
                <p>Henüz taksit seçeneği eklenmemiş. Yukarıdaki "Varsayılanları Oluştur" butonuna tıklayarak başlayın.</p>
            </div>
            <?php else: ?>
            
            <?php foreach ($installmentsByFamily as $family => $items): ?>
            <div class="installment-section">
                <div class="installment-card-header">
                    <h5><i class="fas fa-credit-card" style="color:#6366f1"></i> <?php echo $cardFamilies[$family] ?? $family; ?></h5>
                </div>
                <table class="installment-table">
                    <thead>
                        <tr>
                            <th>Taksit</th>
                            <th>Komisyon (%)</th>
                            <th>Durum</th>
                            <th>İşlem</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($items as $item): ?>
                        <tr>
                            <td><span class="installment-count"><i class="fas fa-calendar-check" style="color:#6366f1;margin-right:8px;"></i> <?php echo $item['installment_count']; ?> Taksit</span></td>
                            <td>
                                <form method="POST" class="d-inline" id="form-<?php echo $item['id']; ?>">
                                    <input type="hidden" name="csrf_token" value="<?php echo generateCSRFToken(); ?>">
                                    <input type="hidden" name="installment_id" value="<?php echo $item['id']; ?>">
                                    <input type="number" name="installment_rate" class="rate-input" step="0.01" min="0" max="50" value="<?php echo number_format($item['installment_rate'], 2); ?>">
                            </td>
                            <td>
                                    <label class="mini-toggle">
                                        <input type="checkbox" name="is_active" value="1" <?php echo $item['is_active'] ? 'checked' : ''; ?>>
                                        <span class="slider"></span>
                                    </label>
                            </td>
                            <td>
                                    <button type="submit" name="update_installment" class="btn-success-custom btn-sm" title="Kaydet"><i class="fas fa-save"></i></button>
                                </form>
                                <form method="POST" class="d-inline" onsubmit="return confirm('Bu taksiti silmek istediğinize emin misiniz?')">
                                    <input type="hidden" name="csrf_token" value="<?php echo generateCSRFToken(); ?>">
                                    <input type="hidden" name="installment_id" value="<?php echo $item['id']; ?>">
                                    <button type="submit" name="delete_installment" class="btn-danger-custom btn-sm" title="Sil"><i class="fas fa-trash"></i></button>
                                </form>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <?php endforeach; ?>
            
            <?php endif; ?>
        </div>
    </div>
</div>

<!-- SON İŞLEMLER TAB -->
<div class="iyzico-tab-content" id="tab-islemler">
    <div class="card">
        <div class="card-header d-flex justify-content-between align-items-center flex-wrap gap-2">
            <h5 class="card-title"><i class="fas fa-history"></i> Son iyzico İşlemleri</h5>
            <a href="<?php echo SITE_URL; ?>/admin/siparisler.php?payment_method=iyzico" class="btn-secondary-custom btn-sm"><i class="fas fa-list"></i> Tümünü Gör</a>
        </div>
        <?php if (empty($recentTransactions)): ?>
        <div class="card-body p-0">
            <div class="empty-state">
                <i class="fas fa-inbox"></i>
                <h5>Henüz İşlem Yok</h5>
                <p>iyzico ile yapılmış ödeme bulunmuyor.</p>
            </div>
        </div>
        <?php else: ?>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table">
                    <thead>
                        <tr>
                            <th>Sipariş No</th>
                            <th>Müşteri</th>
                            <th>Paket</th>
                            <th>Tutar</th>
                            <th>Durum</th>
                            <th>Tarih</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($recentTransactions as $tx): ?>
                        <tr>
                            <td>
                                <a href="<?php echo SITE_URL; ?>/admin/siparis-detay.php?id=<?php echo $tx['id']; ?>">
                                    <?php echo clean($tx['order_number']); ?>
                                </a>
                            </td>
                            <td>
                                <div class="customer-name"><?php echo clean($tx['customer_name']); ?></div>
                                <div class="customer-email"><?php echo clean($tx['customer_email']); ?></div>
                            </td>
                            <td><?php echo clean($tx['package_name']); ?></td>
                            <td><span class="amount"><?php echo formatMoney($tx['amount']); ?></span></td>
                            <td>
                                <?php
                                $statusClass = [
                                    'pending' => 'status-pending',
                                    'paid' => 'status-paid',
                                    'processing' => 'status-processing',
                                    'completed' => 'status-completed',
                                    'cancelled' => 'status-cancelled',
                                    'refunded' => 'status-refunded'
                                ];
                                $statusText = [
                                    'pending' => 'Bekliyor',
                                    'paid' => 'Ödendi',
                                    'processing' => 'İşleniyor',
                                    'completed' => 'Tamamlandı',
                                    'cancelled' => 'İptal',
                                    'refunded' => 'İade'
                                ];
                                ?>
                                <span class="status-badge <?php echo $statusClass[$tx['status']] ?? ''; ?>">
                                    <?php echo $statusText[$tx['status']] ?? $tx['status']; ?>
                                </span>
                            </td>
                            <td><span class="date"><?php echo formatDate($tx['created_at']); ?></span></td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
        <?php endif; ?>
    </div>
</div>

</div>

<script>
// Tab değiştirme
function switchTab(tabName) {
    // Tüm tabları gizle
    document.querySelectorAll('.iyzico-tab-content').forEach(function(tab) {
        tab.classList.remove('active');
    });
    
    // Tüm tab butonlarından active kaldır
    document.querySelectorAll('.iyzico-tab').forEach(function(btn) {
        btn.classList.remove('active');
    });
    
    // Seçilen tabı göster
    document.getElementById('tab-' + tabName).classList.add('active');
    
    // Seçilen butona active ekle
    event.target.closest('.iyzico-tab').classList.add('active');
    
    // URL hash güncelle
    window.location.hash = tabName + '-yonetimi';
}

// Sayfa yüklendiğinde hash kontrolü
document.addEventListener('DOMContentLoaded', function() {
    var hash = window.location.hash;
    if (hash === '#taksit-yonetimi') {
        document.querySelectorAll('.iyzico-tab')[1].click();
    } else if (hash === '#islemler-yonetimi') {
        document.querySelectorAll('.iyzico-tab')[2].click();
    }
});

// Ortam seçimi
function selectEnvironment(env) {
    var sandboxOption = document.getElementById('envSandbox');
    var productionOption = document.getElementById('envProduction');
    var hiddenInput = document.getElementById('iyzicoSandboxInput');
    
    sandboxOption.classList.remove('active');
    productionOption.classList.remove('active');
    
    if (env === 'sandbox') {
        sandboxOption.classList.add('active');
        hiddenInput.value = '1';
    } else {
        productionOption.classList.add('active');
        hiddenInput.value = '0';
    }
}

// API key göster/gizle
function toggleVisibility(inputId, button) {
    var input = document.getElementById(inputId);
    var icon = button.querySelector('i');
    
    if (input.type === 'password') {
        input.type = 'text';
        icon.classList.remove('fa-eye');
        icon.classList.add('fa-eye-slash');
    } else {
        input.type = 'password';
        icon.classList.remove('fa-eye-slash');
        icon.classList.add('fa-eye');
    }
}

// Toggle switch değiştiğinde görsel güncelle
function updateSettingStatus(itemId, dotId, isActive) {
    var item = document.getElementById(itemId);
    var dot = document.getElementById(dotId);
    
    if (isActive) {
        item.classList.add('is-active');
        dot.classList.remove('off');
        dot.classList.add('on');
    } else {
        item.classList.remove('is-active');
        dot.classList.remove('on');
        dot.classList.add('off');
    }
}
</script>

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