@extends('layouts.masterbeta')

@section('title', 'Nexus ERP - Short Codes')

@push('styles')
    <style>
        .network-badge { font-size: 0.65rem; padding: 0.2rem 0.4rem; border-radius: 4px; border: 1px solid #e2e8f0; font-weight: 600; display: inline-flex; align-items: center; gap: 3px; }
        .net-approved { background-color: rgba(16, 185, 129, 0.1); color: #10b981; border-color: rgba(16, 185, 129, 0.2); }
        .net-pending { background-color: rgba(245, 158, 11, 0.1); color: #f59e0b; border-color: rgba(245, 158, 11, 0.2); }
        
        .code-text { font-family: 'Courier New', Courier, monospace; font-size: 1.2rem; font-weight: 700; color: #111425; letter-spacing: 1px; }
        
        .cat-badge { display: inline-flex; align-items: center; justify-content: center; width: 28px; height: 28px; border-radius: 6px; margin-right: 10px; font-size: 1rem; }
        .cat-sms { background-color: rgba(59, 130, 246, 0.1); color: #3b82f6; }
        .cat-ussd { background-color: rgba(139, 92, 246, 0.1); color: #8b5cf6; }
        .cat-voice { background-color: rgba(16, 185, 129, 0.1); color: #10b981; }
    </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;">Operations</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;">Short Codes</span>
@endsection

@section('content')
    <!-- Page Header -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="fw-bold mb-1">Short Code Management</h4>
            <p class="text-secondary mb-0" style="font-size: 0.9rem;">Provision and manage dedicated/shared codes for SMS, USSD, and Voice routing.</p>
        </div>
        <button class="btn text-white fw-bold d-flex align-items-center" style="background-color: #5c4df0; border-radius: 8px;" id="btnOpenCodeModal">
            <i class="bi bi-plus-lg me-2"></i> Provision Short Code
        </button>
    </div>

    <!-- KPIs -->
    <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: #4b5563 !important;">
                <div class="text-secondary fw-bold mb-1" style="font-size: 0.75rem; letter-spacing: 0.5px;">TOTAL CODES</div>
                <h3 class="fw-bold mb-0">34</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: #3b82f6 !important;">
                <div class="text-secondary fw-bold mb-1" style="font-size: 0.75rem; letter-spacing: 0.5px;">ACTIVE SMS CODES</div>
                <h3 class="fw-bold mb-0">18</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: #8b5cf6 !important;">
                <div class="text-secondary fw-bold mb-1" style="font-size: 0.75rem; letter-spacing: 0.5px;">ACTIVE USSD CODES</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: #10b981 !important;">
                <div class="text-secondary fw-bold mb-1" style="font-size: 0.75rem; letter-spacing: 0.5px;">ACTIVE VOICE CODES</div>
                <h3 class="fw-bold mb-0">4</h3>
            </div>
        </div>
    </div>

    <!-- Data Table -->
    <div class="content-card">
        <div class="p-3 border-bottom bg-light bg-opacity-50">
            <div class="row g-3">
                <div class="col-md-4">
                    <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 by code or client...">
                    </div>
                </div>
                <div class="col-md-3">
                    <select class="form-select bg-white">
                        <option value="">All Categories</option>
                        <option value="sms">SMS Only</option>
                        <option value="ussd">USSD</option>
                        <option value="voice">Voice / IVR</option>
                    </select>
                </div>
                <div class="col-md-3">
                    <select class="form-select bg-white">
                        <option value="">Billing: All</option>
                        <option value="standard">Standard Rated</option>
                        <option value="reverse">Reverse Billed (Toll-Free)</option>
                        <option value="premium">Premium Rated</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <button class="btn btn-secondary w-100">Filter</button>
                </div>
            </div>
        </div>

        <div class="table-responsive">
            <table class="table table-custom table-hover mb-0">
                <thead>
                    <tr>
                        <th>Short Code</th>
                        <th>Category</th>
                        <th>Client / Assignment</th>
                        <th>Billing Type</th>
                        <th>Network Provisioning</th>
                        <th class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <!-- Row 1: SMS -->
                    <tr>
                        <td>
                            <div class="d-flex align-items-center">
                                <div class="cat-badge cat-sms"><i class="bi bi-chat-text-fill"></i></div>
                                <div>
                                    <div class="code-text">1334</div>
                                    <div class="text-secondary mt-1" style="font-size: 0.75rem;">Dedicated</div>
                                </div>
                            </div>
                        </td>
                        <td><span class="fw-semibold text-dark">SMS</span></td>
                        <td>
                            <div class="fw-semibold" style="font-size: 0.85rem;">Click Tech Corp</div>
                            <div class="text-secondary text-truncate" style="font-size: 0.75rem; max-width: 150px;" title="https://api.clicktech.com/webhook/sms">https://api.clicktech.com/...</div>
                        </td>
                        <td><span class="badge bg-secondary bg-opacity-10 text-secondary border">Standard</span></td>
                        <td>
                            <div class="d-flex gap-1 flex-wrap">
                                <span class="network-badge net-approved"><i class="bi bi-check-circle-fill"></i> MTN</span>
                                <span class="network-badge net-approved"><i class="bi bi-check-circle-fill"></i> Telecel</span>
                                <span class="network-badge net-approved"><i class="bi bi-check-circle-fill"></i> AT</span>
                            </div>
                        </td>
                        <td class="text-end">
                            <button class="btn btn-sm btn-light text-primary me-1 btn-edit-code" 
                                data-id="1" data-code="1334" data-category="sms" data-type="dedicated" data-client="client_1" data-billing="standard" data-webhook="https://api.clicktech.com/webhook/sms">
                                <i class="bi bi-pencil"></i>
                            </button>
                        </td>
                    </tr>
                    
                    <!-- Row 2: USSD -->
                    <tr>
                        <td>
                            <div class="d-flex align-items-center">
                                <div class="cat-badge cat-ussd"><i class="bi bi-phone-fill"></i></div>
                                <div>
                                    <div class="code-text">*711*50#</div>
                                    <div class="text-secondary mt-1" style="font-size: 0.75rem;">Extension (Shared)</div>
                                </div>
                            </div>
                        </td>
                        <td><span class="fw-semibold text-dark">USSD</span></td>
                        <td>
                            <div class="fw-semibold" style="font-size: 0.85rem;">Alpha Telecom</div>
                            <div class="text-secondary text-truncate" style="font-size: 0.75rem; max-width: 150px;" title="https://ussd.alphatel.com/callback">https://ussd.alphatel.com/...</div>
                        </td>
                        <td><span class="badge bg-danger bg-opacity-10 text-danger border border-danger-subtle">Reverse Billed</span></td>
                        <td>
                            <div class="d-flex gap-1 flex-wrap">
                                <span class="network-badge net-approved"><i class="bi bi-check-circle-fill"></i> MTN</span>
                                <span class="network-badge net-pending"><i class="bi bi-clock-fill"></i> Telecel</span>
                            </div>
                        </td>
                        <td class="text-end">
                            <button class="btn btn-sm btn-light text-primary me-1 btn-edit-code"
                                data-id="2" data-code="*711*50#" data-category="ussd" data-type="shared" data-client="client_2" data-billing="reverse" data-webhook="https://ussd.alphatel.com/callback">
                                <i class="bi bi-pencil"></i>
                            </button>
                        </td>
                    </tr>

                    <!-- Row 3: Voice -->
                    <tr>
                        <td>
                            <div class="d-flex align-items-center">
                                <div class="cat-badge cat-voice"><i class="bi bi-mic-fill"></i></div>
                                <div>
                                    <div class="code-text">3001</div>
                                    <div class="text-secondary mt-1" style="font-size: 0.75rem;">Dedicated IVR</div>
                                </div>
                            </div>
                        </td>
                        <td><span class="fw-semibold text-dark">Voice</span></td>
                        <td>
                            <div class="fw-semibold" style="font-size: 0.85rem;">Internal Customer Service</div>
                            <div class="text-secondary text-truncate" style="font-size: 0.75rem; max-width: 150px;">SIP: trunk.nexus.local</div>
                        </td>
                        <td><span class="badge bg-secondary bg-opacity-10 text-secondary border">Standard</span></td>
                        <td>
                            <div class="d-flex gap-1 flex-wrap">
                                <span class="network-badge net-approved"><i class="bi bi-check-circle-fill"></i> MTN</span>
                            </div>
                        </td>
                        <td class="text-end">
                            <button class="btn btn-sm btn-light text-primary me-1 btn-edit-code"
                                data-id="3" data-code="3001" data-category="voice" data-type="dedicated" data-client="internal" data-billing="standard" data-webhook="SIP: trunk.nexus.local">
                                <i class="bi bi-pencil"></i>
                            </button>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
@endsection

@push('modals')
    <!-- SHORT CODE MODAL -->
    <div class="modal fade" id="codeModal" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered modal-lg">
            <div class="modal-content">
                <div class="modal-header bg-light">
                    <h5 class="modal-title fw-bold" id="codeModalTitle">Provision Short Code</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                
                <form id="codeForm">
                    <div class="modal-body p-4">
                        <input type="hidden" id="recordId" name="id">
                        
                        <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;">Short Code Number *</label>
                                <input type="text" class="form-control fw-bold" id="codeNumber" name="code_number" required placeholder="e.g. 1334 or *711#">
                            </div>
                            <div class="col-md-6">
                                <label class="form-label text-secondary fw-semibold" style="font-size: 0.85rem;">Service Category *</label>
                                <select class="form-select" id="codeCategory" name="category" required>
                                    <option value="" selected disabled>Select Category...</option>
                                    <option value="sms">SMS</option>
                                    <option value="ussd">USSD</option>
                                    <option value="voice">Voice / IVR</option>
                                </select>
                            </div>
                        </div>

                        <div class="row g-3 mb-4">
                            <div class="col-md-4">
                                <label class="form-label text-secondary fw-semibold" style="font-size: 0.85rem;">Setup Type</label>
                                <select class="form-select" id="codeType" name="setup_type">
                                    <option value="dedicated">Dedicated</option>
                                    <option value="shared">Shared (Keyword/Extension)</option>
                                </select>
                            </div>
                            <div class="col-md-4">
                                <label class="form-label text-secondary fw-semibold" style="font-size: 0.85rem;">Billing Model</label>
                                <select class="form-select" id="codeBilling" name="billing_type">
                                    <option value="standard">Standard Rated</option>
                                    <option value="reverse">Reverse Billed (Toll-Free)</option>
                                    <option value="premium">Premium Rated</option>
                                </select>
                            </div>
                            <div class="col-md-4">
                                <label class="form-label text-secondary fw-semibold" style="font-size: 0.85rem;">Assigned Client *</label>
                                <select class="form-select" id="codeClient" name="client_id" required>
                                    <option value="internal">Internal System</option>
                                    <option value="client_1">Click Tech Corp</option>
                                    <option value="client_2">Alpha Telecom</option>
                                </select>
                            </div>
                        </div>

                        <div class="mb-4">
                            <label class="form-label text-secondary fw-semibold" style="font-size: 0.85rem;">Callback URL / Webhook Endpoint</label>
                            <input type="text" class="form-control" id="codeWebhook" name="webhook_url" placeholder="https://api.client.com/receive">
                            <div class="form-text">Where incoming payloads (MO messages, USSD sessions, SIP invites) should be routed.</div>
                        </div>

                        <h6 class="fw-bold mb-3 text-secondary text-uppercase" style="font-size: 0.75rem;">MNO Routing Map</h6>
                        <div class="p-3 bg-light rounded border border-secondary-subtle">
                            <div class="form-check form-check-inline me-4">
                                <input class="form-check-input" type="checkbox" id="routeMtn" name="routing[]" value="mtn" checked>
                                <label class="form-check-label fw-semibold text-dark" for="routeMtn">MTN Ghana</label>
                            </div>
                            <div class="form-check form-check-inline me-4">
                                <input class="form-check-input" type="checkbox" id="routeTelecel" name="routing[]" value="telecel" checked>
                                <label class="form-check-label fw-semibold text-dark" for="routeTelecel">Telecel</label>
                            </div>
                            <div class="form-check form-check-inline">
                                <input class="form-check-input" type="checkbox" id="routeAt" name="routing[]" value="at" checked>
                                <label class="form-check-label fw-semibold text-dark" for="routeAt">AT</label>
                            </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="btnSubmitCode">
                            Save Configuration
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
@endpush

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

            // --- OPEN MODAL FOR CREATE ---
            $('#btnOpenCodeModal').on('click', function() {
                $form[0].reset(); 
                $('#recordId').val('');
                
                $('#codeModalTitle').text('Provision New Short Code');
                $('#btnSubmitCode').html('<i class="bi bi-save me-2"></i>Save Configuration');
                
                codeModal.show();
            });

            // --- OPEN MODAL FOR EDIT ---
            $('.btn-edit-code').on('click', function() {
                const data = $(this).data();
                
                $form[0].reset();
                $('#recordId').val(data.id);
                
                $('#codeNumber').val(data.code);
                $('#codeCategory').val(data.category);
                $('#codeType').val(data.type);
                $('#codeClient').val(data.client);
                $('#codeBilling').val(data.billing);
                $('#codeWebhook').val(data.webhook);
                
                $('#codeModalTitle').text('Edit Route: ' + data.code);
                $('#btnSubmitCode').html('<i class="bi bi-check-circle me-2"></i>Update Route');
                
                codeModal.show();
            });

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

                setTimeout(() => {
                    const formData = $(this).serializeArray();
                    console.log('Short Code Payload:', formData);
                    
                    $btn.html(originalText).prop('disabled', false);
                    codeModal.hide();
                    alert('Short code configuration applied to gateway successfully.');
                }, 800);
            });
        });
    </script>
@endpush