<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/** EVENT FORM — Expects: $event, $event_types, $mode. */
$e = $event;
$gallery = decode_json($e['gallery']);
?>
<div class="page-head">
	<div><h2><?= $mode === 'edit' ? 'Edit' : 'Add' ?> Event</h2></div>
	<a href="<?= base_url('admin/events') ?>" class="btn btn-ghost">← Back</a>
</div>

<form action="<?= base_url('admin/events/save') ?>" method="post" enctype="multipart/form-data">
	<?= form_hidden($this->security->get_csrf_token_name(), $this->security->get_csrf_hash()) ?>
	<input type="hidden" name="id" value="<?= (int) $e['id'] ?>">

	<div class="grid grid-2-1">
		<div>
			<div class="card">
				<div class="card-head"><h3>Event Details</h3></div>
				<div class="card-body">
					<div class="form-grid cols-2">
						<div class="fld"><label>Title <span class="req">*</span></label>
							<input type="text" name="title" value="<?= html_escape($e['title']) ?>" required data-slug-source></div>
						<div class="fld"><label>Slug</label>
							<input type="text" name="slug" value="<?= html_escape($e['slug']) ?>" data-slug-target placeholder="auto"></div>
						<div class="fld"><label>Event Date <span class="req">*</span></label>
							<input type="date" name="event_date" value="<?= html_escape($e['event_date']) ?>" required></div>
						<div class="fld"><label>Event Time</label>
							<input type="text" name="event_time" value="<?= html_escape($e['event_time']) ?>" placeholder="e.g. 6:00 PM"></div>
						<div class="fld fld-full"><label>Location</label>
							<input type="text" name="location" value="<?= html_escape($e['location']) ?>"></div>
						<div class="fld fld-full"><label>Excerpt</label>
							<textarea name="excerpt"><?= html_escape($e['excerpt']) ?></textarea></div>
						<div class="fld fld-full"><label>Description</label>
							<textarea name="description" style="min-height:160px;"><?= html_escape($e['description']) ?></textarea></div>
					</div>
				</div>
			</div>

			<div class="card">
				<div class="card-head"><h3>Recap Gallery</h3></div>
				<div class="card-body">
					<?php if ($gallery): ?>
						<div style="display:flex;flex-wrap:wrap;gap:10px;margin-bottom:12px;">
							<?php foreach ($gallery as $g): ?>
								<label style="cursor:pointer;">
									<img src="<?= upload_url($g) ?>" class="thumb-lg">
									<span style="display:flex;gap:5px;font-size:.76rem;margin-top:4px;">
										<input type="checkbox" name="keep_gallery[]" value="<?= html_escape($g) ?>" checked> Keep
									</span>
								</label>
							<?php endforeach; ?>
						</div>
					<?php endif; ?>
					<div class="fld"><label>Add Photos</label><?php $this->load->view('admin/partials/_upload_field', array('name' => 'gallery', 'multiple' => TRUE, 'accept' => 'image/*', 'label' => 'Choose photos')); ?></div>
				</div>
			</div>

			<div class="card">
				<div class="card-head"><h3>SEO</h3></div>
				<div class="card-body">
					<div class="form-grid">
						<div class="fld"><label>Meta Title</label><input type="text" name="meta_title" value="<?= html_escape($e['meta_title']) ?>"></div>
						<div class="fld"><label>Meta Description</label><textarea name="meta_desc"><?= html_escape($e['meta_desc']) ?></textarea></div>
					</div>
				</div>
			</div>
		</div>

		<div>
			<div class="card">
				<div class="card-head"><h3>Cover Image</h3></div>
				<div class="card-body">
					<?php if ($e['cover_image']): ?>
						<img src="<?= upload_url($e['cover_image']) ?>" class="thumb-lg" id="imgPrev" style="margin-bottom:8px;">
					<?php else: ?>
						<img src="" class="thumb-lg" id="imgPrev" style="display:none;margin-bottom:8px;">
					<?php endif; ?>
					<?php $this->load->view('admin/partials/_upload_field', array(
						'name' => 'cover_image', 
						'accept' => 'image/*', 
						'icon' => 'image', 
						'label' => 'Choose image', 
						'preview' => '#imgPrev',
						'multiple' => FALSE  // ← FIXED: Added this line
					)); ?>
				</div>
			</div>
			<div class="card">
				<div class="card-head"><h3>Ticketing</h3></div>
				<div class="card-body">
					<div class="fld"><label class="check"><input type="checkbox" name="allow_rsvp" value="1" <?= $e['allow_rsvp'] ? 'checked' : '' ?>><span>Allow free RSVP</span></label></div>
					<div class="fld"><label class="check"><input type="checkbox" name="is_paid" value="1" <?= $e['is_paid'] ? 'checked' : '' ?>><span>Paid event (Paystack)</span></label></div>
					<div class="fld"><label>Ticket Price (GHS)</label><input type="number" step="0.01" name="ticket_price" value="<?= html_escape($e['ticket_price']) ?>"></div>
					<div class="fld"><label>Capacity</label><input type="number" name="capacity" value="<?= (int) $e['capacity'] ?>" placeholder="0 = unlimited"></div>
				</div>
			</div>
			<div class="card">
				<div class="card-head"><h3>Settings</h3></div>
				<div class="card-body">
					<div class="fld"><label>Event Type</label>
						<select name="event_type">
							<option value="">Select…</option>
							<?php foreach ($event_types as $k=>$v): ?>
								<option value="<?= $k ?>" <?= $e['event_type'] === $k ? 'selected' : '' ?>><?= html_escape($v) ?></option>
							<?php endforeach; ?>
						</select>
					</div>
					<div class="fld"><label class="check"><input type="checkbox" name="is_featured" value="1" <?= $e['is_featured'] ? 'checked' : '' ?>><span>Featured on homepage</span></label></div>
					<div class="fld"><label class="check"><input type="checkbox" name="is_active" value="1" <?= $e['is_active'] ? 'checked' : '' ?>><span>Active</span></label></div>
				</div>
			</div>
		</div>
	</div>

	<div class="card"><div class="card-body"><div class="form-actions">
		<button type="submit" class="btn btn-primary"><?= bloom_icon('check', 16) ?> Save Event</button>
		<a href="<?= base_url('admin/events') ?>" class="btn btn-ghost">Cancel</a>
	</div></div></div>
</form>