<?php
/**
 * Mail Doctor logs list scaffold.
 *
 * @var string                     $mail_doctor_active_tab
 * @var string                     $mail_doctor_base_url
 * @var \MailDoctor\Logs_Table $mail_doctor_logs_table
 *
 * @package MailDoctor
 */

use MailDoctor\Admin;

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


$mail_doctor_filters_valid = true;

// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Existence check only; nonce verified on line 31-32.
$mail_doctor_has_filter_params =
	isset( $_GET['date_from'] )
	|| isset( $_GET['date_to'] )
	|| isset( $_GET['event'] )
	|| isset( $_GET['recipient'] )
	|| isset( $_GET['provider'] )
	|| isset( $_GET['status'] );

if ( $mail_doctor_has_filter_params ) {
	// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Verifying nonce for read-only GET filters.
	$mail_doctor_nonce         = isset( $_GET['mail_doctor_logs_filter_nonce'] ) ? sanitize_text_field( wp_unslash( $_GET['mail_doctor_logs_filter_nonce'] ) ) : '';
	$mail_doctor_filters_valid = ( ! empty( $mail_doctor_nonce ) && wp_verify_nonce( $mail_doctor_nonce, 'mail_doctor_logs_filter' ) );
}

// Read values only if nonce is valid.
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce verified above for read-only GET filters.
$mail_doctor_date_from = ( $mail_doctor_filters_valid && isset( $_GET['date_from'] ) ) ? sanitize_text_field( wp_unslash( $_GET['date_from'] ) ) : '';
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce verified above for read-only GET filters.
$mail_doctor_date_to   = ( $mail_doctor_filters_valid && isset( $_GET['date_to'] ) ) ? sanitize_text_field( wp_unslash( $_GET['date_to'] ) ) : '';
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce verified above for read-only GET filters.
$mail_doctor_event     = ( $mail_doctor_filters_valid && isset( $_GET['event'] ) ) ? sanitize_key( wp_unslash( $_GET['event'] ) ) : '';
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce verified above for read-only GET filters.
$mail_doctor_recipient = ( $mail_doctor_filters_valid && isset( $_GET['recipient'] ) ) ? sanitize_text_field( wp_unslash( $_GET['recipient'] ) ) : '';
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce verified above for read-only GET filters.
$mail_doctor_provider  = ( $mail_doctor_filters_valid && isset( $_GET['provider'] ) ) ? sanitize_key( wp_unslash( $_GET['provider'] ) ) : '';
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce verified above for read-only GET filters.
$mail_doctor_status    = ( $mail_doctor_filters_valid && isset( $_GET['status'] ) ) ? sanitize_key( wp_unslash( $_GET['status'] ) ) : '';

?>
<div class="wrap maildoctor-admin-wrap">
	<?php settings_errors( 'maildoctor' ); ?>

	<?php if ( ! $mail_doctor_filters_valid ) : ?>
		<div class="notice notice-warning is-dismissible">
			<p><?php esc_html_e( 'Invalid filter request. Please try again.', 'mail-doctor' ); ?></p>
		</div>
	<?php endif; ?>

	<form id="mail-doctor-logs-filter" class="mail-doctor-filter-bar" method="get" action="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>">
		<input type="hidden" name="page" value="<?php echo esc_attr( Admin::PAGE_SLUG ); ?>" />
		<input type="hidden" name="tab" value="logs" />

		<?php wp_nonce_field( 'mail_doctor_logs_filter', 'mail_doctor_logs_filter_nonce' ); ?>

		<label>
			<span class="screen-reader-text"><?php esc_html_e( 'From date', 'mail-doctor' ); ?></span>
			<span class="mail-doctor-filter-bar__label"><?php esc_html_e( 'From', 'mail-doctor' ); ?></span>
			<input type="text" name="date_from" class="mail-doctor-datepicker" placeholder="<?php esc_attr_e( 'From', 'mail-doctor' ); ?>" value="<?php echo esc_attr( $mail_doctor_date_from ); ?>" />
		</label>
		<label>
			<span class="screen-reader-text"><?php esc_html_e( 'To date', 'mail-doctor' ); ?></span>
			<span class="mail-doctor-filter-bar__label"><?php esc_html_e( 'To', 'mail-doctor' ); ?></span>
			<input type="text" name="date_to" class="mail-doctor-datepicker" placeholder="<?php esc_attr_e( 'To', 'mail-doctor' ); ?>" value="<?php echo esc_attr( $mail_doctor_date_to ); ?>" />
		</label>
		<label>
			<span class="screen-reader-text"><?php esc_html_e( 'Status', 'mail-doctor' ); ?></span>
			<span class="mail-doctor-filter-bar__label"><?php esc_html_e( 'Status', 'mail-doctor' ); ?></span>
			<select name="status" class="mail-doctor-select" data-placeholder="<?php esc_attr_e( 'Status', 'mail-doctor' ); ?>">
				<option value=""></option>
				<?php
				$mail_doctor_statuses = array(
					'queued' => esc_html__( 'Queued', 'mail-doctor' ),
					'sent'   => esc_html__( 'Sent', 'mail-doctor' ),
					'failed' => esc_html__( 'Failed', 'mail-doctor' ),
				);
				foreach ( $mail_doctor_statuses as $mail_doctor_value => $mail_doctor_label ) :
					?>
					<option value="<?php echo esc_attr( $mail_doctor_value ); ?>" <?php selected( $mail_doctor_status, $mail_doctor_value ); ?>><?php echo esc_html( $mail_doctor_label ); ?></option>
				<?php endforeach; ?>
			</select>
		</label>
		<label>
			<span class="screen-reader-text"><?php esc_html_e( 'Event', 'mail-doctor' ); ?></span>
			<span class="mail-doctor-filter-bar__label"><?php esc_html_e( 'Event', 'mail-doctor' ); ?></span>
			<input type="text" name="event" placeholder="<?php esc_attr_e( 'Event slug', 'mail-doctor' ); ?>" value="<?php echo esc_attr( $mail_doctor_event ); ?>" />
		</label>
		<label>
			<span class="screen-reader-text"><?php esc_html_e( 'Recipient domain', 'mail-doctor' ); ?></span>
			<span class="mail-doctor-filter-bar__label"><?php esc_html_e( 'Recipient domain', 'mail-doctor' ); ?></span>
			<input type="text" name="recipient" placeholder="<?php esc_attr_e( 'Recipient domain', 'mail-doctor' ); ?>" value="<?php echo esc_attr( $mail_doctor_recipient ); ?>" />
		</label>
		<label>
			<span class="screen-reader-text"><?php esc_html_e( 'Provider code', 'mail-doctor' ); ?></span>
			<span class="mail-doctor-filter-bar__label"><?php esc_html_e( 'Provider code', 'mail-doctor' ); ?></span>
			<input type="text" name="provider" placeholder="<?php esc_attr_e( 'Provider code', 'mail-doctor' ); ?>" value="<?php echo esc_attr( $mail_doctor_provider ); ?>" />
		</label>
		<div class="mail-doctor-filter-bar-actions">
			<button type="submit" class="button button-primary"><?php esc_html_e( 'Filter', 'mail-doctor' ); ?></button>
			<a class="button button-secondary" href="<?php echo esc_url( Admin::page_url( 'logs' ) ); ?>"><?php esc_html_e( 'Reset', 'mail-doctor' ); ?></a>

			<?php if ( \MailDoctor\Features::is_enabled( 'export' ) ) : ?>
				<?php wp_nonce_field( 'md_export_logs_nonce', 'mail-doctor-export-logs-nonce', false ); ?>
				<button type="button" id="mail-doctor-export-logs" class="button button-secondary">
					<?php esc_html_e( 'Export CSV', 'mail-doctor' ); ?>
				</button>
			<?php endif; ?>
		</div>
	</form>

	<form method="post">
		<?php $mail_doctor_logs_table->display(); ?>
	</form>

	<!-- Slide-over Drawer for Log Details -->
	<div id="mail-doctor-log-drawer" class="mail-doctor-drawer" aria-hidden="true">
		<div class="mail-doctor-drawer__overlay" data-close-drawer></div>
		<div class="mail-doctor-drawer__content">
			<div class="mail-doctor-drawer__header">
				<h3 class="mail-doctor-drawer__title"><?php esc_html_e( 'Log Details', 'mail-doctor' ); ?></h3>
				<button type="button" class="mail-doctor-drawer__close" data-close-drawer aria-label="<?php esc_attr_e( 'Close panel', 'mail-doctor' ); ?>">
					<span class="dashicons dashicons-no-alt" aria-hidden="true"></span>
				</button>
			</div>
			<div class="mail-doctor-drawer__body" id="mail-doctor-log-drawer-body">
				<!-- Content loaded via AJAX -->
			</div>
		</div>
	</div>
</div>


