<?php
/**
 * Livada SEO — Redirections Settings View
 *
 * @package Livada_SEO
 */

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

// Pagination & search.
$per_page = 25;
$current_page = isset( $_GET['paged'] ) ? max( 1, absint( $_GET['paged'] ) ) : 1; // phpcs:ignore
$search       = isset( $_GET['s'] ) ? sanitize_text_field( wp_unslash( $_GET['s'] ) ) : ''; // phpcs:ignore

$result      = class_exists( 'Livada_SEO_Redirects' ) ? Livada_SEO_Redirects::get_redirects_paginated( $current_page, $per_page, $search ) : [ 'items' => [], 'total' => 0 ];
$redirects   = $result['items'];
$total       = $result['total'];
$total_pages = (int) ceil( $total / $per_page );

$page_url = admin_url( 'admin.php?page=livada-seo-redirects' );
?>
<div class="wrap livada-seo-wrap has-ls-hero">
<?php include LIVADA_SEO_PATH . 'includes/admin/views/_partials/header.php'; ?>
	<h1><?php esc_html_e( 'Redirections', 'livada-seo' ); ?></h1>

	<?php if ( isset( $_GET['updated'] ) ) : // phpcs:ignore ?>
		<div class="notice notice-success is-dismissible"><p><?php esc_html_e( 'Settings saved.', 'livada-seo' ); ?></p></div>
	<?php endif; ?>

	<!-- ── Add redirect form ── -->
	<div class="livada-card livada-card--form">
		<h2><?php esc_html_e( 'Add Redirect', 'livada-seo' ); ?></h2>
		<form id="livada-add-redirect-form" class="livada-inline-form">
			<div class="livada-inline-form__fields">
				<div class="livada-field">
					<label for="redirect_source"><?php esc_html_e( 'Source URL', 'livada-seo' ); ?></label>
					<input type="text" id="redirect_source" name="source_url" placeholder="/old-page/" class="regular-text" required>
				</div>
				<div class="livada-field">
					<label for="redirect_target"><?php esc_html_e( 'Target URL', 'livada-seo' ); ?></label>
					<input type="text" id="redirect_target" name="target_url" placeholder="/new-page/ or https://…" class="regular-text" required>
				</div>
				<div class="livada-field">
					<label for="redirect_type"><?php esc_html_e( 'Type', 'livada-seo' ); ?></label>
					<select id="redirect_type" name="type">
						<option value="301">301 &mdash; <?php esc_html_e( 'Permanent', 'livada-seo' ); ?></option>
						<option value="302">302 &mdash; <?php esc_html_e( 'Temporary', 'livada-seo' ); ?></option>
					</select>
				</div>
				<div class="livada-field livada-field--action">
					<label>&nbsp;</label>
					<button type="submit" class="button button-primary"><?php esc_html_e( 'Add Redirect', 'livada-seo' ); ?></button>
				</div>
			</div>
			<div id="livada-add-redirect-message" class="livada-inline-message" style="display:none;"></div>
		</form>
	</div>

	<!-- ── CSV import / export ── -->
	<div class="livada-card livada-card--form" style="margin-top: 16px;">
		<h2><?php esc_html_e( 'Import / Export CSV', 'livada-seo' ); ?></h2>
		<p class="description">
			<?php esc_html_e( 'CSV format: header row "source_url,target_url,type" — type defaults to 301 if missing. Comma or semicolon delimiters supported.', 'livada-seo' ); ?>
		</p>
		<div style="display:flex; gap:24px; flex-wrap:wrap; align-items:flex-start;">
			<form id="livada-csv-import-form" enctype="multipart/form-data" style="display:flex; gap:8px; align-items:center;">
				<input type="file" name="csv" accept=".csv,text/csv" required>
				<button type="submit" class="button"><?php esc_html_e( 'Import CSV', 'livada-seo' ); ?></button>
				<span id="livada-csv-import-status" class="description"></span>
			</form>

			<a class="button"
				href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin-post.php?action=livada_seo_export_redirects_csv' ), 'livada_seo_export_redirects' ) ); ?>">
				<?php esc_html_e( 'Export all redirects as CSV', 'livada-seo' ); ?>
			</a>
		</div>
	</div>

	<!-- ── Search form ── -->
	<form method="get" action="<?php echo esc_url( $page_url ); ?>" class="livada-search-form">
		<input type="hidden" name="page" value="livada-seo-redirects">
		<input
			type="search"
			name="s"
			value="<?php echo esc_attr( $search ); ?>"
			placeholder="<?php esc_attr_e( 'Search redirects…', 'livada-seo' ); ?>"
			class="regular-text"
		>
		<button type="submit" class="button"><?php esc_html_e( 'Search', 'livada-seo' ); ?></button>
		<?php if ( $search ) : ?>
			<a href="<?php echo esc_url( $page_url ); ?>" class="button"><?php esc_html_e( 'Clear', 'livada-seo' ); ?></a>
		<?php endif; ?>
	</form>

	<!-- ── Bulk actions toolbar ── -->
	<div class="livada-bulk-toolbar" style="margin: 8px 0; display:flex; gap:8px; align-items:center;">
		<button type="button" id="livada-bulk-delete-btn" class="button" disabled>
			<?php esc_html_e( 'Delete selected', 'livada-seo' ); ?>
		</button>
		<span id="livada-bulk-status" class="description"></span>
	</div>

	<!-- ── Table ── -->
	<table class="wp-list-table widefat striped livada-redirects-table" id="livada-redirects-table">
		<thead>
			<tr>
				<th scope="col" class="column-cb" style="width:32px;"><input type="checkbox" id="livada-redirects-check-all"></th>
				<th scope="col"><?php esc_html_e( 'Source', 'livada-seo' ); ?></th>
				<th scope="col"><?php esc_html_e( 'Target', 'livada-seo' ); ?></th>
				<th scope="col" class="column-type"><?php esc_html_e( 'Type', 'livada-seo' ); ?></th>
				<th scope="col" class="column-hits"><?php esc_html_e( 'Hits', 'livada-seo' ); ?></th>
				<th scope="col" class="column-actions"><?php esc_html_e( 'Actions', 'livada-seo' ); ?></th>
			</tr>
		</thead>
		<tbody id="livada-redirects-tbody">
			<?php if ( empty( $redirects ) ) : ?>
				<tr class="no-items">
					<td colspan="6"><?php esc_html_e( 'No redirects found.', 'livada-seo' ); ?></td>
				</tr>
			<?php else : ?>
				<?php foreach ( $redirects as $r ) : ?>
					<tr data-id="<?php echo absint( $r->id ); ?>">
						<td class="column-cb"><input type="checkbox" class="livada-redirect-cb" value="<?php echo absint( $r->id ); ?>"></td>
						<td class="redirect-source">
							<span class="redirect-display-val"><?php echo esc_html( $r->source_url ); ?></span>
							<input type="text" class="redirect-edit-val" value="<?php echo esc_attr( $r->source_url ); ?>" style="display:none;" aria-label="<?php esc_attr_e( 'Source URL', 'livada-seo' ); ?>">
						</td>
						<td class="redirect-target">
							<span class="redirect-display-val"><?php echo esc_html( $r->target_url ); ?></span>
							<input type="text" class="redirect-edit-val" value="<?php echo esc_attr( $r->target_url ); ?>" style="display:none;" aria-label="<?php esc_attr_e( 'Target URL', 'livada-seo' ); ?>">
						</td>
						<td class="redirect-type column-type">
							<span class="redirect-display-val"><?php echo absint( $r->type ); ?></span>
							<select class="redirect-edit-val" style="display:none;" aria-label="<?php esc_attr_e( 'Type', 'livada-seo' ); ?>">
								<option value="301" <?php selected( $r->type, 301 ); ?>>301</option>
								<option value="302" <?php selected( $r->type, 302 ); ?>>302</option>
							</select>
						</td>
						<td class="column-hits"><?php echo absint( $r->hits ); ?></td>
						<td class="column-actions">
							<button type="button" class="button-link livada-redirect-edit"><?php esc_html_e( 'Edit', 'livada-seo' ); ?></button>
							<button type="button" class="button-link livada-redirect-save" style="display:none;"><?php esc_html_e( 'Save', 'livada-seo' ); ?></button>
							<button type="button" class="button-link livada-redirect-cancel" style="display:none;"><?php esc_html_e( 'Cancel', 'livada-seo' ); ?></button>
							|
							<button type="button" class="button-link livada-redirect-delete" style="color:#b32d2e;"><?php esc_html_e( 'Delete', 'livada-seo' ); ?></button>
						</td>
					</tr>
				<?php endforeach; ?>
			<?php endif; ?>
		</tbody>
		<tfoot>
			<tr>
				<th scope="col" class="column-cb"></th>
				<th scope="col"><?php esc_html_e( 'Source', 'livada-seo' ); ?></th>
				<th scope="col"><?php esc_html_e( 'Target', 'livada-seo' ); ?></th>
				<th scope="col"><?php esc_html_e( 'Type', 'livada-seo' ); ?></th>
				<th scope="col"><?php esc_html_e( 'Hits', 'livada-seo' ); ?></th>
				<th scope="col"><?php esc_html_e( 'Actions', 'livada-seo' ); ?></th>
			</tr>
		</tfoot>
	</table>

	<!-- ── Pagination ── -->
	<?php if ( $total_pages > 1 ) : ?>
		<div class="tablenav">
			<div class="tablenav-pages">
				<span class="displaying-num">
					<?php
					printf(
						/* translators: %d: total count */
						esc_html( _n( '%d redirect', '%d redirects', $total, 'livada-seo' ) ),
						(int) $total
					);
					?>
				</span>
				<span class="pagination-links">
					<?php if ( $current_page > 1 ) : ?>
						<a class="first-page button" href="<?php echo esc_url( add_query_arg( [ 'paged' => 1, 's' => $search ], $page_url ) ); ?>">&laquo;</a>
						<a class="prev-page button" href="<?php echo esc_url( add_query_arg( [ 'paged' => $current_page - 1, 's' => $search ], $page_url ) ); ?>">&lsaquo;</a>
					<?php endif; ?>
					<span class="paging-input">
						<?php echo (int) $current_page; ?> <?php esc_html_e( 'of', 'livada-seo' ); ?> <?php echo (int) $total_pages; ?>
					</span>
					<?php if ( $current_page < $total_pages ) : ?>
						<a class="next-page button" href="<?php echo esc_url( add_query_arg( [ 'paged' => $current_page + 1, 's' => $search ], $page_url ) ); ?>">&rsaquo;</a>
						<a class="last-page button" href="<?php echo esc_url( add_query_arg( [ 'paged' => $total_pages, 's' => $search ], $page_url ) ); ?>">&raquo;</a>
					<?php endif; ?>
				</span>
			</div>
		</div>
	<?php endif; ?>

</div>
