<?php
/**
 * Redirections Tab View.
 */
if (!defined('ABSPATH')) {
	exit;
}
?>
<div class="wpsh-purpose-info">
	<h4>Why Redirections?</h4>
	<p>Since your site serves <strong>Static HTML</strong>, SEO is critical. Redirections solve two main problems:</p>
	<ul>
		<li><strong>SEO Consistency:</strong> If you change a page slug, use a 301 redirect.</li>
		<li><strong>Instant .htaccess Logic:</strong> Rules are written directly to your <code>.htaccess</code> file.
		</li>
		<li><strong>Note:</strong> Redirections are bypassed for logged-in users.</li>
	</ul>
</div>

<div class="wpsh-card wpsh-redirection-header" style="margin-bottom: 25px;">
	<div class="wpsh-header-flex">
		<div>
			<h3>Redirection Manager</h3>
			<p>Manage URL redirections (301, 302, 307).</p>
		</div>
		<div class="wpsh-actions-group">
			<div class="wpsh-import-export">
				<button id="wpsh-export-btn" class="wpsh-btn wpsh-btn-secondary">Export</button>
				<button id="wpsh-import-btn" class="wpsh-btn wpsh-btn-secondary">Import</button>
				<input type="file" id="wpsh-import-file" style="display:none" accept=".csv">
			</div>
			<button id="wpsh-sample-csv-btn" class="wpsh-btn wpsh-btn-secondary">Sample CSV</button>
			<button id="wpsh-sync-redir-btn" class="wpsh-btn wpsh-btn-secondary">Sync Rules</button>
		</div>
	</div>
</div>

<div class="wpsh-grid">
	<div class="wpsh-card">
		<h3>Add New Redirection</h3>
		<div class="wpsh-form-body">
			<div class="wpsh-form-group">
				<label>Source Path</label>
				<input type="text" id="wpsh-new-source" placeholder="/old-path" class="wpsh-input">
			</div>
			<div class="wpsh-form-group">
				<label>Target URL</label>
				<input type="text" id="wpsh-new-target" placeholder="/new-path" class="wpsh-input">
			</div>
			<div class="wpsh-form-row">
				<div class="wpsh-form-group">
					<label>Redirect Type</label>
					<select id="wpsh-new-code" class="wpsh-input">
						<option value="301">301 (Permanent)</option>
						<option value="302">302 (Found)</option>
						<option value="307">307 (Temporary)</option>
					</select>
				</div>
				<button id="wpsh-add-redir-btn" class="wpsh-btn wpsh-btn-primary">Add Redirection</button>
			</div>
		</div>
	</div>
</div>


<div class="wpsh-grid" style="margin-top: 20px;">

	<div class="wpsh-card" style="grid-column: span 2;" id="wpsh-redir-list-card">
		<div class="wpsh-table-header-bar">
			<h3>Active Redirections</h3>
			<button id="wpsh-bulk-delete-btn" class="wpsh-btn wpsh-btn-danger" style="display:none">Delete Selected</button>
		</div>
		<div class="wpsh-table-container wpsh-redir-table-container">
			<table class="wpsh-table" id="wpsh-redir-table">
				<thead>
					<tr>
						<th style="width: 30px;"><input type="checkbox" id="wpsh-select-all"></th>
						<th>Status</th>
						<th>Source</th>
						<th>Target</th>
						<th>Type</th>
						<th>Actions</th>
					</tr>
				</thead>
				<tbody>
				<?php
				$wpsh_redirs = get_option('wpsh_redirections', []);
				foreach ($wpsh_redirs as $wpsh_idx => $wpsh_r): ?>
					<tr data-idx="<?php echo esc_attr($wpsh_idx); ?>">
						<td><input type="checkbox" class="wpsh-select-item" value="<?php echo esc_attr($wpsh_idx); ?>"></td>
						<td>
							<label class="wpsh-switch-small">
								<input type="checkbox" class="wpsh-item-toggle" <?php checked($wpsh_r['active'], '1'); ?>>
								<span class="wpsh-slider-small round"></span>
							</label>
						</td>
						<td class="wpsh-source-cell"><code><?php echo esc_html($wpsh_r['source']); ?></code></td>
						<td>
							<?php
							$wpsh_target_display = $wpsh_r['target'];
							if (is_numeric($wpsh_target_display)) {
								$wpsh_permalink = get_permalink(intval($wpsh_target_display));
								if ($wpsh_permalink) {
									$wpsh_target_display = $wpsh_permalink;
								}
							}
							?>
							<a href="<?php echo esc_url($wpsh_target_display); ?>" target="_blank"
								class="wpsh-target-link"><?php echo esc_html($wpsh_target_display); ?></a>
						</td>
						<td><span
								class="wpsh-badge wpsh-badge-<?php echo esc_attr($wpsh_r['code']); ?>"><?php echo esc_html($wpsh_r['code']); ?></span>
						</td>
						<td>
							<div style="display: flex; gap: 8px; justify-content: flex-end;">
								<button class="wpsh-btn-icon wpsh-edit-redir"><span
										class="dashicons dashicons-edit"></span></button>
								<button class="wpsh-btn-icon wpsh-delete-redir"><span
										class="dashicons dashicons-trash"></span></button>
							</div>
						</td>
					</tr>
				<?php endforeach; ?>
				</tbody>
			</table>
		</di	v>
	</div>
</div>