<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'); }
$fullName = trim((isset($u['firstName']) ? $u['firstName'] : '') . ' ' . (isset($u['lastName']) ? $u['lastName'] : ''));
$fullName = $fullName !== '' ? $fullName : (isset($u['uid']) ? $u['uid'] : 'Au pair');
$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">AOUST portal</p>
				<h1 class="ca-title"><?php echo h($fullName); ?></h1>
				<p class="ca-sub mb-0"><span class="badge bg-success-subtle text-success border border-success-subtle">Au pair</span> · 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_users?type=au_pairs'); ?>">Back</a>
					<a class="btn btn-primary" href="<?php echo base_url('admin/aoust_user_edit/' . rawurlencode($uid)); ?>">Edit</a>
				</div>
			</div>
		</div>
	</div>

	<div class="row g-3">
		<div class="col-12 col-lg-4">
			<div class="ca-card h-100">
				<div class="ca-card-header">
					<h5 class="ca-card-title mb-0"><i class="fa-regular fa-id-badge me-2 text-success"></i>Profile</h5>
				</div>
				<div class="ca-card-body">
					<div class="d-flex align-items-center gap-3">
						<?php if (!empty($u['profileImage'])): ?>
							<img src="<?php echo h($u['profileImage']); ?>" alt="Profile image" style="width:72px;height:72px;border-radius:18px;object-fit:cover;border:1px solid rgba(0,0,0,0.08);">
						<?php else: ?>
							<div style="width:72px;height:72px;border-radius:18px;display:flex;align-items:center;justify-content:center;background:rgba(25,135,84,0.12);border:1px solid rgba(25,135,84,0.22);font-weight:800;color:#0f5132;">
								<?php echo h(strtoupper(substr($fullName, 0, 1))); ?>
							</div>
						<?php endif; ?>
						<div class="min-w-0">
							<div class="fw-semibold"><?php echo h($fullName); ?></div>
							<div class="text-muted small">UID: <span class="font-monospace"><?php echo h($uid); ?></span></div>
							<div class="text-muted small"><?php echo h(isset($u['email']) ? $u['email'] : ''); ?></div>
						</div>
					</div>

					<hr>

					<div class="row g-2">
						<div class="col-6">
							<div class="text-muted small">Status</div>
							<div>
								<?php if (!empty($u['isActive'])): ?>
									<span class="badge bg-success-subtle text-success border border-success-subtle">Active</span>
								<?php else: ?>
									<span class="badge bg-secondary-subtle text-secondary border border-secondary-subtle">Inactive</span>
								<?php endif; ?>
							</div>
						</div>
						<div class="col-6">
							<div class="text-muted small">Verified</div>
							<div>
								<?php if (!empty($u['isVerifiedUser'])): ?>
									<span class="badge bg-primary-subtle text-primary border border-primary-subtle">Yes</span>
								<?php else: ?>
									<span class="badge bg-light text-dark border">No</span>
								<?php endif; ?>
							</div>
						</div>
						<div class="col-6">
							<div class="text-muted small">Gender</div>
							<div class="fw-semibold"><?php echo h(isset($u['gender']) ? $u['gender'] : '—'); ?></div>
						</div>
						<div class="col-6">
							<div class="text-muted small">Smoker</div>
							<div class="fw-semibold">
								<?php echo isset($u['doYouSmoke']) ? ($u['doYouSmoke'] ? 'Yes' : 'No') : '—'; ?>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>

		<div class="col-12 col-lg-8">
			<div class="ca-card">
				<div class="ca-card-header d-flex align-items-center justify-content-between">
					<h5 class="ca-card-title mb-0"><i class="fa-solid fa-location-dot me-2 text-success"></i>Address</h5>
					<?php
					$lat = isset($u['lat']) ? $u['lat'] : null;
					$lon = isset($u['lon']) ? $u['lon'] : null;
					$gmaps = ($lat !== null && $lon !== null) ? ('https://www.google.com/maps?q=' . rawurlencode($lat . ',' . $lon)) : '';
					?>
					<?php if ($gmaps): ?>
						<a class="btn btn-sm btn-light border" target="_blank" rel="noopener noreferrer" href="<?php echo h($gmaps); ?>">
							<i class="fa-solid fa-arrow-up-right-from-square me-1"></i>Open map
						</a>
					<?php endif; ?>
				</div>
				<div class="ca-card-body">
					<div class="row g-3">
						<div class="col-12">
							<div class="text-muted small">Address</div>
							<div class="fw-semibold"><?php echo h(isset($u['address']) ? $u['address'] : '—'); ?></div>
						</div>
						<div class="col-12 col-md-4">
							<div class="text-muted small">City</div>
							<div class="fw-semibold"><?php echo h(isset($u['city']) ? $u['city'] : '—'); ?></div>
						</div>
						<div class="col-12 col-md-4">
							<div class="text-muted small">Postal code</div>
							<div class="fw-semibold"><?php echo h(isset($u['postalCode']) ? $u['postalCode'] : '—'); ?></div>
						</div>
						<div class="col-12 col-md-4">
							<div class="text-muted small">Country</div>
							<div class="fw-semibold"><?php echo h(isset($u['countryName']) ? $u['countryName'] : '—'); ?></div>
						</div>
					</div>
				</div>
			</div>

			<div class="ca-card mt-3">
				<div class="ca-card-header">
					<h5 class="ca-card-title mb-0"><i class="fa-regular fa-file-lines me-2 text-success"></i>Bio</h5>
				</div>
				<div class="ca-card-body">
					<?php echo nl2br(h(isset($u['bio']) ? $u['bio'] : '')); ?>
				</div>
			</div>

			<div class="ca-card mt-3">
				<div class="ca-card-header">
					<h5 class="ca-card-title mb-0"><i class="fa-solid fa-shield-halved me-2 text-success"></i>Documents</h5>
				</div>
				<div class="ca-card-body">
					<div class="row g-2">
						<div class="col-12 col-md-6">
							<div class="p-3 border rounded-3">
								<div class="fw-semibold mb-1">Government ID</div>
								<?php
									$isGovApproved = !empty($u['isApprovedGovernmentID']);
									$govRejectReason = isset($u['governmentIdRejectionReason']) ? trim((string) $u['governmentIdRejectionReason']) : '';
									$hasGovFile = !empty($u['governmentIdUrl']);
									$isGovPending = (!$isGovApproved && $hasGovFile && $govRejectReason === '');
								?>
								<div class="small text-muted mb-2">
									<?php if ($isGovApproved): ?>
										Approved
									<?php elseif ($govRejectReason !== ''): ?>
										Rejected
									<?php else: ?>
										Not approved
									<?php endif; ?>
								</div>
								<?php if (!empty($u['governmentIdUrl'])): ?>
									<a class="btn btn-sm btn-light border" target="_blank" rel="noopener noreferrer" href="<?php echo h($u['governmentIdUrl']); ?>">
										<i class="fa-solid fa-arrow-up-right-from-square me-1"></i>Open file
									</a>
								<?php else: ?>
									<span class="text-muted small">—</span>
								<?php endif; ?>

								<?php if ($govRejectReason !== ''): ?>
									<div class="mt-2 small">
										<div class="text-muted">Rejection reason</div>
										<div class="fw-semibold"><?php echo h($govRejectReason); ?></div>
									</div>
								<?php endif; ?>

								<?php if ($isGovPending): ?>
									<div class="d-flex gap-2 mt-3 flex-wrap">
										<form method="post" action="<?php echo base_url('admin/aoust_user_government_id_approve/' . rawurlencode($uid)); ?>" id="formGovApprove">
											<button type="submit" class="btn btn-sm btn-success">
												<i class="fa-solid fa-check me-1"></i>Approve
											</button>
										</form>
										<button type="button" class="btn btn-sm btn-outline-danger" data-bs-toggle="modal" data-bs-target="#modalGovIdReject">
											<i class="fa-solid fa-xmark me-1"></i>Reject
										</button>
									</div>
								<?php endif; ?>
							</div>
						</div>
						<div class="col-12 col-md-6">
							<div class="p-3 border rounded-3">
								<div class="fw-semibold mb-1">Police check</div>
								<?php
									$isPoliceApproved = !empty($u['isApprovedPoliceChekcurl']);
									$polRejectReason = isset($u['policeChekcRejectionReason']) ? trim((string) $u['policeChekcRejectionReason']) : '';
									$hasPoliceFile = !empty($u['policeChekcUrl']);
									$isPolicePending = (!$isPoliceApproved && $hasPoliceFile && $polRejectReason === '');
								?>
								<div class="small text-muted mb-2">
									<?php if ($isPoliceApproved): ?>
										Approved
									<?php elseif ($polRejectReason !== ''): ?>
										Rejected
									<?php else: ?>
										Not approved
									<?php endif; ?>
								</div>
								<?php if (!empty($u['policeChekcUrl'])): ?>
									<a class="btn btn-sm btn-light border" target="_blank" rel="noopener noreferrer" href="<?php echo h($u['policeChekcUrl']); ?>">
										<i class="fa-solid fa-arrow-up-right-from-square me-1"></i>Open file
									</a>
								<?php else: ?>
									<span class="text-muted small">—</span>
								<?php endif; ?>

								<?php if ($polRejectReason !== ''): ?>
									<div class="mt-2 small">
										<div class="text-muted">Rejection reason</div>
										<div class="fw-semibold"><?php echo h($polRejectReason); ?></div>
									</div>
								<?php endif; ?>

								<?php if ($isPolicePending): ?>
									<div class="d-flex gap-2 mt-3 flex-wrap">
										<form method="post" action="<?php echo base_url('admin/aoust_user_police_check_approve/' . rawurlencode($uid)); ?>" id="formPoliceApprove">
											<button type="submit" class="btn btn-sm btn-success">
												<i class="fa-solid fa-check me-1"></i>Approve
											</button>
										</form>
										<button type="button" class="btn btn-sm btn-outline-danger" data-bs-toggle="modal" data-bs-target="#modalPoliceReject">
											<i class="fa-solid fa-xmark me-1"></i>Reject
										</button>
									</div>
								<?php endif; ?>
							</div>
						</div>
						<div class="col-12 col-md-6">
							<div class="p-3 border rounded-3">
								<div class="fw-semibold mb-1">Reference letter</div>
								<?php
									$isRefApproved = !empty($u['isApproveReferenceLetter']);
									$refRejectReason = isset($u['referenceLetterRejectionReason']) ? trim((string) $u['referenceLetterRejectionReason']) : '';
									$hasRefFile = !empty($u['referenceLetterUrl']);
									$isRefPending = (!$isRefApproved && $hasRefFile && $refRejectReason === '');
								?>
								<div class="small text-muted mb-2">
									<?php if ($isRefApproved): ?>
										Approved
									<?php elseif ($refRejectReason !== ''): ?>
										Rejected
									<?php else: ?>
										Not approved
									<?php endif; ?>
								</div>
								<?php if (!empty($u['referenceLetterUrl'])): ?>
									<a class="btn btn-sm btn-light border" target="_blank" rel="noopener noreferrer" href="<?php echo h($u['referenceLetterUrl']); ?>">
										<i class="fa-solid fa-arrow-up-right-from-square me-1"></i>Open file
									</a>
								<?php else: ?>
									<span class="text-muted small">—</span>
								<?php endif; ?>

								<?php if ($refRejectReason !== ''): ?>
									<div class="mt-2 small">
										<div class="text-muted">Rejection reason</div>
										<div class="fw-semibold"><?php echo h($refRejectReason); ?></div>
									</div>
								<?php endif; ?>

								<?php if ($isRefPending): ?>
									<div class="d-flex gap-2 mt-3 flex-wrap">
										<form method="post" action="<?php echo base_url('admin/aoust_user_reference_letter_approve/' . rawurlencode($uid)); ?>" id="formRefApprove">
											<button type="submit" class="btn btn-sm btn-success">
												<i class="fa-solid fa-check me-1"></i>Approve
											</button>
										</form>
										<button type="button" class="btn btn-sm btn-outline-danger" data-bs-toggle="modal" data-bs-target="#modalRefReject">
											<i class="fa-solid fa-xmark me-1"></i>Reject
										</button>
									</div>
								<?php endif; ?>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>

<?php if (!empty($uid)): ?>
<div class="modal fade" id="modalGovIdReject" tabindex="-1" aria-hidden="true">
	<div class="modal-dialog modal-dialog-centered">
		<div class="modal-content">
			<div class="modal-header">
				<h5 class="modal-title">Reject Government ID</h5>
				<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
			</div>
			<form method="post" action="<?php echo base_url('admin/aoust_user_government_id_reject/' . rawurlencode($uid)); ?>" id="formGovReject">
				<div class="modal-body">
					<label class="form-label">Rejection reason</label>
					<textarea class="form-control" name="reason" id="govRejectReason" rows="4" placeholder="Write reason..." required></textarea>
					<div class="form-text">This will be saved to <strong>governmentIdRejectionReason</strong>.</div>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-light border" data-bs-dismiss="modal">Cancel</button>
					<button type="submit" class="btn btn-danger">Submit rejection</button>
				</div>
			</form>
		</div>
	</div>
</div>

<div class="modal fade" id="modalPoliceReject" tabindex="-1" aria-hidden="true">
	<div class="modal-dialog modal-dialog-centered">
		<div class="modal-content">
			<div class="modal-header">
				<h5 class="modal-title">Reject Police check</h5>
				<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
			</div>
			<form method="post" action="<?php echo base_url('admin/aoust_user_police_check_reject/' . rawurlencode($uid)); ?>" id="formPoliceReject">
				<div class="modal-body">
					<label class="form-label">Rejection reason</label>
					<textarea class="form-control" name="reason" id="policeRejectReason" rows="4" placeholder="Write reason..." required></textarea>
					<div class="form-text">This will be saved to <strong>policeChekcRejectionReason</strong>.</div>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-light border" data-bs-dismiss="modal">Cancel</button>
					<button type="submit" class="btn btn-danger">Submit rejection</button>
				</div>
			</form>
		</div>
	</div>
</div>

<div class="modal fade" id="modalRefReject" tabindex="-1" aria-hidden="true">
	<div class="modal-dialog modal-dialog-centered">
		<div class="modal-content">
			<div class="modal-header">
				<h5 class="modal-title">Reject Reference letter</h5>
				<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
			</div>
			<form method="post" action="<?php echo base_url('admin/aoust_user_reference_letter_reject/' . rawurlencode($uid)); ?>" id="formRefReject">
				<div class="modal-body">
					<label class="form-label">Rejection reason</label>
					<textarea class="form-control" name="reason" id="refRejectReason" rows="4" placeholder="Write reason..." required></textarea>
					<div class="form-text">This will be saved to <strong>referenceLetterRejectionReason</strong>.</div>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-light border" data-bs-dismiss="modal">Cancel</button>
					<button type="submit" class="btn btn-danger">Submit rejection</button>
				</div>
			</form>
		</div>
	</div>
</div>

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
(function () {
	function showProcessing(title) {
		return Swal.fire({
			title: title || 'Processing…',
			allowOutsideClick: false,
			allowEscapeKey: false,
			didOpen: function () { Swal.showLoading(); }
		});
	}

	var approveForm = document.getElementById('formGovApprove');
	if (approveForm) {
		approveForm.addEventListener('submit', function (e) {
			e.preventDefault();
			Swal.fire({
				icon: 'question',
				title: 'Approve Government ID?',
				text: 'This will mark the document as approved.',
				showCancelButton: true,
				confirmButtonText: 'Approve',
				cancelButtonText: 'Cancel',
				confirmButtonColor: '#198754'
			}).then(function (res) {
				if (!res.isConfirmed) return;
				showProcessing('Approving…');
				approveForm.submit();
			});
		});
	}

	function hookApprove(formId, title, text) {
		var f = document.getElementById(formId);
		if (!f) return;
		f.addEventListener('submit', function (e) {
			e.preventDefault();
			Swal.fire({
				icon: 'question',
				title: title,
				text: text,
				showCancelButton: true,
				confirmButtonText: 'Approve',
				cancelButtonText: 'Cancel',
				confirmButtonColor: '#198754'
			}).then(function (res) {
				if (!res.isConfirmed) return;
				showProcessing('Approving…');
				f.submit();
			});
		});
	}

	function hookReject(formId, textareaId) {
		var f = document.getElementById(formId);
		if (!f) return;
		f.addEventListener('submit', function (e) {
			var ta = document.getElementById(textareaId);
			if (!ta) return;
			if (!String(ta.value || '').trim()) {
				e.preventDefault();
				ta.focus();
				return;
			}
			showProcessing('Rejecting…');
		});
	}

	hookApprove('formPoliceApprove', 'Approve Police check?', 'This will mark the document as approved.');
	hookApprove('formRefApprove', 'Approve Reference letter?', 'This will mark the document as approved.');

	var form = document.getElementById('formGovReject');
	if (!form) return;
	form.addEventListener('submit', function (e) {
		var ta = document.getElementById('govRejectReason');
		if (!ta) return;
		if (!String(ta.value || '').trim()) {
			e.preventDefault();
			ta.focus();
			return;
		}
		showProcessing('Rejecting…');
	});

	hookReject('formPoliceReject', 'policeRejectReason');
	hookReject('formRefReject', 'refRejectReason');
})();
</script>
<?php endif; ?>

