<?php require APPROOT . '/app/Views/layouts/header.php'; ?>

<div class="max-w-7xl mx-auto mb-10 space-y-8">
    <div class="flex flex-col md:flex-row md:items-center justify-between pb-4">
        <div class="mb-4 md:mb-0">
            <h1 class="text-2xl md:text-3xl font-bold text-gray-900 dark:text-white mb-1"><?php echo $data['title']; ?></h1>
            <p class="text-sm text-gray-600 dark:text-gray-400"><?php echo $data['description']; ?></p>
        </div>
        <a href="<?php echo URLROOT; ?>/admin/addUser?role=client" class="w-full md:w-auto bg-emerald-600 hover:bg-emerald-700 text-white font-bold py-2 px-4 rounded-lg transition-all focus:ring-4 focus:ring-emerald-500/50 text-sm whitespace-nowrap text-center shadow-sm flex items-center justify-center">
            <svg class="w-5 h-5 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"></path></svg>
            Add New Client
        </a>
    </div>

    <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-200 dark:border-gray-700/50 overflow-hidden">
        <div class="p-5 border-b border-gray-200 dark:border-gray-700/50 bg-gray-50 dark:bg-gray-900/40 flex justify-between items-center">
            <h2 class="text-sm font-bold text-gray-900 dark:text-white flex items-center">
                <svg class="w-4 h-4 mr-2 text-emerald-600 dark:text-emerald-500" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z"></path></svg>
                Registered Clients
            </h2>
        </div>
        <div class="w-full text-gray-900 dark:text-white p-5">
            <table id="clientsTable" class="w-full text-left whitespace-nowrap">
                <thead class="border-b border-gray-200 dark:border-gray-700/50">
                    <tr>
                        <th class="px-6 py-4 text-[11px] font-extrabold text-gray-500 dark:text-gray-400 uppercase tracking-widest">Client / Company</th>
                        <th class="px-6 py-4 text-[11px] font-extrabold text-gray-500 dark:text-gray-400 uppercase tracking-widest">Contact Info</th>
                        <th class="px-6 py-4 text-[11px] font-extrabold text-gray-500 dark:text-gray-400 uppercase tracking-widest">Partner Agency</th>
                        <th class="px-6 py-4 text-[11px] font-extrabold text-gray-500 dark:text-gray-400 uppercase tracking-widest">Status</th>
                        <th class="px-6 py-4 text-right text-[11px] font-extrabold text-gray-500 dark:text-gray-400 uppercase tracking-widest">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if(!empty($data['clients'])): ?>
                        <?php foreach($data['clients'] as $client) : ?>
                        <tr class="border-b border-gray-100 dark:border-gray-700/50 hover:bg-gray-50 dark:hover:bg-gray-700/25 transition-colors">
                            
                            <td class="px-6 py-4 whitespace-nowrap">
                                <span class="text-sm font-bold block"><?php echo htmlspecialchars($client->name); ?></span>
                                <?php if(!empty($client->company_name)): ?>
                                    <span class="text-xs text-gray-500 dark:text-gray-400 block"><?php echo htmlspecialchars($client->company_name); ?></span>
                                <?php endif; ?>
                            </td>

                            <td class="px-6 py-4 whitespace-nowrap">
                                <span class="text-sm text-gray-800 dark:text-gray-200 block"><?php echo htmlspecialchars($client->email); ?></span>
                                <?php if(!empty($client->contact_number)): ?>
                                    <span class="text-xs text-gray-500 dark:text-gray-400 font-mono block"><?php echo htmlspecialchars($client->contact_number); ?></span>
                                <?php endif; ?>
                            </td>

                            <td class="px-6 py-4 whitespace-nowrap text-sm">
                                <?php if(!empty($client->partner_name)): ?>
                                    <span class="text-emerald-600 dark:text-emerald-400 font-bold"><?php echo htmlspecialchars($client->partner_name); ?></span>
                                <?php else: ?>
                                    <span class="text-gray-400 italic">Direct Client</span>
                                <?php endif; ?>
                            </td>
                            
                            <td class="px-6 py-4 whitespace-nowrap text-sm">
                                <?php if($client->is_active == 1): ?>
                                    <span class="px-2 py-0.5 inline-flex text-[10px] font-bold uppercase tracking-wider rounded border bg-green-50 text-green-700 border-green-200 dark:bg-green-900/10 dark:text-green-400 dark:border-green-800/30">Active</span>
                                <?php else: ?>
                                    <span class="px-2 py-0.5 inline-flex text-[10px] font-bold uppercase tracking-wider rounded border bg-red-50 text-red-700 border-red-200 dark:bg-red-900/10 dark:text-red-400 dark:border-red-800/30">Disabled</span>
                                <?php endif; ?>
                            </td>

                            <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium flex justify-end items-center space-x-2">
                                <a href="<?php echo URLROOT; ?>/admin/viewUser/<?php echo $client->id; ?>" class="p-1.5 bg-gray-100 dark:bg-gray-700 hover:bg-gray-200 dark:hover:bg-gray-600 text-gray-600 dark:text-gray-300 rounded-md transition-colors shadow-sm" title="View Profile">
                                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"></path></svg>
                                </a>
                                <a href="<?php echo URLROOT; ?>/admin/editUser/<?php echo $client->id; ?>" class="p-1.5 bg-blue-50 dark:bg-blue-900/10 hover:bg-blue-100 dark:hover:bg-blue-900/30 text-primary dark:text-blue-400 border border-blue-200 dark:border-blue-800/30 rounded-md transition-colors shadow-sm" title="Edit Client">
                                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15.232 5.232l3.536 3.536m-2.036-5.036a2.5 2.5 0 113.536 3.536L6.5 21.036H3v-3.572L16.732 3.732z"></path></svg>
                                </a>
                                <form action="<?php echo URLROOT; ?>/admin/impersonate/<?php echo $client->id; ?>" method="POST" onsubmit="confirmAction(event, this, 'Impersonate Client', 'Log in as this client?');" class="inline m-0">
                                    <button type="submit" class="p-1.5 bg-indigo-50 dark:bg-indigo-900/10 hover:bg-indigo-100 dark:hover:bg-indigo-900/30 text-indigo-600 dark:text-indigo-400 border border-indigo-200 dark:border-indigo-800/30 rounded-md transition-colors shadow-sm cursor-pointer" title="Impersonate">
                                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 16l-4-4m0 0l4-4m-4 4h14m-5 4v1a3 3 0 01-3 3H6a3 3 0 01-3-3V7a3 3 0 013-3h7a3 3 0 013 3v1"></path></svg>
                                    </button>
                                </form>
                                <form action="<?php echo URLROOT; ?>/admin/deleteUser/<?php echo $client->id; ?>" method="POST" onsubmit="confirmAction(event, this, 'Delete Client', 'Are you sure you want to PERMANENTLY delete this client?');" class="inline m-0">
                                    <button type="submit" class="p-1.5 bg-red-50 dark:bg-red-900/10 hover:bg-red-100 dark:hover:bg-red-900/30 text-red-600 dark:text-red-400 border border-red-200 dark:border-red-800/30 rounded-md transition-colors shadow-sm cursor-pointer" title="Delete Client">
                                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"></path></svg>
                                    </button>
                                </form>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<script>
    $(document).ready(function() { $('#clientsTable').DataTable({ responsive: true, columnDefs: [{ orderable: false, targets: 4 }] }); });
</script>

<?php require APPROOT . '/app/Views/layouts/footer.php'; ?>