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

<?php
$u = $user;
$uid = isset($u['uid']) ? (string) $u['uid'] : '';
?>

<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">Customers · Users</p>
				<h1 class="ca-title">Edit user</h1>
				<p class="ca-sub mb-0">Firestore profile plus Firebase Auth email/password.</p>
			</div>
			<div class="col-lg-4">
				<div class="ca-nav justify-content-lg-end flex-wrap">
					<a class="btn btn-light" href="<?php echo base_url('admin/user_view/' . rawurlencode($uid)); ?>">Cancel</a>
					<button form="formUserEdit" type="submit" class="btn btn-primary">Save changes</button>
				</div>
			</div>
		</div>
	</div>

	<div class="ca-card">
		<div class="ca-card-header">
			<h5 class="ca-card-title mb-0"><i class="fa-regular fa-pen-to-square me-2 text-success"></i>Account</h5>
		</div>
		<div class="ca-card-body">
			<form id="formUserEdit" method="post" action="<?php echo base_url('admin/user_update/' . rawurlencode($uid)); ?>" autocomplete="off">
				<input type="hidden" name="collection" value="<?php echo htmlspecialchars($u['_collection']); ?>">
				<div class="row g-3">
					<div class="col-12 col-md-6">
						<label class="form-label">Username</label>
						<input class="form-control" name="userName" value="<?php echo htmlspecialchars($u['userName']); ?>" required>
					</div>
					<div class="col-12 col-md-6">
						<label class="form-label">Email</label>
						<input class="form-control" type="email" name="email" value="<?php echo htmlspecialchars($u['email']); ?>" required>
						<div class="form-text">Changing this also updates Firebase Auth.</div>
					</div>
					<div class="col-12 col-md-6">
						<label class="form-label">New password</label>
						<input class="form-control" type="password" name="password" minlength="6" autocomplete="new-password" placeholder="Leave blank to keep current">
						<div class="form-text">Min 6 characters. Updates Auth and the user document.</div>
					</div>
					<div class="col-12 col-md-6">
						<label class="form-label">Device</label>
						<input class="form-control" name="deviceType" value="<?php echo htmlspecialchars($u['deviceType']); ?>">
					</div>
					<div class="col-12 col-md-4">
						<label class="form-label">Coins</label>
						<input class="form-control" type="number" min="0" name="points" value="<?php echo (int) $u['points']; ?>">
					</div>
					<div class="col-12 col-md-4">
						<label class="form-label">Ads watched</label>
						<input class="form-control" type="number" min="0" name="adWatchCount" value="<?php echo (int) $u['adWatchCount']; ?>">
					</div>
					<div class="col-12 col-md-4 d-flex align-items-end">
						<div class="form-check form-switch mb-2">
							<input class="form-check-input" type="checkbox" role="switch" id="isAdminVerified" name="isAdminVerified" value="1" <?php echo !empty($u['isAdminVerified']) ? 'checked' : ''; ?>>
							<label class="form-check-label" for="isAdminVerified">Admin verified</label>
						</div>
					</div>
					<div class="col-12">
						<p class="small text-muted mb-0">User ID: <code><?php echo htmlspecialchars($u['userId']); ?></code></p>
					</div>
				</div>
			</form>
		</div>
	</div>
</div>
