@extends('layouts.masterbeta')

@section('title', 'Cick ERP - MNO Partners')

@push('styles')
    <style>
        .avatar-circle { width: 38px; height: 38px; display: flex; align-items: center; justify-content: center; border-radius: 50%; font-weight: bold; font-size: 0.85rem; }
        .status-indicator { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
        .tech-badge { font-family: monospace; font-size: 0.75rem; background-color: #f1f3f9; color: #3e445b; padding: 2px 6px; border-radius: 4px; border: 1px solid #e2e8f0; }
    </style>
@endpush

@section('breadcrumbs')
    <a href="{{ url('/') }}" class="text-secondary text-decoration-none"><i class="bi bi-house me-2"></i></a>
    <i class="bi bi-chevron-right text-secondary me-2" style="font-size: 0.8rem;"></i>
    <a href="#" class="text-secondary text-decoration-none me-2" style="font-size: 0.95rem;">Core Entities</a>
    <i class="bi bi-chevron-right text-secondary me-2" style="font-size: 0.8rem;"></i>
    <span class="fw-bold" style="font-size: 0.95rem;">MNO Partners</span>
@endsection

@section('content')
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="fw-bold mb-1">Mobile Network Operators (MNO)</h4>
            <p class="text-secondary mb-0" style="font-size: 0.9rem;">Manage telecommunications carrier connections, signaling links, and gateways.</p>
        </div>
        <button class="btn text-white fw-bold d-flex align-items-center" style="background-color: #5c4df0; border-radius: 8px;" id="btnOpenMnoModal">
            <i class="bi bi-broadcast-pin me-2"></i> Add MNO Partner
        </button>
    </div>

    <div class="row g-4 mb-4">
        <div class="col-xl-3 col-md-6">
            <div class="content-card p-3 h-100 border-start border-4 border-0" style="border-left-color: #5c4df0 !important;">
                <div class="text-secondary fw-bold mb-1" style="font-size: 0.75rem; letter-spacing: 0.5px;">ACTIVE CONNECTIONS</div>
                <h3 class="fw-bold mb-0">4 <span class="fs-6 fw-normal text-success"><i class="bi bi-arrow-up"></i> 100% Up</span></h3>
            </div>
        </div>
        <div class="col-xl-3 col-md-6">
            <div class="content-card p-3 h-100 border-start border-4 border-0" style="border-left-color: #10b981 !important;">
                <div class="text-secondary fw-bold mb-1" style="font-size: 0.75rem; letter-spacing: 0.5px;">PROVISIONED SHORTCODES</div>
                <h3 class="fw-bold mb-0">12</h3>
            </div>
        </div>
        <div class="col-xl-3 col-md-6">
            <div class="content-card p-3 h-100 border-start border-4 border-0" style="border-left-color: #f59e0b !important;">
                <div class="text-secondary fw-bold mb-1" style="font-size: 0.75rem; letter-spacing: 0.5px;">TOTAL SIP TRUNKS</div>
                <h3 class="fw-bold mb-0">6 Links</h3>
            </div>
        </div>
        <div class="col-xl-3 col-md-6">
            <div class="content-card p-3 h-100 border-start border-4 border-0" style="border-left-color: #ef4444 !important;">
                <div class="text-secondary fw-bold mb-1" style="font-size: 0.75rem; letter-spacing: 0.5px;">AGGREGATED DAILY VOL</div>
                <h3 class="fw-bold mb-0">142.8k <span class="fs-6 fw-normal text-secondary" style="font-size:0.75rem !important;">hits</span></h3>
            </div>
        </div>
    </div>

    <div class="content-card">
        <div class="p-3 border-bottom bg-light bg-opacity-50">
            <div class="row g-3">
                <div class="col-md-6">
                    <div class="input-group">
                        <span class="input-group-text bg-white border-end-0 text-secondary"><i class="bi bi-search"></i></span>
                        <input type="text" class="form-control bg-white border-start-0 ps-0" placeholder="Search gateways, IPs, or partner networks...">
                    </div>
                </div>
                <div class="col-md-3">
                    <select class="form-select bg-white">
                        <option value="">All Regions</option>
                        <option value="GH">Ghana (GH)</option>
                        <option value="ZM">Zambia (ZM)</option>
                    </select>
                </div>
                <div class="col-md-3">
                    <select class="form-select bg-white">
                        <option value="">All Link Types</option>
                        <option value="vpn">IPsec VPN</option>
                        <option value="tailscale">Tailscale Mesh</option>
                        <option value="public">Whitelisted Public IP</option>
                    </select>
                </div>
            </div>
        </div>

        <div class="table-responsive">
            <table class="table table-custom table-hover mb-0">
                <thead>
                    <tr>
                        <th>Operator</th>
                        <th>Country</th>
                        <th>Network Codes</th>
                        <th>Infrastructure Links</th>
                        <th>Capabilities</th>
                        <th>Link Status</th>
                        <th class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>
                            <div class="d-flex align-items-center">
                                <div class="avatar-circle bg-warning bg-opacity-10 text-warning me-3">MTN</div>
                                <div>
                                    <div class="fw-bold text-dark">MTN Ghana</div>
                                    <div class="text-secondary" style="font-size: 0.8rem;">Scancom PLC</div>
                                </div>
                            </div>
                        </td>
                        <td><span class="fw-semibold">Ghana (GH)</span></td>
                        <td>
                            <span class="tech-badge">MCC: 620</span>
                            <span class="tech-badge">MNC: 01</span>
                        </td>
                        <td>
                            <div class="text-dark fw-semibold" style="font-size: 0.85rem;"><i class="bi bi-shield-lock me-1 text-primary"></i> IPsec VPN Tunnel</div>
                            <div class="text-secondary style" style="font-size: 0.75rem;">Gateway: 172.16.42.1</div>
                        </td>
                        <td>
                            <div class="d-flex gap-1 flex-wrap">
                                <span class="badge bg-secondary text-dark border bg-light" style="font-size: 0.65rem;">USSD</span>
                                <span class="badge bg-secondary text-dark border bg-light" style="font-size: 0.65rem;">SMS Gateway</span>
                                <span class="badge bg-secondary text-dark border bg-light" style="font-size: 0.65rem;">SIP Trunk</span>
                                <span class="badge bg-secondary text-dark border bg-light" style="font-size: 0.65rem;">MoMo API</span>
                            </div>
                        </td>
                        <td>
                            <span class="d-flex align-items-center gap-2">
                                <span class="status-indicator bg-success"></span>
                                <span class="fw-semibold text-success" style="font-size: 0.85rem;">Online</span>
                            </span>
                        </td>
                        <td class="text-end">
                            <button class="btn btn-sm btn-light text-secondary me-1"><i class="bi bi-terminal"></i></button>
                            <button class="btn btn-sm btn-light text-primary btn-edit-mno" 
                                data-id="1" data-name="MTN Ghana" data-company="Scancom PLC" data-country="GH" data-mcc="620" data-mnc="01" data-link="vpn" data-gateway="172.16.42.1" data-capabilities='["ussd","sms","voice","momo"]' data-status="online">
                                <i class="bi bi-pencil"></i>
                            </button>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <div class="d-flex align-items-center">
                                <div class="avatar-circle bg-danger bg-opacity-10 text-danger me-3">TL</div>
                                <div>
                                    <div class="fw-bold text-dark">Telecel Ghana</div>
                                    <div class="text-secondary" style="font-size: 0.8rem;">Telecel Group</div>
                                </div>
                            </div>
                        </td>
                        <td><span class="fw-semibold">Ghana (GH)</span></td>
                        <td>
                            <span class="tech-badge">MCC: 620</span>
                            <span class="tech-badge">MNC: 02</span>
                        </td>
                        <td>
                            <div class="text-dark fw-semibold" style="font-size: 0.85rem;"><i class="bi bi-shuffle me-1 text-info"></i> Tailscale Node</div>
                            <div class="text-secondary" style="font-size: 0.75rem;">IP: 100.115.2.4</div>
                        </td>
                        <td>
                            <div class="d-flex gap-1 flex-wrap">
                                <span class="badge bg-secondary text-dark border bg-light" style="font-size: 0.65rem;">USSD</span>
                                <span class="badge bg-secondary text-dark border bg-light" style="font-size: 0.65rem;">SMS Gateway</span>
                                <span class="badge bg-secondary text-dark border bg-light" style="font-size: 0.65rem;">MoMo API</span>
                            </div>
                        </td>
                        <td>
                            <span class="d-flex align-items-center gap-2">
                                <span class="status-indicator bg-success"></span>
                                <span class="fw-semibold text-success" style="font-size: 0.85rem;">Online</span>
                            </span>
                        </td>
                        <td class="text-end">
                            <button class="btn btn-sm btn-light text-secondary me-1"><i class="bi bi-terminal"></i></button>
                            <button class="btn btn-sm btn-light text-primary btn-edit-mno" 
                                data-id="2" data-name="Telecel Ghana" data-company="Telecel Group" data-country="GH" data-mcc="620" data-mnc="02" data-link="tailscale" data-gateway="100.115.2.4" data-capabilities='["ussd","sms","momo"]' data-status="online">
                                <i class="bi bi-pencil"></i>
                            </button>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
@endsection

@push('modals')
    <div class="modal fade" id="mnoModal" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog modal-lg modal-dialog-centered">
            <div class="modal-content">
                <div class="modal-header bg-light">
                    <h5 class="modal-title fw-bold" id="mnoModalTitle">Configure MNO Link</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                
                <form id="mnoForm">
                    <div class="modal-body p-4">
                        <input type="hidden" id="mnoId" name="id">
                        
                        <h6 class="fw-bold mb-3 text-secondary text-uppercase" style="font-size: 0.75rem;">Carrier Identity</h6>
                        <div class="row g-3 mb-4">
                            <div class="col-md-6">
                                <label class="form-label text-secondary fw-semibold" style="font-size: 0.85rem;">Brand Name *</label>
                                <input type="text" class="form-control" id="mnoName" name="name" required placeholder="e.g. MTN Ghana">
                            </div>
                            <div class="col-md-6">
                                <label class="form-label text-secondary fw-semibold" style="font-size: 0.85rem;">Registered Corporate Title</label>
                                <input type="text" class="form-control" id="mnoCompany" name="corporate_name" placeholder="e.g. Scancom PLC">
                            </div>
                            <div class="col-md-4">
                                <label class="form-label text-secondary fw-semibold" style="font-size: 0.85rem;">Operating Country *</label>
                                <select class="form-select" id="mnoCountry" name="country_code" required>
                                    <option value="GH">Ghana (GH)</option>
                                    <option value="ZM">Zambia (ZM)</option>
                                </select>
                            </div>
                            <div class="col-md-4">
                                <label class="form-label text-secondary fw-semibold" style="font-size: 0.85rem;">Mobile Country Code (MCC) *</label>
                                <input type="text" class="form-control" id="mnoMcc" name="mcc" required placeholder="e.g. 620">
                            </div>
                            <div class="col-md-4">
                                <label class="form-label text-secondary fw-semibold" style="font-size: 0.85rem;">Mobile Network Code (MNC) *</label>
                                <input type="text" class="form-control" id="mnoMnc" name="mnc" required placeholder="e.g. 01">
                            </div>
                        </div>

                        <h6 class="fw-bold mb-3 text-secondary text-uppercase" style="font-size: 0.75rem;">Network & Transport Configuration</h6>
                        <div class="row g-3 mb-4">
                            <div class="col-md-6">
                                <label class="form-label text-secondary fw-semibold" style="font-size: 0.85rem;">Interconnect Type</label>
                                <select class="form-select" id="mnoLinkType" name="link_type">
                                    <option value="vpn">IPsec VPN Tunnel</option>
                                    <option value="tailscale">Tailscale Overlaid Mesh</option>
                                    <option value="public">Whitelisted Public Endpoint</option>
                                </select>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label text-secondary fw-semibold" style="font-size: 0.85rem;">Remote Gateway IP / Endpoint Address</label>
                                <input type="text" class="form-control" id="mnoGateway" name="remote_gateway" placeholder="e.g. 172.16.42.1">
                            </div>
                        </div>

                        <h6 class="fw-bold mb-3 text-secondary text-uppercase" style="font-size: 0.75rem;">Provisioned Interface Frameworks</h6>
                        <div class="row g-3">
                            <div class="col-md-12">
                                <label class="form-label text-secondary fw-semibold d-block style" style="font-size: 0.85rem; margin-bottom: 0.5rem;">Enable Capabilities</label>
                                <div class="form-check form-check-inline me-4">
                                    <input class="form-check-input" type="checkbox" id="capUssd" name="capabilities[]" value="ussd">
                                    <label class="form-check-label text-dark" for="capUssd">USSD Gateway (SIGTRAN/M3UA)</label>
                                </div>
                                <div class="form-check form-check-inline me-4">
                                    <input class="form-check-input" type="checkbox" id="capSms" name="capabilities[]" value="sms">
                                    <label class="form-check-label text-dark" for="capSms">SMS SMPP Bindings</label>
                                </div>
                                <div class="form-check form-check-inline me-4">
                                    <input class="form-check-input" type="checkbox" id="capVoice" name="capabilities[]" value="voice">
                                    <label class="form-check-label text-dark" for="capVoice">SIP Voice Trunking</label>
                                </div>
                                <div class="form-check form-check-inline">
                                    <input class="form-check-input" type="checkbox" id="capMomo" name="capabilities[]" value="momo">
                                    <label class="form-check-label text-dark" for="capMomo">Mobile Money Node</label>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer bg-light">
                        <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel</button>
                        <button type="submit" class="btn text-white fw-bold px-4" style="background-color: #5c4df0;" id="btnSubmitMno">
                            Save Gateway Profile
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
@endpush

@push('scripts')
    <script>
        $(document).ready(function() {
            const mnoModal = new bootstrap.Modal(document.getElementById('mnoModal'));
            const $form = $('#mnoForm');

            // --- OPEN MODAL FOR CREATE ---
            $('#btnOpenMnoModal').on('click', function() {
                $form[0].reset();
                $('#mnoId').val('');
                $('#mnoModalTitle').text('Configure New MNO Interconnect');
                $('#btnSubmitMno').html('<i class="bi bi-save me-2"></i>Save Gateway Profile');
                mnoModal.show();
            });

            // --- OPEN MODAL FOR EDIT ---
            $('.btn-edit-mno').on('click', function() {
                const data = $(this).data();
                
                $form[0].reset();
                $('#mnoId').val(data.id);
                $('#mnoName').val(data.name);
                $('#mnoCompany').val(data.company);
                $('#mnoCountry').val(data.country);
                $('#mnoMcc').val(data.mcc);
                $('#mnoMnc').val(data.mnc);
                $('#mnoLinkType').val(data.link);
                $('#mnoGateway').val(data.gateway);
                
                // Clear and map checkmarks safely
                $('input[name="capabilities[]"]').prop('checked', false);
                if (data.capabilities && Array.isArray(data.capabilities)) {
                    data.capabilities.forEach(function(cap) {
                        $('input[value="' + cap + '"]').prop('checked', true);
                    });
                }

                $('#mnoModalTitle').text('Modify Link Profiles: ' + data.name);
                $('#btnSubmitMno').html('<i class="bi bi-check-circle me-2"></i>Apply Changes');
                
                mnoModal.show();
            });

            // --- AJAX FORM SUBMISSION SIMULATION ---
            $form.on('submit', function(e) {
                e.preventDefault();
                const $btn = $('#btnSubmitMno');
                const originalText = $btn.html();
                
                $btn.html('<span class="spinner-border spinner-border-sm me-2"></span> Updating Trunk Contexts...');
                $btn.prop('disabled', true);

                setTimeout(() => {
                    console.log('MNO Payload Synchronized:', $form.serializeArray());
                    $btn.html(originalText).prop('disabled', false);
                    mnoModal.hide();
                    alert('Gateway routing map refreshed successfully.');
                }, 900);
            });
        });
    </script>
@endpush