<?php $__env->startSection('title', 'Formulário Cadastrar Aplicação - ZEINER'); ?>

<?php $__env->startSection('content'); ?>
<div class="bg-gray-100">

    <?php
$__split = function ($name, $params = []) {
    return [$name, $params];
};
[$__name, $__params] = $__split('dashboard.client.header', []);

$key = null;

$key ??= \Livewire\Features\SupportCompiledWireKeys\SupportCompiledWireKeys::generateKey('lw-2574043696-0', null);

$__html = app('livewire')->mount($__name, $__params, $key);

echo $__html;

unset($__html);
unset($__name);
unset($__params);
unset($__split);
if (isset($__slots)) unset($__slots);
?>
    <?php if (isset($component)) { $__componentOriginal0333221d2829e137521a391cd9bef91c = $component; } ?>
<?php if (isset($attributes)) { $__attributesOriginal0333221d2829e137521a391cd9bef91c = $attributes; } ?>
<?php $component = Illuminate\View\AnonymousComponent::resolve(['view' => 'components.dashboard.admin-side-bar','data' => []] + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? $attributes->all() : [])); ?>
<?php $component->withName('dashboard.admin-side-bar'); ?>
<?php if ($component->shouldRender()): ?>
<?php $__env->startComponent($component->resolveView(), $component->data()); ?>
<?php if (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag): ?>
<?php $attributes = $attributes->except(\Illuminate\View\AnonymousComponent::ignoredParameterNames()); ?>
<?php endif; ?>
<?php $component->withAttributes([]); ?>
<?php echo $__env->renderComponent(); ?>
<?php endif; ?>
<?php if (isset($__attributesOriginal0333221d2829e137521a391cd9bef91c)): ?>
<?php $attributes = $__attributesOriginal0333221d2829e137521a391cd9bef91c; ?>
<?php unset($__attributesOriginal0333221d2829e137521a391cd9bef91c); ?>
<?php endif; ?>
<?php if (isset($__componentOriginal0333221d2829e137521a391cd9bef91c)): ?>
<?php $component = $__componentOriginal0333221d2829e137521a391cd9bef91c; ?>
<?php unset($__componentOriginal0333221d2829e137521a391cd9bef91c); ?>
<?php endif; ?>

    <main class="lg:ml-64 pt-15 p-6">

        <h1 class="text-3xl font-bold text-gray-700 mb-6">
            Adicionar Aplicação
        </h1>

        <div class="bg-white rounded-xl shadow-md p-6 max-w-4xl">

            <form id="app-upload-form"
                  action="<?php echo e(route('dashboard.admin.store.application')); ?>"
                  method="POST"
                  enctype="multipart/form-data">
                <?php echo csrf_field(); ?>

                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">

                    <div>
                        <label class="block text-sm font-medium text-gray-600 mb-1">Nome do Aplicativo</label>
                        <input type="text" name="app_name"
                               class="w-full px-3 py-2 rounded-md border border-gray-300 focus:outline-none focus:border-blue-400">
                        <span class="text-red-400 text-sm error-text" data-error="app_name"></span>
                    </div>

                    <div>
                        <label class="block text-sm font-medium text-gray-600 mb-1">Versão</label>
                        <input type="text" name="app_version"
                               class="w-full px-3 py-2 rounded-md border border-gray-300 focus:outline-none focus:border-blue-400">
                        <span class="text-red-400 text-sm error-text" data-error="app_version"></span>
                    </div>

                    <div>
                        <label class="block text-sm font-medium text-gray-600 mb-1">Formato</label>
                        <select name="app_format_type"
                           class="w-full px-3 py-2 bg-white rounded-md border border-gray-300 focus:outline-none focus:border-blue-400">
                            <option value="">Selecionar</option>
                            <option value="zip">zip</option>
                            <option value="exe">exe</option>
                            <option value="apk">apk</option>
                            <option value="msi">msi</option>
                            <option value="dmg">dmg</option>
                            <option value="deb">deb</option>
                            <option value="rpm">rpm</option>
                            <option value="gz">gz</option>
                            <option value="7z">7z</option>
                        </select>
                        <span class="text-red-400 text-sm error-text" data-error="app_format_type"></span>
                    </div>

                    <div>
                        <label class="block text-sm font-medium text-gray-600 mb-1">Tamanho (MB)</label>
                        <input type="number" name="app_size"
                               class="w-full px-3 py-2 rounded-md border border-gray-300 focus:outline-none focus:border-blue-400">
                        <span class="text-red-400 text-sm error-text" data-error="app_size"></span>
                    </div>

                    <div>
                        <label class="block text-sm font-medium text-gray-600 mb-1">Preço</label>
                        <input type="number" name="app_price"
                               class="w-full px-3 py-2 rounded-md border border-gray-300 focus:outline-none focus:border-blue-400">
                        <span class="text-red-400 text-sm error-text" data-error="app_price"></span>
                    </div>

                    <div>
                        <label class="block text-sm font-medium text-gray-600 mb-1">Duração de teste</label>
                        <select name="app_duration"  class="w-full px-3 py-2 bg-white rounded-md border border-gray-300 focus:outline-none focus:border-blue-400">
                            <option value="">Selecionar</option>
                            <option value="weekly">Semanal</option>
                            <option value="monthly">Mensal</option>
                            <option value="annual">Anual</option>
                        </select>
                        <span class="text-red-400 text-sm error-text" data-error="app_duration"></span>
                    </div>

                </div>

                <div class="mt-4">
                    <label class="block text-sm font-medium text-gray-600 mb-1">Descrição</label>
                    <textarea name="app_description" rows="4"
                              class="w-full px-3 py-2 rounded-md border border-gray-300 focus:outline-none focus:border-blue-400"></textarea>
                    <span class="text-red-400 text-sm error-text" data-error="app_description"></span>
                </div>

                <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mt-4">

                    <div>
                        <label class="block text-sm font-medium text-gray-600 mb-1">Logotipo</label>
                        <input type="file" name="app_logo"
                               class="w-full px-3 py-2 rounded-md border border-gray-300 focus:outline-none focus:border-blue-400">
                        <span class="text-red-400 text-sm error-text" data-error="app_logo"></span>
                    </div>

                    <div>
                        <label class="block text-sm font-medium text-gray-600 mb-1">Ficheiro</label>
                        <input type="file" name="app_address_link_to_download"
                               class="w-full px-3 py-2 rounded-md border border-gray-300 focus:outline-none focus:border-blue-400">
                        <span class="text-red-400 text-sm error-text" data-error="app_address_link_to_download"></span>
                    </div>

                </div>

                <div class="flex gap-2">
                    
                        <button type="submit"
                                class="mt-4 bg-blue-500 hover:bg-blue-600 text-white px-5 py-2 rounded-md font-semibold">
                            Salvar
                        </button>

                        <a href="<?php echo e(route('dashboard.admin.available.apps')); ?>"
                                class="mt-4 bg-gray-400 hover:bg-gray-500 text-white px-5 py-2 rounded-md font-semibold">
                            Listar
                        </a>
                </div>
               

            </form>
        </div>
    </main>
</div>
<?php $__env->stopSection(); ?>


<?php $__env->startPush('scripts'); ?>
<script>
document.addEventListener('DOMContentLoaded', function () {
    const form = document.getElementById('app-upload-form');
    const fileInput = form.querySelector('input[name="app_address_link_to_download"]');
    const logofileInput = form.querySelector('input[name="app_logo"]');

    function clearValidationErrors() {
        document.querySelectorAll('.error-text').forEach(el => {
            el.textContent = '';
        });
    }

    function showValidationErrors(errors) {
        clearValidationErrors();
        Object.keys(errors).forEach(field => {
            const span = document.querySelector(`[data-error="${field}"]`);
            if (span) {
                span.textContent = errors[field][0];
            }
        });
    }

    function clearFieldError(field) {
        const span = document.querySelector(`[data-error="${field.name}"]`);
        if (span) span.textContent = '';
    }

    
    form.querySelectorAll('input, select, textarea').forEach(field => { // Limpar erro individual ao digitar ou mudar select
        field.addEventListener('input', () => clearFieldError(field));
        field.addEventListener('change', () => clearFieldError(field));
    });

    form.addEventListener('submit', function (e) {
        e.preventDefault();
        clearValidationErrors();

        const formData = new FormData(form);
        const xhr = new XMLHttpRequest();
        xhr.open('POST', form.action, true);
        xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');

        xhr.onload = function () {
            let response;
            try {
                response = JSON.parse(xhr.responseText);
            } catch {
                Swal.fire({
                    icon: 'error',
                    title: 'Erro',
                    text: 'Resposta inválida do servidor'
                });
                return;
            }

            if (xhr.status === 200 && response.success) {
                Swal.fire({
                    icon: 'success',
                    title: response.title,
                    text: response.text
                });

                if (fileInput) fileInput.value = '';
                if (logofileInput) logofileInput.value = '';

                return;
            }

            if (xhr.status === 422 && response.errors) {
                showValidationErrors(response.errors);
                return;
            }

            Swal.fire({
                icon: 'error',
                title: 'Erro',
                text: response.text || 'Erro inesperado'
            });
        };

        xhr.onerror = function () {
            Swal.fire({
                icon: 'error',
                title: 'Erro de conexão',
                text: 'Não foi possível conectar ao servidor'
            });
        };

        xhr.send(formData);
    });
});
</script>

<?php $__env->stopPush(); ?>


<?php echo $__env->make('layouts.dashboard.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\www\sites\zeiner\zeiner-platform\resources\views\dashboard\forms\form-application.blade.php ENDPATH**/ ?>