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

    <!-- App Header -->
    <div class="appHeader bg-primary text-light">
        <div class="left">
            <a href="#" class="headerButton" data-bs-toggle="modal" data-bs-target="#adminSidebar">
                <ion-icon name="menu-outline"></ion-icon>
            </a>
        </div>
        <div class="pageTitle">Manage Users</div>
        <div class="right">
            <a href="<?php echo base_url('admin/create_user'); ?>" class="headerButton">
                <ion-icon name="add-outline"></ion-icon>
            </a>
        </div>
    </div>

    <!-- App Capsule -->
    <div id="appCapsule">
        
        <!-- Search & Filter -->
        <div class="section mt-2">
            <div class="card">
                <div class="card-body">
                    <form method="GET" action="">
                        <div class="row">
                            <div class="col-8">
                                <div class="form-group basic">
                                    <input type="text" name="search" class="form-control" placeholder="Search users..." value="<?php echo htmlspecialchars($filters['search'] ?? ''); ?>">
                                </div>
                            </div>
                            <div class="col-4">
                                <button type="submit" class="btn btn-primary w-100">
                                    <ion-icon name="search-outline"></ion-icon>
                                </button>
                            </div>
                        </div>
                        <div class="row mt-1">
                            <div class="col-12">
                                <select name="status" class="form-control" onchange="this.form.submit()">
                                    <option value="">All Status</option>
                                    <option value="active" <?php echo ($filters['status'] ?? '') == 'active' ? 'selected' : ''; ?>>Active</option>
                                    <option value="pending_kyc" <?php echo ($filters['status'] ?? '') == 'pending_kyc' ? 'selected' : ''; ?>>Pending KYC</option>
                                    <option value="suspended" <?php echo ($filters['status'] ?? '') == 'suspended' ? 'selected' : ''; ?>>Suspended</option>
                                    <option value="inactive" <?php echo ($filters['status'] ?? '') == 'inactive' ? 'selected' : ''; ?>>Inactive</option>
                                </select>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <!-- Users List -->
        <div class="section mt-2 mb-3">
            <div class="section-title">Users (<?php echo number_format($total); ?>)</div>
            
            <?php if (empty($users)): ?>
            <div class="card">
                <div class="card-body text-center py-4">
                    <ion-icon name="people-outline" style="font-size: 48px; color: #ccc;"></ion-icon>
                    <p class="text-muted mt-2 mb-0">No users found</p>
                </div>
            </div>
            <?php else: ?>
            
            <ul class="listview image-listview">
                <?php foreach ($users as $u): ?>
                <li>
                    <a href="<?php echo base_url('admin/user/' . $u->id); ?>" class="item">
                        <img src="<?php echo get_user_avatar($u, 'w36'); ?>" alt="avatar" class="image rounded">
                        <div class="in">
                            <div>
                                <strong><?php echo htmlspecialchars($u->first_name . ' ' . $u->last_name); ?></strong>
                                <p class="mb-0 small text-muted"><?php echo htmlspecialchars($u->email); ?></p>
                            </div>
                            <div>
                                <?php if (isset($u->kyc_status) && $u->kyc_status === 'pending'): ?>
                                <span class="badge badge-warning mb-1">Pending KYC</span>
                                <?php endif; ?>
                                <span class="badge badge-<?php 
                                    echo $u->status == 'active' ? 'success' : 
                                        ($u->status == 'pending' ? 'warning' : 
                                        ($u->status == 'suspended' ? 'danger' : 'secondary')); 
                                ?>">
                                    <?php echo ucfirst($u->status); ?>
                                </span>
                            </div>
                        </div>
                    </a>
                </li>
                <?php endforeach; ?>
            </ul>
            
            <!-- Pagination -->
            <?php if ($pages > 1): ?>
            <div class="mt-2 text-center">
                <div class="btn-group">
                    <?php if ($page > 1): ?>
                    <a href="?page=<?php echo $page - 1; ?>&status=<?php echo urlencode($filters['status'] ?? ''); ?>&search=<?php echo urlencode($filters['search'] ?? ''); ?>" class="btn btn-secondary btn-sm">
                        <ion-icon name="chevron-back-outline"></ion-icon>
                    </a>
                    <?php endif; ?>
                    
                    <button class="btn btn-secondary btn-sm" disabled>
                        Page <?php echo $page; ?> of <?php echo $pages; ?>
                    </button>
                    
                    <?php if ($page < $pages): ?>
                    <a href="?page=<?php echo $page + 1; ?>&status=<?php echo urlencode($filters['status'] ?? ''); ?>&search=<?php echo urlencode($filters['search'] ?? ''); ?>" class="btn btn-secondary btn-sm">
                        <ion-icon name="chevron-forward-outline"></ion-icon>
                    </a>
                    <?php endif; ?>
                </div>
            </div>
            <?php endif; ?>
            
            <?php endif; ?>
        </div>
    </div>

    <!-- Admin Sidebar -->
    <?php $this->load->view('admin/partials/sidebar'); ?>

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