<?php
/**
 * Monitored Pages screen.
 *
 * @package SEO_Flight_Recorder
 */

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

global $wpdb;

$sfrec_pages = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}sfrec_pages WHERE is_active = 1 ORDER BY id ASC" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- own table.
$sfrec_used  = count( (array) $sfrec_pages );
$sfrec_msg   = isset( $_GET['sfrec_msg'] ) ? sanitize_key( wp_unslash( $_GET['sfrec_msg'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only notice key.

$sfrec_snapshots = array();
foreach ( (array) $wpdb->get_results( "SELECT page_id, captured_at FROM {$wpdb->prefix}sfrec_snapshots WHERE kind = 'current'" ) as $sfrec_snapshot_row ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- own table.
	$sfrec_snapshots[ (int) $sfrec_snapshot_row->page_id ] = $sfrec_snapshot_row->captured_at;
}
?>
<div class="wrap sfrec-wrap">
	<h1><?php esc_html_e( 'SEO Flight Recorder — Monitored Pages', 'buildsbyluke-seo-flight-recorder' ); ?></h1>

	<?php if ( 'page-added' === $sfrec_msg ) : ?>
		<div class="notice notice-success is-dismissible"><p><?php esc_html_e( 'Page added. Its first snapshot is captured on the next check.', 'buildsbyluke-seo-flight-recorder' ); ?></p></div>
	<?php elseif ( 'page-removed' === $sfrec_msg ) : ?>
		<div class="notice notice-success is-dismissible"><p><?php esc_html_e( 'Page removed.', 'buildsbyluke-seo-flight-recorder' ); ?></p></div>
	<?php elseif ( 'bad-url' === $sfrec_msg ) : ?>
		<div class="notice notice-error is-dismissible"><p><?php esc_html_e( 'That URL could not be added — it must be a valid URL on this site\'s own domain.', 'buildsbyluke-seo-flight-recorder' ); ?></p></div>
	<?php endif; ?>

	<p>
		<?php
		/* translators: %d: number of monitored pages. */
		echo esc_html( sprintf( _n( 'Monitoring %d page.', 'Monitoring %d pages.', $sfrec_used, 'buildsbyluke-seo-flight-recorder' ), $sfrec_used ) );
		?>
		<?php esc_html_e( 'Add as many as you need — monitor the pages your traffic depends on: the front page, your top landing pages, your money pages.', 'buildsbyluke-seo-flight-recorder' ); ?>
	</p>

	<table class="widefat striped">
		<thead>
			<tr>
				<th><?php esc_html_e( 'Page', 'buildsbyluke-seo-flight-recorder' ); ?></th>
				<th><?php esc_html_e( 'URL', 'buildsbyluke-seo-flight-recorder' ); ?></th>
				<th><?php esc_html_e( 'Snapshot captured', 'buildsbyluke-seo-flight-recorder' ); ?></th>
				<th><?php esc_html_e( 'Actions', 'buildsbyluke-seo-flight-recorder' ); ?></th>
			</tr>
		</thead>
		<tbody>
		<?php if ( empty( $sfrec_pages ) ) : ?>
			<tr><td colspan="4"><?php esc_html_e( 'No pages monitored yet.', 'buildsbyluke-seo-flight-recorder' ); ?></td></tr>
		<?php else : ?>
			<?php foreach ( $sfrec_pages as $sfrec_page ) : ?>
				<tr>
					<td><?php echo esc_html( '' !== $sfrec_page->label ? $sfrec_page->label : '—' ); ?></td>
					<td><a href="<?php echo esc_url( $sfrec_page->url ); ?>" target="_blank" rel="noopener"><?php echo esc_html( $sfrec_page->url ); ?></a></td>
					<td>
						<?php
						if ( isset( $sfrec_snapshots[ (int) $sfrec_page->id ] ) ) {
							echo esc_html( mysql2date( 'Y-m-d H:i', $sfrec_snapshots[ (int) $sfrec_page->id ] ) );
						} else {
							esc_html_e( 'awaiting first check', 'buildsbyluke-seo-flight-recorder' );
						}
						?>
					</td>
					<td>
						<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="sfrec-inline-form">
							<input type="hidden" name="action" value="sfrec_remove_page" />
							<input type="hidden" name="sfrec_page_id" value="<?php echo esc_attr( (string) $sfrec_page->id ); ?>" />
							<?php wp_nonce_field( 'sfrec_remove_page_' . (int) $sfrec_page->id ); ?>
							<button type="submit" class="button-link sfrec-remove"><?php esc_html_e( 'Remove', 'buildsbyluke-seo-flight-recorder' ); ?></button>
						</form>
					</td>
				</tr>
			<?php endforeach; ?>
		<?php endif; ?>
		</tbody>
	</table>

	<h2><?php esc_html_e( 'Add a page', 'buildsbyluke-seo-flight-recorder' ); ?></h2>
	<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
		<input type="hidden" name="action" value="sfrec_add_page" />
		<?php wp_nonce_field( 'sfrec_add_page' ); ?>
		<table class="form-table" role="presentation">
			<tr>
				<th scope="row"><label for="sfrec_url"><?php esc_html_e( 'URL (this site only)', 'buildsbyluke-seo-flight-recorder' ); ?></label></th>
				<td><input type="url" class="regular-text" id="sfrec_url" name="sfrec_url" placeholder="<?php echo esc_attr( home_url( '/important-page/' ) ); ?>" required /></td>
			</tr>
			<tr>
				<th scope="row"><label for="sfrec_label"><?php esc_html_e( 'Label (optional)', 'buildsbyluke-seo-flight-recorder' ); ?></label></th>
				<td><input type="text" class="regular-text" id="sfrec_label" name="sfrec_label" placeholder="<?php esc_attr_e( 'e.g. Pricing page', 'buildsbyluke-seo-flight-recorder' ); ?>" /></td>
			</tr>
		</table>
		<?php submit_button( __( 'Add page', 'buildsbyluke-seo-flight-recorder' ) ); ?>
	</form>
</div>
