<?php $__env->startSection('title', 'Homepage Themes'); ?>

<?php $__env->startSection('breadcrumb'); ?>
    <div class="section-header-breadcrumb">
        <div class="breadcrumb-item"><a href="<?php echo e(route('admin.dashboard')); ?>">Dashboard</a></div>
        <div class="breadcrumb-item">Frontend Manager</div>
        <div class="breadcrumb-item">Homepage Theme</div>
    </div>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('main-content'); ?>
    <div class="row">
        <!-- Theme Upload Card -->
        <div class="col-12 mb-4">
            <div class="card card-primary">
                <div class="card-header">
                    <h4>Install New Theme</h4>
                </div>
                <div class="card-body">
                    <form method="POST" action="<?php echo e(route('admin.frontend.theme.upload')); ?>" enctype="multipart/form-data">
                        <?php echo csrf_field(); ?>
                        <div class="row align-items-center">
                            <div class="col-md-8 col-lg-9">
                                <div class="form-group mb-md-0">
                                    <label class="font-weight-bold">Theme ZIP File</label>
                                    <div class="custom-file">
                                        <input type="file" class="custom-file-input <?php $__errorArgs = ['theme_zip'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>" id="theme_zip" name="theme_zip" required>
                                        <label class="custom-file-label" for="theme_zip">Choose zip file...</label>
                                        <?php $__errorArgs = ['theme_zip'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                            <div class="invalid-feedback"><?php echo e($message); ?></div>
                                        <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                                    </div>
                                    <small class="form-text text-muted">Upload a valid theme zip package containing `theme.json`, template files, and `assets/` directory.</small>
                                </div>
                            </div>
                            <div class="col-md-4 col-lg-3 text-right">
                                <button type="submit" class="btn btn-primary btn-block btn-lg">
                                    <i class="fas fa-upload mr-2"></i> Upload & Install
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <!-- Discovered Themes list -->
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h4>Available Themes</h4>
                </div>
                <div class="card-body">
                    <div class="row">
                        <!-- Default Fallback Theme Card -->
                        <div class="col-md-6 col-lg-4 mb-4">
                            <div class="card h-100 border <?php if($activeTheme === null): ?> border-success shadow-sm <?php else: ?> border-light <?php endif; ?>">
                                <div class="position-relative bg-light" style="height: 200px; overflow: hidden; display: flex; align-items: center; justify-content: center;">
                                    <?php if(file_exists(public_path('assets/admin/img/default.png'))): ?>
                                        <img src="<?php echo e(asset('assets/admin/img/default.png')); ?>" class="w-100 h-100" style="object-fit: cover;" alt="Default Theme">
                                    <?php else: ?>
                                        <div class="text-center text-muted">
                                            <i class="fas fa-desktop fa-3x mb-2"></i>
                                            <p class="mb-0">Default Theme Preview</p>
                                        </div>
                                    <?php endif; ?>
                                    
                                    <?php if($activeTheme === null): ?>
                                        <span class="badge badge-success position-absolute" style="top: 10px; right: 10px; font-size: 0.85rem; padding: 0.4em 0.8em;">
                                            <i class="fas fa-check-circle mr-1"></i> Active
                                        </span>
                                    <?php endif; ?>
                                </div>
                                <div class="card-body d-flex flex-column">
                                    <h5 class="card-title mb-1">Default Built-in Theme</h5>
                                    <p class="text-muted small mb-2">By MHQuickDev</p>
                                    <p class="card-text text-secondary small flex-grow-1">
                                        The default standard homepage layout packaged with the EduEx platform. Uses default front assets.
                                    </p>
                                    
                                    <div class="mt-3">
                                        <?php if($activeTheme === null): ?>
                                            <button class="btn btn-outline-success btn-block disabled" disabled>
                                                Active Theme
                                            </button>
                                        <?php else: ?>
                                            <form method="POST" action="<?php echo e(route('admin.frontend.theme.update')); ?>">
                                                <?php echo csrf_field(); ?>
                                                <?php echo method_field('PUT'); ?>
                                                <button type="submit" class="btn btn-primary btn-block">
                                                    Activate Default Theme
                                                </button>
                                            </form>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <!-- Dynamic Discovered Themes -->
                        <?php $__currentLoopData = $themes; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $slug => $theme): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <?php
                                $isActive = ($activeTheme === $slug);
                                $variants = $theme['variants'] ?? [];
                                $hasMultipleVariants = count($variants) > 1;
                                $thumbnail = $theme['thumbnail'] ?? null;
                                
                                if ($thumbnail) {
                                    $thumbnailUrl = theme_asset($thumbnail, $slug);
                                } else {
                                    $thumbnailUrl = null;
                                }
                            ?>
                            <div class="col-md-6 col-lg-4 mb-4">
                                <div class="card h-100 border <?php if($isActive): ?> border-success shadow-sm <?php else: ?> border-light <?php endif; ?>">
                                    <div class="position-relative bg-light d-flex align-items-center justify-content-center" style="height: 200px; overflow: hidden;">
                                        <?php if($thumbnailUrl): ?>
                                            <img src="<?php echo e($thumbnailUrl); ?>" class="w-100 h-100" style="object-fit: cover;" alt="<?php echo e($theme['name']); ?>" onerror="this.src='https://placehold.co/600x400?text=<?php echo e(urlencode($theme['name'])); ?>'">
                                        <?php else: ?>
                                            <div class="text-center text-muted">
                                                <i class="fas fa-paint-brush fa-3x mb-2"></i>
                                                <p class="mb-0"><?php echo e($theme['name']); ?> Preview</p>
                                            </div>
                                        <?php endif; ?>

                                        <?php if($isActive): ?>
                                            <span class="badge badge-success position-absolute" style="top: 10px; right: 10px; font-size: 0.85rem; padding: 0.4em 0.8em;">
                                                <i class="fas fa-check-circle mr-1"></i> Active
                                            </span>
                                        <?php endif; ?>
                                    </div>
                                    <div class="card-body d-flex flex-column">
                                        <div class="d-flex justify-content-between align-items-start mb-1">
                                            <h5 class="card-title mb-0"><?php echo e($theme['name']); ?></h5>
                                            <span class="badge badge-secondary">v<?php echo e($theme['version'] ?? '1.0.0'); ?></span>
                                        </div>
                                        <p class="text-muted small mb-2">By <?php echo e($theme['author'] ?? 'Unknown'); ?></p>
                                        <p class="card-text text-secondary small flex-grow-1">
                                            <?php echo e($theme['description'] ?? 'No description provided.'); ?>

                                        </p>

                                        <!-- Activation/Variant Form -->
                                        <form method="POST" action="<?php echo e(route('admin.frontend.theme.update')); ?>" class="mt-3">
                                            <?php echo csrf_field(); ?>
                                            <?php echo method_field('PUT'); ?>
                                            <input type="hidden" name="theme" value="<?php echo e($slug); ?>">

                                            <?php if($hasMultipleVariants): ?>
                                                <div class="form-group mb-2">
                                                    <label class="small font-weight-bold">Select Variant</label>
                                                    <select name="variant" class="form-control form-control-sm">
                                                        <?php $__currentLoopData = $variants; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $variant): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                            <option value="<?php echo e($variant['file']); ?>" <?php if($isActive && $activeVariant === $variant['file']): ?> selected <?php endif; ?>>
                                                                <?php echo e($variant['label']); ?>

                                                            </option>
                                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                                    </select>
                                                </div>
                                            <?php endif; ?>

                                            <div class="row no-gutters">
                                                <div class="<?php if($isActive): ?> col-12 <?php else: ?> col-9 pr-1 <?php endif; ?>">
                                                    <?php if($isActive && !$hasMultipleVariants): ?>
                                                        <button type="button" class="btn btn-outline-success btn-block disabled" disabled>
                                                            Active Theme
                                                        </button>
                                                    <?php else: ?>
                                                        <button type="submit" class="btn btn-primary btn-block">
                                                            <?php if($isActive): ?> Update Variant <?php else: ?> Activate <?php endif; ?>
                                                        </button>
                                                    <?php endif; ?>
                                                </div>
                                                <?php if(!$isActive): ?>
                                                    <div class="col-3 pl-1">
                                                        <button type="button" class="btn btn-outline-danger btn-block" onclick="confirmDeleteTheme('<?php echo e($slug); ?>')">
                                                            <i class="fas fa-trash-alt"></i>
                                                        </button>
                                                    </div>
                                                <?php endif; ?>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Delete Confirmation Form -->
    <form id="delete-theme-form" method="POST" action="<?php echo e(route('admin.frontend.theme.delete')); ?>" style="display:none;">
        <?php echo csrf_field(); ?>
        <?php echo method_field('DELETE'); ?>
        <input type="hidden" name="theme" id="delete-theme-slug">
    </form>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('scripts'); ?>
    <script>
        // Custom File Input Display Name
        document.getElementById('theme_zip').addEventListener('change', function(e) {
            var fileName = e.target.files[0].name;
            var nextSibling = e.target.nextElementSibling;
            nextSibling.innerText = fileName;
        });

        // Theme Delete Confirmation
        function confirmDeleteTheme(slug) {
            Swal.fire({
                title: 'Are you sure?',
                text: "This will permanently delete the theme '" + slug + "' and all its assets. This action cannot be undone!",
                icon: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#dc3545',
                cancelButtonColor: '#6c757d',
                confirmButtonText: 'Yes, delete it!'
            }).then((result) => {
                if (result.isConfirmed) {
                    document.getElementById('delete-theme-slug').value = slug;
                    document.getElementById('delete-theme-form').submit();
                }
            });
        }
    </script>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('layouts.admin', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH /Users/mahmudul/Codecanyone/edu_ex_full/Eduex_laravel/resources/views/admin/frontend/settings/theme.blade.php ENDPATH**/ ?>