<?php if (isset($component)) { $__componentOriginal9ac128a9029c0e4701924bd2d73d7f54 = $component; } ?>
<?php if (isset($attributes)) { $__attributesOriginal9ac128a9029c0e4701924bd2d73d7f54 = $attributes; } ?>
<?php $component = App\View\Components\AppLayout::resolve([] + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? $attributes->all() : [])); ?>
<?php $component->withName('app-layout'); ?>
<?php if ($component->shouldRender()): ?>
<?php $__env->startComponent($component->resolveView(), $component->data()); ?>
<?php if (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag): ?>
<?php $attributes = $attributes->except(\App\View\Components\AppLayout::ignoredParameterNames()); ?>
<?php endif; ?>
<?php $component->withAttributes([]); ?>
     <?php $__env->slot('header', null, []); ?> 
        <div class="d-flex align-items-center justify-content-between">
            <h2 class="h4 mb-0">OLT List</h2>
            <div>
                <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('olt.create')): ?>
                    <a href="<?php echo e(route('admin.olts.create')); ?>" class="btn btn-primary">Add OLT</a>
                <?php endif; ?>
            </div>
        </div>
     <?php $__env->endSlot(); ?>

    <div class="card">
        <div class="card-body">
            <div class="table-responsive">
                <table id="oltsTable" class="table table-striped table-hover align-middle" style="width:100%">
                    <thead>
                        <tr>
                            <th>Status</th>
                            <th>Name</th>
                            <th>Brand</th>
                            <th>Model</th>
                            <th>IP</th>
                            <th>MikroTik</th>
                            <th>Port Forward</th>
                            <th>Connection</th>
                            <th>Gateway</th>
                            <th>Last Check</th>
                            <th class="text-end">Actions</th>
                        </tr>
                    </thead>
                </table>
            </div>
        </div>
    </div>

    <!-- Modal -->
    <div class="modal fade" id="oltModal" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog modal-lg modal-dialog-scrollable">
            <div class="modal-content">
                <form id="oltForm">
                    <div class="modal-header">
                        <h5 class="modal-title">Edit OLT</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body p-3">
                        <input type="hidden" id="olt_id" name="olt_id" />

                        <div class="row g-3">
                            <div class="col-md-6">
                                <label class="form-label">OLT Name</label>
                                <input type="text" name="name" id="name" class="form-control" required>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label">Brand</label>
                                <select name="brand" id="brand" class="form-select" required><option value="">Select vendor</option><option value="huawei">Huawei</option><option value="zte">ZTE</option><option value="vsol">VSOL</option><option value="bdcom">BDCOM</option><option value="cdata">C-DATA</option><option value="raisecom">Raisecom</option><option value="fiberhome">FiberHome</option><option value="other">Other</option></select>
                            </div>

                            <div class="col-md-6">
                                <label class="form-label">Model</label>
                                <input type="text" name="model" id="model" class="form-control">
                            </div>

                            <div class="col-md-6">
                                <label class="form-label">OLT / Public Forwarded Host</label>
                                <input type="text" name="ip_address" id="ip_address" class="form-control" placeholder="192.168.100.2 or 103.186.238.34">
                                <div class="form-text">Enter the direct OLT IP or an already forwarded public IP/domain.</div>
                            </div>
                            <div class="col-md-6"><label class="form-label">Management Protocol</label><select name="protocol" id="protocol" class="form-select" required><option value="snmp_telnet">SNMP + Telnet</option><option value="snmp">SNMP only</option><option value="telnet">Telnet only</option><option value="ssh">SSH</option></select></div>

                            <div class="col-md-6">
                                <label class="form-label">Username</label>
                                <input type="text" name="username" id="username" class="form-control">
                            </div>

                            <div class="col-md-6">
                                <label class="form-label">Password</label>
                                <input type="password" name="password" id="password" class="form-control">
                            </div>

                            <div class="col-md-6">
                                <label class="form-label">SNMP Community</label>
                                <input type="text" name="community" id="community" class="form-control">
                            </div>

                            <div class="col-md-3">
                                <label class="form-label">SNMP UDP Port</label>
                                <input type="number" name="snmp_port" id="snmp_port" class="form-control" value="161">
                                <div class="form-text">Example: 5002 (UDP)</div>
                            </div>

                            <div class="col-md-3">
                                <label class="form-label">Management Port</label>
                                <input type="number" name="telnet_port" id="telnet_port" class="form-control" value="23">
                                <div class="form-text">Telnet/SSH port</div>
                            </div>

                            <div class="col-md-6">
                                <label class="form-label">MikroTik</label>
                                <select name="mikrotik_id" id="mikrotik_id" class="form-select">
                                    <option value="">Not needed for manual forwarded endpoint</option>
                                    <?php $__currentLoopData = $mikrotiks; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $m): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <option value="<?php echo e($m->id); ?>"><?php echo e($m->name); ?> (<?php echo e($m->ip_address); ?>) · <?php echo e($m->connected ? 'Connected' : 'Offline'); ?></option>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </select>
                            </div>

                            <div class="col-12">
                                <div class="border rounded-3 p-3 bg-light">
                                    <div class="form-check form-switch app-toggle"><input class="form-check-input" type="checkbox" value="1" name="auto_port_forward" id="auto_port_forward"><label class="form-check-label fw-semibold" for="auto_port_forward">Secure automatic port forwarding</label></div>
                                    <p class="small text-muted mb-2 mt-1">Off by default. OLT access will be allowed only from the trusted management server IP.</p>
                                    <label class="form-label">Trusted management source IP</label>
                                    <input type="text" class="form-control" name="management_source_ip" id="management_source_ip" placeholder="Software server public IP">
                                    <div class="form-text">No open rule will be created for everyone.</div>
                                </div>
                            </div>

                            <div class="col-md-12">
                                <label class="form-label">Remarks</label>
                                <textarea name="remarks" id="remarks" class="form-control"></textarea>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                        <button type="submit" id="saveOltBtn" class="btn btn-primary">Save</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <?php $__env->startPush('scripts'); ?>
    <script>
        document.addEventListener('DOMContentLoaded', function(){
            const oltsTable = $('#oltsTable').DataTable({
                processing: true,
                serverSide: true,
                ajax: '<?php echo route('admin.olts.data'); ?>',
                columns: [
                    { data: 'status_label', name: 'status_label', orderable:false, searchable:false },
                    { data: 'name', name: 'name' },
                    { data: 'brand', name: 'brand' },
                    { data: 'model', name: 'model' },
                    { data: 'ip_address', name: 'ip_address' },
                    { data: 'mikrotik_name', name: 'mikrotik_name', orderable:false, searchable:false },
                    { data: 'port_forward_status_label', name: 'port_forward_status_label', orderable:false, searchable:false },
                    { data: 'connected_label', name: 'connected_label', orderable:false, searchable:false },
                    { data: 'gateway_label', name: 'gateway_label', orderable:false, searchable:false },
                    { data: 'last_checked', name: 'last_checked' },
                    { data: 'actions', name: 'actions', orderable:false, searchable:false },
                ],
                drawCallback: function () {
                    if (window.feather) window.feather.replace();
                }
            });

            const oltModal = new bootstrap.Modal(document.getElementById('oltModal'));

            $('#oltsTable').on('click', '.edit-olt', function(){
                const id = $(this).data('id');
                axios.get(`<?php echo e(url('admin/olts')); ?>/${id}/edit`).then(res=>{
                    const data = res.data.data;
                    $('#olt_id').val(data.id);
                    $('#name').val(data.name);
                    $('#brand').val(data.brand);
                    $('#model').val(data.model);
                    $('#protocol').val(data.protocol || 'snmp_telnet');
                    $('#ip_address').val(data.ip_address);
                    $('#username').val(data.username);
                    $('#password').val('');
                    $('#community').val(data.community);
                    $('#snmp_port').val(data.snmp_port);
                    $('#telnet_port').val(data.telnet_port);
                    $('#mikrotik_id').val(data.mikrotik_id);
                    $('#auto_port_forward').prop('checked', !!data.auto_port_forward);
                    $('#management_source_ip').val(data.management_source_ip);
                    $('#remarks').val(data.remarks);
                    $('#oltModal .modal-title').text('Edit OLT');
                    oltModal.show();
                });
            });

            $('#oltsTable').on('click', '.delete-olt', function(){
                const id = $(this).data('id');
                Swal.fire({title:'Delete?', text:'Are you sure?', icon:'warning', showCancelButton:true}).then(r=>{
                    if(!r.isConfirmed) return;
                    axios.delete(`<?php echo e(url('admin/olts')); ?>/${id}`).then(()=> oltsTable.ajax.reload(null,false)).catch(error=>Swal.fire('Delete blocked',error.response?.data?.message||'Unable to delete OLT.','error'));
                });
            });

            $('#oltForm').on('submit', function(e){
                e.preventDefault();
                const id = $('#olt_id').val();
                const payload = {
                    name: $('#name').val(),
                    brand: $('#brand').val(),
                    model: $('#model').val(),
                    protocol: $('#protocol').val(),
                    ip_address: $('#ip_address').val(),
                    username: $('#username').val(),
                    password: $('#password').val(),
                    community: $('#community').val(),
                    snmp_port: $('#snmp_port').val(),
                    telnet_port: $('#telnet_port').val(),
                    mikrotik_id: $('#mikrotik_id').val(),
                    auto_port_forward: $('#auto_port_forward').is(':checked') ? 1 : 0,
                    management_source_ip: $('#management_source_ip').val(),
                    remarks: $('#remarks').val(),
                };
                const url = id ? `<?php echo e(url('admin/olts')); ?>/${id}` : '<?php echo route('admin.olts.store'); ?>';
                const method = id ? 'put' : 'post';
                axios({method, url, data: payload}).then(()=>{
                    oltModal.hide();
                    oltsTable.ajax.reload(null,false);
                }).catch(err=>{
                    console.error(err);
                    Swal.fire('Error','Failed to save OLT','error');
                });
            });

            // Test connection
            $('#oltsTable').on('click', '.test-olt', function(){
                const id = $(this).data('id');
                axios.post(`<?php echo e(url('admin/olts')); ?>/${id}/test-connection`).then(()=>{
                    Swal.fire('Done','Live OLT data synced successfully','success');
                    oltsTable.ajax.reload(null,false);
                }).catch(()=> Swal.fire('Error','Unable to dispatch','error'));
            });
        });
    </script>
    <?php $__env->stopPush(); ?>
 <?php echo $__env->renderComponent(); ?>
<?php endif; ?>
<?php if (isset($__attributesOriginal9ac128a9029c0e4701924bd2d73d7f54)): ?>
<?php $attributes = $__attributesOriginal9ac128a9029c0e4701924bd2d73d7f54; ?>
<?php unset($__attributesOriginal9ac128a9029c0e4701924bd2d73d7f54); ?>
<?php endif; ?>
<?php if (isset($__componentOriginal9ac128a9029c0e4701924bd2d73d7f54)): ?>
<?php $component = $__componentOriginal9ac128a9029c0e4701924bd2d73d7f54; ?>
<?php unset($__componentOriginal9ac128a9029c0e4701924bd2d73d7f54); ?>
<?php endif; ?>
<?php /**PATH D:\herd\billing\laravel-app\resources\views/admin/mikrotiks/olts/index.blade.php ENDPATH**/ ?>