<link href="<?php echo base_url('assets/css/admin-panel.css'); ?>?v=20260826b" rel="stylesheet">

<div class="care-admin">
	<div class="ca-hero">
		<div class="ca-hero-inner row align-items-center g-3">
			<div class="col-lg-8">
				<p class="ca-kicker">Sleeper</p>
				<h1 class="ca-title"><?php echo isset($page_title) ? htmlspecialchars($page_title) : 'Dashboard'; ?></h1>
				<p class="ca-sub mb-0">Live snapshot of app users with role <code>user</code>.</p>
			</div>
			<div class="col-lg-4">
				<div class="ca-nav justify-content-lg-end">
					<a class="btn btn-light" href="<?php echo base_url('admin/users'); ?>">Users</a>
					<a class="btn btn-primary" href="<?php echo base_url('admin/payments'); ?>">Payments</a>
				</div>
			</div>
		</div>
	</div>

	<div class="row g-3 mb-4">
		<div class="col-12 col-md-6 col-xl-3">
			<div class="ca-stat h-100">
				<div class="ca-stat-icon"><i class="fa-solid fa-users"></i></div>
				<p class="ca-stat-label">Total users</p>
				<div class="display-6 fw-bold mb-0"><?php echo (int) $kpis['total']; ?></div>
			</div>
		</div>
		<div class="col-12 col-md-6 col-xl-3">
			<div class="ca-stat h-100">
				<div class="ca-stat-icon"><i class="fa-solid fa-circle-check"></i></div>
				<p class="ca-stat-label">Admin verified</p>
				<div class="display-6 fw-bold mb-0"><?php echo (int) $kpis['verified']; ?></div>
			</div>
		</div>
		<div class="col-12 col-md-6 col-xl-3">
			<div class="ca-stat h-100">
				<div class="ca-stat-icon"><i class="fa-solid fa-hourglass-half"></i></div>
				<p class="ca-stat-label">Pending verify</p>
				<div class="display-6 fw-bold mb-0"><?php echo (int) $kpis['pending']; ?></div>
			</div>
		</div>
		<div class="col-12 col-md-6 col-xl-3">
			<div class="ca-stat h-100">
				<div class="ca-stat-icon"><i class="fa-solid fa-coins"></i></div>
				<p class="ca-stat-label">Total coins</p>
				<div class="display-6 fw-bold mb-0"><?php echo (int) $kpis['points']; ?></div>
			</div>
		</div>
	</div>

	<div class="row g-3 mb-4">
		<div class="col-md-6">
			<div class="ca-stat h-100">
				<div class="ca-stat-icon"><i class="fa-brands fa-android"></i></div>
				<p class="ca-stat-label">Android devices</p>
				<div class="display-6 fw-bold mb-0"><?php echo (int) $kpis['android']; ?></div>
			</div>
		</div>
		<div class="col-md-6">
			<div class="ca-stat h-100">
				<div class="ca-stat-icon"><i class="fa-solid fa-clapperboard"></i></div>
				<p class="ca-stat-label">Ads watched</p>
				<div class="display-6 fw-bold mb-0"><?php echo (int) $kpis['ads']; ?></div>
			</div>
		</div>
	</div>

	<div class="ca-card">
		<div class="ca-card-header d-flex flex-column flex-md-row align-items-md-center justify-content-md-between gap-2">
			<div>
				<h5 class="ca-card-title mb-1"><i class="fa-solid fa-clock-rotate-left me-2 text-success"></i>Recent users</h5>
				<p class="mb-0">Newest sign-ups from the users collection.</p>
			</div>
			<a class="btn btn-sm btn-light border rounded-pill px-3" href="<?php echo base_url('admin/users'); ?>">View all</a>
		</div>
		<div class="ca-card-body p-0">
			<div class="ca-table-wrap border-0 rounded-0 border-top">
				<table class="table ca-table table-striped mb-0 align-middle">
					<thead>
						<tr>
							<th>Name</th>
							<th>Email</th>
							<th>Device</th>
							<th>Coins</th>
							<th>Verified</th>
							<th>Joined</th>
							<th class="text-end">Action</th>
						</tr>
					</thead>
					<tbody>
						<?php if (empty($recent_users)): ?>
							<tr>
								<td colspan="7" class="text-center text-muted py-4">No users found.</td>
							</tr>
						<?php else: ?>
							<?php foreach ($recent_users as $u): ?>
								<tr>
									<td class="fw-semibold"><?php echo htmlspecialchars($u['userName'] !== '' ? $u['userName'] : '—'); ?></td>
									<td class="small"><?php echo htmlspecialchars($u['email']); ?></td>
									<td><span class="badge bg-light text-dark border"><?php echo htmlspecialchars($u['deviceType'] !== '' ? $u['deviceType'] : '—'); ?></span></td>
									<td><?php echo (int) $u['points']; ?></td>
									<td>
										<?php if (!empty($u['isAdminVerified'])): ?>
											<span class="badge bg-success-subtle text-success border border-success-subtle">Verified</span>
										<?php else: ?>
											<span class="badge bg-warning-subtle text-warning border border-warning-subtle">Pending</span>
										<?php endif; ?>
									</td>
									<td class="small text-muted"><?php echo htmlspecialchars($u['createdAtLabel']); ?></td>
									<td class="text-end">
										<a class="btn btn-sm btn-light border" href="<?php echo base_url('admin/user_view/' . rawurlencode($u['uid'])); ?>">View</a>
										<a class="btn btn-sm btn-light border" href="<?php echo base_url('admin/user_edit/' . rawurlencode($u['uid'])); ?>">Edit</a>
									</td>
								</tr>
							<?php endforeach; ?>
						<?php endif; ?>
					</tbody>
				</table>
			</div>
		</div>
	</div>
</div>
