<?php
$theme_name = isset($segments[3]) ? $segments[3] : '';

if (empty($theme_name)) {
    m_redirect(ADMIN_URL . '/themes/lists');
}

// Tema klasörünün varlığını kontrol et
$theme_dir = ROOT_FOLDER . '/theme/' . $theme_name;
if (!is_dir($theme_dir)) {
    $_SESSION['error'] = 'Tema bulunamadı: ' . $theme_name;
    m_redirect(ADMIN_URL . '/themes/lists');
}

// Tema bilgilerini al
$theme_info_file = $theme_dir . '/theme.json';
$theme_info = [];
if (file_exists($theme_info_file)) {
    $theme_info = json_decode(file_get_contents($theme_info_file), true);
}

$theme_title = isset($theme_info['title']) ? $theme_info['title'] : ucfirst($theme_name);
$theme_description = isset($theme_info['description']) ? $theme_info['description'] : '';
$theme_version = isset($theme_info['version']) ? $theme_info['version'] : '1.0';
$theme_author = isset($theme_info['author']) ? $theme_info['author'] : '';

// Tema dosyalarını listele
$theme_files = [];
$assets_dir = $theme_dir . '/assets';
if (is_dir($assets_dir)) {
    $theme_files = scan_directory($assets_dir);
}

function scan_directory($dir, $prefix = '') {
    $files = [];
    $items = scandir($dir);
    
    foreach ($items as $item) {
        if ($item != '.' && $item != '..') {
            $path = $dir . '/' . $item;
            $relative_path = $prefix . '/' . $item;
            
            if (is_dir($path)) {
                $files = array_merge($files, scan_directory($path, $relative_path));
            } else {
                $files[] = $relative_path;
            }
        }
    }
    
    return $files;
}
?>

<!DOCTYPE html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Tema Önizleme - <?= $theme_title ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/boxicons@2.0.7/css/boxicons.min.css" rel="stylesheet">
    <style>
        .preview-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 2rem 0;
        }
        .theme-preview-frame {
            border: 2px solid #e9ecef;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 4px 12px rgba(0,0,0,0.1);
        }
        .file-list {
            max-height: 400px;
            overflow-y: auto;
        }
        .file-item {
            padding: 0.5rem;
            border-bottom: 1px solid #f8f9fa;
            font-family: 'Courier New', monospace;
            font-size: 0.9rem;
        }
        .file-item:hover {
            background-color: #f8f9fa;
        }
    </style>
</head>
<body>
    <div class="preview-header">
        <div class="container">
            <div class="row align-items-center">
                <div class="col-md-8">
                    <h1 class="mb-2"><?= $theme_title ?></h1>
                    <p class="mb-0 opacity-75"><?= $theme_description ?></p>
                </div>
                <div class="col-md-4 text-end">
                    <a href="<?= ADMIN_URL ?>/themes/lists" class="btn btn-light">
                        <i class="bx bx-arrow-back"></i> Geri Dön
                    </a>
                    <a href="<?= DOMAIN ?>/theme/<?= $theme_name ?>/editor.php?id=1" 
                       class="btn btn-primary" target="_blank">
                        <i class="bx bx-show"></i> Canlı Önizle
                    </a>
                </div>
            </div>
        </div>
    </div>

    <div class="container mt-4">
        <div class="row">
            <!-- Tema Bilgileri -->
            <div class="col-lg-4">
                <div class="card mb-4">
                    <div class="card-header">
                        <h5 class="card-title mb-0">Tema Bilgileri</h5>
                    </div>
                    <div class="card-body">
                        <table class="table table-borderless">
                            <tr>
                                <td><strong>Tema Adı:</strong></td>
                                <td><?= $theme_name ?></td>
                            </tr>
                            <tr>
                                <td><strong>Başlık:</strong></td>
                                <td><?= $theme_title ?></td>
                            </tr>
                            <tr>
                                <td><strong>Versiyon:</strong></td>
                                <td><?= $theme_version ?></td>
                            </tr>
                            <tr>
                                <td><strong>Geliştirici:</strong></td>
                                <td><?= $theme_author ?: 'Bilinmiyor' ?></td>
                            </tr>
                            <tr>
                                <td><strong>Durum:</strong></td>
                                <td>
                                    <?php if (get_active_theme() == $theme_name): ?>
                                        <span class="badge bg-success">Aktif</span>
                                    <?php else: ?>
                                        <span class="badge bg-secondary">Pasif</span>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        </table>
                        
                        <?php if (get_active_theme() != $theme_name): ?>
                        <div class="d-grid">
                                                    <a href="#" 
                           class="btn btn-primary"
                           onclick="activateTheme('<?= $theme_name ?>', '<?= $theme_title ?>')">
                            <i class="bx bx-check"></i> Aktifleştir
                        </a>
                        </div>
                        <?php endif; ?>
                    </div>
                </div>

                <!-- Tema Dosyaları -->
                <div class="card">
                    <div class="card-header">
                        <h5 class="card-title mb-0">Tema Dosyaları</h5>
                    </div>
                    <div class="card-body">
                        <div class="file-list">
                            <?php if (!empty($theme_files)): ?>
                                <?php foreach ($theme_files as $file): ?>
                                <div class="file-item">
                                    <i class="bx bx-file"></i> <?= $file ?>
                                </div>
                                <?php endforeach; ?>
                            <?php else: ?>
                                <p class="text-muted">Dosya bulunamadı</p>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Tema Önizleme -->
            <div class="col-lg-8">
                <div class="card">
                    <div class="card-header">
                        <h5 class="card-title mb-0">Tema Önizleme</h5>
                    </div>
                    <div class="card-body">
                        <div class="theme-preview-frame">
                            <iframe src="<?= DOMAIN ?>/theme/<?= $theme_name ?>/editor.php?id=1" 
                                    width="100%" height="600" frameborder="0"></iframe>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    
    <script>
    function activateTheme(themeName, themeTitle) {
        Swal.fire({
            title: 'Tema Aktifleştirme',
            text: `"${themeTitle}" temasını aktif hale getirmek istediğinizden emin misiniz?`,
            icon: 'question',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Evet, Aktifleştir',
            cancelButtonText: 'İptal'
        }).then((result) => {
            if (result.isConfirmed) {
                // Loading göster
                Swal.fire({
                    title: 'Aktifleştiriliyor...',
                    text: 'Tema aktifleştiriliyor, lütfen bekleyin.',
                    allowOutsideClick: false,
                    allowEscapeKey: false,
                    showConfirmButton: false,
                    didOpen: () => {
                        Swal.showLoading();
                    }
                });
                
                // Tema aktivasyon sayfasına yönlendir
                window.location.href = '<?= ADMIN_URL ?>/themes/activate/' + themeName;
            }
        });
    }
    </script>
</body>
</html> 