<?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">User Matrix</h1>
            <p class="text-sm text-gray-600 dark:text-gray-400">Manage all registered accounts, roles, and system access.</p>
        </div>
        <a href="<?php echo URLROOT; ?>/admin/addUser" class="w-full md:w-auto bg-primary hover:opacity-90 text-white font-bold py-2 px-4 rounded-lg transition-all focus:ring-4 focus:ring-primary/50 text-sm whitespace-nowrap text-center shadow-sm">
            + Add New User
        </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-primary" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4.354a4 4 0 110 5.292M15 21H3v-1a6 6 0 0112 0v1zm0 0h6v-1a6 6 0 00-9-5.197M13 7a4 4 0 11-8 0 4 4 0 018 0z"></path></svg>
                Registered Users
            </h2>
        </div>
        
        <div class="w-full text-gray-900 dark:text-gray-100 p-5">
            <table id="usersTable" 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">Name / Details</th>
                        <th class="px-6 py-4 text-[11px] font-extrabold text-gray-500 dark:text-gray-400 uppercase tracking-widest">Email</th>
                        <th class="px-6 py-4 text-[11px] font-extrabold text-gray-500 dark:text-gray-400 uppercase tracking-widest">Role</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['users'])): ?>
                        <?php foreach($data['users'] as $user) : ?>
                        <tr class="border-b border-gray-100 dark:border-gray-700/50 last:border-0 hover:bg-gray-50 dark:hover:bg-gray-700/25 transition-colors">
                            
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-bold text-gray-900 dark:text-white">
                                <?php echo htmlspecialchars($user->name); ?>
                                
                                <?php if(!empty($user->partner_name)): ?>
                                    <span class="block text-[10px] font-mono font-normal text-indigo-500 mt-0.5">Agency: <?php echo htmlspecialchars($user->partner_name); ?></span>
                                <?php endif; ?>

                                <?php if(!empty($user->company_name)): ?>
                                    <span class="block text-[10px] font-mono font-normal text-gray-500 dark:text-gray-400 mt-0.5">Company: <?php echo htmlspecialchars($user->company_name); ?></span>
                                <?php endif; ?>

                                <?php if(!empty($user->staff_role_name)): ?>
                                    <span class="block text-[10px] font-mono font-normal text-blue-500 mt-0.5">Title: <?php echo htmlspecialchars($user->staff_role_name); ?></span>
                                <?php endif; ?>
                            </td>

                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500 dark:text-gray-400"><?php echo htmlspecialchars($user->email); ?></td>
                            
                            <td class="px-6 py-4 whitespace-nowrap text-sm">
                                <span class="px-2 py-0.5 inline-flex text-[10px] font-bold uppercase tracking-wider rounded border 
                                     <?php 
                                     if ($user->role === 'admin') echo 'bg-purple-50 dark:bg-purple-900/10 text-purple-700 dark:text-purple-400 border-purple-200 dark:border-purple-800/30'; 
                                     elseif ($user->role === 'staff') echo 'bg-blue-50 dark:bg-blue-900/10 text-blue-700 dark:text-blue-400 border-blue-200 dark:border-blue-800/30'; 
                                     elseif ($user->role === 'partner') echo 'bg-indigo-50 dark:bg-indigo-900/10 text-indigo-700 dark:text-indigo-400 border-indigo-200 dark:border-indigo-800/30';
                                     else echo 'bg-emerald-50 dark:bg-emerald-900/10 text-emerald-700 dark:text-emerald-400 border-emerald-200 dark:border-emerald-800/30'; 
                                     ?>">
                                    <?php echo $user->role === 'partner' ? 'Agency' : ucfirst($user->role); ?>
                                </span>
                            </td>
                            
                            <td class="px-6 py-4 whitespace-nowrap text-sm">
                                <?php if($user->is_active == 1): ?>
                                    <span class="text-green-600 dark:text-green-400 font-bold">Active</span>
                                <?php else: ?>
                                    <span class="text-red-600 dark:text-red-400 font-bold">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 $user->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 border border-gray-200 dark:border-gray-600/50" 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>
                                
                                <?php if($user->id !== $_SESSION['user_id'] && !isset($_SESSION['admin_impersonating'])): ?>
                                    <form action="<?php echo URLROOT; ?>/admin/impersonate/<?php echo $user->id; ?>" method="POST" onsubmit="confirmAction(event, this, 'Impersonate User', 'Are you sure you want to login as this user?');" 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 flex items-center justify-center" title="Impersonate User">
                                            <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="M8 7h12m0 0l-4-4m4 4l-4 4m0 6H4m0 0l4 4m-4-4l4-4"></path></svg>
                                        </button>
                                    </form>
                                <?php endif; ?>
                                
                                <a href="<?php echo URLROOT; ?>/admin/editUser/<?php echo $user->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 User">
                                    <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>
                                
                                <?php if($user->id !== $_SESSION['user_id']): ?>
                                    <form action="<?php echo URLROOT; ?>/admin/deleteUser/<?php echo $user->id; ?>" method="POST" onsubmit="confirmAction(event, this, 'Delete User', 'Are you sure you want to PERMANENTLY delete this user? This cannot be undone.');" 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 flex items-center justify-center" title="Delete User">
                                            <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>
                                <?php endif; ?>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<script>
    $(document).ready(function() {
        $('#usersTable').DataTable({
            responsive: true,
            pageLength: 25,
            language: {
                search: "",
                searchPlaceholder: "Search users...",
                lengthMenu: "Show _MENU_ entries",
                emptyTable: `<div class="flex flex-col items-center justify-center py-10">
                                <div class="h-16 w-16 bg-gray-100 dark:bg-gray-800 rounded-full flex items-center justify-center mb-4">
                                    <svg class="w-8 h-8 text-gray-400 dark:text-gray-500" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4.354a4 4 0 110 5.292M15 21H3v-1a6 6 0 0112 0v1zm0 0h6v-1a6 6 0 00-9-5.197M13 7a4 4 0 11-8 0 4 4 0 018 0z"></path></svg>
                                </div>
                                <h3 class="text-lg font-bold text-gray-900 dark:text-white">No Users Found</h3>
                                <p class="text-sm text-gray-500 dark:text-gray-400 mt-1">There are no registered users in the system.</p>
                            </div>`
            },
            columnDefs: [
                { orderable: false, targets: 4 }
            ]
        });
    });
</script>

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