<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; }
	.btn-ca-success:hover, .btn-ca-success:focus { color: #fff !important; }
	.aoust-preview {
		width: 100%;
		max-width: 520px;
		border-radius: 14px;
		border: 1px solid rgba(0,0,0,0.08);
		background: #f8f9fa;
	}
</style>

<?php
	$a = isset($activity) && is_array($activity) ? $activity : array();
	$mode = isset($mode) ? $mode : 'create';
	$is_edit = ($mode === 'edit');
	$id = isset($a['id']) ? $a['id'] : '';
	$action = $is_edit ? base_url('aoust_activity/update/' . rawurlencode($id)) : base_url('aoust_activity/store');
?>

<div class="care-admin" id="aoustActivityForm">
	<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 activity' : 'Add activity'); ?></h1>
				<p class="ca-sub mb-0">Fields: title, image, description, document (URL stored).</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_activity'); ?>">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 activity' : 'Add activity'; ?></h5>
					<p class="mb-0">Saved to Firestore collection <strong>Activities</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">Title</label>
							<input class="form-control" name="title" required value="<?php echo htmlspecialchars(isset($a['title']) ? $a['title'] : ''); ?>">
						</div>

						<div class="mb-3">
							<label class="form-label">Description</label>
							<textarea class="form-control" name="description" rows="4"><?php echo htmlspecialchars(isset($a['description']) ? $a['description'] : ''); ?></textarea>
						</div>

						<div class="row g-3">
							<div class="col-md-6">
								<label class="form-label">Image</label>
								<input class="form-control" type="file" name="image" accept="image/*">
								<div class="form-text aoust-muted">Uploaded to Firebase Storage; URL stored in Firestore.</div>
							</div>
							<div class="col-md-6">
								<label class="form-label">Document</label>
								<input class="form-control" type="file" name="document" accept=".pdf,.doc,.docx,.png,.jpg,.jpeg,.webp">
								<div class="form-text aoust-muted">Uploaded to Firebase Storage; document URL stored.</div>
							</div>
						</div>

						<div class="mt-3">
							<label class="form-label">User ID</label>
							<input class="form-control" name="userId" value="<?php echo htmlspecialchars(isset($a['userId']) ? $a['userId'] : ''); ?>">
						</div>

						<div class="d-flex gap-2 mt-4">
							<a class="btn btn-light border" href="<?php echo base_url('aoust_activity'); ?>">Cancel</a>
							<button class="btn btn-primary" type="submit"><?php echo $is_edit ? 'Save changes' : 'Create activity'; ?></button>
							<?php if ($is_edit): ?>
								<a class="btn btn-outline-danger ms-auto" href="<?php echo base_url('aoust_activity/delete/' . rawurlencode($id)); ?>" onclick="return confirm('Delete this activity?');">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-regular fa-image me-2 text-success"></i>Preview</h5>
					<p class="mb-0">Existing uploads (if saved).</p>
				</div>
				<div class="ca-card-body">
					<div class="mb-3">
						<div class="fw-semibold mb-2">Image</div>
						<?php if (!empty($a['image'])): ?>
							<img class="aoust-preview" src="<?php echo htmlspecialchars($a['image']); ?>" alt="">
						<?php else: ?>
							<div class="aoust-muted small">No image yet.</div>
						<?php endif; ?>
					</div>
					<div>
						<div class="fw-semibold mb-2">Document</div>
						<?php if (!empty($a['documentUrl'])): ?>
							<a class="btn btn-light border" href="<?php echo htmlspecialchars($a['documentUrl']); ?>" target="_blank" rel="noopener">
								<i class="fa-regular fa-file-lines me-1"></i>Open document
							</a>
						<?php else: ?>
							<div class="aoust-muted small">No document yet.</div>
						<?php endif; ?>
					</div>
				</div>
			</div>

			<?php if ($is_edit): ?>
			<div class="ca-card">
				<div class="ca-card-header">
					<h5 class="ca-card-title mb-1"><i class="fa-solid fa-circle-info me-2 text-success"></i>Metadata</h5>
					<p class="mb-0">Record info.</p>
				</div>
				<div class="ca-card-body">
					<div class="aoust-muted small">Document ID</div>
					<div class="fw-semibold"><?php echo htmlspecialchars($id); ?></div>
				</div>
			</div>
			<?php endif; ?>
		</div>
	</div>
</div>

