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

<?php
$u = isset($user) && is_array($user) ? $user : array();
function h($v) { return htmlspecialchars((string) $v, ENT_QUOTES, 'UTF-8'); }
$uid = isset($u['uid']) ? (string) $u['uid'] : '';
$fullName = trim((isset($u['firstName']) ? $u['firstName'] : '') . ' ' . (isset($u['lastName']) ? $u['lastName'] : ''));
$fullName = $fullName !== '' ? $fullName : ($uid !== '' ? $uid : 'User');
?>

<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">AOUST portal</p>
				<h1 class="ca-title">Edit user</h1>
				<p class="ca-sub mb-0"><?php echo h($fullName); ?> · Firestore <strong>Users</strong></p>
			</div>
			<div class="col-lg-4">
				<div class="ca-nav justify-content-lg-end">
					<a class="btn btn-light border" href="<?php echo base_url('admin/aoust_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>Details</h5>
		</div>
		<div class="ca-card-body">
			<form id="formUserEdit" method="post" action="<?php echo base_url('admin/aoust_user_update/' . rawurlencode($uid)); ?>">
				<div class="row g-3">
					<div class="col-12 col-md-6">
						<label class="form-label">First name</label>
						<input class="form-control" name="firstName" value="<?php echo h(isset($u['firstName']) ? $u['firstName'] : ''); ?>" required>
					</div>
					<div class="col-12 col-md-6">
						<label class="form-label">Last name</label>
						<input class="form-control" name="lastName" value="<?php echo h(isset($u['lastName']) ? $u['lastName'] : ''); ?>" 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 h(isset($u['email']) ? $u['email'] : ''); ?>">
					</div>
					<div class="col-12 col-md-6">
						<label class="form-label">Phone</label>
						<input class="form-control" name="phone" value="<?php echo h(isset($u['phone']) ? $u['phone'] : ''); ?>">
					</div>

					<div class="col-12 col-md-6">
						<label class="form-label">User type</label>
						<select class="form-select" name="userType" required>
							<?php $t = isset($u['userType']) ? (string) $u['userType'] : 'au_pair'; ?>
							<option value="au_pair" <?php echo $t === 'au_pair' ? 'selected' : ''; ?>>Au pair</option>
							<option value="family_host" <?php echo $t === 'family_host' ? 'selected' : ''; ?>>Family host</option>
							<option value="agency" <?php echo $t === 'agency' ? 'selected' : ''; ?>>Agency</option>
						</select>
					</div>
					<div class="col-12 col-md-6">
						<label class="form-label">Status</label>
						<div class="d-flex flex-wrap gap-3 align-items-center">
							<div class="form-check form-switch m-0">
								<input class="form-check-input" type="checkbox" role="switch" id="isActive" name="isActive" <?php echo !empty($u['isActive']) ? 'checked' : ''; ?>>
								<label class="form-check-label" for="isActive">Active</label>
							</div>
							<div class="form-check form-switch m-0">
								<input class="form-check-input" type="checkbox" role="switch" id="allowNotification" name="allowNotification" <?php echo !empty($u['allowNotification']) ? 'checked' : ''; ?>>
								<label class="form-check-label" for="allowNotification">Allow notification</label>
							</div>
							<div class="form-check form-switch m-0">
								<input class="form-check-input" type="checkbox" role="switch" id="emailNotification" name="emailNotification" <?php echo !empty($u['emailNotification']) ? 'checked' : ''; ?>>
								<label class="form-check-label" for="emailNotification">Email notification</label>
							</div>
						</div>
					</div>

					<div class="col-12">
						<label class="form-label">Address</label>
						<input class="form-control" name="address" value="<?php echo h(isset($u['address']) ? $u['address'] : ''); ?>">
					</div>
					<div class="col-12 col-md-4">
						<label class="form-label">City</label>
						<input class="form-control" name="city" value="<?php echo h(isset($u['city']) ? $u['city'] : ''); ?>">
					</div>
					<div class="col-12 col-md-4">
						<label class="form-label">Postal code</label>
						<input class="form-control" name="postalCode" value="<?php echo h(isset($u['postalCode']) ? $u['postalCode'] : ''); ?>">
					</div>
					<div class="col-12 col-md-4">
						<label class="form-label">Country</label>
						<input class="form-control" name="countryName" value="<?php echo h(isset($u['countryName']) ? $u['countryName'] : ''); ?>">
					</div>
					<div class="col-12 col-md-4">
						<label class="form-label">Country code</label>
						<input class="form-control" name="countryCode" value="<?php echo h(isset($u['countryCode']) ? $u['countryCode'] : ''); ?>">
					</div>
				</div>
			</form>
		</div>
	</div>
</div>

