<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Nexus ERP - Client Management</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
    <style>
        body { background-color: #f8f9fc; }
        
        /* Sidebar Styling */
        #sidebar { width: 260px; background-color: #111425; position: fixed; height: 100vh; overflow-y: auto; z-index: 1000; }
        .sidebar-brand { color: #fff; padding: 1.5rem 1.25rem; font-weight: 700; }
        .sidebar-brand-icon { background: #5c4df0; padding: 0.5rem; border-radius: 8px; margin-right: 10px; }
        .sidebar-nav-item { color: #8a90a5; text-decoration: none; padding: 0.75rem 1.25rem; display: block; border-radius: 8px; margin: 0.2rem 1rem; font-size: 0.9rem; }
        .sidebar-nav-item:hover, .sidebar-nav-item.active { background-color: #5c4df0; color: #fff; }
        .sidebar-heading { color: #5a617a; font-size: 0.75rem; text-transform: uppercase; padding: 1rem 1.25rem 0.5rem; font-weight: 600; }
        
        /* Main Layout */
        #main-content { margin-left: 260px; min-height: 100vh; }
        .topbar { background: #fff; height: 70px; border-bottom: 1px solid #eaedf1; padding: 0 1.5rem; }
        
        /* Component Styling */
        .content-card { background: #fff; border: 1px solid #eaedf1; border-radius: 12px; box-shadow: 0 2px 4px rgba(0,0,0,0.02); }
        .stat-card { border-left: 4px solid transparent; }
        .stat-card.primary { border-left-color: #5c4df0; }
        .stat-card.success { border-left-color: #10b981; }
        .stat-card.warning { border-left-color: #f59e0b; }
        
        /* Table Styling */
        .table-custom th { background-color: #f8f9fc; color: #5a617a; font-weight: 600; font-size: 0.8rem; text-transform: uppercase; border-bottom: 2px solid #eaedf1; padding: 1rem; }
        .table-custom td { padding: 1rem; vertical-align: middle; font-size: 0.9rem; border-bottom: 1px solid #eaedf1; }
        .avatar-circle { width: 36px; height: 36px; display: flex; align-items: center; justify-content: center; border-radius: 50%; font-weight: bold; font-size: 0.85rem; }
        
        /* Modal Customization */
        .modal-content { border-radius: 12px; border: none; box-shadow: 0 10px 30px rgba(0,0,0,0.1); }
        .modal-header { border-bottom: 1px solid #eaedf1; background-color: #f8f9fc; border-radius: 12px 12px 0 0; }
        .modal-footer { border-top: 1px solid #eaedf1; }
        .form-control:focus, .form-select:focus { border-color: #5c4df0; box-shadow: 0 0 0 0.25rem rgba(92, 77, 240, 0.25); }
    </style>
</head>
<body>

    <aside id="sidebar">
        <div class="sidebar-brand d-flex align-items-center">
            <span class="sidebar-brand-icon"><i class="bi bi-buildings"></i></span>
            <div>
                <div class="mb-0 fs-6">NEXUS <span class="badge bg-secondary ms-1" style="font-size: 0.6rem;">ERP</span></div>
            </div>
        </div>

        <div class="px-3 mb-3">
            <div class="input-group input-group-sm">
                <span class="input-group-text bg-dark border-secondary text-secondary"><i class="bi bi-search"></i></span>
                <input type="text" class="form-control bg-dark border-secondary text-light placeholder-secondary" placeholder="Quick search...">
            </div>
        </div>

        <div class="sidebar-heading mt-2">Core Entities</div>
        <a href="#" class="sidebar-nav-item"><i class="bi bi-person-badge me-2"></i> Staff Directory</a>
        <a href="#" class="sidebar-nav-item active"><i class="bi bi-people me-2"></i> Clients</a>
        <a href="#" class="sidebar-nav-item"><i class="bi bi-broadcast-pin me-2"></i> MNO Partners</a>
        <div class="sidebar-heading mt-2">Resources</div>
        <a href="#" class="sidebar-nav-item"><i class="bi bi-folder2-open me-2"></i> Document Vault</a>
    </aside>

    <main id="main-content">
        <header class="topbar d-flex justify-content-between align-items-center">
            <div class="d-flex align-items-center">
                <a href="#" class="text-secondary text-decoration-none hover-primary"><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;">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;">Clients</span>
            </div>
            <div class="d-flex align-items-center">
                <div class="text-secondary me-4" style="font-size: 0.9rem;">
                    <i class="bi bi-clock me-1"></i> <span id="current-time">--:--:--</span>
                </div>
                <div class="d-flex align-items-center">
                    <div class="rounded-circle bg-light d-flex justify-content-center align-items-center me-2 border text-dark fw-bold" style="width: 32px; height: 32px; font-size: 0.8rem;">AU</div>
                    <span class="fw-bold" style="font-size: 0.9rem;">Admin User</span>
                </div>
            </div>
        </header>

        <div class="container-fluid p-4">
            
            <div class="d-flex justify-content-between align-items-center mb-4">
                <div>
                    <h4 class="fw-bold mb-1">Client Management</h4>
                    <p class="text-secondary mb-0" style="font-size: 0.9rem;">Manage corporate accounts, billing types, and service provisions.</p>
                </div>
                <div class="d-flex gap-2">
                    <button class="btn btn-outline-secondary d-flex align-items-center" style="border-radius: 8px;">
                        <i class="bi bi-download me-2"></i> Export
                    </button>
                    <button class="btn text-white fw-bold d-flex align-items-center" style="background-color: #5c4df0; border-radius: 8px;" id="btnOpenClientModal">
                        <i class="bi bi-plus-lg me-2"></i> Add New Client
                    </button>
                </div>
            </div>

            <div class="row g-4 mb-4">
                <div class="col-md-4">
                    <div class="content-card stat-card primary p-3 h-100 border-0">
                        <div class="text-secondary fw-bold mb-2" style="font-size: 0.75rem; letter-spacing: 0.5px;">TOTAL CLIENTS</div>
                        <h3 class="fw-bold mb-0">24</h3>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="content-card stat-card success p-3 h-100 border-0">
                        <div class="text-secondary fw-bold mb-2" style="font-size: 0.75rem; letter-spacing: 0.5px;">ACTIVE SIP TRUNKS</div>
                        <h3 class="fw-bold mb-0">18</h3>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="content-card stat-card warning p-3 h-100 border-0">
                        <div class="text-secondary fw-bold mb-2" style="font-size: 0.75rem; letter-spacing: 0.5px;">PREPAID ACCOUNTS</div>
                        <h3 class="fw-bold mb-0">12</h3>
                    </div>
                </div>
            </div>

            <div class="content-card">
                <div class="p-3 border-bottom bg-light bg-opacity-50 rounded-top" style="border-top-left-radius: 12px; border-top-right-radius: 12px;">
                    <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 clients, email, or contact...">
                            </div>
                        </div>
                        <div class="col-md-3">
                            <select class="form-select bg-white">
                                <option value="">All Service Types</option>
                                <option value="voip">PBX / SIP Trunking</option>
                                <option value="gis">GIS Mapping Services</option>
                                <option value="web">Web Infrastructure</option>
                            </select>
                        </div>
                        <div class="col-md-3">
                            <select class="form-select bg-white">
                                <option value="">Billing: All</option>
                                <option value="prepaid">Prepaid</option>
                                <option value="postpaid">Postpaid</option>
                            </select>
                        </div>
                    </div>
                </div>

                <div class="table-responsive">
                    <table class="table table-custom table-hover mb-0">
                        <thead>
                            <tr>
                                <th>Client Details</th>
                                <th>Primary Contact</th>
                                <th>Services</th>
                                <th>Billing</th>
                                <th>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-primary bg-opacity-10 text-primary me-3">CT</div>
                                        <div>
                                            <div class="fw-bold text-dark">Click Tech Team</div>
                                            <div class="text-secondary" style="font-size: 0.8rem;"><i class="bi bi-geo-alt me-1"></i>Accra, GH</div>
                                        </div>
                                    </div>
                                </td>
                                <td>
                                    <div class="fw-semibold">Admin Desk</div>
                                    <div class="text-secondary" style="font-size: 0.8rem;">hello@clicktechteam.com</div>
                                </td>
                                <td><span class="badge bg-secondary bg-opacity-10 text-secondary border">Web Infrastructure</span></td>
                                <td><span class="badge bg-info bg-opacity-10 text-info">Postpaid</span></td>
                                <td><span class="badge bg-success bg-opacity-10 text-success px-2 py-1"><i class="bi bi-check-circle me-1"></i>Active</span></td>
                                <td class="text-end">
                                    <button class="btn btn-sm btn-light text-secondary me-1"><i class="bi bi-eye"></i></button>
                                    <button class="btn btn-sm btn-light text-primary me-1 btn-edit-client" 
                                        data-id="1" data-name="Click Tech Team" data-email="hello@clicktechteam.com" data-contact="Admin Desk" data-phone="+233 20 000 0000" data-location="Accra, GH" data-service="web" data-billing="postpaid" data-status="active">
                                        <i class="bi bi-pencil"></i>
                                    </button>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <div class="d-flex align-items-center">
                                        <div class="avatar-circle bg-success bg-opacity-10 text-success me-3">AT</div>
                                        <div>
                                            <div class="fw-bold text-dark">Alpha Telecom Services</div>
                                            <div class="text-secondary" style="font-size: 0.8rem;"><i class="bi bi-geo-alt me-1"></i>Kumasi, GH</div>
                                        </div>
                                    </div>
                                </td>
                                <td>
                                    <div class="fw-semibold">Michael Osei</div>
                                    <div class="text-secondary" style="font-size: 0.8rem;">m.osei@alphatel.com.gh</div>
                                </td>
                                <td><span class="badge bg-secondary bg-opacity-10 text-secondary border">SIP Trunking / PBX</span></td>
                                <td><span class="badge bg-warning bg-opacity-10 text-warning text-dark">Prepaid</span></td>
                                <td><span class="badge bg-success bg-opacity-10 text-success px-2 py-1"><i class="bi bi-check-circle me-1"></i>Active</span></td>
                                <td class="text-end">
                                    <button class="btn btn-sm btn-light text-secondary me-1"><i class="bi bi-eye"></i></button>
                                    <button class="btn btn-sm btn-light text-primary me-1 btn-edit-client"
                                        data-id="2" data-name="Alpha Telecom Services" data-email="m.osei@alphatel.com.gh" data-contact="Michael Osei" data-phone="+233 24 000 0000" data-location="Kumasi, GH" data-service="pbx" data-billing="prepaid" data-status="active">
                                        <i class="bi bi-pencil"></i>
                                    </button>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <div class="d-flex align-items-center">
                                        <div class="avatar-circle bg-warning bg-opacity-10 text-warning me-3">LS</div>
                                        <div>
                                            <div class="fw-bold text-dark">LiveScores Media ZM</div>
                                            <div class="text-secondary" style="font-size: 0.8rem;"><i class="bi bi-geo-alt me-1"></i>Lusaka, ZM</div>
                                        </div>
                                    </div>
                                </td>
                                <td>
                                    <div class="fw-semibold">David Phiri</div>
                                    <div class="text-secondary" style="font-size: 0.8rem;">david@livescores.co.zm</div>
                                </td>
                                <td><span class="badge bg-secondary bg-opacity-10 text-secondary border">USSD App / API</span></td>
                                <td><span class="badge bg-info bg-opacity-10 text-info">Postpaid</span></td>
                                <td><span class="badge bg-warning bg-opacity-10 text-warning px-2 py-1"><i class="bi bi-clock me-1"></i>Onboarding</span></td>
                                <td class="text-end">
                                    <button class="btn btn-sm btn-light text-secondary me-1"><i class="bi bi-eye"></i></button>
                                    <button class="btn btn-sm btn-light text-primary me-1 btn-edit-client"
                                        data-id="3" data-name="LiveScores Media ZM" data-email="david@livescores.co.zm" data-contact="David Phiri" data-phone="+260 97 000 0000" data-location="Lusaka, ZM" data-service="api" data-billing="postpaid" data-status="onboarding">
                                        <i class="bi bi-pencil"></i>
                                    </button>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
                
                <div class="p-3 d-flex justify-content-between align-items-center">
                    <span class="text-secondary" style="font-size: 0.85rem;">Showing 1 to 3 of 24 entries</span>
                    <nav>
                        <ul class="pagination pagination-sm mb-0">
                            <li class="page-item disabled"><a class="page-link" href="#">Previous</a></li>
                            <li class="page-item active"><a class="page-link" href="#" style="background-color: #5c4df0; border-color: #5c4df0;">1</a></li>
                            <li class="page-item"><a class="page-link text-dark" href="#">2</a></li>
                            <li class="page-item"><a class="page-link text-dark" href="#">Next</a></li>
                        </ul>
                    </nav>
                </div>
            </div>
        </div>
    </main>

    <div class="modal fade" id="clientModal" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog modal-lg modal-dialog-centered">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title fw-bold" id="clientModalTitle">Add New Client</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                
                <form id="clientForm">
                    <div class="modal-body p-4">
                        <input type="hidden" id="clientId" name="id">
                        
                        <h6 class="fw-bold mb-3 text-secondary text-uppercase" style="font-size: 0.75rem;">Company Details</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;">Company Name *</label>
                                <input type="text" class="form-control" id="clientName" name="name" required placeholder="e.g. Acme Corp">
                            </div>
                            <div class="col-md-6">
                                <label class="form-label text-secondary fw-semibold" style="font-size: 0.85rem;">Location / Region</label>
                                <input type="text" class="form-control" id="clientLocation" name="location" placeholder="e.g. Accra, GH">
                            </div>
                        </div>

                        <h6 class="fw-bold mb-3 text-secondary text-uppercase" style="font-size: 0.75rem;">Primary Contact</h6>
                        <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;">Full Name</label>
                                <input type="text" class="form-control" id="clientContact" name="contact_name" placeholder="Contact person">
                            </div>
                            <div class="col-md-4">
                                <label class="form-label text-secondary fw-semibold" style="font-size: 0.85rem;">Email Address *</label>
                                <input type="email" class="form-control" id="clientEmail" name="email" required placeholder="name@company.com">
                            </div>
                            <div class="col-md-4">
                                <label class="form-label text-secondary fw-semibold" style="font-size: 0.85rem;">Phone Number</label>
                                <input type="text" class="form-control" id="clientPhone" name="phone" placeholder="+233 ...">
                            </div>
                        </div>

                        <h6 class="fw-bold mb-3 text-secondary text-uppercase" style="font-size: 0.75rem;">Service & Billing Configuration</h6>
                        <div class="row g-3">
                            <div class="col-md-4">
                                <label class="form-label text-secondary fw-semibold" style="font-size: 0.85rem;">Primary Service</label>
                                <select class="form-select" id="clientService" name="service_type">
                                    <option value="pbx">SIP Trunking / PBX</option>
                                    <option value="api">USSD App / API</option>
                                    <option value="gis">GIS Data / Mapping</option>
                                    <option value="web">Web Infrastructure</option>
                                </select>
                            </div>
                            <div class="col-md-4">
                                <label class="form-label text-secondary fw-semibold" style="font-size: 0.85rem;">Billing Type</label>
                                <select class="form-select" id="clientBilling" name="billing_type">
                                    <option value="postpaid">Postpaid (Invoice)</option>
                                    <option value="prepaid">Prepaid (Balance)</option>
                                </select>
                            </div>
                            <div class="col-md-4">
                                <label class="form-label text-secondary fw-semibold" style="font-size: 0.85rem;">Account Status</label>
                                <select class="form-select" id="clientStatus" name="status">
                                    <option value="active">Active</option>
                                    <option value="onboarding">Onboarding</option>
                                    <option value="suspended">Suspended</option>
                                </select>
                            </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="btnSubmitClient">
                            Save Client
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
    
    <script>
        $(document).ready(function() {
            // Reusable Clock Update
            function updateClock() {
                const now = new Date();
                $('#current-time').text(now.toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: true }));
            }
            updateClock();
            setInterval(updateClock, 1000);

            const clientModal = new bootstrap.Modal(document.getElementById('clientModal'));
            const $form = $('#clientForm');

            // --- OPEN MODAL FOR CREATE ---
            $('#btnOpenClientModal').on('click', function() {
                $form[0].reset(); 
                $('#clientId').val('');
                $('#clientModalTitle').text('Add New Client');
                $('#btnSubmitClient').html('<i class="bi bi-save me-2"></i>Save Client');
                clientModal.show();
            });

            // --- OPEN MODAL FOR EDIT ---
            $('.btn-edit-client').on('click', function() {
                // Fetch data attributes from the clicked button
                const data = $(this).data();
                
                // Populate the form
                $('#clientId').val(data.id);
                $('#clientName').val(data.name);
                $('#clientLocation').val(data.location);
                $('#clientContact').val(data.contact);
                $('#clientEmail').val(data.email);
                $('#clientPhone').val(data.phone);
                $('#clientService').val(data.service);
                $('#clientBilling').val(data.billing);
                $('#clientStatus').val(data.status);
                
                // Update UI text
                $('#clientModalTitle').text('Edit Client: ' + data.name);
                $('#btnSubmitClient').html('<i class="bi bi-check-circle me-2"></i>Update Client');
                
                clientModal.show();
            });

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

                // Simulate AJAX Request to Laravel Backend
                setTimeout(() => {
                    const formData = $(this).serializeArray();
                    console.log('Submitted Client Data:', formData);
                    
                    $btn.html(originalText);
                    $btn.prop('disabled', false);
                    clientModal.hide();
                    
                    alert(isEdit ? 'Client updated successfully!' : 'New client created successfully!');
                    // Normally you would reload the datatable here or append the new row
                }, 800);
            });
        });
    </script>
</body>
</html>