<x-app-layout>
    <x-slot name="header">
        <div class="d-flex flex-wrap align-items-center justify-content-between gap-2">
            <h2 class="h4 mb-0">Add OLT</h2>
            <a href="{{ route('admin.olts.index') }}" class="btn btn-outline-secondary">Back to OLT List</a>
        </div>
    </x-slot>

    <form method="POST" action="{{ route('admin.olts.store') }}" class="row g-4" autocomplete="off">
        @csrf
        <div class="col-xl-8">
            <div class="card border-0 shadow-sm mb-4">
                <div class="card-header bg-white py-3"><h3 class="h6 mb-0">OLT Information</h3></div>
                <div class="card-body">
                    <div class="row g-3">
                        <div class="col-md-6"><label class="form-label">OLT Name *</label><input name="name" value="{{ old('name') }}" class="form-control @error('name') is-invalid @enderror" required>@error('name')<div class="invalid-feedback">{{ $message }}</div>@enderror</div>
                        <div class="col-md-6"><label class="form-label">Brand *</label><select name="brand" class="form-select @error('brand') is-invalid @enderror" required><option value="">Select vendor</option>@foreach(['huawei'=>'Huawei','zte'=>'ZTE','vsol'=>'VSOL','bdcom'=>'BDCOM','cdata'=>'C-DATA','raisecom'=>'Raisecom','fiberhome'=>'FiberHome','other'=>'Other'] as $value=>$label)<option value="{{ $value }}" @selected(old('brand')===$value)>{{ $label }}</option>@endforeach</select>@error('brand')<div class="invalid-feedback">{{ $message }}</div>@enderror</div>
                        <div class="col-md-6"><label class="form-label">Model</label><input name="model" value="{{ old('model') }}" class="form-control"></div>
                        <div class="col-md-6"><label class="form-label">OLT / Public Forwarded Host *</label><input name="ip_address" value="{{ old('ip_address') }}" class="form-control @error('ip_address') is-invalid @enderror" placeholder="192.168.100.2 or 103.186.238.34">@error('ip_address')<div class="invalid-feedback">{{ $message }}</div>@enderror<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" class="form-select" required>@foreach(['snmp_telnet'=>'SNMP + Telnet','snmp'=>'SNMP only','telnet'=>'Telnet only','ssh'=>'SSH'] as $value=>$label)<option value="{{ $value }}" @selected(old('protocol','snmp_telnet')===$value)>{{ $label }}</option>@endforeach</select></div>
                        <div class="col-md-6"><label class="form-label">MikroTik</label><select name="mikrotik_id" class="form-select @error('mikrotik_id') is-invalid @enderror"><option value="">Not needed for manual forwarded endpoint</option>@foreach($mikrotiks as $mikrotik)<option value="{{ $mikrotik->id }}" @selected((string)old('mikrotik_id')===(string)$mikrotik->id)>{{ $mikrotik->name }} ({{ $mikrotik->ip_address }}) · {{ $mikrotik->connected ? 'Connected' : 'Offline' }}</option>@endforeach</select>@error('mikrotik_id')<div class="invalid-feedback">{{ $message }}</div>@enderror<div class="form-text">Select this only if automatic MikroTik port forwarding is needed.</div></div>
                    </div>
                </div>
            </div>

            <div class="card border-0 shadow-sm">
                <div class="card-header bg-white py-3"><h3 class="h6 mb-0">Access & Protocol</h3></div>
                <div class="card-body"><div class="row g-3">
                    <div class="col-md-6"><label class="form-label">Username</label><input name="username" value="{{ old('username') }}" class="form-control"></div>
                    <div class="col-md-6"><label class="form-label">Password</label><input type="password" name="password" class="form-control"></div>
                    <div class="col-md-6"><label class="form-label">Current SNMP Community</label><input name="community" value="{{ old('community') }}" class="form-control @error('community') is-invalid @enderror" autocomplete="new-password" placeholder="Currently configured community on the OLT">@error('community')<div class="invalid-feedback">{{ $message }}</div>@enderror<div class="form-text">Using the current community is recommended. If left blank, a secure value will be generated, but no write action will be sent to the OLT without a tested model profile.</div></div>
                    <div class="col-md-3"><label class="form-label">SNMP UDP Port *</label><input type="number" name="snmp_port" value="{{ old('snmp_port',161) }}" min="1" max="65535" class="form-control"><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" value="{{ old('telnet_port',23) }}" min="1" max="65535" class="form-control"><div class="form-text">Telnet/SSH port</div></div>
                    <div class="col-12"><label class="form-label">Remarks</label><textarea name="remarks" rows="3" class="form-control">{{ old('remarks') }}</textarea></div>
                </div></div>
            </div>
        </div>

        <div class="col-xl-4">
            <div class="card border-0 shadow-sm sticky-xl-top" style="top:90px">
                <div class="card-header bg-white py-3"><h3 class="h6 mb-0">Secure Port Forwarding</h3></div>
                <div class="card-body">
                    <div class="form-check form-switch app-toggle mb-3"><input type="hidden" name="auto_port_forward" value="0"><input class="form-check-input" type="checkbox" name="auto_port_forward" id="autoPortForward" value="1" @checked(old('auto_port_forward'))><label class="form-check-label fw-semibold" for="autoPortForward">Enable automatic forwarding</label></div>
                    <label class="form-label">Trusted Management Source IP</label><input name="management_source_ip" value="{{ old('management_source_ip') }}" class="form-control @error('management_source_ip') is-invalid @enderror" placeholder="Your software server public IP">@error('management_source_ip')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    <div class="alert alert-info small mt-3 mb-0">If a public forwarded IP:port already exists, keep this switch off. Turn it on only if MikroTik should create the rule automatically.</div>
                </div>
                <div class="card-footer bg-white p-3"><button class="btn btn-primary w-100 py-2">Save OLT & Start Connection Test</button></div>
            </div>
        </div>
    </form>
</x-app-layout>
