<?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>
</div>

<div class="mail-doctor-modal mail-doctor-hidden" id="mail-doctor-log-modal" role="dialog" aria-modal="true" aria-labelledby="mail-doctor-log-modal-title">
	<div class="mail-doctor-modal__overlay" data-dismiss></div>
	<div class="mail-doctor-modal__content" role="document">
		<button type="button" class="mail-doctor-modal__close" aria-label="<?php esc_attr_e( 'Close details', 'mail-doctor' ); ?>" data-dismiss>&times;</button>
		<h2 id="mail-doctor-log-modal-title"><?php esc_html_e( 'Log details', 'mail-doctor' ); ?></h2>
		<div class="mail-doctor-modal__body">
			<dl>
				<dt><?php esc_html_e( 'Status', 'mail-doctor' ); ?></dt>
				<dd data-field="status">—</dd>
				<dt><?php esc_html_e( 'Event', 'mail-doctor' ); ?></dt>
				<dd data-field="event">—</dd>
				<dt><?php esc_html_e( 'Recipient', 'mail-doctor' ); ?></dt>
				<dd data-field="email">—</dd>
				<dt><?php esc_html_e( 'From', 'mail-doctor' ); ?></dt>
				<dd data-field="from">—</dd>
				<dt><?php esc_html_e( 'Reply-to', 'mail-doctor' ); ?></dt>
				<dd data-field="replyTo">—</dd>
				<dt><?php esc_html_e( 'Return path', 'mail-doctor' ); ?></dt>
				<dd data-field="returnPath">—</dd>
				<dt><?php esc_html_e( 'Subject', 'mail-doctor' ); ?></dt>
				<dd data-field="subject">—</dd>
				<dt><?php esc_html_e( 'Provider response', 'mail-doctor' ); ?></dt>
				<dd data-field="provider">—</dd>
				<dt><?php esc_html_e( 'Delivery method', 'mail-doctor' ); ?></dt>
				<dd data-field="transport">—</dd>
				<dt><?php esc_html_e( 'Profile', 'mail-doctor' ); ?></dt>
				<dd data-field="profile">—</dd>
				<dt><?php esc_html_e( 'Attempts', 'mail-doctor' ); ?></dt>
				<dd data-field="attempts">—</dd>
				<dt><?php esc_html_e( 'Next retry', 'mail-doctor' ); ?></dt>
				<dd data-field="nextRetry">—</dd>
				<dt><?php esc_html_e( 'Latency', 'mail-doctor' ); ?></dt>
				<dd data-field="latency">—</dd>
				<dt><?php esc_html_e( 'Attempt history', 'mail-doctor' ); ?></dt>
				<dd><pre data-field="history"></pre></dd>
				<dt><?php esc_html_e( 'Headers', 'mail-doctor' ); ?></dt>
				<dd><pre data-field="headers"></pre></dd>
				<dt><?php esc_html_e( 'Transport debug', 'mail-doctor' ); ?></dt>
				<dd><pre data-field="transportDebug"></pre></dd>
				<dt><?php esc_html_e( 'Content hash', 'mail-doctor' ); ?></dt>
				<dd data-field="contentHash">—</dd>
				<dt><?php esc_html_e( 'Body preview', 'mail-doctor' ); ?></dt>
				<dd><pre data-field="bodyPreview"></pre></dd>
				<dt><?php esc_html_e( 'Last error', 'mail-doctor' ); ?></dt>
				<dd data-field="error">—</dd>
				<dt><?php esc_html_e( 'Environment', 'mail-doctor' ); ?></dt>
				<dd data-field="environment">—</dd>
			</dl>
			<?php if ( \MailDoctor\Features::is_enabled( 'eml_download' ) ) : ?>
				<div class="mail-doctor-modal__footer">
					<a href="#" id="mail-doctor-eml-download-link" class="button button-secondary mail-doctor-hidden">
						<?php esc_html_e( 'Download .eml', 'mail-doctor' ); ?>
					</a>
				</div>
			<?php endif; ?>
		</div>
	</div>
</div>
