<?php $__env->startSection("title", env("APP_NAME") . " :: " . __("Editar Arquivo")); ?>
<?php $__env->startSection('content'); ?>
    <form id="repositorio-form" action="<?php echo e(route('admin.repositorio.update', $repository->id)); ?>" method="post" enctype="multipart/form-data">
        <div class="container">
            <div class="row">
                <div class="col-12">
                    <div class="card shadow">
                        <div class="card-header bg-primary text-white d-flex justify-content-between align-items-center">
                            <h5 class="my-2"><?php echo e(__("Editar Arquivo do Repositório")); ?></h5>
                        </div>
                        <div class="card-body">
                            <?php echo csrf_field(); ?>
                            <?php echo method_field('patch'); ?>
                            <?php echo $__env->make("errors.messageFlash", array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>

                            <div class="mb-3">
                                <input type="hidden" name="page" value="<?php echo e(request()->input('page', 1)); ?>">
                                <input type="hidden" name="name" value="">
                                <label for="title" class="form-label text-bold"><?php echo e(__("Título")); ?>: <span class="span-admin-create">*</span></label>
                                <input type="text" class="form-control <?php $__errorArgs = ['title'];
$__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); ?>" name="title" id="title" autocomplete="off" value="<?php echo e(old('title', $repository->title)); ?>">
                                <?php $__errorArgs = ['title'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                    <small class="text-danger text-bold"><?php echo e($message); ?></small>
                                <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                            </div>
                            <div class="mb-3 text-wrap">
                                <label for="summary" class="form-label text-bold"><?php echo e(__("Descrição sumária")); ?>:</label>
                                <textarea class="form-control <?php $__errorArgs = ['summary'];
$__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); ?>" name="summary" id="summary"><?php echo e(old('summary', $repository->summary)); ?></textarea>
                                <?php $__errorArgs = ['summary'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                    <small class="text-danger text-bold"><?php echo e($message); ?></small>
                                <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                            </div>
                            <div class="mb-3">
                                <label for="target" class="form-label text-bold"><?php echo e(__("Tipo de link")); ?> <span class="span-admin-create">*</span></label>
                                <select class="form-control <?php $__errorArgs = ['target'];
$__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); ?>" name="target" id="target">
                                    <?php $__currentLoopData = App\Enums\StatusLinkEnum::cases(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $status): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <?php if($repository->target == $status->name): ?>
                                            <option value="<?php echo e($status->name); ?>" selected="true"><?php echo e($status->value); ?></option>
                                        <?php else: ?>
                                            <option value="<?php echo e($status->name); ?>"><?php echo e($status->value); ?></option>
                                        <?php endif; ?>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </select>
                                <?php $__errorArgs = ['target'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                    <small class="text-danger text-bold"><?php echo e($message); ?></small>
                                <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                            </div>
                            <div class="mb-3">
                                <label for="url" class="form-label text-bold"><?php echo e(__("Arquivo")); ?></label>
                                <input type="file" class="form-control <?php $__errorArgs = ['url'];
$__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); ?>" name="url" id="url"/>
                                <?php if($repository->url): ?>
                                    <small class="text-muted"><?php echo e(__("Arquivo atual")); ?>:
                                        <a href="<?php echo e(asset($repository->url)); ?>" target="_blank"><?php echo e($repository->name); ?></a>
                                    </small>
                                <?php endif; ?>
                                <?php $__errorArgs = ['url'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                    <small class="text-danger text-bold"><?php echo e($message); ?></small>
                                <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                            </div>
                            <div class="mb-3">
                                <label for="status" class="form-label text-bold"><?php echo e(__("Status")); ?> <span class="span-admin-create">*</span></label>
                                <select class="form-control <?php $__errorArgs = ['status'];
$__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); ?>" name="status" id="status">
                                    <?php $__currentLoopData = App\Enums\StatusEnum::cases(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $status): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <?php if($repository->status == $status->name): ?>
                                            <option value="<?php echo e($status->name); ?>" selected="true"><?php echo e($status->value); ?></option>
                                        <?php else: ?>
                                            <option value="<?php echo e($status->name); ?>"><?php echo e($status->value); ?></option>
                                        <?php endif; ?>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </select>
                                <?php $__errorArgs = ['status'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                    <small class="text-danger text-bold"><?php echo e($message); ?></small>
                                <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                            </div>
                            <div id="repositorio-upload-progress" class="mb-3" style="display: none;">
                                <p id="repositorio-upload-status" class="text-muted mb-1"></p>
                                <div class="progress">
                                    <div id="repositorio-upload-progress-bar" class="progress-bar progress-bar-striped progress-bar-animated" role="progressbar" style="width: 0%" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100">0%</div>
                                </div>
                            </div>
                            <?php echo $__env->make('components.buttons._backAndSend', [
                                    'textBack' => __('Voltar'),
                                    'textSend' => __('Enviar'),
                                    'routeBack' => 'admin.repositorio.index'
                                ], array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
<?php $__env->startPush('scripts'); ?>
<script>
document.addEventListener('DOMContentLoaded', function() {
    var form = document.getElementById('repositorio-form');
    if (!form) return;
    var progressWrap = document.getElementById('repositorio-upload-progress');
    var progressBar = document.getElementById('repositorio-upload-progress-bar');
    var statusEl = document.getElementById('repositorio-upload-status');
    var submitBtn = form.querySelector('button[type="submit"]');
    
    function showError(message) {
        statusEl.textContent = message;
        statusEl.className = 'text-danger mb-1';
        progressWrap.style.display = 'block';
    }

    form.addEventListener('submit', function(e) {
        e.preventDefault();
        if (submitBtn.disabled) return;

        var formData = new FormData(form);
        var xhr = new XMLHttpRequest();
        var action = form.getAttribute('action');

        submitBtn.disabled = true;
        progressWrap.style.display = 'block';
        statusEl.className = 'text-muted mb-1';
        statusEl.textContent = '<?php echo e(__("Enviando… Para vídeos grandes (até 500MB) pode levar vários minutos. Não feche a página.")); ?>';
        progressBar.style.width = '0%';
        progressBar.textContent = '0%';
        progressBar.setAttribute('aria-valuenow', 0);

        xhr.upload.addEventListener('progress', function(ev) {
            if (ev.lengthComputable) {
                var pct = Math.round((ev.loaded / ev.total) * 100);
                progressBar.style.width = pct + '%';
                progressBar.textContent = pct + '%';
                progressBar.setAttribute('aria-valuenow', pct);
            }
        });

        xhr.addEventListener('load', function() {
            if (xhr.status === 422) {
                try {
                    var data = JSON.parse(xhr.responseText);
                    if (data.errors) {
                        for (var field in data.errors) {
                            var input = form.querySelector('[name="' + field + '"]');
                            var msg = data.errors[field][0];
                            if (input) {
                                input.classList.add('is-invalid');
                                var errEl = input.parentNode.querySelector('.invalid-feedback') || document.createElement('small');
                                if (!errEl.classList.contains('invalid-feedback')) {
                                    errEl.className = 'text-danger text-bold invalid-feedback d-block';
                                    input.parentNode.appendChild(errEl);
                                }
                                errEl.textContent = msg;
                            }
                        }
                        showError(data.message || '<?php echo e(__("Verifique os campos do formulário.")); ?>');
                    } else if (data.message) {
                        showError(data.message);
                    }
                } catch (e) {
                    showError('<?php echo e(__("Verifique os campos do formulário.")); ?>');
                }
                submitBtn.disabled = false;
                return;
            }
            if (xhr.status === 200) {
                try {
                    var data = JSON.parse(xhr.responseText);
                    if (data.message) {
                        sessionStorage.setItem('success_message', data.message);
                    }
                    if (data.redirect) {
                        window.location.href = data.redirect;
                        return;
                    }
                } catch (e) {
                    // Se não for JSON, tenta usar responseURL
                    if (xhr.responseURL && xhr.responseURL !== window.location.href) {
                        window.location.href = xhr.responseURL;
                        return;
                    }
                }
                // Fallback: volta para a listagem
                window.location.href = '<?php echo e(route("admin.repositorio.index")); ?>';
                return;
            }
            progressWrap.style.display = 'none';
            submitBtn.disabled = false;
            if (xhr.status >= 400 || xhr.status === 302 || xhr.status === 303) {
                var errorMessage = '<?php echo e(__("Ocorreu um erro ao enviar o arquivo. Por favor, tente novamente.")); ?>';
                try {
                    var contentType = xhr.getResponseHeader('Content-Type');
                    if (contentType && contentType.indexOf('application/json') !== -1) {
                        var errorData = JSON.parse(xhr.responseText);
                        if (errorData.message) {
                            errorMessage = errorData.message;
                        }
                    }
                } catch (e) {
                    // Mantém mensagem padrão
                }
                showError(errorMessage);
            }
        });

        xhr.addEventListener('error', function() {
            progressWrap.style.display = 'block';
            statusEl.textContent = '<?php echo e(__("Erro de conexão. Verifique sua rede e tente novamente.")); ?>';
            submitBtn.disabled = false;
        });

        xhr.open('POST', action);
        xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
        xhr.setRequestHeader('Accept', 'application/json');
        xhr.send(formData);
    });
});
</script>
<?php $__env->stopPush(); ?>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.admin.theme', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH /var/www/html/portal/resources/views/admin/repositorio/edit.blade.php ENDPATH**/ ?>