<?php
if (!defined('ABSPATH')) {
	exit;
}

use BLR\Backline\Core\Views;
use BLR\Backline\Modules\Outreach\Module as OutreachModule;
use BLR\Backline\Modules\Outreach\Pages as OutreachPages;

$__blr_ba_data = isset($ctx['outreach_list']) && is_array($ctx['outreach_list']) ? $ctx['outreach_list'] : OutreachPages::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__('Outreach', 'blr-backline'); ?>
			<span class="text-muted small">(<?php echo esc_html(number_format_i18n($campaigns_summary['total'])); ?>)</span>
		</h1>

		<div class="d-flex flex-wrap align-items-center gap-2">
			<div class="btn-group" role="group" aria-label="<?php echo esc_attr__('Outreach 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($base . 'outreach/new/'); ?>">
				<?php echo esc_html__('New Campaign', '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-8">
					<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__('Campaign name', '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="status">
						<option value=""><?php echo esc_html__('All statuses', 'blr-backline'); ?></option>
						<?php foreach ($campaign_statuses as $campaign_status) : ?>
							<option value="<?php echo esc_attr($campaign_status); ?>" <?php selected($status_filter, $campaign_status); ?>>
								<?php echo esc_html(OutreachModule::status_label($campaign_status)); ?>
							</option>
						<?php endforeach; ?>
					</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 . 'outreach/'); ?>"><?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($campaign_items)) : ?>
					<p class="text-muted mb-0"><?php echo esc_html__('No campaigns yet.', 'blr-backline'); ?></p>
				<?php else : ?>
					<div class="row g-3">
						<?php foreach ($campaign_items as $item) : ?>
							<div class="col-12 col-md-4 col-lg-3">
								<div class="card h-100 shadow-sm border-0">
									<div class="card-body d-flex flex-column">
										<div class="d-flex align-items-start justify-content-between gap-3 mb-2">
											<div>
												<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>
												<div class="text-muted small">
													<?php echo esc_html($item['subject'] ?: __('No subject yet', 'blr-backline')); ?>
												</div>
											</div>

											<div><?php echo wp_kses_post($item['status_badge']); ?></div>
										</div>

										<div class="small text-muted mb-3">
											<div>
												<strong><?php echo esc_html__('Last Sent:', 'blr-backline'); ?></strong>
												<?php echo esc_html($item['last_sent'] ?: __('—', 'blr-backline')); ?>
											</div>
											<div class="mt-1">
												<?php /* translators: %d: sent recipient count. */ ?>
												<span class="text-success"><?php printf(esc_html__('%d sent', 'blr-backline'), (int) $item['stats']['sent']); ?></span>

												<?php if ((int) $item['stats']['failed'] > 0) : ?>
													<?php /* translators: %d: failed recipient count. */ ?>
													<span class="text-danger ms-2"><?php printf(esc_html__('%d failed', 'blr-backline'), (int) $item['stats']['failed']); ?></span>
												<?php endif; ?>

												<?php if ((int) $item['stats']['replied'] > 0) : ?>
													<?php /* translators: %d: replied recipient count. */ ?>
													<span class="text-primary ms-2"><?php printf(esc_html__('%d replied', 'blr-backline'), (int) $item['stats']['replied']); ?></span>
												<?php endif; ?>
											</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: campaign 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(sprintf(__('campaign “%s”', 'blr-backline'), $item['title'])); ?>">
													<?php wp_nonce_field('blr_ba_delete_blr_outreach_' . $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__('Campaign', 'blr-backline'); ?></th>
							<th class="d-none d-lg-table-cell"><?php echo esc_html__('Status', 'blr-backline'); ?></th>
							<th class="d-none d-xl-table-cell"><?php echo esc_html__('Last Sent', 'blr-backline'); ?></th>
							<th class="d-none d-md-table-cell"><?php echo esc_html__('Recipients', 'blr-backline'); ?></th>
							<th class="text-end"><?php echo esc_html__('Actions', 'blr-backline'); ?></th>
						</tr>
					</thead>
					<tbody>
						<?php if (empty($campaign_items)) : ?>
							<tr><td colspan="5" class="text-muted"><?php echo esc_html__('No campaigns yet.', 'blr-backline'); ?></td></tr>
						<?php else : ?>
							<?php foreach ($campaign_items as $item) : ?>
								<tr>
									<td>
										<a href="<?php echo esc_url($item['view_url']); ?>"><strong><?php echo esc_html($item['title']); ?></strong></a>
										<div class="text-muted small"><?php echo esc_html($item['subject'] ?: __('No subject yet', 'blr-backline')); ?></div>
									</td>
									<td class="d-none d-lg-table-cell"><?php echo wp_kses_post($item['status_badge']); ?></td>
									<td class="d-none d-xl-table-cell"><?php echo esc_html($item['last_sent'] ?: __('—', 'blr-backline')); ?></td>
									<td class="d-none d-md-table-cell">
										<?php /* translators: %d: sent recipient count. */ ?>
										<span class="text-success"><?php printf(esc_html__('%d sent', 'blr-backline'), (int) $item['stats']['sent']); ?></span>
										<?php if ((int) $item['stats']['failed'] > 0) : ?>
											<?php /* translators: %d: failed recipient count. */ ?>
											<span class="text-danger ms-2"><?php printf(esc_html__('%d failed', 'blr-backline'), (int) $item['stats']['failed']); ?></span>
										<?php endif; ?>
										<?php if ((int) $item['stats']['replied'] > 0) : ?>
											<?php /* translators: %d: replied recipient count. */ ?>
											<span class="text-primary ms-2"><?php printf(esc_html__('%d replied', 'blr-backline'), (int) $item['stats']['replied']); ?></span>
										<?php endif; ?>
									</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: campaign 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(sprintf(__('campaign “%s”', 'blr-backline'), $item['title'])); ?>">
												<?php wp_nonce_field('blr_ba_delete_blr_outreach_' . $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($campaigns, 'campaigns', 'pg')); ?></div>
		<div><?php echo wp_kses_post(Views::render_pagination($campaigns, $pagination_base_url)); ?></div>
	</div>
</div>