<?php $__env->startSection('title', 'Provision router'); ?>

<?php $__env->startSection('header'); ?>
    <?php if (isset($component)) { $__componentOriginal111218edafe37a3ff745a947bea8c7e5 = $component; } ?>
<?php if (isset($attributes)) { $__attributesOriginal111218edafe37a3ff745a947bea8c7e5 = $attributes; } ?>
<?php $component = Illuminate\View\AnonymousComponent::resolve(['view' => 'components.isp-page-header','data' => ['title' => 'Provision router','module' => 'Network','description' => 'Add a MikroTik device to the fleet. The first telemetry poll is queued automatically after a successful connection test.']] + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? $attributes->all() : [])); ?>
<?php $component->withName('isp-page-header'); ?>
<?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(['title' => 'Provision router','module' => 'Network','description' => 'Add a MikroTik device to the fleet. The first telemetry poll is queued automatically after a successful connection test.']); ?>
         <?php $__env->slot('actions', null, []); ?> 
            <a href="<?php echo e(route('isp.routers.index')); ?>" class="pb-btn">Back to fleet</a>
         <?php $__env->endSlot(); ?>
     <?php echo $__env->renderComponent(); ?>
<?php endif; ?>
<?php if (isset($__attributesOriginal111218edafe37a3ff745a947bea8c7e5)): ?>
<?php $attributes = $__attributesOriginal111218edafe37a3ff745a947bea8c7e5; ?>
<?php unset($__attributesOriginal111218edafe37a3ff745a947bea8c7e5); ?>
<?php endif; ?>
<?php if (isset($__componentOriginal111218edafe37a3ff745a947bea8c7e5)): ?>
<?php $component = $__componentOriginal111218edafe37a3ff745a947bea8c7e5; ?>
<?php unset($__componentOriginal111218edafe37a3ff745a947bea8c7e5); ?>
<?php endif; ?>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('content'); ?>
    <form method="POST" action="<?php echo e(route('isp.routers.store')); ?>" id="routerForm" class="space-y-6">
        <?php echo csrf_field(); ?>

        <?php echo $__env->make('isp.routers.partials.form', ['router' => null, 'owners' => $owners], array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>

        <div class="pb-card p-4 sm:p-5">
            <div class="flex flex-wrap items-center justify-between gap-3">
                <div>
                    <h3 class="text-[11px] font-extrabold uppercase tracking-[0.16em] text-slate-500 dark:text-slate-400">Test connection</h3>
                    <p class="mt-1 text-xs text-slate-500 dark:text-slate-400">Validates credentials without saving. Saved routers are polled every minute automatically.</p>
                </div>
                <button type="button" id="testConnectionBtn" class="pb-btn pb-btn-primary">Test connection</button>
            </div>
            <div id="connectionStatus" class="mt-4 hidden"></div>
        </div>

        <div class="flex justify-end gap-3">
            <a href="<?php echo e(route('isp.routers.index')); ?>" class="pb-btn">Cancel</a>
            <button type="submit" class="pb-btn pb-btn-primary">Save router</button>
        </div>
    </form>

    <script>
        document.getElementById('testConnectionBtn').addEventListener('click', function () {
            const btn = this;
            const statusDiv = document.getElementById('connectionStatus');

            const payload = {
                ip_address: document.getElementById('ip_address').value,
                port: document.getElementById('port').value,
                username: document.getElementById('username').value,
                password: document.getElementById('password').value,
                use_ssl: document.querySelector('[name="use_ssl"]:checked') ? true : false,
            };

            if (!payload.ip_address || !payload.port || !payload.username || !payload.password) {
                statusDiv.classList.remove('hidden');
                statusDiv.innerHTML = '<div class="rounded-xl border border-amber-300/60 bg-amber-50 px-4 py-3 text-sm text-amber-800 dark:bg-amber-500/10 dark:text-amber-100">Fill in IP, port, username, and password before testing.</div>';
                return;
            }

            btn.disabled = true;
            const originalLabel = btn.textContent;
            btn.textContent = 'Testing...';
            statusDiv.classList.remove('hidden');
            statusDiv.innerHTML = '<div class="rounded-xl border border-indigo-300/60 bg-indigo-50 px-4 py-3 text-sm text-indigo-800 dark:bg-indigo-500/10 dark:text-indigo-100">Opening RouterOS API session...</div>';

            fetch('<?php echo e(route('isp.routers.test-connection')); ?>', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
                },
                body: JSON.stringify(payload),
            })
                .then((r) => r.json())
                .then((data) => {
                    btn.disabled = false;
                    btn.textContent = originalLabel;
                    if (data.success) {
                        const count = data.hotspot_servers_count || 0;
                        statusDiv.innerHTML = `<div class="rounded-xl border border-emerald-300/60 bg-emerald-50 px-4 py-3 text-sm text-emerald-800 dark:bg-emerald-500/10 dark:text-emerald-100">
                            <p class="font-semibold">Connection OK.</p>
                            <p>${count} hotspot server(s) detected. They will be imported automatically on save.</p>
                        </div>`;
                    } else {
                        statusDiv.innerHTML = `<div class="rounded-xl border border-rose-300/60 bg-rose-50 px-4 py-3 text-sm text-rose-800 dark:bg-rose-500/10 dark:text-rose-100">
                            <p class="font-semibold">Connection failed.</p>
                            <p class="font-mono text-xs mt-1">${data.error || 'Unknown error'}</p>
                        </div>`;
                    }
                })
                .catch((err) => {
                    btn.disabled = false;
                    btn.textContent = originalLabel;
                    statusDiv.innerHTML = `<div class="rounded-xl border border-rose-300/60 bg-rose-50 px-4 py-3 text-sm text-rose-800 dark:bg-rose-500/10 dark:text-rose-100">Request error: ${err}</div>`;
                });
        });
    </script>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.admin', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\xampp\htdocs\ispbilling\resources\views/isp/routers/create.blade.php ENDPATH**/ ?>