<?php
// Kurulum kontrolü
if (!defined('INSTALL_LOCK_FILE')) {
    require_once __DIR__ . '/config/config.php';
}

// Kurulum tamamlanmamış mı kontrol et
if (!file_exists(INSTALL_LOCK_FILE)) {
    header('Location: install.php');
    exit;
}

require_once __DIR__ . '/config/database.php'; // Veritabanı ve temel ayarlar
require_once __DIR__ . '/includes/header.php'; // Header'ı dahil et

global $db, $translations, $settings;

// Kullanıcı giriş yapmış mı kontrol et
$is_logged_in = is_logged_in();
$user_id = $is_logged_in ? $_SESSION['user_id'] : null;

$message = null;
$message_type = 'info';

// Eğer kullanıcı giriş yapmışsa dashboard içeriğini göster
if ($is_logged_in) {
    // Kullanıcı bilgilerini çekiyoruz
    $package_info = null;
    $limits = [
        'daily_total' => 0, 'daily_used' => 0, 'daily_remaining' => 0,
        'monthly_total' => 0, 'monthly_used' => 0, 'monthly_remaining' => 0
    ];
    $total_downloads = 0;
    $downloads = [];
    $chart_data = [];
    $page = 1;
    $total_pages = 1;
    $total_download_records = 0;
    $limit = 5;
    
    if ($db) {
        try {
            // Paket bilgileri
            $stmt_pkg = $db->prepare("SELECT p.id as package_id, p.name, p.daily_limit, p.monthly_limit, u.package_expires_at
                                FROM users u
                                LEFT JOIN packages p ON u.package_id = p.id
                                WHERE u.id = ? AND (u.package_expires_at IS NULL OR u.package_expires_at > NOW())");
            $stmt_pkg->execute([$user_id]);
            $package_info = $stmt_pkg->fetch();

            if ($package_info) {
                // Günlük ve aylık limitler
                $today = date('Y-m-d');
                $stmt_daily = $db->prepare("SELECT COUNT(*) FROM downloads WHERE user_id = ? AND DATE(created_at) = ?");
                $stmt_daily->execute([$user_id, $today]);
                $limits['daily_used'] = (int)$stmt_daily->fetchColumn();

                $firstDayOfMonth = date('Y-m-01');
                $lastDayOfMonth = date('Y-m-t');
                $stmt_monthly = $db->prepare("SELECT COUNT(*) FROM downloads WHERE user_id = ? AND created_at BETWEEN ? AND ?");
                $stmt_monthly->execute([$user_id, $firstDayOfMonth . ' 00:00:00', $lastDayOfMonth . ' 23:59:59']);
                $limits['monthly_used'] = (int)$stmt_monthly->fetchColumn();

                $limits['daily_total'] = (int)$package_info['daily_limit'];
                $limits['monthly_total'] = (int)$package_info['monthly_limit'];
                $limits['daily_remaining'] = max(0, $limits['daily_total'] - $limits['daily_used']);
                $limits['monthly_remaining'] = max(0, $limits['monthly_total'] - $limits['monthly_used']);
            }
            
            // Toplam indirmeler
            $stmt_total = $db->prepare("SELECT COUNT(*) FROM downloads WHERE user_id = ?");
            $stmt_total->execute([$user_id]);
            $total_downloads = (int)$stmt_total->fetchColumn();
            
            // Grafik verileri
            $labels = [];
            $downloads_data = [];
            for ($i = 6; $i >= 0; $i--) {
                $day = date('Y-m-d', strtotime("-$i days"));
                $labels[] = date('d.m', strtotime($day));
                
                $stmt_day = $db->prepare("SELECT COUNT(*) FROM downloads WHERE user_id = ? AND DATE(created_at) = ?");
                $stmt_day->execute([$user_id, $day]);
                $downloads_data[] = (int)$stmt_day->fetchColumn();
            }
            $chart_data = ['labels' => $labels, 'downloads' => $downloads_data];
            
            // Son indirmeler
            $page = filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, ['options' => ['default' => 1, 'min_range' => 1]]);
            $offset = ($page - 1) * $limit;
            
            $count_stmt = $db->prepare("SELECT COUNT(*) FROM downloads WHERE user_id = ?");
            $count_stmt->execute([$user_id]);
            $total_download_records = (int)$count_stmt->fetchColumn();
            
            if ($total_download_records > 0) {
                $stmt_downloads = $db->prepare("SELECT platform, url, created_at FROM downloads 
                                              WHERE user_id = ? 
                                              ORDER BY created_at DESC 
                                              LIMIT ? OFFSET ?");
                $stmt_downloads->bindValue(1, $user_id, PDO::PARAM_INT);
                $stmt_downloads->bindValue(2, $limit, PDO::PARAM_INT);
                $stmt_downloads->bindValue(3, $offset, PDO::PARAM_INT);
                $stmt_downloads->execute();
                $downloads = $stmt_downloads->fetchAll(PDO::FETCH_ASSOC);
            }
            
            $total_pages = ceil($total_download_records / max(1, $limit));
            
        } catch (PDOException $e) {
            error_log("Database Error: " . $e->getMessage());
            $message = __('db_error', $translations);
            $message_type = 'error';
        }
    } else {
        $message = __('db_error', $translations);
        $message_type = 'error';
    }
    
    // Şimdi kullanıcı dashboard'unu göster
    ?>
    <!-- Kullanıcı Dashboard -->
    <div class="bg-white dark:bg-gray-800 shadow-lg rounded-lg overflow-hidden h-full flex flex-col">
        <!-- Başlık & Hata Mesajı -->
        <div class="bg-gray-50 dark:bg-gray-700 px-6 py-4 border-b border-gray-200 dark:border-gray-600">
            <h1 class="text-xl sm:text-2xl font-semibold text-gray-800 dark:text-white"><?php echo __('dashboard', $translations); ?></h1>
            
            <?php if ($message): ?>
                <div class="mt-3 <?php echo $message_type === 'success' ? 'bg-green-100 border-green-400 text-green-700' : ($message_type === 'error' ? 'bg-red-100 border-red-400 text-red-700' : 'bg-blue-100 border-blue-400 text-blue-700'); ?> border px-4 py-3 rounded relative" role="alert">
                    <span class="block sm:inline"><?php echo e($message); ?></span>
                </div>
            <?php endif; ?>
        </div>

        <!-- Ana İçerik Bölümü -->
        <div class="flex-1 overflow-auto p-4 sm:p-6">
            <!-- İstatistik Kartları -->
            <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4 mb-8">
                <!-- Günlük Limit Kartı -->
                <div class="bg-white dark:bg-gray-700 shadow rounded-lg p-4 border border-gray-100 dark:border-gray-600">
                    <div class="flex items-center">
                        <div class="flex-shrink-0 bg-primary-500 rounded-md p-3">
                            <i class="fas fa-calendar-day fa-lg text-white"></i>
                        </div>
                        <div class="ml-4 flex-1">
                            <div class="text-sm font-medium text-gray-500 dark:text-gray-300"><?php echo __('remaining_daily', $translations); ?></div>
                            <div class="text-xl font-semibold text-gray-900 dark:text-white"><?php echo e($limits['daily_remaining']); ?> / <?php echo e($limits['daily_total']); ?></div>
                        </div>
                    </div>
                </div>
                
                <!-- Aylık Limit Kartı -->
                <div class="bg-white dark:bg-gray-700 shadow rounded-lg p-4 border border-gray-100 dark:border-gray-600">
                    <div class="flex items-center">
                        <div class="flex-shrink-0 bg-green-500 rounded-md p-3">
                            <i class="fas fa-calendar-alt fa-lg text-white"></i>
                        </div>
                        <div class="ml-4 flex-1">
                            <div class="text-sm font-medium text-gray-500 dark:text-gray-300"><?php echo __('remaining_monthly', $translations); ?></div>
                            <div class="text-xl font-semibold text-gray-900 dark:text-white"><?php echo e($limits['monthly_remaining']); ?> / <?php echo e($limits['monthly_total']); ?></div>
                        </div>
                    </div>
                </div>
                
                <!-- Toplam İndirme Kartı -->
                <div class="bg-white dark:bg-gray-700 shadow rounded-lg p-4 border border-gray-100 dark:border-gray-600">
                    <div class="flex items-center">
                        <div class="flex-shrink-0 bg-blue-500 rounded-md p-3">
                            <i class="fas fa-download fa-lg text-white"></i>
                        </div>
                        <div class="ml-4 flex-1">
                            <div class="text-sm font-medium text-gray-500 dark:text-gray-300"><?php echo __('total_downloads', $translations); ?></div>
                            <div class="text-xl font-semibold text-gray-900 dark:text-white"><?php echo e($total_downloads); ?></div>
                        </div>
                    </div>
                </div>
                
                <!-- Aktif Paket Kartı -->
                <div class="bg-white dark:bg-gray-700 shadow rounded-lg p-4 border border-gray-100 dark:border-gray-600">
                    <div class="flex items-center">
                        <div class="flex-shrink-0 bg-purple-500 rounded-md p-3">
                            <i class="fas fa-box-open fa-lg text-white"></i>
                        </div>
                        <div class="ml-4 flex-1 min-w-0">
                            <div class="text-sm font-medium text-gray-500 dark:text-gray-300"><?php echo __('current_package', $translations); ?></div>
                            <div class="text-md font-semibold text-gray-900 dark:text-white truncate" title="<?php echo e($package_info['name'] ?? __('no_active_package', $translations)); ?>">
                                <?php echo e($package_info['name'] ?? __('no_active_package', $translations)); ?>
                            </div>
                            <?php if (isset($package_info['package_expires_at'])): ?>
                            <div class="text-xs text-gray-500 dark:text-gray-400">
                                <?php echo __('expires_at', $translations); ?>: <?php echo date('d.m.Y', strtotime($package_info['package_expires_at'])); ?>
                            </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Son İndirmeler Tablosu -->
            <div class="bg-white dark:bg-gray-700 shadow rounded-lg p-4 border border-gray-100 dark:border-gray-600 mb-8">
                <h2 class="text-lg font-semibold text-gray-700 dark:text-gray-200 mb-4"><?php echo __('recent_downloads', $translations); ?></h2>
                
                <div class="overflow-x-auto">
                    <table class="min-w-full divide-y divide-gray-200 dark:divide-gray-600">
                        <thead class="bg-gray-50 dark:bg-gray-800">
                            <tr>
                                <th scope="col" class="px-4 sm:px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider"><?php echo __('platform', $translations); ?></th>
                                <th scope="col" class="px-4 sm:px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider"><?php echo __('original_url', $translations); ?></th>
                                <th scope="col" class="px-4 sm:px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider"><?php echo __('date', $translations); ?></th>
                            </tr>
                        </thead>
                        <tbody class="bg-white dark:bg-gray-700 divide-y divide-gray-200 dark:divide-gray-600">
                            <?php if (empty($downloads)): ?>
                                <tr>
                                    <td colspan="3" class="px-4 sm:px-6 py-4 text-sm text-gray-500 dark:text-gray-400 text-center">
                                        <?php echo __('no_downloads_yet', $translations); ?>
                                    </td>
                                </tr>
                            <?php else: ?>
                                <?php foreach ($downloads as $download): ?>
                                    <tr>
                                        <td class="px-4 sm:px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900 dark:text-white">
                                            <?php if (strtolower($download['platform']) === 'freepik'): ?>
                                                <span class="inline-block bg-blue-100 text-blue-800 text-xs font-medium mr-2 px-2.5 py-0.5 rounded dark:bg-blue-900 dark:text-blue-300">Freepik</span>
                                            <?php elseif (strtolower($download['platform']) === 'envato'): ?>
                                                <span class="inline-block bg-green-100 text-green-800 text-xs font-medium mr-2 px-2.5 py-0.5 rounded dark:bg-green-900 dark:text-green-300">Envato</span>
                                            <?php else: ?>
                                                <?php echo e($download['platform']); ?>
                                            <?php endif; ?>
                                        </td>
                                        <td class="px-4 sm:px-6 py-4 text-sm text-gray-500 dark:text-gray-400">
                                            <div class="truncate max-w-[150px] sm:max-w-[250px] md:max-w-[350px]" title="<?php echo e($download['url']); ?>">
                                                <?php echo e($download['url']); ?>
                                            </div>
                                        </td>
                                        <td class="px-4 sm:px-6 py-4 whitespace-nowrap text-sm text-gray-500 dark:text-gray-400">
                                            <?php echo date('d.m.Y H:i', strtotime($download['created_at'])); ?>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
                
                <!-- Sayfalama -->
                <?php if ($total_pages > 1): ?>
                <div class="mt-6 flex justify-center">
                    <nav class="relative z-0 inline-flex rounded-md shadow-sm -space-x-px" aria-label="Pagination">
                        <a href="?page=<?php echo max(1, $page - 1); ?>" 
                           class="<?php echo $page <= 1 ? 'pointer-events-none bg-gray-100 dark:bg-gray-700' : 'bg-white dark:bg-gray-800 hover:bg-gray-50 dark:hover:bg-gray-700'; ?> relative inline-flex items-center px-2 py-2 rounded-l-md border border-gray-300 dark:border-gray-600 text-sm font-medium text-gray-500 dark:text-gray-300">
                            <span class="sr-only">Previous</span>
                            <i class="fas fa-chevron-left h-5 w-5"></i>
                        </a>
                        
                        <?php 
                        $start_page = max(1, $page - 2);
                        $end_page = min($total_pages, $page + 2);
                        if ($start_page > 1) echo '<span class="relative inline-flex items-center px-4 py-2 border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-800 text-sm font-medium text-gray-700 dark:text-gray-300">...</span>';
                        for ($i = $start_page; $i <= $end_page; $i++): 
                        ?>
                            <a href="?page=<?php echo $i; ?>" 
                               aria-current="<?php echo $i === $page ? 'page' : 'false'; ?>" 
                               class="<?php echo $i === $page ? 'z-10 bg-primary-50 border-primary-500 text-primary-600 dark:bg-gray-900 dark:border-primary-500 dark:text-white' : 'bg-white dark:bg-gray-800 border-gray-300 dark:border-gray-600 text-gray-500 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-700'; ?> relative inline-flex items-center px-4 py-2 border text-sm font-medium">
                                <?php echo $i; ?>
                            </a>
                        <?php 
                        endfor;
                        if ($end_page < $total_pages) echo '<span class="relative inline-flex items-center px-4 py-2 border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-800 text-sm font-medium text-gray-700 dark:text-gray-300">...</span>';
                        ?>
                        
                        <a href="?page=<?php echo min($total_pages, $page + 1); ?>" 
                           class="<?php echo $page >= $total_pages ? 'pointer-events-none bg-gray-100 dark:bg-gray-700' : 'bg-white dark:bg-gray-800 hover:bg-gray-50 dark:hover:bg-gray-700'; ?> relative inline-flex items-center px-2 py-2 rounded-r-md border border-gray-300 dark:border-gray-600 text-sm font-medium text-gray-500 dark:text-gray-300">
                            <span class="sr-only">Next</span>
                            <i class="fas fa-chevron-right h-5 w-5"></i>
                        </a>
                    </nav>
                </div>
                <?php endif; ?>
            </div>
            
            <!-- İndirme Aracına Hızlı Erişim -->
            <div class="mb-8 bg-primary-50 dark:bg-gray-700 shadow rounded-lg p-6 border border-primary-200 dark:border-gray-600">
                <div class="flex flex-col md:flex-row items-center justify-between">
                    <div class="mb-4 md:mb-0">
                        <h3 class="text-lg font-semibold text-gray-800 dark:text-white"><?php echo __('quick_download', $translations); ?></h3>
                        <p class="text-gray-600 dark:text-gray-300"><?php echo __('download_description', $translations); ?></p>
                    </div>
                    <a href="download.php" class="bg-primary-600 hover:bg-primary-700 text-white font-bold py-2 px-6 rounded-md shadow-md transition duration-150 ease-in-out">
                        <?php echo __('go_to_download', $translations); ?> <i class="fas fa-arrow-right ml-2"></i>
                    </a>
                </div>
            </div>
        </div>
    </div>
    <?php
} else {
    // Giriş yapmamış kullanıcılar için tanıtım sayfası
    ?>
    <!-- Tanıtım Sayfası -->
    <div class="bg-white dark:bg-gray-800 shadow-lg rounded-lg overflow-hidden">
        <!-- Hero Bölümü -->
        <div class="relative bg-gradient-to-br from-primary-600 via-primary-700 to-primary-800 text-white overflow-hidden">
            <!-- Animasyonlu arka plan desenleri -->
            <div class="absolute inset-0">
                <div class="absolute inset-0 bg-gradient-to-br from-primary-600/20 to-primary-800/20"></div>
                <div class="absolute inset-0 opacity-30">
                    <svg class="absolute left-0 transform -translate-y-1/2 animate-pulse" width="404" height="784" fill="none" viewBox="0 0 404 784">
                        <defs>
                            <pattern id="pattern-squares" x="0" y="0" width="20" height="20" patternUnits="userSpaceOnUse">
                                <rect x="0" y="0" width="4" height="4" fill="currentColor" />
                            </pattern>
                        </defs>
                        <rect width="404" height="784" fill="url(#pattern-squares)" />
                    </svg>
                    <svg class="absolute right-0 transform translate-x-1/2 animate-pulse" width="404" height="784" fill="none" viewBox="0 0 404 784">
                        <defs>
                            <pattern id="pattern-squares-2" x="0" y="0" width="20" height="20" patternUnits="userSpaceOnUse">
                                <rect x="0" y="0" width="4" height="4" fill="currentColor" />
                            </pattern>
                        </defs>
                        <rect width="404" height="784" fill="url(#pattern-squares-2)" />
                    </svg>
                </div>
            </div>

            <div class="relative px-6 py-24 sm:px-8 sm:py-32">
                <div class="mx-auto max-w-[1000px] text-center">
                    <?php if (!empty($settings['site_logo'])): ?>
                    <div class="flex justify-center mb-8 animate-fade-in">
                        <img src="<?php echo e($settings['site_logo']); ?>" alt="<?php echo e($settings['site_title'] ?? 'StockPanel'); ?>" 
                             class="h-20 w-auto transform hover:scale-105 transition-transform duration-300">
                    </div>
                    <?php endif; ?>

                    <h1 class="text-4xl font-extrabold tracking-tight sm:text-5xl lg:text-6xl mb-8 animate-fade-in-up">
                        <span class="block"><?php echo e($settings['site_title'] ?? 'StockPanel'); ?></span>
                        <span class="block text-primary-200 mt-2 text-2xl sm:text-3xl lg:text-4xl">
                            <?php echo e($settings['site_description'] ?? 'Dijital Medya İndirme Platformu'); ?>
                        </span>
                    </h1>

                    <p class="mx-auto mt-6 max-w-2xl text-xl sm:text-2xl text-primary-100 animate-fade-in-up" style="animation-delay: 0.2s">
                        Envato Elements ve Freepik'ten sınırsız indirme imkanı ile dijital içeriklerinize hemen ulaşın.
                    </p>

                    <div class="mt-12 flex flex-col sm:flex-row gap-4 justify-center animate-fade-in-up" style="animation-delay: 0.4s">
                        <a href="login.php" class="inline-flex items-center justify-center px-8 py-4 text-lg font-semibold rounded-xl bg-white text-primary-700 hover:bg-primary-50 transition-all duration-300 transform hover:scale-105 shadow-lg hover:shadow-xl">
                            <?php echo __('login', $translations); ?>
                            <svg class="ml-2 -mr-1 w-5 h-5" fill="currentColor" viewBox="0 0 20 20">
                                <path fill-rule="evenodd" d="M10.293 3.293a1 1 0 011.414 0l6 6a1 1 0 010 1.414l-6 6a1 1 0 01-1.414-1.414L14.586 11H3a1 1 0 110-2h11.586l-4.293-4.293a1 1 0 010-1.414z" clip-rule="evenodd" />
                            </svg>
                        </a>
                        <a href="register.php" class="inline-flex items-center justify-center px-8 py-4 text-lg font-semibold rounded-xl bg-primary-800 text-white hover:bg-primary-900 border-2 border-primary-400 transition-all duration-300 transform hover:scale-105 shadow-lg hover:shadow-xl">
                            <?php echo __('register', $translations); ?>
                            <svg class="ml-2 -mr-1 w-5 h-5" fill="currentColor" viewBox="0 0 20 20">
                                <path fill-rule="evenodd" d="M10 3a1 1 0 011 1v5h5a1 1 0 110 2h-5v5a1 1 0 11-2 0v-5H4a1 1 0 110-2h5V4a1 1 0 011-1z" clip-rule="evenodd" />
                            </svg>
                        </a>
                    </div>

                    <!-- Animasyonlu dalga efekti -->
                    <div class="absolute bottom-0 left-0 right-0 transform translate-y-1/2">
                        <svg class="w-full h-24 text-white dark:text-gray-800 fill-current" viewBox="0 0 1000 100" preserveAspectRatio="none">
                            <path d="M0,0 C200,100 800,100 1000,0 L1000,100 L0,100 Z"></path>
                        </svg>
                    </div>
                </div>
            </div>
        </div>

        <style>
            @keyframes fade-in {
                from { opacity: 0; }
                to { opacity: 1; }
            }

            @keyframes fade-in-up {
                from {
                    opacity: 0;
                    transform: translateY(20px);
                }
                to {
                    opacity: 1;
                    transform: translateY(0);
                }
            }

            .animate-fade-in {
                animation: fade-in 1s ease-out forwards;
            }

            .animate-fade-in-up {
                opacity: 0;
                animation: fade-in-up 1s ease-out forwards;
            }

            .animate-pulse {
                animation: pulse 4s cubic-bezier(0.4, 0, 0.6, 1) infinite;
            }

            @keyframes pulse {
                0%, 100% {
                    opacity: 0.3;
                }
                50% {
                    opacity: 0.1;
                }
            }
        </style>
        
        <!-- Özellikler Bölümü -->
        <div class="p-6 md:p-12">
            <h2 class="text-2xl font-bold text-gray-800 dark:text-white text-center mb-12"><?php echo __('features', $translations); ?></h2>
            
            <div class="grid md:grid-cols-2 lg:grid-cols-3 gap-8">
                <!-- Özellik 1 -->
                <div class="bg-white dark:bg-gray-700 p-6 rounded-lg shadow-md border border-gray-100 dark:border-gray-600">
                    <div class="bg-primary-100 dark:bg-primary-900 p-3 rounded-full w-12 h-12 flex items-center justify-center mb-4">
                        <i class="fas fa-bolt text-primary-600 dark:text-primary-400 text-xl"></i>
                    </div>
                    <h3 class="text-xl font-semibold text-gray-800 dark:text-white mb-2"><?php echo __('quick_downloads', $translations); ?></h3>
                    <p class="text-gray-600 dark:text-gray-300"><?php echo __('quick_downloads_desc', $translations); ?></p>
                </div>
                
                <!-- Özellik 2 -->
                <div class="bg-white dark:bg-gray-700 p-6 rounded-lg shadow-md border border-gray-100 dark:border-gray-600">
                    <div class="bg-blue-100 dark:bg-blue-900 p-3 rounded-full w-12 h-12 flex items-center justify-center mb-4">
                        <i class="fas fa-paint-brush text-blue-600 dark:text-blue-400 text-xl"></i>
                    </div>
                    <h3 class="text-xl font-semibold text-gray-800 dark:text-white mb-2"><?php echo __('envato_elements', $translations); ?></h3>
                    <p class="text-gray-600 dark:text-gray-300"><?php echo __('envato_elements_desc', $translations); ?></p>
                </div>
                
                <!-- Özellik 3 -->
                <div class="bg-white dark:bg-gray-700 p-6 rounded-lg shadow-md border border-gray-100 dark:border-gray-600">
                    <div class="bg-green-100 dark:bg-green-900 p-3 rounded-full w-12 h-12 flex items-center justify-center mb-4">
                        <i class="fas fa-image text-green-600 dark:text-green-400 text-xl"></i>
                    </div>
                    <h3 class="text-xl font-semibold text-gray-800 dark:text-white mb-2"><?php echo __('freepik_content', $translations); ?></h3>
                    <p class="text-gray-600 dark:text-gray-300"><?php echo __('freepik_content_desc', $translations); ?></p>
                </div>
                
                <!-- Özellik 4 -->
                <div class="bg-white dark:bg-gray-700 p-6 rounded-lg shadow-md border border-gray-100 dark:border-gray-600">
                    <div class="bg-yellow-100 dark:bg-yellow-900 p-3 rounded-full w-12 h-12 flex items-center justify-center mb-4">
                        <i class="fas fa-dollar-sign text-yellow-600 dark:text-yellow-400 text-xl"></i>
                    </div>
                    <h3 class="text-xl font-semibold text-gray-800 dark:text-white mb-2"><?php echo __('affordable_packages', $translations); ?></h3>
                    <p class="text-gray-600 dark:text-gray-300"><?php echo __('affordable_packages_desc', $translations); ?></p>
                </div>
                
                <!-- Özellik 5 -->
                <div class="bg-white dark:bg-gray-700 p-6 rounded-lg shadow-md border border-gray-100 dark:border-gray-600">
                    <div class="bg-purple-100 dark:bg-purple-900 p-3 rounded-full w-12 h-12 flex items-center justify-center mb-4">
                        <i class="fas fa-shield-alt text-purple-600 dark:text-purple-400 text-xl"></i>
                    </div>
                    <h3 class="text-xl font-semibold text-gray-800 dark:text-white mb-2"><?php echo __('secure_downloads', $translations); ?></h3>
                    <p class="text-gray-600 dark:text-gray-300"><?php echo __('secure_downloads_desc', $translations); ?></p>
                </div>
                
                <!-- Özellik 6 -->
                <div class="bg-white dark:bg-gray-700 p-6 rounded-lg shadow-md border border-gray-100 dark:border-gray-600">
                    <div class="bg-red-100 dark:bg-red-900 p-3 rounded-full w-12 h-12 flex items-center justify-center mb-4">
                        <i class="fas fa-headset text-red-600 dark:text-red-400 text-xl"></i>
                    </div>
                    <h3 class="text-xl font-semibold text-gray-800 dark:text-white mb-2"><?php echo __('support', $translations); ?></h3>
                    <p class="text-gray-600 dark:text-gray-300"><?php echo __('support_desc', $translations); ?></p>
                </div>
            </div>
        </div>
        
        <!-- Paketler Bölümü -->
        <div class="bg-white dark:bg-gray-800 px-6 py-12">
            <?php
            // Paketleri veritabanından çek
            $packages = [];
            
            if ($db) {
                try {
                    $stmt = $db->prepare("SELECT * FROM packages WHERE status = 1 ORDER BY price ASC");
                    $stmt->execute();
                    $packages = $stmt->fetchAll(PDO::FETCH_ASSOC);
                } catch (PDOException $e) {
                    error_log("Paketler yüklenirken hata: " . $e->getMessage());
                }
            }
            
            if (!empty($packages)): ?>
            <div class="max-w-6xl mx-auto grid md:grid-cols-2 lg:grid-cols-3 gap-8">
                <?php foreach ($packages as $package): 
                    $supports_envato = (bool)($package['supports_envato'] ?? 0);
                    $supports_freepik = (bool)($package['supports_freepik'] ?? 0);
                    $price = number_format((float)$package['price'], 2, ',', '.');
                    $is_featured = (bool)($package['is_featured'] ?? 0);
                ?>
                <div class="bg-white dark:bg-gray-700 rounded-lg shadow-lg border <?php echo $is_featured ? 'border-primary-500 dark:border-primary-400 transform scale-105' : 'border-gray-200 dark:border-gray-600'; ?> overflow-hidden flex flex-col">
                    <?php if ($is_featured): ?>
                    <div class="bg-primary-500 text-white text-center py-1 px-4 text-sm font-bold">
                        <?php echo __('popular_choice', $translations); ?>
                    </div>
                    <?php endif; ?>
                    
                    <div class="p-6 flex-1 flex flex-col">
                        <h3 class="text-xl font-bold text-gray-800 dark:text-white mb-4 text-center"><?php echo e($package['name']); ?></h3>
                        
                        <div class="text-center mb-6">
                            <span class="text-3xl font-bold text-gray-900 dark:text-white"><?php echo $price; ?> ₺</span>
                            <?php if (isset($package['duration']) && $package['duration'] > 0): ?>
                            <span class="text-gray-500 dark:text-gray-300">/<?php echo __('month', $translations); ?></span>
                            <?php endif; ?>
                        </div>
                        
                        <ul class="space-y-3 mb-6 flex-1">
                            <li class="flex items-center text-gray-600 dark:text-gray-300">
                                <i class="fas fa-check text-green-500 mr-2"></i>
                                <?php echo __('daily_limit', $translations); ?>: <span class="font-semibold ml-1"><?php echo (int)$package['daily_limit']; ?></span>
                            </li>
                            <li class="flex items-center text-gray-600 dark:text-gray-300">
                                <i class="fas fa-check text-green-500 mr-2"></i>
                                <?php echo __('monthly_limit', $translations); ?>: <span class="font-semibold ml-1"><?php echo (int)$package['monthly_limit']; ?></span>
                            </li>
                            <li class="flex items-center text-gray-600 dark:text-gray-300">
                                <i class="<?php echo $supports_envato ? 'fas fa-check text-green-500' : 'fas fa-times text-red-500'; ?> mr-2"></i>
                                Envato Elements
                            </li>
                            <li class="flex items-center text-gray-600 dark:text-gray-300">
                                <i class="<?php echo $supports_freepik ? 'fas fa-check text-green-500' : 'fas fa-times text-red-500'; ?> mr-2"></i>
                                Freepik
                            </li>
                            <?php if (!empty($package['description'])): ?>
                            <li class="text-gray-600 dark:text-gray-300 pt-2">
                                <?php echo e($package['description']); ?>
                            </li>
                            <?php endif; ?>
                        </ul>
                        
                        <a href="register.php" class="mt-auto bg-primary-600 hover:bg-primary-700 text-white text-center font-bold py-2 px-4 rounded-md transition duration-150 ease-in-out">
                            <?php echo __('get_started', $translations); ?>
                        </a>
                    </div>
                </div>
                <?php endforeach; ?>
            </div>
            <?php endif; ?>
        </div>

        <!-- Sayfalar Bölümü -->
        <?php
        // Görünür sayfaları veritabanından çek
        $pages = [];
        if ($db) {
            try {
                $stmt = $db->prepare("SELECT title, slug FROM pages WHERE is_visible = 1 ORDER BY `order` ASC");
                $stmt->execute();
                $pages = $stmt->fetchAll(PDO::FETCH_ASSOC);
            } catch (PDOException $e) {
                error_log("Sayfalar yüklenirken hata: " . $e->getMessage());
            }
        }

        if (!empty($pages)): ?>
        <div class="bg-gray-50 dark:bg-gray-900 px-6 py-12">
            <div class="max-w-[1000px] mx-auto">
                <div class="flex flex-wrap justify-center gap-4">
                    <?php foreach ($pages as $page): ?>
                    <a href="page/<?php echo e($page['slug']); ?>" 
                       class="inline-flex items-center px-6 py-3 border-2 border-primary-500 dark:border-primary-400 text-base font-medium rounded-xl text-primary-600 dark:text-primary-300 hover:bg-primary-50 dark:hover:bg-primary-900 transition-all duration-300 transform hover:scale-105 hover:shadow-lg">
                        <?php echo e($page['title']); ?>
                        <svg class="ml-2 -mr-1 w-4 h-4" fill="currentColor" viewBox="0 0 20 20">
                            <path fill-rule="evenodd" d="M10.293 3.293a1 1 0 011.414 0l6 6a1 1 0 010 1.414l-6 6a1 1 0 01-1.414-1.414L14.586 11H3a1 1 0 110-2h11.586l-4.293-4.293a1 1 0 010-1.414z" clip-rule="evenodd" />
                        </svg>
                    </a>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>
        <?php endif; ?>

        <!-- CTA Bölümü -->
        <div class="bg-gray-50 dark:bg-gray-700 px-6 py-12 text-center">
            <h2 class="text-2xl font-bold text-gray-800 dark:text-white mb-6"><?php echo __('ready_to_start', $translations); ?></h2>
            <p class="text-gray-600 dark:text-gray-300 max-w-2xl mx-auto mb-8"><?php echo __('ready_to_start_desc', $translations); ?></p>
            <a href="register.php" class="bg-primary-600 hover:bg-primary-700 text-white font-bold py-3 px-8 rounded-lg shadow-lg transition duration-150 ease-in-out">
                <?php echo __('get_started', $translations); ?>
            </a>
        </div>
    </div>
    <?php
}

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