@extends('layouts.masterbeta')

@section('title', 'Click ERP - SMS Sender IDs')

@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); }
        .net-rejected { background-color: rgba(239, 68, 68, 0.1); color: #ef4444; border-color: rgba(239, 68, 68, 0.2); }
        
        .sender-id-text { font-family: monospace; font-size: 1.1rem; letter-spacing: 1px; color: #111425; }
    </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;">Sender IDs</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">SMS Sender IDs</h4>
            <p class="text-secondary mb-0" style="font-size: 0.9rem;">Manage alphanumeric masks, shortcodes, and MNO registration statuses.</p>
        </div>
        <button class="btn text-white fw-bold d-flex align-items-center" style="background-color: #5c4df0; border-radius: 8px;" id="btnOpenSenderModal">
            <i class="bi bi-plus-lg me-2"></i> Request Sender ID
        </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: #5c4df0 !important;">
                <div class="text-secondary fw-bold mb-1" style="font-size: 0.75rem; letter-spacing: 0.5px;">TOTAL SENDER IDs</div>
                <h3 class="fw-bold mb-0">48</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;">FULLY APPROVED</div>
                <h3 class="fw-bold mb-0">36</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;">PENDING MNO REVIEW</div>
                <h3 class="fw-bold mb-0">9</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;">REJECTED / BLOCKED</div>
                <h3 class="fw-bold mb-0">3</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-5">
                    <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 sender name or client...">
                    </div>
                </div>
                <div class="col-md-3">
                    <select class="form-select bg-white">
                        <option value="">All Clients</option>
                        <option value="client_1">Click Tech Corp</option>
                        <option value="client_2">Alpha Telecom</option>
                        <option value="internal">Internal System</option>
                    </select>
                </div>
                <div class="col-md-3">
                    <select class="form-select bg-white">
                        <option value="">Status: All</option>
                        <option value="approved">Fully Approved</option>
                        <option value="pending">Pending Review</option>
                        <option value="rejected">Rejected</option>
                    </select>
                </div>
                <div class="col-md-1">
                    <button class="btn btn-secondary w-100"><i class="bi bi-funnel"></i></button>
                </div>
            </div>
        </div>

        <div class="table-responsive">
            <table class="table table-custom table-hover mb-0">
                <thead>
                    <tr>
                        <th>Sender Name</th>
                        <th>Client / Owner</th>
                        <th>Type</th>
                        <th>Network Status</th>
                        <th>Global Status</th>
                        <th class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <!-- Row 1 -->
                    <tr>
                        <td>
                            <div class="sender-id-text fw-bold">NEXUS-OTP</div>
                            <div class="text-secondary mt-1" style="font-size: 0.75rem;">Created: May 12, 2026</div>
                        </td>
                        <td><span class="badge bg-dark bg-opacity-10 text-dark border">Internal System</span></td>
                        <td><span class="badge bg-secondary bg-opacity-10 text-secondary border">Alphanumeric</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><span class="badge bg-success bg-opacity-10 text-success px-2 py-1"><i class="bi bi-check-all me-1"></i>Active</span></td>
                        <td class="text-end">
                            <button class="btn btn-sm btn-light text-primary me-1 btn-edit-sender" 
                                data-id="1" data-sender="NEXUS-OTP" data-client="internal" data-type="alpha" data-purpose="Sending internal authentication codes." data-status="approved">
                                <i class="bi bi-pencil"></i>
                            </button>
                            <button class="btn btn-sm btn-light text-danger"><i class="bi bi-trash"></i></button>
                        </td>
                    </tr>
                    <!-- Row 2 -->
                    <tr>
                        <td>
                            <div class="sender-id-text fw-bold">CLICKTECH</div>
                            <div class="text-secondary mt-1" style="font-size: 0.75rem;">Created: Jun 02, 2026</div>
                        </td>
                        <td><span class="badge bg-info bg-opacity-10 text-info border border-info-subtle">Client: Click Tech</span></td>
                        <td><span class="badge bg-secondary bg-opacity-10 text-secondary border">Alphanumeric</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>
                                <span class="network-badge net-pending"><i class="bi bi-clock-fill"></i> AT</span>
                            </div>
                        </td>
                        <td><span class="badge bg-warning bg-opacity-10 text-warning px-2 py-1"><i class="bi bi-hourglass-split me-1"></i>Pending</span></td>
                        <td class="text-end">
                            <button class="btn btn-sm btn-light text-primary me-1 btn-edit-sender" 
                                data-id="2" data-sender="CLICKTECH" data-client="client_1" data-type="alpha" data-purpose="Marketing updates and promotions for hosting services." data-status="pending">
                                <i class="bi bi-pencil"></i>
                            </button>
                            <button class="btn btn-sm btn-light text-danger"><i class="bi bi-trash"></i></button>
                        </td>
                    </tr>
                    <!-- Row 3 -->
                    <tr>
                        <td>
                            <div class="sender-id-text fw-bold">1334</div>
                            <div class="text-secondary mt-1" style="font-size: 0.75rem;">Created: Jun 10, 2026</div>
                        </td>
                        <td><span class="badge bg-info bg-opacity-10 text-info border border-info-subtle">Client: Alpha Telecom</span></td>
                        <td><span class="badge bg-primary bg-opacity-10 text-primary border border-primary-subtle">Shortcode</span></td>
                        <td>
                            <div class="d-flex gap-1 flex-wrap">
                                <span class="network-badge net-rejected"><i class="bi bi-x-circle-fill"></i> MTN</span>
                            </div>
                        </td>
                        <td><span class="badge bg-danger bg-opacity-10 text-danger px-2 py-1"><i class="bi bi-x-octagon me-1"></i>Rejected</span></td>
                        <td class="text-end">
                            <button class="btn btn-sm btn-light text-primary me-1 btn-edit-sender" 
                                data-id="3" data-sender="1334" data-client="client_2" data-type="shortcode" data-purpose="Interactive USSD/SMS fallback." data-status="rejected">
                                <i class="bi bi-pencil"></i>
                            </button>
                            <button class="btn btn-sm btn-light text-danger"><i class="bi bi-trash"></i></button>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
@endsection

@push('modals')
    <!-- SENDER ID MODAL -->
    <div class="modal fade" id="senderModal" 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="senderModalTitle">Request Sender ID</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                
                <form id="senderForm">
                    <div class="modal-body p-4">
                        <input type="hidden" id="senderRecordId" 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;">Sender Name / Shortcode *</label>
                                <input type="text" class="form-control fw-bold" id="senderName" name="sender_name" required maxlength="11" placeholder="e.g. NEXUS">
                                <div class="form-text">Max 11 chars (Alphanumeric) or 15 digits (Numeric). No spaces.</div>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label text-secondary fw-semibold" style="font-size: 0.85rem;">Type</label>
                                <select class="form-select" id="senderType" name="type">
                                    <option value="alpha">Alphanumeric</option>
                                    <option value="shortcode">Numeric Shortcode</option>
                                </select>
                            </div>
                        </div>

                        <div class="mb-4">
                            <label class="form-label text-secondary fw-semibold" style="font-size: 0.85rem;">Client / Owner *</label>
                            <select class="form-select" id="senderClient" name="client_id" required>
                                <option value="" selected disabled>Select Client...</option>
                                <option value="internal">Internal System (Nexus ERP)</option>
                                <option value="client_1">Click Tech Corp</option>
                                <option value="client_2">Alpha Telecom</option>
                            </select>
                        </div>

                        <div class="mb-4">
                            <label class="form-label text-secondary fw-semibold" style="font-size: 0.85rem;">Purpose & Sample Message *</label>
                            <textarea class="form-control" id="senderPurpose" name="purpose" rows="3" required placeholder="Required by MNOs for whitelisting. Provide a sample of the SMS content that will be sent."></textarea>
                            <div class="form-text">Example: "Your verification code is 12345. Valid for 5 minutes."</div>
                        </div>

                        <h6 class="fw-bold mb-3 text-secondary text-uppercase" style="font-size: 0.75rem;">MNO Submission Targets</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="mnoMtn" name="networks[]" value="mtn" checked>
                                <label class="form-check-label fw-semibold text-dark" for="mnoMtn">MTN Ghana</label>
                            </div>
                            <div class="form-check form-check-inline me-4">
                                <input class="form-check-input" type="checkbox" id="mnoTelecel" name="networks[]" value="telecel" checked>
                                <label class="form-check-label fw-semibold text-dark" for="mnoTelecel">Telecel</label>
                            </div>
                            <div class="form-check form-check-inline">
                                <input class="form-check-input" type="checkbox" id="mnoAt" name="networks[]" value="at" checked>
                                <label class="form-check-label fw-semibold text-dark" for="mnoAt">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="btnSubmitSender">
                            Submit Registration
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
@endpush

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

            // Force Uppercase and trim spaces on Sender ID input
            $('#senderName').on('input', function() {
                $(this).val($(this).val().toUpperCase().replace(/\s/g, ''));
            });

            // --- OPEN MODAL FOR CREATE ---
            $('#btnOpenSenderModal').on('click', function() {
                $form[0].reset(); 
                $('#senderRecordId').val('');
                
                $('#senderModalTitle').text('Request Sender ID');
                $('#btnSubmitSender').html('<i class="bi bi-send me-2"></i>Submit Registration');
                
                senderModal.show();
            });

            // --- OPEN MODAL FOR EDIT ---
            $('.btn-edit-sender').on('click', function() {
                const data = $(this).data();
                
                $form[0].reset();
                $('#senderRecordId').val(data.id);
                
                $('#senderName').val(data.sender);
                $('#senderType').val(data.type);
                $('#senderClient').val(data.client);
                $('#senderPurpose').val(data.purpose);
                
                $('#senderModalTitle').text('Edit Sender ID: ' + data.sender);
                $('#btnSubmitSender').html('<i class="bi bi-save me-2"></i>Update Request');
                
                senderModal.show();
            });

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

                setTimeout(() => {
                    const formData = $(this).serializeArray();
                    console.log('Sender ID Request Payload:', formData);
                    
                    $btn.html(originalText).prop('disabled', false);
                    senderModal.hide();
                    alert('Sender ID request saved and queued for MNO submission.');
                }, 800);
            });
        });
    </script>
@endpush