<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'] : '';
$display = trim((isset($u['firstName']) ? $u['firstName'] : '') . ' ' . (isset($u['lastName']) ? $u['lastName'] : ''));
$display = $display !== '' ? $display : ($uid !== '' ? $uid : 'Agency');
?>

<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($display); ?></h1>
				<p class="ca-sub mb-0"><span class="badge bg-warning-subtle text-warning border border-warning-subtle">Agency</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=agencies'); ?>">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-solid fa-building me-2 text-success"></i>Agency</h5>
				</div>
				<div class="ca-card-body">
					<div class="vstack gap-2">
						<div>
							<div class="text-muted small">Display</div>
							<div class="fw-semibold"><?php echo h($display); ?></div>
						</div>
						<div>
							<div class="text-muted small">UID</div>
							<div class="fw-semibold font-monospace"><?php echo h($uid); ?></div>
						</div>
						<div>
							<div class="text-muted small">Email</div>
							<div class="fw-semibold"><?php echo h(isset($u['email']) ? $u['email'] : '—'); ?></div>
						</div>
						<div>
							<div class="text-muted small">Phone</div>
							<div class="fw-semibold"><?php echo h(isset($u['phone']) ? $u['phone'] : '—'); ?></div>
						</div>
						<div>
							<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; ?>
								<?php if (!empty($u['isVerifiedUser'])): ?>
									<span class="badge bg-primary-subtle text-primary border border-primary-subtle ms-1">Verified</span>
								<?php else: ?>
									<span class="badge bg-light text-dark border ms-1">Not verified</span>
								<?php endif; ?>
							</div>
						</div>
					</div>
					<hr>
					<div class="text-muted small">
						Agency-specific fields (company profile, packages, staff, contracts) can be added here as your dataset grows.
					</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>Office location</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>About</h5>
				</div>
				<div class="ca-card-body">
					<?php echo nl2br(h(isset($u['bio']) ? $u['bio'] : '')); ?>
				</div>
			</div>
		</div>
	</div>
</div>

