<?php
use BLR\Backline\Core\Views;
use BLR\Backline\Modules\Contacts\Contacts;
use BLR\Backline\Modules\Contacts\NewsletterDoubleOptIn;
use BLR\Backline\Modules\Contacts\Pages as ContactsPages;
use BLR\Backline\Modules\Outreach\Module as OutreachModule;

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

$__blr_ba_data = isset($ctx['contacts_list']) && is_array($ctx['contacts_list']) ? $ctx['contacts_list'] : ContactsPages::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__('Contacts', 'blr-backline'); ?>
			<span class="text-muted small">
				(<?php echo esc_html(number_format_i18n($contacts_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__('Contacts 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 Contact', 'blr-backline'); ?>
			</a>
		</div>
	</div>

	<?php if ($saved_notice): ?>
		<div class="alert alert-success alert-dismissible fade show mb-3" 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 mb-3" 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-4">
					<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__('Contact name', 'blr-backline'); ?>"
					>
				</div>

				<div class="col-12 col-md-6 col-lg-2">
					<label class="form-label mb-1"><?php echo esc_html__('Type', 'blr-backline'); ?></label>
					<select class="form-select" name="contact_type">
						<option value=""><?php echo esc_html__('All types', 'blr-backline'); ?></option>
						<?php foreach ($type_options as $type_option): ?>
							<option value="<?php echo esc_attr($type_option); ?>" <?php selected($contact_type_filter, $type_option); ?>>
								<?php echo esc_html(Contacts::type_label($type_option)); ?>
							</option>
						<?php endforeach; ?>
					</select>
				</div>

				<div class="col-12 col-md-6 col-lg-2">
					<label class="form-label mb-1"><?php echo esc_html__('Newsletter', 'blr-backline'); ?></label>
					<select class="form-select" name="newsletter">
						<option value=""><?php echo esc_html__('All contacts', 'blr-backline'); ?></option>
						<option value="subscribed" <?php selected($newsletter_filter, 'subscribed'); ?>><?php echo esc_html__('Subscribed', 'blr-backline'); ?></option>
						<option value="pending" <?php selected($newsletter_filter, 'pending'); ?>><?php echo esc_html__('Pending confirmation', 'blr-backline'); ?></option>
						<option value="not_subscribed" <?php selected($newsletter_filter, 'not_subscribed'); ?>><?php echo esc_html__('Not subscribed', 'blr-backline'); ?></option>
					</select>
				</div>

				<div class="col-12 col-md-6 col-lg-2">
					<label class="form-label mb-1"><?php echo esc_html__('Outreach', 'blr-backline'); ?></label>
					<select class="form-select" name="outreach_status">
						<option value=""><?php echo esc_html__('All statuses', 'blr-backline'); ?></option>
						<?php foreach (OutreachModule::recipient_statuses() as $status_option): ?>
							<option value="<?php echo esc_attr($status_option); ?>" <?php selected($outreach_status_filter, $status_option); ?>>
								<?php echo esc_html(OutreachModule::status_label($status_option)); ?>
							</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 . 'contacts/'); ?>"><?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($contact_items)): ?>
					<p class="text-muted mb-0"><?php echo esc_html__('No contacts found.', 'blr-backline'); ?></p>
				<?php else: ?>
					<div class="row g-3">
						<?php foreach ($contact_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>

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

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

										<div class="small text-muted mb-3">
											<div>
												<strong><?php echo esc_html__('Email:', 'blr-backline'); ?></strong>
												<?php echo esc_html($item['email'] !== '' ? $item['email'] : '—'); ?>
											</div>
											<div>
												<strong><?php echo esc_html__('Newsletter:', 'blr-backline'); ?></strong>
												<?php echo esc_html($item['newsletter_label']); ?>
											</div>
											<div>
												<strong><?php echo esc_html__('Outreach:', 'blr-backline'); ?></strong>
												<?php echo esc_html($item['outreach_label'] !== '' ? $item['outreach_label'] : '—'); ?>
											</div>
											<div>
												<strong><?php echo esc_html__('Last Contacted:', 'blr-backline'); ?></strong>
												<?php echo esc_html($item['last_contacted'] !== '' ? $item['last_contacted'] : '—'); ?>
											</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: contact title used in the delete confirmation prompt. */
													$delete_contact_label = sprintf(__('contact "%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_contact_label); ?>"
													>
													<?php wp_nonce_field('blr_ba_delete_blr_contact_' . $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__('Name', 'blr-backline'); ?></th>
							<th class="d-none d-lg-table-cell"><?php echo esc_html__('Type', 'blr-backline'); ?></th>
							<th class="d-none d-md-table-cell"><?php echo esc_html__('Email', 'blr-backline'); ?></th>
							<th class="d-none d-lg-table-cell"><?php echo esc_html__('Newsletter', 'blr-backline'); ?></th>
							<th class="d-none d-xl-table-cell"><?php echo esc_html__('Last Contacted', 'blr-backline'); ?></th>
							<th class="text-end"><?php echo esc_html__('Actions', 'blr-backline'); ?></th>
						</tr>
					</thead>
					<tbody>
						<?php if (empty($contact_items)): ?>
							<tr><td colspan="6" class="text-muted"><?php echo esc_html__('No contacts found.', 'blr-backline'); ?></td></tr>
						<?php else: ?>
							<?php foreach ($contact_items as $item): ?>
								<tr>
									<td>
										<a href="<?php echo esc_url($item['view_url']); ?>"><strong><?php echo esc_html($item['title']); ?></strong></a>
										<?php if ($item['company'] !== ''): ?>
											<div class="text-muted small"><?php echo esc_html($item['company']); ?></div>
										<?php endif; ?>
									</td>
									<td class="d-none d-lg-table-cell"><?php echo esc_html($item['type_label'] !== '' ? $item['type_label'] : __('—', 'blr-backline')); ?></td>
									<td class="d-none d-md-table-cell"><?php echo esc_html($item['email'] !== '' ? $item['email'] : __('—', 'blr-backline')); ?></td>
									<td class="d-none d-lg-table-cell">
										<?php if ($item['newsletter_status'] === NewsletterDoubleOptIn::STATUS_SUBSCRIBED): ?>
											<span class="badge bg-success"><?php echo esc_html($item['newsletter_label']); ?></span>
										<?php elseif ($item['newsletter_status'] === NewsletterDoubleOptIn::STATUS_PENDING): ?>
											<span class="badge bg-warning text-dark"><?php echo esc_html($item['newsletter_label']); ?></span>
										<?php else: ?>
											<span class="text-muted"><?php echo esc_html($item['newsletter_label']); ?></span>
										<?php endif; ?>
									</td>
									<td class="d-none d-xl-table-cell"><?php echo esc_html($item['last_contacted'] !== '' ? $item['last_contacted'] : __('—', 'blr-backline')); ?></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: contact title used in the delete confirmation prompt. */ ?>
											<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(__('contact “%s”', 'blr-backline'), $item['title'])); ?>">
												<?php wp_nonce_field('blr_ba_delete_blr_contact_' . $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($contacts, 'contacts', 'pg')); ?></div>
		<div><?php echo wp_kses_post(Views::render_pagination($contacts, $pagination_base_url)); ?></div>
	</div>
</div>