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

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

<?php
	$d = isset($driver) && is_array($driver) ? $driver : array();
	$mode = isset($mode) ? $mode : 'create';
	$is_edit = ($mode === 'edit');
	$id = isset($d['id']) ? $d['id'] : '';
	$action = $is_edit ? base_url('aoust_driver/update/' . rawurlencode($id)) : base_url('aoust_driver/store');
	$existingImg = isset($d['image']) ? trim((string) $d['image']) : '';
?>

<div class="care-admin" id="aoustDriverForm">
	<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 driver' : 'Add driver'); ?></h1>
				<p class="ca-sub mb-0">Phone is stored on the pickup as <strong>driverNumber</strong>; name and image match <strong>driverName</strong> / <strong>driverImage</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_driver'); ?>">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 driver' : 'Add driver'; ?></h5>
					<p class="mb-0">Firestore collection <strong>Drivers</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($d['name']) ? $d['name'] : ''); ?>">
						</div>
						<div class="mb-3">
							<label class="form-label">Phone</label>
							<input class="form-control" name="phone" required value="<?php echo htmlspecialchars(isset($d['phone']) ? $d['phone'] : ''); ?>">
						</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. Stored in Firebase Storage; URL saved on the driver record.</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_driver'); ?>">Cancel</a>
							<button class="btn btn-primary" type="submit"><?php echo $is_edit ? 'Save changes' : 'Create driver'; ?></button>
							<?php if ($is_edit): ?>
								<a class="btn btn-outline-danger ms-auto" href="<?php echo base_url('aoust_driver/delete/' . rawurlencode($id)); ?>" onclick="return confirm('Delete this driver?');">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">
						<li>No Firebase Auth link — directory only.</li>
						<li>Assign drivers from <a href="<?php echo base_url('aoust_pickup'); ?>">Pickup 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>
