<?php
$title = lang('page_title_themes', [], 'Temalar');
$page_title = lang('themes_list_title', [], 'Tema Listesi');

// Mevcut temaları al
$themes = [];
$theme_dir = ROOT_FOLDER . '/theme/';
if (is_dir($theme_dir)) {
    $theme_folders = scandir($theme_dir);
    foreach ($theme_folders as $folder) {
        if ($folder != '.' && $folder != '..' && is_dir($theme_dir . $folder)) {
            $theme_info_file = $theme_dir . $folder . '/theme.json';
            $theme_info = [];
            
            if (file_exists($theme_info_file)) {
                $theme_info = json_decode(file_get_contents($theme_info_file), true);
            }
            
            $themes[] = [
                'name' => $folder,
                'title' => isset($theme_info['title']) ? $theme_info['title'] : ucfirst($folder),
                'description' => isset($theme_info['description']) ? $theme_info['description'] : '',
                'version' => isset($theme_info['version']) ? $theme_info['version'] : '1.0',
                'author' => isset($theme_info['author']) ? $theme_info['author'] : '',
                'preview_image' => isset($theme_info['preview_image']) ? $theme_info['preview_image'] : '',
                'is_active' => (get_active_theme() == $folder)
            ];
        }
    }
}

// Aktif temayı başa taşı
usort($themes, function($a, $b) {
    if ($a['is_active'] && !$b['is_active']) {
        return -1;
    }
    if (!$a['is_active'] && $b['is_active']) {
        return 1;
    }
    return 0;
});
?>

<div class="row">
    <div class="col-lg-12">
        <div class="card">
            <div class="card-header">
                <h4 class="card-title mb-0"><?= $page_title ?></h4>
            </div>
            <div class="card-body">
                <div class="row">
                    <?php foreach ($themes as $theme): ?>
                    <div class="col-lg-4 col-md-6 mb-4">
                        <div class="card theme-card h-100 <?= $theme['is_active'] ? 'active-theme' : '' ?>">
                            <div class="card-body">
                                <div class="theme-preview mb-3">
                                    <?php if ($theme['preview_image'] && file_exists($theme_dir . $theme['name'] . '/' . $theme['preview_image'])): ?>
                                        <img src="<?= DOMAIN ?>/theme/<?= $theme['name'] ?>/<?= $theme['preview_image'] ?>" 
                                             class="img-fluid rounded" alt="<?= $theme['title'] ?>">
                                    <?php else: ?>
                                        <div class="theme-placeholder bg-light rounded d-flex align-items-center justify-content-center" style="height: 200px;">
                                            <i class="bx bx-palette fs-1 text-muted"></i>
                                        </div>
                                    <?php endif; ?>
                                </div>
                                
                                <h5 class="card-title"><?= $theme['title'] ?></h5>
                                <p class="card-text text-muted"><?= $theme['description'] ?: lang('theme.description.empty', [], 'Tema açıklaması bulunmuyor.') ?></p>
                                
                                <div class="theme-meta mb-3">
                                    <small class="text-muted">
                                        <strong><?= lang('theme.meta.version', [], 'Versiyon:') ?></strong> <?= $theme['version'] ?><br>
                                        <strong><?= lang('theme.meta.author', [], 'Geliştirici:') ?></strong> <?= $theme['author'] ?: 'Bilinmiyor' ?>
                                    </small>
                                </div>
                                
                                <div class="d-flex justify-content-between align-items-center">
                                    <?php if ($theme['is_active']): ?>
                                        <div class="d-flex align-items-center gap-2">
                                            <span class="badge bg-success"><?= lang('theme.active', [], 'Aktif Tema') ?></span>
                                            <a href="<?= ADMIN_URL ?>/theme_options/lists" 
                                               class="btn btn-outline-primary btn-sm">
                                                <i class="bx bx-cog"></i> <?= lang('settings', [], 'Ayarlar') ?>
                                            </a>
                                        </div>
                                    <?php else: ?>
                                        <a href="#" 
                                           class="btn btn-primary btn-sm"
                                           onclick="activateTheme('<?= $theme['name'] ?>', '<?= $theme['title'] ?>')">
                                            <?= lang('theme.activate', [], 'Aktifleştir') ?>
                                        </a>
                                    <?php endif; ?>
                                    
                                    <a href="<?= ADMIN_URL ?>/themes/preview/<?= $theme['name'] ?>" 
                                       class="btn btn-outline-secondary btn-sm" target="_blank">
                                        <i class="bx bx-show"></i> <?= lang('preview', [], 'Önizle') ?>
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div>
                    <?php endforeach; ?>
                </div>
                
                <?php if (empty($themes)): ?>
                <div class="text-center py-5">
                    <i class="bx bx-palette fs-1 text-muted mb-3"></i>
                    <h5 class="text-muted"><?= lang('theme.empty.title', [], 'Henüz tema bulunmuyor') ?></h5>
                    <p class="text-muted"><?= lang('theme.empty.desc', [], 'Tema klasörünüzde tema dosyaları bulunamadı.') ?></p>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

<style>
.theme-card {
    transition: transform 0.2s, box-shadow 0.2s;
    border: 1px solid #e9ecef;
}

.theme-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.theme-placeholder {
    background: linear-gradient(45deg, #f8f9fa 25%, transparent 25%), 
                linear-gradient(-45deg, #f8f9fa 25%, transparent 25%), 
                linear-gradient(45deg, transparent 75%, #f8f9fa 75%), 
                linear-gradient(-45deg, transparent 75%, #f8f9fa 75%);
    background-size: 20px 20px;
    background-position: 0 0, 0 10px, 10px -10px, -10px 0px;
}

.active-theme {
    border: 2px solid #28a745 !important;
    box-shadow: 0 0 0 0.2rem rgba(40, 167, 69, 0.25) !important;
}

.active-theme .card-title {
    color: #28a745;
    font-weight: bold;
}
</style>

<script>
function activateTheme(themeName, themeTitle) {
    Swal.fire({
        title: '<?= lang('theme.activate.title', [], 'Tema Aktifleştirme') ?>',
        text: `"${themeTitle}" <?= lang('theme.activate.question', [], 'temasını aktif hale getirmek istediğinizden emin misiniz?') ?>`,
        icon: 'question',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: '<?= lang('theme.activate.confirm', [], 'Evet, Aktifleştir') ?>',
        cancelButtonText: '<?= lang('cancel', [], 'İptal') ?>'
    }).then((result) => {
        if (result.isConfirmed) {
            // Loading göster
            Swal.fire({
                title: '<?= lang('theme.activate.loading.title', [], 'Aktifleştiriliyor...') ?>',
                text: '<?= lang('theme.activate.loading.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> 