<?php
use BLR\Backline\Core\Views;
use BLR\Backline\Modules\Events\Module as EventsModule;
use BLR\Backline\Modules\Events\Pages as EventsPages;

if (!defined('ABSPATH')) {
	exit;
}

$__blr_ba_data = isset($ctx['events_list']) && is_array($ctx['events_list']) ? $ctx['events_list'] : EventsPages::list_data($ctx);
foreach ($__blr_ba_data as $__blr_ba_key => $__blr_ba_value) {
	${$__blr_ba_key} = $__blr_ba_value;
}
unset($__blr_ba_data, $__blr_ba_key, $__blr_ba_value);
?>
<div class="container-fluid p-0">
	<div class="d-flex flex-column flex-md-row align-items-md-center justify-content-between gap-3 mb-3">
		<h1 class="h3 mb-0">
			<?php echo esc_html__('Events', 'blr-backline'); ?>
			<span class="text-muted small">(<?php echo esc_html(number_format_i18n($events_summary['total'])); ?>)</span>
		</h1>

		<div class="d-flex align-items-center gap-2">
			<div class="btn-group" role="group" aria-label="<?php echo esc_attr__('Events view mode', 'blr-backline'); ?>">
				<a
					class="btn btn-outline-secondary<?php echo esc_attr($view_mode === 'list' ? ' active' : ''); ?>"
					href="<?php echo esc_url($list_url); ?>"
					aria-pressed="<?php echo esc_attr($view_mode === 'list' ? 'true' : 'false'); ?>"
				>
					<?php echo esc_html__('List', 'blr-backline'); ?>
				</a>
				<a
					class="btn btn-outline-secondary<?php echo esc_attr($view_mode === 'grid' ? ' active' : ''); ?>"
					href="<?php echo esc_url($grid_url); ?>"
					aria-pressed="<?php echo esc_attr($view_mode === 'grid' ? 'true' : 'false'); ?>"
				>
					<?php echo esc_html__('Grid', 'blr-backline'); ?>
				</a>
			</div>

			<a class="btn btn-primary" href="<?php echo esc_url($new_url); ?>">
				<?php echo esc_html__('New Event', 'blr-backline'); ?>
			</a>
		</div>
	</div>

	<?php if ($saved_notice): ?>
		<div class="alert alert-success alert-dismissible fade show" role="alert">
			<?php echo esc_html__('Saved.', 'blr-backline'); ?>
			<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="<?php echo esc_attr__('Close', 'blr-backline'); ?>"></button>
		</div>
	<?php endif; ?>

	<?php if ($delete_error_message !== '') : ?>
	<div class="alert alert-danger" role="alert"><?php echo esc_html($delete_error_message); ?></div>
<?php endif; ?>

<?php if ($deleted_notice): ?>
		<div class="alert alert-success alert-dismissible fade show" role="alert">
			<?php echo esc_html__('Deleted.', 'blr-backline'); ?>
			<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="<?php echo esc_attr__('Close', 'blr-backline'); ?>"></button>
		</div>
	<?php endif; ?>

	<div class="card flex-fill mb-3">
		<div class="card-body py-3">
			<form method="get" class="row g-2 align-items-end">
				<input type="hidden" name="view" value="<?php echo esc_attr($view_mode); ?>">

				<div class="col-12 col-md-6 col-lg-5">
					<label class="form-label mb-1"><?php echo esc_html__('Search', 'blr-backline'); ?></label>
					<input
						class="form-control"
						type="search"
						name="s"
						value="<?php echo esc_attr($search_term); ?>"
						placeholder="<?php echo esc_attr__('Event title', 'blr-backline'); ?>"
					>
				</div>

				<div class="col-12 col-md-6 col-lg-2">
					<label class="form-label mb-1"><?php echo esc_html__('Status', 'blr-backline'); ?></label>
					<select class="form-select" name="event_status">
						<option value=""><?php echo esc_html__('All statuses', 'blr-backline'); ?></option>
						<?php foreach ($event_status_options as $event_status_value => $event_status_label): ?>
							<option value="<?php echo esc_attr($event_status_value); ?>" <?php selected($status_filter, $event_status_value); ?>>
								<?php echo esc_html($event_status_label); ?>
							</option>
						<?php endforeach; ?>
					</select>
				</div>

				<div class="col-12 col-md-6 col-lg-3">
					<label class="form-label mb-1"><?php echo esc_html__('When', 'blr-backline'); ?></label>
					<select class="form-select" name="timeframe">
						<option value=""><?php echo esc_html__('All events', 'blr-backline'); ?></option>
						<option value="upcoming" <?php selected($time_filter, 'upcoming'); ?>><?php echo esc_html__('Upcoming', 'blr-backline'); ?></option>
						<option value="past" <?php selected($time_filter, 'past'); ?>><?php echo esc_html__('Past', 'blr-backline'); ?></option>
					</select>
				</div>

				<div class="col-12 col-lg-2">
					<button class="btn btn-outline-secondary" type="submit">
						<?php echo esc_html__('Filter', 'blr-backline'); ?>
					</button>
					<a class="btn btn-link text-decoration-none" href="<?php echo esc_url($base . 'events/'); ?>">
						<?php echo esc_html__('Reset', 'blr-backline'); ?>
					</a>
				</div>
			</form>
		</div>
	</div>

	<div class="card flex-fill">
		<?php if ($view_mode === 'grid'): ?>
			<div class="card-body">
				<?php if (empty($event_items)): ?>
					<p class="text-muted mb-0"><?php echo esc_html__('No events found.', 'blr-backline'); ?></p>
				<?php else: ?>
					<div class="row g-3">
						<?php foreach ($event_items as $item): ?>
							<div class="col-12 col-md-4 col-lg-3">
								<div class="card h-100 shadow-sm border-0 overflow-hidden">
									<a
										href="<?php echo esc_url($item['view_url']); ?>"
										class="d-block text-decoration-none bg-light"
										<?php /* translators: %s: event title. */ ?>
										aria-label="<?php echo esc_attr(sprintf(__('View %s', 'blr-backline'), $item['title'])); ?>"
									>
										<div class="ratio ratio-16x9 bg-light">
											<?php if (has_post_thumbnail($item['id'])): ?>
												<?php
												echo get_the_post_thumbnail(
													$item['id'],
													'medium_large',
													array(
														'class'   => 'w-100 h-100 blr-ba-object-cover',
														'loading' => 'lazy',
														'alt'     => '',
													)
												);
												?>
											<?php else: ?>
												<div class="d-flex align-items-center justify-content-center h-100 bg-light">
													<img
														src="<?php echo esc_url(BLR_BA_URL . 'assets/blr-ba/img/placeholder-image.png'); ?>"
														alt=""
														class="opacity-50 blr-ba-placeholder-image"
														loading="lazy"
													>
												</div>
											<?php endif; ?>
										</div>
									</a>

									<div class="card-body d-flex flex-column">
										<div class="d-flex align-items-start justify-content-between gap-3 mb-2">
											<div class="min-w-0">
												<h2 class="h5 mb-1">
													<a class="text-decoration-none" href="<?php echo esc_url($item['view_url']); ?>">
														<?php echo esc_html($item['title']); ?>
													</a>
												</h2>

												<?php if ($item['status_label'] !== ''): ?>
													<div class="text-muted small"><?php echo esc_html($item['status_label']); ?></div>
												<?php endif; ?>
											</div>

											<?php if ($item['status_label'] !== ''): ?>
												<span class="badge text-bg-light flex-shrink-0"><?php echo esc_html($item['status_label']); ?></span>
											<?php endif; ?>
										</div>

										<div class="small text-muted mb-3">
											<div>
												<strong><?php echo esc_html__('Start:', 'blr-backline'); ?></strong>
												<?php echo esc_html($item['start'] !== '' ? $item['start'] : '—'); ?>
											</div>
											<div>
												<strong><?php echo esc_html__('Venue:', 'blr-backline'); ?></strong>
												<?php echo esc_html($item['venue'] !== '' ? $item['venue'] : '—'); ?>
											</div>
											<div>
												<strong><?php echo esc_html__('City:', 'blr-backline'); ?></strong>
												<?php echo esc_html($item['location'] !== '' ? $item['location'] : '—'); ?>
											</div>
										</div>

										<div class="mt-auto">
											<div class="btn-group" role="group">
												<a class="btn btn-sm btn-outline-secondary" href="<?php echo esc_url($item['view_url']); ?>">
													<?php echo esc_html__('View', 'blr-backline'); ?>
												</a>
												<a class="btn btn-sm btn-outline-secondary" href="<?php echo esc_url($item['edit_url']); ?>">
													<?php echo esc_html__('Edit', 'blr-backline'); ?>
												</a>
												<?php
												/* translators: %s: event title used in the delete confirmation prompt. */
												$delete_event_label = sprintf(__('event "%s"', 'blr-backline'), $item['title']);
												?>
												<form
													method="post"
													action="<?php echo esc_url($item['delete_url']); ?>"
													class="d-inline blr-ba-delete-form"
													data-item-label="<?php echo esc_attr($delete_event_label); ?>"
												>
													<?php wp_nonce_field('blr_ba_delete_blr_event_' . $item['id']); ?>
													<button type="submit" class="btn btn-sm btn-outline-danger">
														<?php echo esc_html__('Delete', 'blr-backline'); ?>
													</button>
												</form>
											</div>
										</div>
									</div>
								</div>
							</div>
						<?php endforeach; ?>
					</div>
				<?php endif; ?>
			</div>
		<?php else: ?>
			<div class="table-responsive">
				<table class="table table-hover my-0">
					<thead>
						<tr>
							<th><?php echo esc_html__('Event', 'blr-backline'); ?></th>
							<th class="d-none d-md-table-cell"><?php echo esc_html__('Start', 'blr-backline'); ?></th>
							<th class="d-none d-md-table-cell"><?php echo esc_html__('Venue', 'blr-backline'); ?></th>
							<th class="d-none d-md-table-cell"><?php echo esc_html__('City', 'blr-backline'); ?></th>
							<th class="text-end"><?php echo esc_html__('Actions', 'blr-backline'); ?></th>
						</tr>
					</thead>
					<tbody>
						<?php if (empty($event_items)): ?>
							<tr>
								<td colspan="5" class="text-muted"><?php echo esc_html__('No events found.', 'blr-backline'); ?></td>
							</tr>
						<?php else: ?>
							<?php foreach ($event_items as $item): ?>
								<tr>
									<td>
										<div class="d-flex align-items-center gap-3">
											<?php if (has_post_thumbnail($item['id'])): ?>
												<a
													href="<?php echo esc_url($item['view_url']); ?>"
													class="d-inline-flex flex-shrink-0"
													<?php /* translators: %s: event title. */ ?>
													aria-label="<?php echo esc_attr(sprintf(__('View %s', 'blr-backline'), $item['title'])); ?>"
												>
													<?php
													echo get_the_post_thumbnail(
														$item['id'],
														'thumbnail',
														array(
															'class'   => 'rounded-circle flex-shrink-0 blr-ba-list-thumb',
															'loading' => 'lazy',
															'alt'     => $item['title'],
														)
													);
													?>
												</a>
											<?php endif; ?>

											<div>
												<a href="<?php echo esc_url($item['view_url']); ?>">
													<strong><?php echo esc_html($item['title']); ?></strong>
												</a>

												<?php if ($item['status_label'] !== ''): ?>
													<div class="text-muted small"><?php echo esc_html($item['status_label']); ?></div>
												<?php endif; ?>
											</div>
										</div>
									</td>
									<td class="d-none d-md-table-cell"><?php echo esc_html($item['start'] !== '' ? $item['start'] : '—'); ?></td>
									<td class="d-none d-md-table-cell"><?php echo esc_html($item['venue'] !== '' ? $item['venue'] : '—'); ?></td>
									<td class="d-none d-md-table-cell"><?php echo esc_html($item['location'] !== '' ? $item['location'] : '—'); ?></td>
									<td class="text-end">
										<div class="btn-group" role="group">
											<a class="btn btn-sm btn-outline-secondary" href="<?php echo esc_url($item['view_url']); ?>">
												<?php echo esc_html__('View', 'blr-backline'); ?>
											</a>
											<a class="btn btn-sm btn-outline-secondary" href="<?php echo esc_url($item['edit_url']); ?>">
												<?php echo esc_html__('Edit', 'blr-backline'); ?>
											</a>
												<?php
												/* translators: %s: event title used in the delete confirmation prompt. */
												$delete_event_label = sprintf(__('event "%s"', 'blr-backline'), $item['title']);
												?>
												<form
													method="post"
													action="<?php echo esc_url($item['delete_url']); ?>"
													class="d-inline blr-ba-delete-form"
													data-item-label="<?php echo esc_attr($delete_event_label); ?>"
												>
												<?php wp_nonce_field('blr_ba_delete_blr_event_' . $item['id']); ?>
												<button type="submit" class="btn btn-sm btn-outline-danger">
													<?php echo esc_html__('Delete', 'blr-backline'); ?>
												</button>
											</form>
										</div>
									</td>
								</tr>
							<?php endforeach; ?>
						<?php endif; ?>
					</tbody>
				</table>
			</div>
		<?php endif; ?>
	</div>

	<div class="d-flex flex-column flex-lg-row align-items-lg-center justify-content-between gap-3 mt-4">
		<div class="text-muted small">
			<?php echo esc_html(Views::render_pagination_summary($events, 'events', 'pg')); ?>
		</div>
		<div>
			<?php echo wp_kses_post(Views::render_pagination($events, $pagination_base_url)); ?>
		</div>
	</div>
</div>
