<?php
/**
 * SMM Panel - Profesyonel Kurulum Sistemi
 * @version 2.0.0
 * @author Paketix
 */

// Hata raporlama
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Session
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

date_default_timezone_set('Europe/Istanbul');

// Dosya yolları
$configFile = __DIR__ . '/config.php';
$sqlFile = __DIR__ . '/database.sql';
$installLockFile = __DIR__ . '/install.lock';

// Kurulum tamamlandı mı
if (file_exists($installLockFile)) {
    showCompletedPage();
    exit;
}

// Adım kontrolü
$step = isset($_GET['step']) ? (int)$_GET['step'] : 1;
$step = max(1, min(5, $step));

if ($step > 1 && (!isset($_SESSION['install_step']) || $_SESSION['install_step'] < $step - 1)) {
    header('Location: install.php?step=1');
    exit;
}

$error = '';
$success = '';

// POST İşlemleri
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    
    // ADIM 1: Veritabanı + Site URL
    if ($step === 1 && isset($_POST['db_test'])) {
        $siteUrl = rtrim(trim($_POST['site_url'] ?? ''), '/');
        $dbHost = trim($_POST['db_host'] ?? 'localhost');
        $dbName = trim($_POST['db_name'] ?? '');
        $dbUser = trim($_POST['db_user'] ?? '');
        $dbPass = $_POST['db_pass'] ?? '';
        
        if (empty($siteUrl)) {
            $error = 'Site URL zorunludur.';
        } elseif (!filter_var($siteUrl, FILTER_VALIDATE_URL)) {
            $error = 'Geçerli bir URL girin. (Örn: https://siteadi.com)';
        } elseif (empty($dbName) || empty($dbUser)) {
            $error = 'Veritabanı adı ve kullanıcı adı zorunludur.';
        } else {
            try {
                $dsn = "mysql:host={$dbHost};charset=utf8mb4";
                $pdo = new PDO($dsn, $dbUser, $dbPass, [
                    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
                ]);
                
                $pdo->exec("CREATE DATABASE IF NOT EXISTS `{$dbName}` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
                $pdo->exec("USE `{$dbName}`");
                
                if (!file_exists($configFile)) {
                    $error = 'config.php dosyası bulunamadı!';
                } elseif (!is_writable($configFile)) {
                    $error = 'config.php yazma izni yok. Dosyaya chmod 666 uygulayın.';
                } else {
                    $configContent = file_get_contents($configFile);
                    
                    $configContent = preg_replace("/define\('DB_HOST',\s*'[^']*'\);/", "define('DB_HOST', '{$dbHost}');", $configContent);
                    $configContent = preg_replace("/define\('DB_NAME',\s*'[^']*'\);/", "define('DB_NAME', '{$dbName}');", $configContent);
                    $configContent = preg_replace("/define\('DB_USER',\s*'[^']*'\);/", "define('DB_USER', '{$dbUser}');", $configContent);
                    
                    $dbPassEscaped = addslashes($dbPass);
                    $configContent = preg_replace("/define\('DB_PASS',\s*'[^']*'\);/", "define('DB_PASS', '{$dbPassEscaped}');", $configContent);
                    
                    if (file_put_contents($configFile, $configContent)) {
                        $_SESSION['install_step'] = 1;
                        $_SESSION['db_host'] = $dbHost;
                        $_SESSION['db_name'] = $dbName;
                        $_SESSION['db_user'] = $dbUser;
                        $_SESSION['db_pass'] = $dbPass;
                        $_SESSION['site_url'] = $siteUrl;
                        
                        header('Location: install.php?step=2');
                        exit;
                    } else {
                        $error = 'config.php dosyasına yazılamadı!';
                    }
                }
            } catch (PDOException $e) {
                $error = 'Veritabanı bağlantı hatası: ' . $e->getMessage();
            }
        }
    }
    
    // ADIM 2: SQL Yükleme
    if ($step === 2 && isset($_POST['import_sql'])) {
        if (!file_exists($sqlFile)) {
            $error = 'database.sql dosyası bulunamadı! Ana dizine yükleyin.';
        } else {
            try {
                $dsn = "mysql:host={$_SESSION['db_host']};dbname={$_SESSION['db_name']};charset=utf8mb4";
                $pdo = new PDO($dsn, $_SESSION['db_user'], $_SESSION['db_pass'], [
                    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
                ]);
                
                $sql = file_get_contents($sqlFile);
                $pdo->exec($sql);
                
                // Site URL'yi settings tablosuna yaz
                $siteUrl = $_SESSION['site_url'] ?? '';
                if (!empty($siteUrl)) {
                    $stmt = $pdo->prepare("UPDATE settings SET setting_value = ? WHERE setting_key = 'site_url'");
                    $stmt->execute([$siteUrl]);
                }
                
                $_SESSION['install_step'] = 2;
                header('Location: install.php?step=3');
                exit;
                
            } catch (PDOException $e) {
                $error = 'SQL yükleme hatası: ' . $e->getMessage();
            }
        }
    }
    
    // ADIM 3: Sistem Kontrolü
    if ($step === 3 && isset($_POST['continue_step3'])) {
        $_SESSION['install_step'] = 3;
        header('Location: install.php?step=4');
        exit;
    }
    
    // ADIM 4: Admin Oluştur/Düzenle
    if ($step === 4 && isset($_POST['save_admin'])) {
        $adminUser = trim($_POST['admin_username'] ?? '');
        $adminEmail = trim($_POST['admin_email'] ?? '');
        $adminPass = $_POST['admin_password'] ?? '';
        $adminPassConfirm = $_POST['admin_password_confirm'] ?? '';
        $adminName = trim($_POST['admin_name'] ?? '');
        $adminPhone = trim($_POST['admin_phone'] ?? '');
        
        if (empty($adminUser) || empty($adminEmail)) {
            $error = 'Kullanıcı adı ve e-posta zorunludur.';
        } elseif (!filter_var($adminEmail, FILTER_VALIDATE_EMAIL)) {
            $error = 'Geçerli bir e-posta adresi girin.';
        } elseif (!empty($adminPass) && strlen($adminPass) < 6) {
            $error = 'Şifre en az 6 karakter olmalı.';
        } elseif (!empty($adminPass) && $adminPass !== $adminPassConfirm) {
            $error = 'Şifreler eşleşmiyor.';
        } else {
            try {
                $dsn = "mysql:host={$_SESSION['db_host']};dbname={$_SESSION['db_name']};charset=utf8mb4";
                $pdo = new PDO($dsn, $_SESSION['db_user'], $_SESSION['db_pass'], [
                    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
                ]);
                
                $checkStmt = $pdo->prepare("SELECT id, password FROM users WHERE role = 'admin' LIMIT 1");
                $checkStmt->execute();
                $existingAdmin = $checkStmt->fetch(PDO::FETCH_ASSOC);
                
                if ($existingAdmin) {
                    // Güncelle
                    if (!empty($adminPass)) {
                        $hashedPass = password_hash($adminPass, PASSWORD_DEFAULT);
                        $stmt = $pdo->prepare("UPDATE users SET username = ?, email = ?, password = ?, full_name = ?, phone = ?, updated_at = NOW() WHERE id = ?");
                        $stmt->execute([$adminUser, $adminEmail, $hashedPass, $adminName, $adminPhone, $existingAdmin['id']]);
                    } else {
                        $stmt = $pdo->prepare("UPDATE users SET username = ?, email = ?, full_name = ?, phone = ?, updated_at = NOW() WHERE id = ?");
                        $stmt->execute([$adminUser, $adminEmail, $adminName, $adminPhone, $existingAdmin['id']]);
                    }
                } else {
                    // Yeni oluştur
                    if (empty($adminPass)) {
                        $error = 'Yeni admin için şifre zorunludur.';
                    } else {
                        $hashedPass = password_hash($adminPass, PASSWORD_DEFAULT);
                        $stmt = $pdo->prepare("INSERT INTO users (username, email, password, full_name, phone, role, status, created_at) VALUES (?, ?, ?, ?, ?, 'admin', 'active', NOW())");
                        $stmt->execute([$adminUser, $adminEmail, $hashedPass, $adminName, $adminPhone]);
                    }
                }
                
                if (empty($error)) {
                    $_SESSION['install_step'] = 4;
                    header('Location: install.php?step=5');
                    exit;
                }
                
            } catch (PDOException $e) {
                $error = 'Admin işlem hatası: ' . $e->getMessage();
            }
        }
    }
    
    // ADIM 5: Tamamla
    if ($step === 5 && isset($_POST['finish_install'])) {
        $installData = [
            'installed_at' => date('Y-m-d H:i:s'),
            'php_version' => PHP_VERSION,
            'server' => $_SERVER['SERVER_SOFTWARE'] ?? 'Unknown',
            'ip' => $_SERVER['SERVER_ADDR'] ?? 'Unknown'
        ];
        file_put_contents($installLockFile, json_encode($installData, JSON_PRETTY_PRINT));
        
        unset($_SESSION['install_step'], $_SESSION['db_host'], $_SESSION['db_name'], $_SESSION['db_user'], $_SESSION['db_pass'], $_SESSION['site_url']);
        
        header('Location: install.php?completed=1');
        exit;
    }
}

// Sistem Kontrolü
function checkSystem() {
    $checks = [];
    
    // PHP Sürümü
    $checks['php_version'] = [
        'current' => PHP_VERSION,
        'required' => '7.4.0',
        'ok' => version_compare(PHP_VERSION, '7.4.0', '>=')
    ];
    
    // PHP Uzantıları
    $extensions = ['pdo', 'pdo_mysql', 'json', 'mbstring', 'curl', 'openssl', 'gd'];
    foreach ($extensions as $ext) {
        $checks['extensions'][$ext] = extension_loaded($ext);
    }
    
    // Klasörler
    $folders = [
        'uploads' => ['path' => __DIR__ . '/uploads', 'required' => true],
        'uploads/logos' => ['path' => __DIR__ . '/uploads/logos', 'required' => false],
        'uploads/popups' => ['path' => __DIR__ . '/uploads/popups', 'required' => false],
        'logs' => ['path' => __DIR__ . '/logs', 'required' => true],
        'includes' => ['path' => __DIR__ . '/includes', 'required' => true],
        'cron' => ['path' => __DIR__ . '/cron', 'required' => true],
        'admin' => ['path' => __DIR__ . '/admin', 'required' => true]
    ];
    
    foreach ($folders as $name => $info) {
        $checks['folders'][$name] = [
            'exists' => is_dir($info['path']),
            'writable' => is_dir($info['path']) && is_writable($info['path']),
            'required' => $info['required']
        ];
    }
    
    // Dosyalar
    $files = [
        'config.php' => ['path' => __DIR__ . '/config.php', 'writable' => true],
        'database.sql' => ['path' => __DIR__ . '/database.sql', 'writable' => false],
        'header.php' => ['path' => __DIR__ . '/header.php', 'writable' => false],
        'footer.php' => ['path' => __DIR__ . '/footer.php', 'writable' => false]
    ];
    
    foreach ($files as $name => $info) {
        $exists = file_exists($info['path']);
        $checks['files'][$name] = [
            'exists' => $exists,
            'writable' => $info['writable'] ? ($exists && is_writable($info['path'])) : true,
            'needs_writable' => $info['writable']
        ];
    }
    
    // Veritabanı Tabloları
    $requiredTables = ['users', 'settings', 'categories', 'packages', 'orders', 'payment_methods', 'themes', 'notifications', 'popups'];
    $checks['tables'] = [];
    
    if (isset($_SESSION['db_host'])) {
        try {
            $dsn = "mysql:host={$_SESSION['db_host']};dbname={$_SESSION['db_name']};charset=utf8mb4";
            $pdo = new PDO($dsn, $_SESSION['db_user'], $_SESSION['db_pass']);
            
            foreach ($requiredTables as $table) {
                $stmt = $pdo->query("SHOW TABLES LIKE '{$table}'");
                $checks['tables'][$table] = $stmt->rowCount() > 0;
            }
        } catch (PDOException $e) {
            $checks['db_error'] = $e->getMessage();
        }
    }
    
    return $checks;
}

// Admin bilgilerini çek
function getExistingAdmin() {
    if (!isset($_SESSION['db_host'])) return null;
    
    try {
        $dsn = "mysql:host={$_SESSION['db_host']};dbname={$_SESSION['db_name']};charset=utf8mb4";
        $pdo = new PDO($dsn, $_SESSION['db_user'], $_SESSION['db_pass']);
        
        $stmt = $pdo->prepare("SELECT * FROM users WHERE role = 'admin' LIMIT 1");
        $stmt->execute();
        return $stmt->fetch(PDO::FETCH_ASSOC);
    } catch (PDOException $e) {
        return null;
    }
}

// Cron Bilgileri
function getCronInfo() {
    $serverPath = __DIR__;
    
    $phpPaths = [
        '/usr/bin/php',
        '/usr/local/bin/php',
        '/opt/cpanel/ea-php74/root/usr/bin/php',
        '/opt/cpanel/ea-php80/root/usr/bin/php',
        '/opt/cpanel/ea-php81/root/usr/bin/php',
        '/opt/cpanel/ea-php82/root/usr/bin/php',
        '/opt/alt/php74/usr/bin/php',
        '/opt/alt/php80/usr/bin/php',
        '/opt/alt/php81/usr/bin/php',
        PHP_BINDIR . '/php'
    ];
    
    $foundPhp = '/usr/bin/php';
    foreach ($phpPaths as $path) {
        if (@file_exists($path)) {
            $foundPhp = $path;
            break;
        }
    }
    
    return [
        'server_path' => $serverPath,
        'php_path' => $foundPhp,
        'crons' => [
            [
                'file' => 'cron/cron1.php',
                'name' => 'Ana Sipariş İşleyici',
                'description' => 'API siparişlerini işler ve durumları günceller',
                'interval' => 'Her 5 dakikada bir',
                'cron_time' => '*/5 * * * *',
                'command' => "*/5 * * * * {$foundPhp} {$serverPath}/cron/cron1.php >/dev/null 2>&1"
            ],
            [
                'file' => 'cron/price-monitor.php',
                'name' => 'Fiyat Monitörü',
                'description' => 'API fiyatlarını kontrol eder ve günceller',
                'interval' => 'Her 6 saatte bir',
                'cron_time' => '0 */6 * * *',
                'command' => "0 */6 * * * {$foundPhp} {$serverPath}/cron/price-monitor.php >/dev/null 2>&1"
            ]
        ]
    ];
}

// Tamamlandı Sayfası
function showCompletedPage() {
    if (isset($_GET['completed'])) {
        showSuccessPage();
        exit;
    }
    
    echo '<!DOCTYPE html><html lang="tr"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Kurulum Tamamlandı</title>';
    echo '<style>*{margin:0;padding:0;box-sizing:border-box}body{font-family:Inter,sans-serif;background:#0f172a;color:#f8fafc;display:flex;align-items:center;justify-content:center;min-height:100vh}.box{background:#1e293b;padding:40px;border-radius:20px;text-align:center;max-width:500px}h1{color:#10b981;margin-bottom:20px}p{color:#94a3b8;margin-bottom:30px}a{background:linear-gradient(135deg,#6366f1,#8b5cf6);color:#fff;padding:15px 30px;border-radius:10px;text-decoration:none;font-weight:600;display:inline-block}</style>';
    echo '</head><body><div class="box"><h1>✅ Kurulum Tamamlandı</h1><p>Güvenlik için bu dosyayı silin.</p><a href="./">Siteye Git</a></div></body></html>';
}

function showSuccessPage() {
    ?>
    <!DOCTYPE html>
    <html lang="tr">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>🎉 Kurulum Tamamlandı!</title>
        <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
        <style>
            * { margin: 0; padding: 0; box-sizing: border-box; }
            body { 
                font-family: 'Inter', sans-serif; 
                background: linear-gradient(135deg, #0f172a 0%, #1e1b4b 50%, #0f172a 100%); 
                min-height: 100vh; 
                display: flex; 
                align-items: center; 
                justify-content: center; 
                padding: 20px;
            }
            .success-container {
                text-align: center;
                max-width: 600px;
            }
            .success-icon {
                width: 120px;
                height: 120px;
                background: linear-gradient(135deg, #10b981, #059669);
                border-radius: 50%;
                display: flex;
                align-items: center;
                justify-content: center;
                margin: 0 auto 30px;
                font-size: 60px;
                color: #fff;
                box-shadow: 0 20px 60px rgba(16, 185, 129, 0.4);
                animation: bounce 2s ease infinite;
            }
            @keyframes bounce {
                0%, 100% { transform: translateY(0); }
                50% { transform: translateY(-10px); }
            }
            .confetti {
                position: fixed;
                top: 0;
                left: 0;
                width: 100%;
                height: 100%;
                pointer-events: none;
                overflow: hidden;
                z-index: -1;
            }
            .confetti-piece {
                position: absolute;
                width: 10px;
                height: 20px;
                top: -20px;
                animation: confetti-fall 3s ease-out infinite;
            }
            @keyframes confetti-fall {
                0% { transform: translateY(0) rotate(0deg); opacity: 1; }
                100% { transform: translateY(100vh) rotate(720deg); opacity: 0; }
            }
            h1 {
                font-size: 36px;
                font-weight: 800;
                color: #fff;
                margin-bottom: 15px;
            }
            .subtitle {
                font-size: 18px;
                color: #94a3b8;
                margin-bottom: 40px;
                line-height: 1.6;
            }
            .buttons {
                display: flex;
                gap: 15px;
                justify-content: center;
                flex-wrap: wrap;
                margin-bottom: 40px;
            }
            .btn {
                padding: 18px 35px;
                border-radius: 14px;
                font-weight: 700;
                font-size: 16px;
                text-decoration: none;
                display: inline-flex;
                align-items: center;
                gap: 10px;
                transition: all 0.3s;
            }
            .btn-primary {
                background: linear-gradient(135deg, #6366f1, #8b5cf6);
                color: #fff;
                box-shadow: 0 10px 30px rgba(99, 102, 241, 0.4);
            }
            .btn-secondary {
                background: rgba(255,255,255,0.1);
                color: #fff;
                border: 2px solid rgba(255,255,255,0.2);
            }
            .btn:hover {
                transform: translateY(-3px);
            }
            .warning-box {
                background: linear-gradient(135deg, rgba(239, 68, 68, 0.15), rgba(239, 68, 68, 0.05));
                border: 2px solid rgba(239, 68, 68, 0.3);
                border-radius: 16px;
                padding: 25px;
                max-width: 500px;
                margin: 0 auto;
            }
            .warning-box h4 {
                color: #ef4444;
                font-size: 16px;
                margin-bottom: 10px;
                display: flex;
                align-items: center;
                justify-content: center;
                gap: 10px;
            }
            .warning-box p {
                color: #fca5a5;
                font-size: 14px;
                line-height: 1.6;
            }
            .warning-box code {
                background: rgba(0,0,0,0.3);
                padding: 3px 8px;
                border-radius: 5px;
                font-family: monospace;
            }
            .checklist {
                background: rgba(30, 41, 59, 0.8);
                border: 1px solid rgba(255,255,255,0.1);
                border-radius: 16px;
                padding: 25px;
                margin: 30px auto;
                max-width: 400px;
                text-align: left;
            }
            .checklist h5 {
                color: #fff;
                margin-bottom: 15px;
                font-size: 14px;
                text-transform: uppercase;
                letter-spacing: 1px;
            }
            .checklist-item {
                display: flex;
                align-items: center;
                gap: 12px;
                padding: 10px 0;
                color: #94a3b8;
                font-size: 14px;
                border-bottom: 1px solid rgba(255,255,255,0.05);
            }
            .checklist-item:last-child { border: none; }
            .checklist-item i { color: #10b981; }
        </style>
    </head>
    <body>
        <div class="confetti">
            <?php for($i = 0; $i < 50; $i++): ?>
            <div class="confetti-piece" style="left: <?php echo rand(0, 100); ?>%; animation-delay: <?php echo rand(0, 30) / 10; ?>s; background: <?php echo ['#6366f1', '#8b5cf6', '#10b981', '#f59e0b', '#ef4444'][rand(0, 4)]; ?>;"></div>
            <?php endfor; ?>
        </div>
        
        <div class="success-container">
            <div class="success-icon">
                <i class="fas fa-check"></i>
            </div>
            
            <h1>🎉 Tebrikler!</h1>
            <p class="subtitle">SMM Panel kurulumu başarıyla tamamlandı.<br>Sisteminiz kullanıma hazır!</p>
            
            <div class="buttons">
                <a href="admin/" class="btn btn-primary">
                    <i class="fas fa-cog"></i> Admin Paneli
                </a>
                <a href="./" class="btn btn-secondary">
                    <i class="fas fa-globe"></i> Siteyi Görüntüle
                </a>
            </div>
            
            <div class="checklist">
                <h5><i class="fas fa-clipboard-check"></i> Sonraki Adımlar</h5>
                <div class="checklist-item"><i class="fas fa-check-circle"></i> Admin panelden site ayarlarını yapılandırın</div>
                <div class="checklist-item"><i class="fas fa-check-circle"></i> Ödeme yöntemlerini aktifleştirin</div>
                <div class="checklist-item"><i class="fas fa-check-circle"></i> Kategoriler ve paketler ekleyin</div>
                <div class="checklist-item"><i class="fas fa-check-circle"></i> API sağlayıcılarını bağlayın</div>
                <div class="checklist-item"><i class="fas fa-check-circle"></i> Cron görevlerini ayarlayın</div>
            </div>
            
            <div class="warning-box">
                <h4><i class="fas fa-exclamation-triangle"></i> Güvenlik Uyarısı</h4>
                <p>Kurulum tamamlandıktan sonra <code>install.php</code> dosyasını sunucunuzdan <strong>mutlaka silin!</strong> Bu dosya silinmezse güvenlik açığı oluşturabilir.</p>
            </div>
        </div>
    </body>
    </html>
    <?php
}

// Mevcut admin bilgilerini al
$existingAdmin = ($step === 4) ? getExistingAdmin() : null;
$systemChecks = ($step === 3) ? checkSystem() : [];
$cronInfo = ($step === 5) ? getCronInfo() : [];

// Site URL otomatik algılama
$autoSiteUrl = '';
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') {
    $autoSiteUrl = 'https://';
} else {
    $autoSiteUrl = 'http://';
}
$autoSiteUrl .= $_SERVER['HTTP_HOST'];
$autoSiteUrl = rtrim($autoSiteUrl, '/');

?>
<!DOCTYPE html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SMM Panel Kurulumu - Adım <?php echo $step; ?>/5</title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Fira+Code:wght@400;500&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        
        body { 
            font-family: 'Inter', sans-serif; 
            background: linear-gradient(135deg, #0f172a 0%, #1e1b4b 50%, #0f172a 100%);
            background-attachment: fixed;
            min-height: 100vh; 
            color: #f8fafc;
            padding: 40px 20px;
        }
        
        .container { max-width: 750px; margin: 0 auto; }
        
        /* Header */
        .install-header { text-align: center; margin-bottom: 40px; }
        
        .logo-box {
            width: 90px;
            height: 90px;
            background: linear-gradient(135deg, #6366f1, #8b5cf6);
            border-radius: 24px;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 auto 25px;
            font-size: 42px;
            color: #fff;
            box-shadow: 0 25px 50px rgba(99, 102, 241, 0.4);
            position: relative;
        }
        
        .logo-box::after {
            content: '';
            position: absolute;
            inset: -3px;
            background: linear-gradient(135deg, #6366f1, #8b5cf6);
            border-radius: 27px;
            z-index: -1;
            opacity: 0.3;
            filter: blur(15px);
        }
        
        .install-header h1 {
            font-size: 32px;
            font-weight: 800;
            margin-bottom: 10px;
            background: linear-gradient(135deg, #fff, #94a3b8);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        
        .install-header p {
            color: #64748b;
            font-size: 16px;
        }
        
        /* Progress Steps */
        .progress-steps {
            display: flex;
            justify-content: center;
            align-items: center;
            gap: 0;
            margin-bottom: 40px;
            padding: 0 20px;
        }
        
        .step-item {
            display: flex;
            flex-direction: column;
            align-items: center;
            position: relative;
            z-index: 2;
        }
        
        .step-circle {
            width: 50px;
            height: 50px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: 700;
            font-size: 16px;
            transition: all 0.3s;
            border: 3px solid transparent;
        }
        
        .step-item.pending .step-circle {
            background: rgba(100, 116, 139, 0.2);
            border-color: rgba(100, 116, 139, 0.3);
            color: #64748b;
        }
        
        .step-item.active .step-circle {
            background: linear-gradient(135deg, #6366f1, #8b5cf6);
            border-color: rgba(99, 102, 241, 0.5);
            color: #fff;
            box-shadow: 0 0 0 6px rgba(99, 102, 241, 0.2), 0 10px 30px rgba(99, 102, 241, 0.4);
            animation: pulse-step 2s ease infinite;
        }
        
        @keyframes pulse-step {
            0%, 100% { box-shadow: 0 0 0 6px rgba(99, 102, 241, 0.2), 0 10px 30px rgba(99, 102, 241, 0.4); }
            50% { box-shadow: 0 0 0 10px rgba(99, 102, 241, 0.1), 0 10px 30px rgba(99, 102, 241, 0.4); }
        }
        
        .step-item.done .step-circle {
            background: linear-gradient(135deg, #10b981, #059669);
            border-color: rgba(16, 185, 129, 0.5);
            color: #fff;
        }
        
        .step-label {
            margin-top: 10px;
            font-size: 12px;
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            color: #64748b;
        }
        
        .step-item.active .step-label { color: #8b5cf6; }
        .step-item.done .step-label { color: #10b981; }
        
        .step-line {
            width: 60px;
            height: 3px;
            background: rgba(100, 116, 139, 0.2);
            margin: 0 -5px;
            margin-bottom: 30px;
            border-radius: 2px;
            position: relative;
            z-index: 1;
        }
        
        .step-line.done {
            background: linear-gradient(90deg, #10b981, #10b981);
        }
        
        .step-line.half {
            background: linear-gradient(90deg, #10b981 50%, rgba(100, 116, 139, 0.2) 50%);
        }
        
        /* Card */
        .install-card {
            background: rgba(30, 41, 59, 0.9);
            backdrop-filter: blur(20px);
            border: 1px solid rgba(255,255,255,0.1);
            border-radius: 28px;
            overflow: hidden;
            box-shadow: 0 25px 80px rgba(0,0,0,0.4);
        }
        
        .card-header {
            padding: 30px 35px;
            background: linear-gradient(135deg, rgba(99, 102, 241, 0.1), rgba(139, 92, 246, 0.05));
            border-bottom: 1px solid rgba(255,255,255,0.05);
        }
        
        .card-header h2 {
            font-size: 22px;
            font-weight: 700;
            display: flex;
            align-items: center;
            gap: 14px;
            margin-bottom: 8px;
        }
        
        .card-header h2 i {
            width: 45px;
            height: 45px;
            background: linear-gradient(135deg, #6366f1, #8b5cf6);
            border-radius: 12px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 20px;
        }
        
        .card-header p {
            color: #94a3b8;
            font-size: 14px;
            margin-left: 59px;
        }
        
        .card-body { padding: 35px; }
        
        /* Form */
        .form-group { margin-bottom: 24px; }
        
        .form-label {
            display: flex;
            align-items: center;
            gap: 8px;
            font-size: 13px;
            font-weight: 600;
            color: #e2e8f0;
            margin-bottom: 10px;
        }
        
        .form-label .required { color: #ef4444; }
        
        .form-label i {
            color: #8b5cf6;
            font-size: 14px;
        }
        
        .form-input {
            width: 100%;
            padding: 16px 20px;
            background: rgba(15, 23, 42, 0.8);
            border: 2px solid rgba(255,255,255,0.08);
            border-radius: 14px;
            color: #fff;
            font-size: 15px;
            font-family: inherit;
            transition: all 0.3s;
        }
        
        .form-input:focus {
            outline: none;
            border-color: #8b5cf6;
            box-shadow: 0 0 0 4px rgba(139, 92, 246, 0.15);
            background: rgba(15, 23, 42, 1);
        }
        
        .form-input::placeholder { color: #475569; }
        
        .form-hint {
            display: flex;
            align-items: center;
            gap: 6px;
            font-size: 12px;
            color: #64748b;
            margin-top: 8px;
        }
        
        .form-hint i { font-size: 12px; }
        
        .form-row {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 20px;
        }
        
        /* Buttons */
        .btn {
            padding: 18px 35px;
            border-radius: 14px;
            font-weight: 700;
            font-size: 16px;
            border: none;
            cursor: pointer;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            gap: 12px;
            transition: all 0.3s;
            width: 100%;
            position: relative;
            overflow: hidden;
        }
        
        .btn::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255,255,255,0.2), transparent);
            transition: left 0.5s;
        }
        
        .btn:hover::before { left: 100%; }
        
        .btn-primary {
            background: linear-gradient(135deg, #6366f1, #8b5cf6);
            color: #fff;
            box-shadow: 0 10px 40px rgba(99, 102, 241, 0.35);
        }
        
        .btn-primary:hover {
            transform: translateY(-3px);
            box-shadow: 0 15px 50px rgba(99, 102, 241, 0.45);
        }
        
        .btn-success {
            background: linear-gradient(135deg, #10b981, #059669);
            color: #fff;
            box-shadow: 0 10px 40px rgba(16, 185, 129, 0.35);
        }
        
        .btn-success:hover {
            transform: translateY(-3px);
            box-shadow: 0 15px 50px rgba(16, 185, 129, 0.45);
        }
        
        /* Alerts */
        .alert {
            padding: 18px 22px;
            border-radius: 14px;
            margin-bottom: 25px;
            display: flex;
            align-items: flex-start;
            gap: 14px;
            font-size: 14px;
            line-height: 1.6;
        }
        
        .alert i { font-size: 20px; margin-top: 2px; flex-shrink: 0; }
        
        .alert-error {
            background: linear-gradient(135deg, rgba(239, 68, 68, 0.15), rgba(239, 68, 68, 0.05));
            border: 1px solid rgba(239, 68, 68, 0.3);
            color: #fca5a5;
        }
        .alert-error i { color: #ef4444; }
        
        .alert-success {
            background: linear-gradient(135deg, rgba(16, 185, 129, 0.15), rgba(16, 185, 129, 0.05));
            border: 1px solid rgba(16, 185, 129, 0.3);
            color: #6ee7b7;
        }
        .alert-success i { color: #10b981; }
        
        .alert-info {
            background: linear-gradient(135deg, rgba(59, 130, 246, 0.15), rgba(59, 130, 246, 0.05));
            border: 1px solid rgba(59, 130, 246, 0.3);
            color: #93c5fd;
        }
        .alert-info i { color: #3b82f6; }
        
        .alert-warning {
            background: linear-gradient(135deg, rgba(245, 158, 11, 0.15), rgba(245, 158, 11, 0.05));
            border: 1px solid rgba(245, 158, 11, 0.3);
            color: #fcd34d;
        }
        .alert-warning i { color: #f59e0b; }
        
        /* Check List */
        .section-title {
            font-size: 13px;
            font-weight: 700;
            color: #8b5cf6;
            margin: 30px 0 15px;
            text-transform: uppercase;
            letter-spacing: 1px;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .section-title::after {
            content: '';
            flex: 1;
            height: 1px;
            background: linear-gradient(90deg, rgba(139, 92, 246, 0.3), transparent);
        }
        
        .check-list { list-style: none; }
        
        .check-item {
            display: flex;
            align-items: center;
            gap: 14px;
            padding: 16px 20px;
            background: rgba(15, 23, 42, 0.5);
            border: 1px solid rgba(255,255,255,0.05);
            border-radius: 12px;
            margin-bottom: 10px;
            transition: all 0.2s;
        }
        
        .check-item:hover {
            background: rgba(15, 23, 42, 0.8);
            border-color: rgba(255,255,255,0.1);
        }
        
        .check-icon {
            width: 32px;
            height: 32px;
            border-radius: 10px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 14px;
            flex-shrink: 0;
        }
        
        .check-icon.success { background: rgba(16, 185, 129, 0.2); color: #10b981; }
        .check-icon.error { background: rgba(239, 68, 68, 0.2); color: #ef4444; }
        .check-icon.warning { background: rgba(245, 158, 11, 0.2); color: #f59e0b; }
        
        .check-text { flex: 1; }
        .check-text .name { font-weight: 600; color: #e2e8f0; }
        .check-text .desc { font-size: 12px; color: #64748b; margin-top: 2px; }
        
        /* Cron Box */
        .cron-box {
            background: rgba(15, 23, 42, 0.6);
            border: 1px solid rgba(255,255,255,0.08);
            border-radius: 16px;
            padding: 24px;
            margin-bottom: 20px;
            transition: all 0.3s;
        }
        
        .cron-box:hover {
            border-color: rgba(139, 92, 246, 0.3);
        }
        
        .cron-header {
            display: flex;
            align-items: center;
            gap: 14px;
            margin-bottom: 15px;
        }
        
        .cron-icon {
            width: 46px;
            height: 46px;
            background: linear-gradient(135deg, #6366f1, #8b5cf6);
            border-radius: 12px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 20px;
            color: #fff;
        }
        
        .cron-info h4 {
            font-size: 16px;
            font-weight: 700;
            color: #fff;
            margin-bottom: 3px;
        }
        
        .cron-info p {
            font-size: 13px;
            color: #64748b;
        }
        
        .cron-badge {
            margin-left: auto;
            background: rgba(16, 185, 129, 0.15);
            border: 1px solid rgba(16, 185, 129, 0.3);
            color: #10b981;
            padding: 6px 12px;
            border-radius: 8px;
            font-size: 12px;
            font-weight: 600;
        }
        
        .cron-desc {
            font-size: 13px;
            color: #94a3b8;
            margin-bottom: 15px;
            padding-left: 60px;
        }
        
        .cron-command {
            background: #0f172a;
            border: 1px solid rgba(255,255,255,0.1);
            border-radius: 12px;
            padding: 16px 20px;
            font-family: 'Fira Code', monospace;
            font-size: 13px;
            color: #10b981;
            word-break: break-all;
            line-height: 1.6;
            position: relative;
        }
        
        .copy-btn {
            position: absolute;
            top: 10px;
            right: 10px;
            background: rgba(139, 92, 246, 0.2);
            border: 1px solid rgba(139, 92, 246, 0.3);
            color: #a78bfa;
            padding: 8px 14px;
            border-radius: 8px;
            font-size: 12px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.2s;
            display: flex;
            align-items: center;
            gap: 6px;
        }
        
        .copy-btn:hover {
            background: #8b5cf6;
            border-color: #8b5cf6;
            color: #fff;
        }
        
        /* Server Path Box */
        .server-path-box {
            background: linear-gradient(135deg, rgba(59, 130, 246, 0.1), rgba(59, 130, 246, 0.05));
            border: 1px solid rgba(59, 130, 246, 0.2);
            border-radius: 14px;
            padding: 20px;
            margin-bottom: 25px;
        }
        
        .server-path-box h5 {
            font-size: 13px;
            font-weight: 600;
            color: #93c5fd;
            margin-bottom: 10px;
            display: flex;
            align-items: center;
            gap: 8px;
        }
        
        .server-path-box code {
            display: block;
            background: rgba(15, 23, 42, 0.8);
            padding: 14px 18px;
            border-radius: 10px;
            font-family: 'Fira Code', monospace;
            font-size: 13px;
            color: #60a5fa;
            word-break: break-all;
        }
        
        /* Admin Edit Notice */
        .admin-notice {
            background: linear-gradient(135deg, rgba(245, 158, 11, 0.1), rgba(245, 158, 11, 0.05));
            border: 1px solid rgba(245, 158, 11, 0.3);
            border-radius: 14px;
            padding: 20px;
            margin-bottom: 25px;
            display: flex;
            align-items: center;
            gap: 15px;
        }
        
        .admin-notice-icon {
            width: 50px;
            height: 50px;
            background: rgba(245, 158, 11, 0.2);
            border-radius: 12px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 24px;
            color: #f59e0b;
            flex-shrink: 0;
        }
        
        .admin-notice-text h5 {
            font-size: 15px;
            font-weight: 700;
            color: #fcd34d;
            margin-bottom: 4px;
        }
        
        .admin-notice-text p {
            font-size: 13px;
            color: #fde68a;
        }
        
        /* Responsive */
        @media (max-width: 650px) {
            .form-row { grid-template-columns: 1fr; }
            .step-line { width: 30px; }
            .step-circle { width: 40px; height: 40px; font-size: 14px; }
            .step-label { font-size: 10px; }
            .card-header, .card-body { padding: 25px; }
            .cron-desc { padding-left: 0; }
            .cron-header { flex-wrap: wrap; }
            .cron-badge { margin-left: 0; margin-top: 10px; }
        }
        
        /* Footer */
        .install-footer {
            text-align: center;
            margin-top: 30px;
            color: #475569;
            font-size: 13px;
        }
        
        .install-footer a {
            color: #8b5cf6;
            text-decoration: none;
        }
    </style>
</head>
<body>

<div class="container">
    <!-- Header -->
    <div class="install-header">
        <div class="logo-box"><i class="fas fa-bolt"></i></div>
        <h1>SMM Panel Kurulumu</h1>
        <p>Profesyonel sosyal medya satış sisteminizi kurun</p>
    </div>
    
    <!-- Progress Steps -->
    <div class="progress-steps">
        <div class="step-item <?php echo $step == 1 ? 'active' : ($step > 1 ? 'done' : 'pending'); ?>">
            <div class="step-circle"><?php echo $step > 1 ? '<i class="fas fa-check"></i>' : '1'; ?></div>
            <span class="step-label">Veritabanı</span>
        </div>
        <div class="step-line <?php echo $step > 1 ? 'done' : ($step == 1 ? '' : ''); ?>"></div>
        
        <div class="step-item <?php echo $step == 2 ? 'active' : ($step > 2 ? 'done' : 'pending'); ?>">
            <div class="step-circle"><?php echo $step > 2 ? '<i class="fas fa-check"></i>' : '2'; ?></div>
            <span class="step-label">SQL</span>
        </div>
        <div class="step-line <?php echo $step > 2 ? 'done' : ''; ?>"></div>
        
        <div class="step-item <?php echo $step == 3 ? 'active' : ($step > 3 ? 'done' : 'pending'); ?>">
            <div class="step-circle"><?php echo $step > 3 ? '<i class="fas fa-check"></i>' : '3'; ?></div>
            <span class="step-label">Kontrol</span>
        </div>
        <div class="step-line <?php echo $step > 3 ? 'done' : ''; ?>"></div>
        
        <div class="step-item <?php echo $step == 4 ? 'active' : ($step > 4 ? 'done' : 'pending'); ?>">
            <div class="step-circle"><?php echo $step > 4 ? '<i class="fas fa-check"></i>' : '4'; ?></div>
            <span class="step-label">Admin</span>
        </div>
        <div class="step-line <?php echo $step > 4 ? 'done' : ''; ?>"></div>
        
        <div class="step-item <?php echo $step == 5 ? 'active' : 'pending'; ?>">
            <div class="step-circle">5</div>
            <span class="step-label">Tamamla</span>
        </div>
    </div>
    
    <!-- Card -->
    <div class="install-card">
        
        <?php if ($step === 1): ?>
        <!-- ADIM 1 -->
        <div class="card-header">
            <h2><i class="fas fa-database"></i> Veritabanı & Site Ayarları</h2>
            <p>MySQL veritabanı bilgilerinizi ve site URL'nizi girin</p>
        </div>
        <div class="card-body">
            <?php if ($error): ?>
            <div class="alert alert-error"><i class="fas fa-exclamation-circle"></i><div><?php echo $error; ?></div></div>
            <?php endif; ?>
            
            <form method="POST">
                <div class="form-group">
                    <label class="form-label"><i class="fas fa-globe"></i> Site URL <span class="required">*</span></label>
                    <input type="url" name="site_url" class="form-input" value="<?php echo htmlspecialchars($autoSiteUrl); ?>" placeholder="https://siteadi.com" required>
                    <p class="form-hint"><i class="fas fa-info-circle"></i> Sitenizin tam URL'si (sonunda / olmadan)</p>
                </div>
                
                <div class="form-group">
                    <label class="form-label"><i class="fas fa-server"></i> Veritabanı Sunucusu</label>
                    <input type="text" name="db_host" class="form-input" value="localhost" placeholder="localhost">
                    <p class="form-hint"><i class="fas fa-info-circle"></i> Genellikle "localhost" olarak kalır</p>
                </div>
                
                <div class="form-group">
                    <label class="form-label"><i class="fas fa-database"></i> Veritabanı Adı <span class="required">*</span></label>
                    <input type="text" name="db_name" class="form-input" placeholder="veritabani_adi" required>
                </div>
                
                <div class="form-row">
                    <div class="form-group">
                        <label class="form-label"><i class="fas fa-user"></i> Kullanıcı Adı <span class="required">*</span></label>
                        <input type="text" name="db_user" class="form-input" placeholder="kullanici" required>
                    </div>
                    <div class="form-group">
                        <label class="form-label"><i class="fas fa-key"></i> Şifre</label>
                        <input type="password" name="db_pass" class="form-input" placeholder="••••••••">
                    </div>
                </div>
                
                <button type="submit" name="db_test" class="btn btn-primary">
                    <i class="fas fa-plug"></i> Bağlantıyı Test Et ve Devam Et
                </button>
            </form>
        </div>
        
        <?php elseif ($step === 2): ?>
        <!-- ADIM 2 -->
        <div class="card-header">
            <h2><i class="fas fa-file-import"></i> Veritabanı Kurulumu</h2>
            <p>Tablo ve verilerin yüklenmesi</p>
        </div>
        <div class="card-body">
            <?php if ($error): ?>
            <div class="alert alert-error"><i class="fas fa-exclamation-circle"></i><div><?php echo $error; ?></div></div>
            <?php endif; ?>
            
            <?php if (file_exists($sqlFile)): ?>
            <div class="alert alert-success">
                <i class="fas fa-check-circle"></i>
                <div>
                    <strong>database.sql dosyası bulundu!</strong><br>
                    Dosya boyutu: <?php echo number_format(filesize($sqlFile) / 1024, 2); ?> KB
                </div>
            </div>
            
            <div class="alert alert-warning">
                <i class="fas fa-exclamation-triangle"></i>
                <div>
                    <strong>Dikkat!</strong> Bu işlem veritabanına tüm tabloları oluşturacak ve varsayılan verileri yükleyecektir. Mevcut tablolar güncellenecektir.
                </div>
            </div>
            
            <form method="POST">
                <button type="submit" name="import_sql" class="btn btn-primary">
                    <i class="fas fa-upload"></i> Veritabanını Kur
                </button>
            </form>
            <?php else: ?>
            <div class="alert alert-error">
                <i class="fas fa-times-circle"></i>
                <div>
                    <strong>database.sql dosyası bulunamadı!</strong><br>
                    Ana dizine database.sql dosyasını yükleyin ve bu sayfayı yenileyin.
                </div>
            </div>
            <button type="button" class="btn btn-primary" onclick="location.reload()">
                <i class="fas fa-sync"></i> Sayfayı Yenile
            </button>
            <?php endif; ?>
        </div>
        
        <?php elseif ($step === 3): ?>
        <!-- ADIM 3 -->
        <div class="card-header">
            <h2><i class="fas fa-clipboard-check"></i> Sistem Kontrolü</h2>
            <p>Gerekli dosya, klasör ve bileşenler kontrol ediliyor</p>
        </div>
        <div class="card-body">
            
            <div class="section-title">PHP Gereksinimleri</div>
            <ul class="check-list">
                <li class="check-item">
                    <span class="check-icon <?php echo $systemChecks['php_version']['ok'] ? 'success' : 'error'; ?>">
                        <i class="fas fa-<?php echo $systemChecks['php_version']['ok'] ? 'check' : 'times'; ?>"></i>
                    </span>
                    <span class="check-text">
                        <span class="name">PHP Sürümü: <?php echo $systemChecks['php_version']['current']; ?></span>
                        <span class="desc">Gerekli: PHP <?php echo $systemChecks['php_version']['required']; ?> veya üzeri</span>
                    </span>
                </li>
                <?php foreach ($systemChecks['extensions'] as $ext => $loaded): ?>
                <li class="check-item">
                    <span class="check-icon <?php echo $loaded ? 'success' : 'error'; ?>">
                        <i class="fas fa-<?php echo $loaded ? 'check' : 'times'; ?>"></i>
                    </span>
                    <span class="check-text">
                        <span class="name"><?php echo strtoupper($ext); ?></span>
                        <span class="desc"><?php echo $loaded ? 'Yüklü' : 'Yüklü değil - Gerekli!'; ?></span>
                    </span>
                </li>
                <?php endforeach; ?>
            </ul>
            
            <div class="section-title">Klasörler</div>
            <ul class="check-list">
                <?php foreach ($systemChecks['folders'] as $folder => $status): ?>
                <li class="check-item">
                    <span class="check-icon <?php 
                        if (!$status['exists']) echo 'error';
                        elseif (!$status['writable']) echo 'warning';
                        else echo 'success';
                    ?>">
                        <i class="fas fa-<?php 
                            if (!$status['exists']) echo 'times';
                            elseif (!$status['writable']) echo 'exclamation';
                            else echo 'check';
                        ?>"></i>
                    </span>
                    <span class="check-text">
                        <span class="name">/<?php echo $folder; ?>/</span>
                        <span class="desc">
                            <?php 
                            if (!$status['exists']) {
                                echo $status['required'] ? 'Klasör bulunamadı! (Zorunlu)' : 'Klasör bulunamadı (Opsiyonel)';
                            } elseif (!$status['writable']) {
                                echo 'Yazma izni yok - chmod 755 uygulayın';
                            } else {
                                echo 'Tamam';
                            }
                            ?>
                        </span>
                    </span>
                </li>
                <?php endforeach; ?>
            </ul>
            
            <div class="section-title">Dosyalar</div>
            <ul class="check-list">
                <?php foreach ($systemChecks['files'] as $file => $status): ?>
                <li class="check-item">
                    <span class="check-icon <?php 
                        if (!$status['exists']) echo 'error';
                        elseif ($status['needs_writable'] && !$status['writable']) echo 'warning';
                        else echo 'success';
                    ?>">
                        <i class="fas fa-<?php 
                            if (!$status['exists']) echo 'times';
                            elseif ($status['needs_writable'] && !$status['writable']) echo 'exclamation';
                            else echo 'check';
                        ?>"></i>
                    </span>
                    <span class="check-text">
                        <span class="name"><?php echo $file; ?></span>
                        <span class="desc">
                            <?php 
                            if (!$status['exists']) {
                                echo 'Dosya bulunamadı!';
                            } elseif ($status['needs_writable'] && !$status['writable']) {
                                echo 'Yazma izni yok - chmod 666 uygulayın';
                            } else {
                                echo 'Tamam';
                            }
                            ?>
                        </span>
                    </span>
                </li>
                <?php endforeach; ?>
            </ul>
            
            <div class="section-title">Veritabanı Tabloları</div>
            <ul class="check-list">
                <?php 
                $tableCount = count($systemChecks['tables']);
                $tableOk = 0;
                foreach ($systemChecks['tables'] as $exists) { if ($exists) $tableOk++; }
                ?>
                <?php foreach ($systemChecks['tables'] as $table => $exists): ?>
                <li class="check-item">
                    <span class="check-icon <?php echo $exists ? 'success' : 'error'; ?>">
                        <i class="fas fa-<?php echo $exists ? 'check' : 'times'; ?>"></i>
                    </span>
                    <span class="check-text">
                        <span class="name"><?php echo $table; ?></span>
                        <span class="desc"><?php echo $exists ? 'Tablo mevcut' : 'Tablo bulunamadı!'; ?></span>
                    </span>
                </li>
                <?php endforeach; ?>
            </ul>
            
            <?php
            // Kritik kontroller
            $criticalErrors = 0;
            if (!$systemChecks['php_version']['ok']) $criticalErrors++;
            foreach ($systemChecks['extensions'] as $loaded) { if (!$loaded) $criticalErrors++; }
            foreach ($systemChecks['folders'] as $name => $status) { 
                if ($status['required'] && !$status['exists']) $criticalErrors++; 
            }
            foreach ($systemChecks['tables'] as $exists) { if (!$exists) $criticalErrors++; }
            ?>
            
            <?php if ($criticalErrors === 0): ?>
            <div class="alert alert-success" style="margin-top: 25px;">
                <i class="fas fa-check-circle"></i>
                <div><strong>Tüm kontroller başarılı!</strong> Kuruluma devam edebilirsiniz.</div>
            </div>
            <?php else: ?>
            <div class="alert alert-warning" style="margin-top: 25px;">
                <i class="fas fa-exclamation-triangle"></i>
                <div>
                    <strong><?php echo $criticalErrors; ?> sorun tespit edildi.</strong><br>
                    Sorunları düzeltmeniz önerilir, ancak yine de devam edebilirsiniz.
                </div>
            </div>
            <?php endif; ?>
            
            <form method="POST" style="margin-top: 20px;">
                <button type="submit" name="continue_step3" class="btn <?php echo $criticalErrors === 0 ? 'btn-success' : 'btn-primary'; ?>">
                    <i class="fas fa-arrow-right"></i> Devam Et
                </button>
            </form>
        </div>
        
        <?php elseif ($step === 4): ?>
        <!-- ADIM 4 -->
        <div class="card-header">
            <h2><i class="fas fa-user-shield"></i> Admin Hesabı</h2>
            <p><?php echo $existingAdmin ? 'Mevcut admin hesabını düzenleyin' : 'Yönetici hesabınızı oluşturun'; ?></p>
        </div>
        <div class="card-body">
            <?php if ($error): ?>
            <div class="alert alert-error"><i class="fas fa-exclamation-circle"></i><div><?php echo $error; ?></div></div>
            <?php endif; ?>
            
            <?php if ($existingAdmin): ?>
            <div class="admin-notice">
                <div class="admin-notice-icon"><i class="fas fa-user-edit"></i></div>
                <div class="admin-notice-text">
                    <h5>Mevcut Admin Hesabı Bulundu</h5>
                    <p>Aşağıdaki bilgileri düzenleyebilir veya olduğu gibi bırakabilirsiniz. Şifre alanını boş bırakırsanız mevcut şifre korunur.</p>
                </div>
            </div>
            <?php endif; ?>
            
            <form method="POST">
                <div class="form-row">
                    <div class="form-group">
                        <label class="form-label"><i class="fas fa-user"></i> Kullanıcı Adı <span class="required">*</span></label>
                        <input type="text" name="admin_username" class="form-input" 
                               value="<?php echo htmlspecialchars($existingAdmin['username'] ?? ''); ?>" 
                               placeholder="admin" required>
                    </div>
                    <div class="form-group">
                        <label class="form-label"><i class="fas fa-id-card"></i> Ad Soyad</label>
                        <input type="text" name="admin_name" class="form-input" 
                               value="<?php echo htmlspecialchars($existingAdmin['full_name'] ?? ''); ?>" 
                               placeholder="Admin User">
                    </div>
                </div>
                
                <div class="form-row">
                    <div class="form-group">
                        <label class="form-label"><i class="fas fa-envelope"></i> E-posta Adresi <span class="required">*</span></label>
                        <input type="email" name="admin_email" class="form-input" 
                               value="<?php echo htmlspecialchars($existingAdmin['email'] ?? ''); ?>" 
                               placeholder="admin@site.com" required>
                    </div>
                    <div class="form-group">
                        <label class="form-label"><i class="fas fa-phone"></i> Telefon</label>
                        <input type="text" name="admin_phone" class="form-input" 
                               value="<?php echo htmlspecialchars($existingAdmin['phone'] ?? ''); ?>" 
                               placeholder="+90 555 555 55 55">
                    </div>
                </div>
                
                <div class="form-row">
                    <div class="form-group">
                        <label class="form-label">
                            <i class="fas fa-lock"></i> Şifre 
                            <?php if (!$existingAdmin): ?><span class="required">*</span><?php endif; ?>
                        </label>
                        <input type="password" name="admin_password" class="form-input" 
                               placeholder="••••••••" <?php echo !$existingAdmin ? 'required' : ''; ?>>
                        <?php if ($existingAdmin): ?>
                        <p class="form-hint"><i class="fas fa-info-circle"></i> Boş bırakırsanız mevcut şifre korunur</p>
                        <?php else: ?>
                        <p class="form-hint"><i class="fas fa-info-circle"></i> En az 6 karakter</p>
                        <?php endif; ?>
                    </div>
                    <div class="form-group">
                        <label class="form-label"><i class="fas fa-lock"></i> Şifre Tekrar</label>
                        <input type="password" name="admin_password_confirm" class="form-input" placeholder="••••••••">
                    </div>
                </div>
                
                <button type="submit" name="save_admin" class="btn btn-primary">
                    <i class="fas fa-<?php echo $existingAdmin ? 'save' : 'user-plus'; ?>"></i> 
                    <?php echo $existingAdmin ? 'Değişiklikleri Kaydet' : 'Admin Hesabı Oluştur'; ?>
                </button>
            </form>
        </div>
        
        <?php elseif ($step === 5): ?>
        <!-- ADIM 5 -->
        <div class="card-header">
            <h2><i class="fas fa-clock"></i> Cron Görevleri & Tamamlama</h2>
            <p>Otomatik işlemler için cron ayarları</p>
        </div>
        <div class="card-body">
            
            <div class="server-path-box">
                <h5><i class="fas fa-folder-open"></i> Sunucu Yolu (Document Root)</h5>
                <code><?php echo htmlspecialchars($cronInfo['server_path']); ?></code>
            </div>
            
            <div class="server-path-box" style="background: linear-gradient(135deg, rgba(16, 185, 129, 0.1), rgba(16, 185, 129, 0.05)); border-color: rgba(16, 185, 129, 0.2);">
                <h5 style="color: #6ee7b7;"><i class="fas fa-terminal"></i> PHP Yolu</h5>
                <code style="color: #10b981;"><?php echo htmlspecialchars($cronInfo['php_path']); ?></code>
            </div>
            
            <div class="section-title">Cron Görevleri</div>
            
            <?php foreach ($cronInfo['crons'] as $index => $cron): ?>
            <div class="cron-box">
                <div class="cron-header">
                    <div class="cron-icon"><i class="fas fa-<?php echo $index === 0 ? 'sync' : 'chart-line'; ?>"></i></div>
                    <div class="cron-info">
                        <h4><?php echo htmlspecialchars($cron['name']); ?></h4>
                        <p><?php echo htmlspecialchars($cron['file']); ?></p>
                    </div>
                    <div class="cron-badge"><i class="fas fa-clock"></i> <?php echo htmlspecialchars($cron['interval']); ?></div>
                </div>
                <p class="cron-desc"><?php echo htmlspecialchars($cron['description']); ?></p>
                <div class="cron-command">
                    <code><?php echo htmlspecialchars($cron['command']); ?></code>
                    <button type="button" class="copy-btn" onclick="copyCommand(this, '<?php echo htmlspecialchars(addslashes($cron['command'])); ?>')">
                        <i class="fas fa-copy"></i> Kopyala
                    </button>
                </div>
            </div>
            <?php endforeach; ?>
            
            <div class="alert alert-info" style="margin-top: 25px;">
                <i class="fas fa-info-circle"></i>
                <div>
                    <strong>Cron Nasıl Eklenir?</strong><br>
                    cPanel: Cron Jobs → Add New Cron Job → Komutu yapıştırın<br>
                    DirectAdmin: Cronjobs → Create Cron Job → Komutu yapıştırın<br>
                    Plesk: Scheduled Tasks → Add Task → Komutu yapıştırın
                </div>
            </div>
            
            <div class="alert alert-success" style="margin-top: 20px;">
                <i class="fas fa-rocket"></i>
                <div>
                    <strong>Kurulum tamamlanmaya hazır!</strong><br>
                    Cron görevlerini daha sonra da ekleyebilirsiniz. Şimdi kurulumu tamamlayın.
                </div>
            </div>
            
            <form method="POST" style="margin-top: 25px;">
                <button type="submit" name="finish_install" class="btn btn-success">
                    <i class="fas fa-check-circle"></i> Kurulumu Tamamla
                </button>
            </form>
        </div>
        <?php endif; ?>
        
    </div>
    
    <!-- Footer -->
    <div class="install-footer">
        <p>SMM Panel v1.0 &copy; <?php echo date('Y'); ?> | Powered by <a href="#">Paketix</a></p>
    </div>
</div>

<script>
function copyCommand(btn, text) {
    if (navigator.clipboard && navigator.clipboard.writeText) {
        navigator.clipboard.writeText(text).then(function() {
            showCopySuccess(btn);
        }).catch(function() {
            fallbackCopy(btn, text);
        });
    } else {
        fallbackCopy(btn, text);
    }
}

function fallbackCopy(btn, text) {
    var textarea = document.createElement('textarea');
    textarea.value = text;
    textarea.style.position = 'fixed';
    textarea.style.opacity = '0';
    document.body.appendChild(textarea);
    textarea.select();
    try {
        document.execCommand('copy');
        showCopySuccess(btn);
    } catch (err) {
        alert('Kopyalama başarısız. Manuel olarak kopyalayın.');
    }
    document.body.removeChild(textarea);
}

function showCopySuccess(btn) {
    var originalHtml = btn.innerHTML;
    btn.innerHTML = '<i class="fas fa-check"></i> Kopyalandı!';
    btn.style.background = '#10b981';
    btn.style.borderColor = '#10b981';
    btn.style.color = '#fff';
    
    setTimeout(function() {
        btn.innerHTML = originalHtml;
        btn.style.background = '';
        btn.style.borderColor = '';
        btn.style.color = '';
    }, 2000);
}

// Form input animasyonları
document.querySelectorAll('.form-input').forEach(function(input) {
    input.addEventListener('focus', function() {
        this.parentElement.classList.add('focused');
    });
    input.addEventListener('blur', function() {
        this.parentElement.classList.remove('focused');
    });
});
</script>

</body>
</html>