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

<style>
	.aoust-muted { color: rgba(33, 37, 41, 0.7); }
	.aoust-table-actions .btn { padding-inline: 0.75rem; }
	.btn-ca-success { color: #fff !important; }
	.aoust-acc-thumb {
		width: 40px;
		height: 40px;
		object-fit: cover;
		border-radius: 10px;
		border: 1px solid rgba(0,0,0,0.08);
		background: #f8f9fa;
	}
</style>

<div class="care-admin" id="aoustAccCrud">
	<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 isset($page_title) ? htmlspecialchars($page_title) : 'AOUST — Accommodators'; ?></h1>
				<p class="ca-sub mb-0">Firestore <strong>Accommodators</strong> · assign to stay requests when approving.</p>
			</div>
			<div class="col-lg-4">
				<div class="ca-nav justify-content-lg-end">
					<a class="btn btn-light" href="<?php echo base_url('admin/aoust'); ?>">Dashboard</a>
					<a class="btn btn-light" href="<?php echo base_url('aoust_stay'); ?>">Stay</a>
					<a class="btn btn-primary" href="<?php echo base_url('aoust_accommodator'); ?>">Accommodators</a>
				</div>
			</div>
		</div>
	</div>

	<div class="ca-card mb-4">
		<div class="ca-card-header d-flex flex-column flex-lg-row align-items-lg-center justify-content-lg-between gap-2">
			<div>
				<h5 class="ca-card-title mb-1"><i class="fa-solid fa-house-chimney-user me-2 text-success"></i>Accommodators</h5>
				<p class="mb-0">Name, phone, address, photo, latitude &amp; longitude.</p>
			</div>
			<div class="d-flex align-items-stretch gap-2 flex-wrap justify-content-lg-end">
				<span class="badge rounded-pill ca-pill bg-light text-dark border">Total: <?php echo isset($accommodators) ? (int) count($accommodators) : 0; ?></span>
				<a class="btn btn-ca-success" href="<?php echo base_url('aoust_accommodator/create'); ?>">
					<i class="fa-solid fa-plus me-2"></i>Add accommodator
				</a>
			</div>
		</div>

		<div class="ca-card-body p-0">
			<div class="ca-table-wrap border-0 rounded-0 border-top">
				<table class="table ca-table table-striped mb-0 align-middle">
					<thead>
						<tr>
							<th>Photo</th>
							<th>Name</th>
							<th>Phone</th>
							<th>Address</th>
							<th>Lat / Lng</th>
							<th class="text-end">Actions</th>
						</tr>
					</thead>
					<tbody>
					<?php if (!empty($accommodators)): ?>
						<?php foreach ($accommodators as $a): ?>
							<?php
								$id = isset($a['id']) ? $a['id'] : '';
								$name = isset($a['name']) ? $a['name'] : '';
								$phone = isset($a['phone']) ? $a['phone'] : '';
								$addr = isset($a['address']) ? $a['address'] : '';
								$img = isset($a['image']) ? trim((string) $a['image']) : '';
								$alat = isset($a['lat']) && $a['lat'] !== null && $a['lat'] !== '' ? $a['lat'] : '';
								$alng = isset($a['lng']) && $a['lng'] !== null && $a['lng'] !== '' ? $a['lng'] : '';
							?>
							<tr>
								<td>
									<?php if ($img !== ''): ?>
										<img class="aoust-acc-thumb" src="<?php echo htmlspecialchars($img); ?>" alt="">
									<?php else: ?>
										<span class="aoust-muted small">—</span>
									<?php endif; ?>
								</td>
								<td class="fw-semibold"><?php echo htmlspecialchars($name); ?></td>
								<td class="text-muted small"><?php echo htmlspecialchars($phone); ?></td>
								<td class="small text-muted" style="max-width: 200px;"><div class="text-truncate" title="<?php echo htmlspecialchars($addr); ?>"><?php echo htmlspecialchars($addr); ?></div></td>
								<td class="small text-muted"><?php echo htmlspecialchars(($alat !== '' && $alng !== '') ? ($alat . ', ' . $alng) : '—'); ?></td>
								<td class="text-end">
									<div class="btn-group aoust-table-actions" role="group">
										<a class="btn btn-sm btn-light border" href="<?php echo base_url('aoust_accommodator/edit/' . rawurlencode($id)); ?>" title="Edit">
											<i class="fa-regular fa-pen-to-square"></i>
										</a>
										<a class="btn btn-sm btn-outline-danger" href="<?php echo base_url('aoust_accommodator/delete/' . rawurlencode($id)); ?>" onclick="return confirm('Delete this accommodator?');" title="Delete">
											<i class="fa-regular fa-trash-can"></i>
										</a>
									</div>
								</td>
							</tr>
						<?php endforeach; ?>
					<?php else: ?>
						<tr>
							<td colspan="6" class="p-4 text-center">
								<div class="mb-2"><i class="fa-regular fa-face-frown fs-2 text-muted"></i></div>
								<div class="fw-semibold">No accommodators yet</div>
								<div class="aoust-muted small">Add records to assign them on pending stay requests.</div>
							</td>
						</tr>
					<?php endif; ?>
					</tbody>
				</table>
			</div>
		</div>
	</div>
</div>
