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

<?php
$u = $user;
$name = $u['displayName'] !== '' ? $u['displayName'] : ($u['username'] !== '' ? $u['username'] : 'Teen');
$posts = isset($posts) && is_array($posts) ? $posts : array();
$audit = isset($audit) && is_array($audit) ? $audit : array();
$reports_received = isset($reports_received) && is_array($reports_received) ? $reports_received : array();
$reports_submitted = isset($reports_submitted) && is_array($reports_submitted) ? $reports_submitted : array();
if (!function_exists('tu_initials')) {
	function tu_initials($name) {
		$parts = preg_split('/\s+/', trim((string) $name));
		$a = isset($parts[0][0]) ? $parts[0][0] : '?';
		$b = (count($parts) > 1 && isset($parts[count($parts) - 1][0])) ? $parts[count($parts) - 1][0] : '';
		return strtoupper($a . $b);
	}
}
if (!function_exists('tu_st')) {
	function tu_st($s) {
		$map = array('active' => 'ca-status--active', 'suspended' => 'ca-status--suspended', 'blocked' => 'ca-status--danger', 'deleted' => 'ca-status--muted');
		return isset($map[$s]) ? $map[$s] : 'ca-status--muted';
	}
}
?>

<div class="care-admin">
	<div class="ca-hero">
		<div class="ca-hero-inner row align-items-center g-3">
			<div class="col-lg-7">
				<p class="ca-kicker">Users · Profile</p>
				<div class="d-flex align-items-center gap-3 mb-2">
					<?php if (!empty($u['photoUrl'])): ?>
						<img class="tu-avatar tu-avatar--lg" src="<?php echo htmlspecialchars($u['photoUrl']); ?>" alt="">
					<?php else: ?>
						<span class="tu-avatar tu-avatar--lg tu-avatar--fallback"><?php echo htmlspecialchars(tu_initials($name)); ?></span>
					<?php endif; ?>
					<div>
						<h1 class="ca-title mb-1"><?php echo htmlspecialchars($name); ?></h1>
						<p class="ca-sub mb-0"><?php echo $u['username'] !== '' ? '@' . htmlspecialchars($u['username']) : htmlspecialchars($u['email']); ?></p>
					</div>
				</div>
				<div class="d-flex flex-wrap gap-2">
					<span class="ca-status <?php echo tu_st($u['status']); ?>"><?php echo htmlspecialchars(ucfirst($u['status'])); ?></span>
					<?php if ($u['ageGroup'] !== ''): ?><span class="ca-role-chip"><?php echo htmlspecialchars($u['ageGroup']); ?></span><?php endif; ?>
					<span class="ca-role-chip"><?php echo htmlspecialchars($u['subscription']); ?></span>
				</div>
			</div>
			<div class="col-lg-5">
				<div class="ca-nav justify-content-lg-end flex-wrap">
					<a href="<?php echo base_url('admin/users'); ?>" class="btn btn-light"><i class="fa-solid fa-arrow-left me-2"></i>Back</a>
					<a href="<?php echo base_url('admin/user_edit/' . rawurlencode($u['uid'])); ?>" class="btn btn-primary"><i class="fa-regular fa-pen-to-square me-2"></i>Edit</a>
					<?php if ($u['status'] === 'suspended'): ?>
						<button type="button" class="btn btn-outline-success" data-act="status" data-status="active">Unsuspend</button>
					<?php elseif ($u['status'] !== 'deleted'): ?>
						<button type="button" class="btn btn-outline-warning" data-act="status" data-status="suspended">Suspend</button>
					<?php endif; ?>
					<?php if ($u['status'] !== 'blocked' && $u['status'] !== 'deleted'): ?>
						<button type="button" class="btn btn-outline-danger" data-act="status" data-status="blocked">Block</button>
					<?php endif; ?>
					<?php if ($u['status'] !== 'deleted'): ?>
						<button type="button" class="btn btn-danger" data-act="delete">Delete</button>
					<?php else: ?>
						<button type="button" class="btn btn-outline-success" data-act="status" data-status="active">Restore</button>
					<?php endif; ?>
				</div>
			</div>
		</div>
	</div>

	<div class="row g-3">
		<div class="col-lg-4">
			<div class="ca-card mb-3">
				<div class="ca-card-header"><h5 class="ca-card-title mb-0">Basic information</h5></div>
				<div class="ca-card-body">
					<p class="mb-2"><span class="text-muted small d-block">Display name</span><?php echo htmlspecialchars($u['displayName'] !== '' ? $u['displayName'] : '—'); ?></p>
					<p class="mb-2"><span class="text-muted small d-block">Username</span><?php echo htmlspecialchars($u['username'] !== '' ? $u['username'] : '—'); ?></p>
					<p class="mb-2"><span class="text-muted small d-block">Email</span>
						<?php if ($u['email'] !== ''): ?><a href="mailto:<?php echo htmlspecialchars($u['email']); ?>"><?php echo htmlspecialchars($u['email']); ?></a><?php else: ?>—<?php endif; ?>
					</p>
					<p class="mb-2"><span class="text-muted small d-block">Age group</span><?php echo htmlspecialchars($u['ageGroup'] !== '' ? $u['ageGroup'] : '—'); ?></p>
					<p class="mb-2"><span class="text-muted small d-block">Location</span><?php echo htmlspecialchars($u['locationLabel']); ?></p>
					<p class="mb-2"><span class="text-muted small d-block">Registered</span><?php echo htmlspecialchars($u['createdAtLabel']); ?></p>
					<p class="mb-0"><span class="text-muted small d-block">Account status</span><span class="ca-status <?php echo tu_st($u['status']); ?>"><?php echo htmlspecialchars(ucfirst($u['status'])); ?></span></p>
				</div>
			</div>
			<div class="ca-card mb-3">
				<div class="ca-card-header"><h5 class="ca-card-title mb-0">Interests</h5></div>
				<div class="ca-card-body">
					<?php if (empty($u['interests'])): ?>
						<p class="text-muted mb-0">No interests listed.</p>
					<?php else: ?>
						<?php foreach ($u['interests'] as $tag): ?>
							<span class="ca-role-chip"><?php echo htmlspecialchars($tag); ?></span>
						<?php endforeach; ?>
					<?php endif; ?>
					<?php if (!empty($u['lookingFor'])): ?>
						<p class="text-muted small mt-3 mb-1">Looking for</p>
						<?php foreach ($u['lookingFor'] as $tag): ?>
							<span class="ca-role-chip"><?php echo htmlspecialchars($tag); ?></span>
						<?php endforeach; ?>
					<?php endif; ?>
					<?php if ($u['bio'] !== ''): ?>
						<p class="text-muted small mt-3 mb-1">Bio</p>
						<p class="mb-0"><?php echo nl2br(htmlspecialchars($u['bio'])); ?></p>
					<?php endif; ?>
				</div>
			</div>
			<div class="ca-card">
				<div class="ca-card-header"><h5 class="ca-card-title mb-0">Activity</h5></div>
				<div class="ca-card-body">
					<div class="row g-2">
						<div class="col-6"><div class="ca-stat"><p class="ca-stat-label">Posts</p><div class="ca-stat-value"><?php echo (int) $u['postsCount']; ?></div></div></div>
						<div class="col-6"><div class="ca-stat"><p class="ca-stat-label">Photos</p><div class="ca-stat-value"><?php echo (int) $u['photosCount']; ?></div></div></div>
						<div class="col-6"><div class="ca-stat"><p class="ca-stat-label">Groups</p><div class="ca-stat-value">—</div></div></div>
						<div class="col-6"><div class="ca-stat"><p class="ca-stat-label">Activities</p><div class="ca-stat-value"><?php echo (int) $u['activitiesCount']; ?></div></div></div>
						<div class="col-6"><div class="ca-stat"><p class="ca-stat-label">Reports received</p><div class="ca-stat-value"><?php echo count($reports_received); ?></div></div></div>
						<div class="col-6"><div class="ca-stat"><p class="ca-stat-label">Reports submitted</p><div class="ca-stat-value"><?php echo count($reports_submitted); ?></div></div></div>
					</div>
					<p class="small text-muted mt-3 mb-0">Connections: <?php echo (int) $u['connectionsCount']; ?> · Subscription: <?php echo htmlspecialchars($u['subscription']); ?></p>
				</div>
			</div>
		</div>
		<div class="col-lg-8">
			<div class="ca-card mb-3">
				<div class="ca-card-header">
					<h5 class="ca-card-title mb-1">Public posts</h5>
					<p class="mb-0">Captions and photo previews only. Private conversations are not shown.</p>
				</div>
				<div class="ca-card-body p-0">
					<?php if (empty($posts)): ?>
						<div class="tu-empty"><i class="fa-regular fa-image"></i><div class="fw-semibold">No posts</div></div>
					<?php else: ?>
						<div class="ca-table-wrap border-0 rounded-0 border-top">
							<table class="table ca-table mb-0 align-middle">
								<thead><tr><th>Preview</th><th>Caption</th><th>Topics</th><th>When</th></tr></thead>
								<tbody>
									<?php foreach ($posts as $p): ?>
										<tr>
											<td>
												<?php if ($p['preview'] !== ''): ?>
													<img src="<?php echo htmlspecialchars($p['preview']); ?>" alt="" style="width:56px;height:56px;object-fit:cover;border-radius:10px;">
												<?php else: ?><span class="text-muted">—</span><?php endif; ?>
											</td>
											<td class="small"><?php echo htmlspecialchars($p['caption'] !== '' ? $p['caption'] : $p['category']); ?></td>
											<td><?php foreach ($p['topics'] as $t): ?><span class="ca-role-chip"><?php echo htmlspecialchars($t); ?></span><?php endforeach; ?></td>
											<td class="small text-muted"><?php echo htmlspecialchars($p['createdAtLabel']); ?></td>
										</tr>
									<?php endforeach; ?>
								</tbody>
							</table>
						</div>
					<?php endif; ?>
				</div>
			</div>
			<div class="ca-card mb-3">
				<div class="ca-card-header"><h5 class="ca-card-title mb-0">Moderation history</h5></div>
				<div class="ca-card-body p-0">
					<?php if (empty($audit)): ?>
						<div class="tu-empty"><i class="fa-regular fa-clipboard"></i><div class="fw-semibold">No admin actions on this account yet</div></div>
					<?php else: ?>
						<div class="ca-table-wrap border-0 rounded-0 border-top">
							<table class="table ca-table mb-0">
								<thead><tr><th>When</th><th>Admin</th><th>Action</th><th>Notes</th></tr></thead>
								<tbody>
									<?php foreach ($audit as $row): ?>
										<tr>
											<td class="small text-muted"><?php echo htmlspecialchars($row['createdAtLabel']); ?></td>
											<td><?php echo htmlspecialchars($row['adminName']); ?></td>
											<td><?php echo htmlspecialchars($row['action']); ?></td>
											<td class="small"><?php echo htmlspecialchars($row['description']); ?></td>
										</tr>
									<?php endforeach; ?>
								</tbody>
							</table>
						</div>
					<?php endif; ?>
				</div>
			</div>
			<div class="ca-card">
				<div class="ca-card-header"><h5 class="ca-card-title mb-0">Account history</h5></div>
				<div class="ca-card-body">
					<p class="mb-2"><span class="text-muted small d-block">Created</span><?php echo htmlspecialchars($u['createdAtLabel']); ?></p>
					<p class="mb-0"><span class="text-muted small d-block">Last updated</span><?php echo htmlspecialchars($u['updatedAtLabel']); ?></p>
				</div>
			</div>
		</div>
	</div>
</div>

<script>
(function () {
	var uid = <?php echo json_encode($u['uid']); ?>;
	var name = <?php echo json_encode($name); ?>;
	document.querySelectorAll('[data-act]').forEach(function (btn) {
		btn.addEventListener('click', function () {
			var act = btn.getAttribute('data-act');
			if (act === 'delete') {
				if (!confirm('Mark "' + name + '" as deleted? This can be restored later.')) return;
				fetch(<?php echo json_encode(base_url('admin/user_delete/')); ?> + encodeURIComponent(uid), {
					method: 'POST', headers: { 'Accept': 'application/json' }
				}).then(function (r) { return r.json(); }).then(function (j) {
					if (!j || !j.ok) throw new Error((j && j.error) || 'Failed');
					window.location.href = <?php echo json_encode(base_url('admin/users')); ?>;
				}).catch(function (e) { alert(e.message); });
				return;
			}
			var st = btn.getAttribute('data-status');
			if (!confirm('Set this user to ' + st + '?')) return;
			var body = new FormData();
			body.append('status', st);
			fetch(<?php echo json_encode(base_url('admin/user_status/')); ?> + encodeURIComponent(uid), {
				method: 'POST', headers: { 'Accept': 'application/json' }, body: body
			}).then(function (r) { return r.json(); }).then(function (j) {
				if (!j || !j.ok) throw new Error((j && j.error) || 'Failed');
				window.location.reload();
			}).catch(function (e) { alert(e.message); });
		});
	});
})();
</script>
