<?php
$pageTitle = 'Users';
$breadcrumb = [['label' => 'Users', 'url' => null]];
ob_start();
?>
<div class="flex flex-col md:flex-row justify-between items-center mb-6">
    <h1 class="text-2xl font-bold text-gray-800">Users</h1>
    <a href="<?= APP_URL ?>/users/create" class="bg-[#0A3A6B] hover:bg-blue-700 text-white font-bold py-2 px-4 rounded shadow">Add User</a>
</div>

<!-- Stats -->
<div class="grid grid-cols-1 md:grid-cols-4 gap-6 mb-6">
    <div class="bg-white rounded-lg shadow p-5 border-l-4 border-[#0A3A6B]">
        <div class="text-sm text-gray-500 uppercase font-semibold">Total Users</div>
        <div class="text-3xl font-bold text-gray-800"><?= htmlspecialchars($stats['total'] ?? 0) ?></div>
    </div>
    <div class="bg-white rounded-lg shadow p-5 border-l-4 border-green-500">
        <div class="text-sm text-gray-500 uppercase font-semibold">Active</div>
        <div class="text-3xl font-bold text-green-600"><?= htmlspecialchars($stats['active'] ?? 0) ?></div>
    </div>
    <div class="bg-white rounded-lg shadow p-5 border-l-4 border-blue-500">
        <div class="text-sm text-gray-500 uppercase font-semibold">Org Users</div>
        <div class="text-3xl font-bold text-blue-600"><?= htmlspecialchars($stats['org_users'] ?? 0) ?></div>
    </div>
    <div class="bg-white rounded-lg shadow p-5 border-l-4 border-amber-500">
        <div class="text-sm text-gray-500 uppercase font-semibold">Contractor Users</div>
        <div class="text-3xl font-bold text-amber-600"><?= htmlspecialchars($stats['contractor_users'] ?? 0) ?></div>
    </div>
</div>

<!-- Filters -->
<div class="bg-white rounded-lg shadow p-4 mb-6">
    <form method="GET" action="<?= APP_URL ?>/users" class="flex flex-col md:flex-row gap-4">
        <div class="flex-grow">
            <input type="text" name="search" placeholder="Search users by name or email..." value="<?= htmlspecialchars($filters['search'] ?? '') ?>" class="w-full px-4 py-2 border rounded-md focus:outline-none focus:ring-2 focus:ring-[#0A3A6B]">
        </div>
        <div class="w-full md:w-48">
            <select name="entity_type" class="w-full px-4 py-2 border rounded-md focus:outline-none focus:ring-2 focus:ring-[#0A3A6B]">
                <option value="">All Types</option>
                <option value="org_user" <?= ($filters['entity_type'] ?? '') === 'org_user' ? 'selected' : '' ?>>Org User</option>
                <option value="contractor_user" <?= ($filters['entity_type'] ?? '') === 'contractor_user' ? 'selected' : '' ?>>Contractor</option>
                <option value="supplier_user" <?= ($filters['entity_type'] ?? '') === 'supplier_user' ? 'selected' : '' ?>>Supplier</option>
            </select>
        </div>
        <div class="w-full md:w-48">
            <select name="role" class="w-full px-4 py-2 border rounded-md focus:outline-none focus:ring-2 focus:ring-[#0A3A6B]">
                <option value="">All Roles</option>
                <?php foreach ($roles ?? [] as $r): ?>
                <option value="<?= htmlspecialchars($r['id']) ?>" <?= ($filters['role'] ?? '') == $r['id'] ? 'selected' : '' ?>><?= htmlspecialchars($r['name']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="w-full md:w-48">
            <select name="status" class="w-full px-4 py-2 border rounded-md focus:outline-none focus:ring-2 focus:ring-[#0A3A6B]">
                <option value="">All Statuses</option>
                <option value="active" <?= ($filters['status'] ?? '') === 'active' ? 'selected' : '' ?>>Active</option>
                <option value="inactive" <?= ($filters['status'] ?? '') === 'inactive' ? 'selected' : '' ?>>Inactive</option>
            </select>
        </div>
        <div>
            <button type="submit" class="w-full md:w-auto bg-gray-800 hover:bg-gray-700 text-white font-bold py-2 px-6 rounded">Filter</button>
        </div>
    </form>
</div>

<!-- Table -->
<div class="bg-white rounded-lg shadow overflow-hidden">
    <div class="overflow-x-auto">
    <table class="min-w-full divide-y divide-gray-200">
        <thead class="bg-gray-50">
            <tr>
                <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">User</th>
                <?php if (!empty($isPlatformView)): ?><th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Organization</th><?php endif; ?>
                <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Type</th>
                <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Roles</th>
                <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Status</th>
                <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Last Login</th>
                <th scope="col" class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">Actions</th>
            </tr>
        </thead>
        <tbody class="bg-white divide-y divide-gray-200">
            <?php if (empty($users)): ?>
            <tr><td colspan="<?= !empty($isPlatformView) ? 7 : 6 ?>" class="px-6 py-12 text-center text-gray-500">No users found.</td></tr>
            <?php else: ?>
                <?php foreach ($users as $user): ?>
                <?php 
                    $initials = strtoupper(substr($user['first_name'] ?? 'U', 0, 1) . substr($user['last_name'] ?? '', 0, 1));
                    $typeClass = 'bg-gray-100 text-gray-800';
                    $typeLabel = 'Unknown';
                    if (($user['entity_type'] ?? '') === 'org_user') { $typeClass = 'bg-blue-100 text-blue-800'; $typeLabel = 'Org'; }
                    if (($user['entity_type'] ?? '') === 'contractor_user') { $typeClass = 'bg-green-100 text-green-800'; $typeLabel = 'Contractor'; }
                    if (($user['entity_type'] ?? '') === 'supplier_user') { $typeClass = 'bg-purple-100 text-purple-800'; $typeLabel = 'Supplier'; }
                    if (($user['is_super_admin'] ?? false)) { $typeClass = 'bg-red-100 text-red-800'; $typeLabel = 'Super Admin'; }
                ?>
                <tr class="hover:bg-gray-50">
                    <td class="px-6 py-4 whitespace-nowrap">
                        <div class="flex items-center">
                            <div class="flex-shrink-0 h-10 w-10">
                                <div class="h-10 w-10 rounded-full bg-[#0A3A6B] text-white flex items-center justify-center font-bold text-sm">
                                    <?= $initials ?>
                                </div>
                            </div>
                            <div class="ml-4">
                                <div class="text-sm font-medium text-gray-900"><?= htmlspecialchars(($user['first_name'] ?? '') . ' ' . ($user['last_name'] ?? '')) ?></div>
                                <div class="text-sm text-gray-500"><?= htmlspecialchars($user['email'] ?? '') ?></div>
                            </div>
                        </div>
                    </td>
                    <?php if (!empty($isPlatformView)): ?><td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500"><?= htmlspecialchars($user['org_name'] ?? '-') ?></td><?php endif; ?>
                    <td class="px-6 py-4 whitespace-nowrap">
                        <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full <?= $typeClass ?>"><?= $typeLabel ?></span>
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                        <?= htmlspecialchars($user['roles'] ?: '-') ?>
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap">
                        <?php if (!empty($user['is_active'])): ?>
                            <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-green-100 text-green-800">Active</span>
                        <?php else: ?>
                            <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-red-100 text-red-800">Inactive</span>
                        <?php endif; ?>
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                        <?= htmlspecialchars($user['last_login_at'] ?? 'Never') ?>
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                        <a href="<?= APP_URL ?>/users/<?= $user['id'] ?>" class="text-[#0A3A6B] hover:text-blue-900 mr-3">View</a>
                        <a href="<?= APP_URL ?>/users/<?= $user['id'] ?>/edit" class="text-gray-600 hover:text-gray-900">Edit</a>
                    </td>
                </tr>
                <?php endforeach; ?>
            <?php endif; ?>
        </tbody>
    </table>
    </div>
    <div class="px-6 py-4 border-t border-gray-200">
        <?php @include ROOT_PATH . '/modules/shared/views/partials/pagination.php'; ?>
    </div>
</div>
<?php
$content = ob_get_clean();
require ROOT_PATH . '/modules/shared/views/layouts/main.php';
?>