<div>
    
    <div class="fixed top-0 left-0 right-0 w-full bg-white flex items-center justify-between p-4 z-50 border-b border-gray-200">
        <div class="flex items-center">
            <img src="<?php echo e(Storage::url('images_static/tresipunt-square.svg')); ?>" alt="Logo" class="w-10 h-10 mr-3">
            <h1 class="text-2xl font-bold">Editar archivo SCSS CDN</h1>
        </div>
        <div class="flex items-center gap-3">
            <a href="<?php echo e(route('products.scss-cdn.bundle.show', [$product, $bundle])); ?>"
                class="text-gray-700 hover:text-gray-900 px-4 py-2">
                Cancelar
            </a>
            <button type="submit" form="file-edit-form"
                    wire:loading.attr="disabled"
                    class="bg-3ip hover:bg-3ip-light text-white px-4 py-2 rounded-lg disabled:opacity-50 disabled:cursor-not-allowed">
                <span wire:loading.remove>Guardar cambios</span>
                <span wire:loading>Guardando...</span>
            </button>
        </div>
    </div>

    
    <div class="w-full px-20 pt-24 pb-8">
        <div class="mx-auto max-w-6xl">
            
            <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if(session()->has('success')): ?>
                <div class="mb-4 rounded bg-green-100 px-4 py-2 text-green-700">
                    <?php echo e(session('success')); ?>

                </div>
            <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>

            <?php if(session()->has('error')): ?>
                <div class="mb-4 rounded bg-red-100 px-4 py-2 text-red-700">
                    <?php echo e(session('error')); ?>

                </div>
            <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>

            
            <div class="w-full rounded-lg bg-white border border-gray-200 px-8 py-6 mb-6">
                <div class="mb-4">
                    <h3 class="text-lg font-semibold text-gray-900">Archivo: <?php echo e($file->relative_path); ?></h3>
                    <?php
                        $fileContent = \App\Services\ScssCdn\ScssCdnStorageService::getFile($product, $bundle, $file->relative_path);
                        $fileSize = $fileContent ? strlen($fileContent) : 0;
                    ?>
                    <p class="text-sm text-gray-600 mt-1">Tamaño: <?php echo e(number_format($fileSize, 0, ',', '.')); ?> bytes</p>
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">URL securizada:</label>
                    <div class="flex items-center gap-2">
                        <input type="text"
                               value="<?php echo e($fileUrl); ?>"
                               readonly
                               class="flex-1 rounded-lg border border-gray-200 px-4 py-2 bg-gray-50 font-mono text-sm">
                        <button onclick="navigator.clipboard.writeText('<?php echo e($fileUrl); ?>')"
                                class="rounded-lg bg-gray-600 px-4 py-2 text-white hover:bg-gray-700 text-sm font-semibold">
                            Copiar
                        </button>
                    </div>
                </div>
            </div>

            
            <div class="w-full rounded-lg bg-white border border-gray-200 px-8 py-6">
                <form wire:submit.prevent="save" id="file-edit-form">
                    <div class="mb-4">
                        <div class="flex items-center justify-between mb-2">
                            <label class="block text-sm font-medium text-gray-700">
                                Contenido SCSS
                            </label>
                            <button type="button"
                                    wire:click="validateScss"
                                    class="text-sm px-4 py-2 bg-gray-200 rounded-lg hover:bg-gray-300 font-semibold">
                                Validar SCSS
                            </button>
                        </div>

                        <div wire:ignore>
                            <textarea id="scss-editor"
                                      wire:model="content"
                                      class="w-full rounded-lg border border-gray-200 px-4 py-2 font-mono text-sm <?php $__errorArgs = ['content'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> border-red-500 <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>"
                                      rows="20"
                                      placeholder="Contenido SCSS..."></textarea>
                        </div>

                        <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if($scssValidationError): ?>
                            <p class="text-red-600 text-sm mt-1"><?php echo e($scssValidationError); ?></p>
                        <?php elseif($scssValid && !empty($content)): ?>
                            <p class="text-green-600 text-sm mt-1">✓ SCSS válido</p>
                        <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>

                        <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php $__errorArgs = ['content'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                            <p class="text-red-600 text-sm mt-1"><?php echo e($message); ?></p>
                        <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <script>
        let scssEditor = null;
        let updateTimeout = null;

        function initializeScssEditor() {
            const textarea = document.getElementById('scss-editor');
            if (!textarea) {
                return;
            }

            // Si ya existe un editor, destruirlo primero
            if (scssEditor) {
                try {
                    scssEditor.destroy();
                } catch (e) {
                    console.warn('Error al destruir editor anterior:', e);
                }
                scssEditor = null;
            }

            // Inicializar nuevo editor
            scssEditor = CodeMirror.fromTextArea(textarea, {
                theme: 'oneDark',
            });

            // Cargar contenido inicial
            scssEditor.setValue(<?php echo \Illuminate\Support\Js::from($content)->toHtml() ?>);

            // Sincronizar con Livewire (con debounce)
            scssEditor.on('change', function(cm) {
                if (updateTimeout) {
                    clearTimeout(updateTimeout);
                }
                updateTimeout = setTimeout(function() {
                    window.Livewire.find('<?php echo e($_instance->getId()); ?>').set('content', cm.getValue(), false);
                }, 300);
            });
        }

        function destroyScssEditor() {
            if (scssEditor) {
                try {
                    scssEditor.destroy();
                } catch (e) {
                    console.warn('Error al destruir editor:', e);
                }
                scssEditor = null;
            }
            if (updateTimeout) {
                clearTimeout(updateTimeout);
                updateTimeout = null;
            }
        }

        // Inicializar cuando el DOM esté listo
        document.addEventListener('DOMContentLoaded', function() {
            if (document.getElementById('scss-editor')) {
                initializeScssEditor();
            }
        });

        // Escuchar cambios de Livewire
        document.addEventListener('livewire:init', function() {
            // Escuchar cuando se actualiza el componente
            Livewire.hook('morph.updated', ({ el, component }) => {
                // Verificar si el textarea existe ahora
                const textarea = document.getElementById('scss-editor');
                if (textarea) {
                    // Si no hay editor o el textarea no está siendo usado por el editor, inicializar
                    if (!scssEditor || !textarea.previousElementSibling || !textarea.previousElementSibling.classList.contains('cm-editor')) {
                        setTimeout(initializeScssEditor, 150);
                    }
                } else {
                    // Si el textarea desapareció, destruir el editor
                    destroyScssEditor();
                }
            });
        });

        // También usar un observer para detectar cuando aparece el textarea
        if (typeof MutationObserver !== 'undefined') {
            const observer = new MutationObserver(function(mutations) {
                const textarea = document.getElementById('scss-editor');
                if (textarea && !scssEditor) {
                    // Verificar que el textarea esté visible
                    if (textarea.offsetParent !== null) {
                        setTimeout(initializeScssEditor, 100);
                    }
                }
            });

            // Observar cambios en el body
            document.addEventListener('DOMContentLoaded', function() {
                if (document.body) {
                    observer.observe(document.body, {
                        childList: true,
                        subtree: true,
                    });
                }
            });
        }
    </script>
</div><?php /**PATH /var/www/html/resources/views/livewire/scss-cdn/file-edit.blade.php ENDPATH**/ ?>