<?php require_once __DIR__ . '/../layout.php'; ?>

<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 2rem;">
    <div>
        <h3 style="margin: 0 0 0.5rem; color: white;">Global Users Directory</h3>
        <p style="color: var(--text-muted); margin: 0;">Administrate accounts, roles, access statuses, and branch mappings across all tenants.</p>
    </div>
    <a href="/superadmin/userCreate" class="btn-gold">
        <i class="ph ph-plus" style="font-weight: bold;"></i> Add User
    </a>
</div>

<div class="card responsive-table-container" style="padding: 0; overflow-x: auto; border: none; margin-bottom: 2rem; border-radius: 12px;">
    <table class="data-table" style="width: 100%; border-collapse: collapse;">
        <thead>
            <tr>
                <th style="width: 5%;">ID</th>
                <th style="width: 25%;">User Details</th>
                <th style="width: 25%;">Tenant/Branch Assignment</th>
                <th style="width: 15%;">Role</th>
                <th style="width: 15%;">Status</th>
                <th style="width: 15%; text-align: center;">Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php if (empty($records)): ?>
                <tr>
                    <td colspan="6" style="text-align: center; color: var(--text-muted); padding: 2rem;">No users registered.</td>
                </tr>
            <?php else: ?>
                <?php foreach ($records as $rec): ?>
                    <tr>
                        <td style="color: var(--text-muted);"><?php echo htmlspecialchars($rec['id']); ?></td>
                        <td>
                            <strong style="color: white;"><?php echo htmlspecialchars($rec['name']); ?></strong><br>
                            <small style="color: var(--text-muted);"><?php echo htmlspecialchars($rec['email']); ?></small>
                        </td>
                        <td>
                            <span style="color: #fbbf24; font-weight: 500;"><?php echo htmlspecialchars($rec['tenant_name'] ?: 'System-wide'); ?></span><br>
                            <small style="color: var(--text-muted);"><?php echo htmlspecialchars($rec['branch_name'] ?: 'All Branches / Admin'); ?></small>
                        </td>
                        <td>
                            <?php 
                            $roleClass = '';
                            if ($rec['role'] === 'superadmin') $roleClass = 'color: #fbbf24; background: rgba(245,158,11,0.1); border: 1px solid rgba(245,158,11,0.2);';
                            elseif ($rec['role'] === 'admin') $roleClass = 'color: #818cf8; background: rgba(129,140,248,0.1); border: 1px solid rgba(129,140,248,0.2);';
                            elseif ($rec['role'] === 'pharmacist') $roleClass = 'color: #34d399; background: rgba(52,211,153,0.1); border: 1px solid rgba(52,211,153,0.2);';
                            else $roleClass = 'color: #94a3b8; background: rgba(148,163,184,0.1); border: 1px solid rgba(148,163,184,0.2);';
                            ?>
                            <span style="display: inline-block; padding: 0.2rem 0.5rem; border-radius: 6px; font-size: 0.8rem; font-weight: 600; text-transform: uppercase; <?php echo $roleClass; ?>">
                                <?php echo htmlspecialchars($rec['role']); ?>
                            </span>
                        </td>
                        <td>
                            <?php if ($rec['status'] === 'active'): ?>
                                <span style="display: inline-flex; align-items: center; gap: 0.25rem; color: #34d399; font-weight: 600; font-size: 0.9rem;">
                                    <span style="height: 8px; width: 8px; border-radius: 50%; background: #34d399;"></span> Active
                                </span>
                            <?php else: ?>
                                <span style="display: inline-flex; align-items: center; gap: 0.25rem; color: #ef4444; font-weight: 600; font-size: 0.9rem;">
                                    <span style="height: 8px; width: 8px; border-radius: 50%; background: #ef4444;"></span> Inactive
                                </span>
                            <?php endif; ?>
                        </td>
                        <td style="text-align: center; white-space: nowrap;">
                            <a href="/superadmin/userEdit/<?php echo $rec['id']; ?>" class="action-icon edit" title="Edit">
                                <i class="ph ph-pencil-simple"></i>
                            </a>
                            <?php if ($rec['role'] !== 'superadmin' || $_SESSION['user_id'] != $rec['id']): ?>
                                <a href="/superadmin/userDelete/<?php echo $rec['id']; ?>" class="action-icon delete" title="Delete" onclick="return confirm('Are you sure you want to delete this user account? All access privileges mapping will be permanently deleted.');">
                                    <i class="ph ph-trash"></i>
                                </a>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
            <?php endif; ?>
        </tbody>
    </table>
</div>

</div> <!-- main-content -->
</div> <!-- dashboard-layout -->
</body>
</html>
