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

<style>
	.aoust-muted { color: rgba(33, 37, 41, 0.7); }
	.aoust-preview { max-width: 200px; border-radius: 12px; border: 1px solid rgba(0,0,0,0.08); }
</style>

<?php
	$a = isset($accommodator) && is_array($accommodator) ? $accommodator : array();
	$mode = isset($mode) ? $mode : 'create';
	$is_edit = ($mode === 'edit');
	$id = isset($a['id']) ? $a['id'] : '';
	$action = $is_edit ? base_url('aoust_accommodator/update/' . rawurlencode($id)) : base_url('aoust_accommodator/store');
	$existingImg = isset($a['image']) ? trim((string) $a['image']) : '';
	$latVal = isset($a['lat']) && $a['lat'] !== null && $a['lat'] !== '' ? (string) $a['lat'] : '';
	$lngVal = isset($a['lng']) && $a['lng'] !== null && $a['lng'] !== '' ? (string) $a['lng'] : '';
?>

<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 isset($page_title) ? htmlspecialchars($page_title) : ($is_edit ? 'Edit accommodator' : 'Add accommodator'); ?></h1>
				<p class="ca-sub mb-0">On approval, stay gets <strong>accommodatorName</strong>, <strong>accommodatorNumber</strong>, <strong>accommodatorImage</strong>, <strong>accommodatorAddress</strong>, <strong>lat</strong>, <strong>lon</strong>.</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('aoust_accommodator'); ?>">Back to list</a>
				</div>
			</div>
		</div>
	</div>

	<div class="row g-3">
		<div class="col-lg-8">
			<div class="ca-card mb-4">
				<div class="ca-card-header">
					<h5 class="ca-card-title mb-1"><i class="fa-solid fa-pen-to-square me-2 text-success"></i><?php echo $is_edit ? 'Edit' : 'Add'; ?> accommodator</h5>
					<p class="mb-0">Collection <strong>Accommodators</strong>.</p>
				</div>
				<div class="ca-card-body">
					<form method="post" action="<?php echo $action; ?>" enctype="multipart/form-data">
						<div class="mb-3">
							<label class="form-label">Name</label>
							<input class="form-control" name="name" required value="<?php echo htmlspecialchars(isset($a['name']) ? $a['name'] : ''); ?>">
						</div>
						<div class="mb-3">
							<label class="form-label">Phone</label>
							<input class="form-control" name="phone" required value="<?php echo htmlspecialchars(isset($a['phone']) ? $a['phone'] : ''); ?>">
						</div>
						<div class="mb-3">
							<label class="form-label">Address</label>
							<textarea class="form-control" name="address" rows="2" required><?php echo htmlspecialchars(isset($a['address']) ? $a['address'] : ''); ?></textarea>
						</div>
						<div class="row g-2 mb-3">
							<div class="col-md-6">
								<label class="form-label">Latitude</label>
								<input class="form-control" name="lat" value="<?php echo htmlspecialchars($latVal); ?>" placeholder="e.g. 33.6680">
							</div>
							<div class="col-md-6">
								<label class="form-label">Longitude</label>
								<input class="form-control" name="lng" value="<?php echo htmlspecialchars($lngVal); ?>" placeholder="e.g. 73.0000">
							</div>
						</div>
						<div class="mb-3">
							<label class="form-label">Photo</label>
							<input class="form-control" type="file" name="image" accept="image/*">
							<div class="form-text aoust-muted">Optional · Firebase Storage URL stored on this document.</div>
						</div>
						<?php if ($is_edit && $existingImg !== ''): ?>
							<div class="mb-3">
								<div class="aoust-muted small mb-1">Current photo</div>
								<img class="aoust-preview" src="<?php echo htmlspecialchars($existingImg); ?>" alt="">
							</div>
						<?php endif; ?>

						<div class="d-flex gap-2 mt-4">
							<a class="btn btn-light border" href="<?php echo base_url('aoust_accommodator'); ?>">Cancel</a>
							<button class="btn btn-primary" type="submit"><?php echo $is_edit ? 'Save' : 'Create'; ?></button>
							<?php if ($is_edit): ?>
								<a class="btn btn-outline-danger ms-auto" href="<?php echo base_url('aoust_accommodator/delete/' . rawurlencode($id)); ?>" onclick="return confirm('Delete?');">Delete</a>
							<?php endif; ?>
						</div>
					</form>
				</div>
			</div>
		</div>
		<div class="col-lg-4">
			<div class="ca-card mb-4">
				<div class="ca-card-header">
					<h5 class="ca-card-title mb-1"><i class="fa-solid fa-circle-info me-2 text-success"></i>Notes</h5>
				</div>
				<div class="ca-card-body">
					<ul class="mb-0 small">
						<li>No Firebase Auth link.</li>
						<li>Assign from <a href="<?php echo base_url('aoust_stay'); ?>">Stay Requests</a> when status is pending.</li>
					</ul>
					<?php if ($is_edit): ?>
						<hr>
						<div class="aoust-muted small">Document ID</div>
						<div class="fw-semibold text-break"><?php echo htmlspecialchars($id); ?></div>
					<?php endif; ?>
				</div>
			</div>
		</div>
	</div>
</div>
