<?php $this->load->view('superadmin/layouts/header'); ?>

<!-- Page Header -->
<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h3 class="mb-1">All Users</h3>
        <p class="text-muted mb-0">View and manage users across all portals</p>
    </div>
    <div class="d-flex gap-2">
        <a href="<?= base_url('superadmin/users/create') ?>" class="btn btn-sa-primary">
            <i class="bi bi-person-plus me-2"></i>Add New User
        </a>
        <div class="btn-group">
            <button class="btn btn-outline-secondary dropdown-toggle" data-bs-toggle="dropdown">
                <i class="bi bi-funnel me-2"></i>Filter by Portal
            </button>
            <ul class="dropdown-menu dropdown-menu-end">
                <li><a class="dropdown-item <?= empty($selected_portal) ? 'active' : '' ?>" href="<?= base_url('superadmin/users') ?>">All Portals</a></li>
                <li><hr class="dropdown-divider"></li>
                <?php if(!empty($portals)): ?>
                <?php foreach($portals as $portal): ?>
                <li><a class="dropdown-item <?= $selected_portal == $portal->code ? 'active' : '' ?>" href="<?= base_url('superadmin/users?portal=' . $portal->code) ?>"><?= htmlspecialchars($portal->name) ?></a></li>
                <?php endforeach; ?>
                <?php endif; ?>
            </ul>
        </div>
    </div>
</div>

<!-- Flash Messages -->
<?php if($this->session->flashdata('success')): ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
    <i class="bi bi-check-circle me-2"></i><?= $this->session->flashdata('success') ?>
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>

<?php if($this->session->flashdata('error')): ?>
<div class="alert alert-danger alert-dismissible fade show" role="alert">
    <i class="bi bi-exclamation-circle me-2"></i><?= $this->session->flashdata('error') ?>
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>

<!-- Stats Cards -->
<div class="row mb-4">
    <div class="col-md-3">
        <div class="stat-card">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <p class="stat-label mb-1">Total Users</p>
                    <h3 class="stat-value mb-0"><?= number_format($stats['total'] ?? 0) ?></h3>
                </div>
                <div class="stat-icon bg-gradient-primary">
                    <i class="bi bi-people-fill"></i>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="stat-card">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <p class="stat-label mb-1">Active Users</p>
                    <h3 class="stat-value mb-0"><?= number_format($stats['active'] ?? 0) ?></h3>
                </div>
                <div class="stat-icon bg-gradient-success">
                    <i class="bi bi-person-check-fill"></i>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="stat-card">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <p class="stat-label mb-1">Admins</p>
                    <h3 class="stat-value mb-0"><?= number_format($stats['admins'] ?? 0) ?></h3>
                </div>
                <div class="stat-icon bg-gradient-warning">
                    <i class="bi bi-shield-fill"></i>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="stat-card">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <p class="stat-label mb-1">Inactive</p>
                    <h3 class="stat-value mb-0"><?= number_format($stats['inactive'] ?? 0) ?></h3>
                </div>
                <div class="stat-icon bg-gradient-danger">
                    <i class="bi bi-person-x-fill"></i>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Users Table -->
<div class="table-card">
    <div class="card-header d-flex justify-content-between align-items-center">
        <h5><i class="bi bi-people me-2"></i>Users List</h5>
        <?php if(!empty($selected_portal)): ?>
        <span class="badge bg-primary">Filtered: <?= htmlspecialchars($selected_portal) ?></span>
        <?php endif; ?>
    </div>
    <div class="card-body">
        <table class="table table-hover datatable">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Name</th>
                    <th>Email</th>
                    <th>Portal</th>
                    <th>Role</th>
                    <th>Status</th>
                    <th>Created</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php if(!empty($users)): ?>
                <?php foreach($users as $user): ?>
                <tr>
                    <td><?= $user->id ?></td>
                    <td>
                        <div class="d-flex align-items-center">
                            <div class="user-avatar me-2" style="width: 35px; height: 35px; font-size: 0.8rem;">
                                <?= strtoupper(substr($user->username ?? 'U', 0, 1)) ?>
                            </div>
                            <div>
                                <strong><?= htmlspecialchars($user->username ?? '') ?></strong>
                            </div>
                        </div>
                    </td>
                    <td><?= htmlspecialchars($user->email ?? '-') ?></td>
                    <td>
                        <span class="badge bg-secondary"><?= htmlspecialchars($user->portal_name ?? $user->portal_code ?? 'Unknown') ?></span>
                    </td>
                    <td>
                        <?php if(($user->role ?? '') == 'admin'): ?>
                        <span class="badge bg-warning text-dark"><i class="bi bi-shield-fill me-1"></i>Admin</span>
                        <?php else: ?>
                        <span class="badge bg-light text-dark">User</span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <?php if(($user->status ?? 'active') == 'active'): ?>
                        <span class="portal-status active">Active</span>
                        <?php else: ?>
                        <span class="portal-status inactive">Inactive</span>
                        <?php endif; ?>
                    </td>
                    <td><?= date('M j, Y', strtotime($user->created_at ?? 'now')) ?></td>
                    <td>
                        <div class="btn-group btn-group-sm">
                            <a href="<?= base_url('superadmin/users/edit/' . $user->id) ?>" class="btn btn-outline-primary" title="Edit">
                                <i class="bi bi-pencil"></i>
                            </a>
                            <a href="<?= base_url('superadmin/users/toggle_status/' . $user->id) ?>" class="btn btn-outline-<?= $user->status == 'active' ? 'warning' : 'success' ?>" title="<?= $user->status == 'active' ? 'Deactivate' : 'Activate' ?>">
                                <i class="bi bi-<?= $user->status == 'active' ? 'pause-circle' : 'play-circle' ?>"></i>
                            </a>
                            <a href="<?= base_url('superadmin/users/delete/' . $user->id) ?>" class="btn btn-outline-danger" title="Delete" onclick="return confirm('Are you sure you want to delete this user?')">
                                <i class="bi bi-trash"></i>
                            </a>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
                <?php else: ?>
                <tr>
                    <td colspan="8" class="text-center py-4 text-muted">
                        <i class="bi bi-people fs-1 d-block mb-2"></i>
                        No users found
                    </td>
                </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<?php $this->load->view('superadmin/layouts/footer'); ?>
