<?php
/**
 * Admin.
 *
 * @package Noesia_Commerce_Suite
 */

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

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- View templates receive local variables from their renderer.
$counts        = isset( $counts ) && is_array( $counts ) ? $counts : array(
	'pending'  => 0,
	'sent'     => 0,
	'skipped'  => 0,
	'failed'   => 0,
);
$queue_items    = isset( $queue_items ) && is_array( $queue_items ) ? $queue_items : array();
$opt_out_items  = isset( $opt_out_items ) && is_array( $opt_out_items ) ? $opt_out_items : array();
$recent_logs    = isset( $recent_logs ) && is_array( $recent_logs ) ? $recent_logs : array();
$runtime        = isset( $runtime ) && is_array( $runtime ) ? $runtime : array();
$email_preview  = isset( $email_preview ) ? $email_preview : '';
$enabled       = 'yes' === ( $settings['reorder_reminders_enabled'] ?? 'no' ) && 'yes' === ( $settings['reorder_email_enabled'] ?? 'no' );
$sections      = array(
	'overview'  => __( 'Overview', 'noesia-commerce-suite' ),
	'settings'  => __( 'Reminder Settings', 'noesia-commerce-suite' ),
	'checkout'  => __( 'Checkout Request', 'noesia-commerce-suite' ),
	'email'     => __( 'Email Template', 'noesia-commerce-suite' ),
	'queue'     => __( 'Email History', 'noesia-commerce-suite' ),
);

$render_reorder_form = function ( $title, $eyebrow, $description, $cards, $button_text ) use ( $admin, $settings ) {
	?>
	<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="noesia-cs-settings-form noesia-cs-hub-form noesia-cs-reorder-settings-form">
		<?php wp_nonce_field( 'noesia_cs_save_settings' ); ?>
		<input type="hidden" name="action" value="noesia_cs_save_settings">
		<input type="hidden" name="noesia_cs_tab" value="reorder-reminders">
		<section class="noesia-cs-panel noesia-cs-hub-smart-intro noesia-cs-reorder-intro">
			<div class="noesia-cs-panel__head">
				<h2><?php echo esc_html( $title ); ?></h2>
				<span><?php echo esc_html( $eyebrow ); ?></span>
			</div>
			<p><?php echo esc_html( $description ); ?></p>
		</section>
		<div class="noesia-cs-hub-smart-grid noesia-cs-reorder-settings-grid">
			<?php foreach ( $cards as $card ) : ?>
				<section class="noesia-cs-hub-smart-card noesia-cs-hub-smart-card--<?php echo esc_attr( sanitize_html_class( $card['tone'] ?? 'default' ) ); ?>">
					<div class="noesia-cs-hub-smart-card__head">
						<span class="noesia-cs-hub-smart-card__icon" aria-hidden="true"><?php echo esc_html( $card['icon'] ?? '●' ); ?></span>
						<div>
							<h3><?php echo esc_html( $card['title'] ?? '' ); ?></h3>
							<p><?php echo esc_html( $card['description'] ?? '' ); ?></p>
						</div>
						<?php if ( ! empty( $card['badge'] ) ) : ?><em class="is-active"><?php echo esc_html( $card['badge'] ); ?></em><?php endif; ?>
					</div>
					<div class="noesia-cs-hub-smart-card__fields">
						<?php foreach ( $card['fields'] ?? array() as $field ) : ?>
							<?php $admin->render_field( $field, $settings[ $field ] ?? '' ); ?>
						<?php endforeach; ?>
					</div>
				</section>
			<?php endforeach; ?>
		</div>
		<div class="noesia-cs-sticky-save"><button class="noesia-cs-primary-button" type="submit"><?php echo esc_html( $button_text ); ?></button></div>
	</form>
	<?php
};
?>
<section class="noesia-cs-feature-hero noesia-cs-feature-hero--reorder">
	<div>
		<span><?php esc_html_e( 'Customer-requested reminders', 'noesia-commerce-suite' ); ?></span>
		<h2><?php esc_html_e( 'Reorder Reminders', 'noesia-commerce-suite' ); ?></h2>
		<p><?php esc_html_e( 'Keep the public Order Again button separate from scheduled reminder emails. Reminder emails are sent only when the store enables them and a customer asks for a future reorder reminder.', 'noesia-commerce-suite' ); ?></p>
	</div>
	<div class="noesia-cs-hero-actions"><div class="noesia-cs-hero-orb">↻</div></div>
</section>

<nav class="noesia-cs-hub-subtabs noesia-cs-reorder-subtabs" aria-label="<?php esc_attr_e( 'Reorder reminder sections', 'noesia-commerce-suite' ); ?>">
	<?php foreach ( $sections as $section_key => $section_label ) : ?>
		<a class="<?php echo 'overview' === $section_key ? 'is-active' : ''; ?>" href="#noesia-cs-reorder-<?php echo esc_attr( $section_key ); ?>" data-noesia-cs-reorder-subtab="<?php echo esc_attr( $section_key ); ?>"><?php echo esc_html( $section_label ); ?></a>
	<?php endforeach; ?>
</nav>

<section id="noesia-cs-reorder-overview" class="noesia-cs-reorder-section is-active" data-noesia-cs-reorder-section="overview">
	<section class="noesia-cs-reorder-explainer-grid">
		<div class="noesia-cs-panel noesia-cs-reorder-explainer">
			<div class="noesia-cs-panel__head"><h2><?php esc_html_e( 'Order Again button', 'noesia-commerce-suite' ); ?></h2><span><?php esc_html_e( 'public hub', 'noesia-commerce-suite' ); ?></span></div>
			<p><?php esc_html_e( 'This appears inside the public Order Care Hub and lets customers buy again immediately. It is a direct customer action, not an email reminder.', 'noesia-commerce-suite' ); ?></p>
		</div>
		<div class="noesia-cs-panel noesia-cs-reorder-explainer">
			<div class="noesia-cs-panel__head"><h2><?php esc_html_e( 'Reminder emails', 'noesia-commerce-suite' ); ?></h2><span><?php esc_html_e( 'scheduled email only', 'noesia-commerce-suite' ); ?></span></div>
			<p><?php esc_html_e( 'These are delayed emails sent only when a customer has asked for a future reminder. The feature stays inactive unless reminders and reminder email are both enabled.', 'noesia-commerce-suite' ); ?></p>
		</div>
		<div class="noesia-cs-panel noesia-cs-reorder-explainer">
			<div class="noesia-cs-panel__head"><h2><?php esc_html_e( 'Checkout reminder request', 'noesia-commerce-suite' ); ?></h2><span><?php esc_html_e( 'customer opt-in', 'noesia-commerce-suite' ); ?></span></div>
			<p><?php esc_html_e( 'An optional checkout checkbox can let customers choose a future reminder date. It is designed for standard WooCommerce checkout. The manual shortcode fallback is only for builders that render fields inside the real checkout form and must be tested before production.', 'noesia-commerce-suite' ); ?></p>
		</div>
	</section>
	<section class="noesia-cs-metrics">
		<?php $admin->metric_card( __( 'Pending', 'noesia-commerce-suite' ), number_format_i18n( $counts['pending'] ), '●', __( 'customer reminders', 'noesia-commerce-suite' ), 'bag' ); ?>
		<?php $admin->metric_card( __( 'Sent', 'noesia-commerce-suite' ), number_format_i18n( $counts['sent'] ), '●', __( 'real emails sent', 'noesia-commerce-suite' ), 'trend' ); ?>
		<?php $admin->metric_card( __( 'Skipped', 'noesia-commerce-suite' ), number_format_i18n( $counts['skipped'] ), '●', __( 'opt-out or invalid data', 'noesia-commerce-suite' ), 'target' ); ?>
		<?php $admin->metric_card( __( 'Failed', 'noesia-commerce-suite' ), number_format_i18n( $counts['failed'] ), '●', __( 'mail/order issues', 'noesia-commerce-suite' ), 'heart' ); ?>
		<?php $admin->metric_card( __( 'Opted out', 'noesia-commerce-suite' ), number_format_i18n( $counts['opted_out'] ?? 0 ), '●', __( 'reorder email opt-outs', 'noesia-commerce-suite' ), 'target' ); ?>
	</section>
	<div class="noesia-cs-panel noesia-cs-reorder-status-panel">
		<div class="noesia-cs-panel__head"><h2><?php esc_html_e( 'Test Reminder Email', 'noesia-commerce-suite' ); ?></h2><span><?php echo ! empty( $runtime['email_engine_ready'] ) ? esc_html__( 'email test ready', 'noesia-commerce-suite' ) : esc_html__( 'email test unavailable', 'noesia-commerce-suite' ); ?></span></div>
		<p class="noesia-cs-panel__body-note"><?php esc_html_e( 'Send one safe test email to yourself. No customers are contacted and no real reminder records are changed.', 'noesia-commerce-suite' ); ?></p>
		<div class="noesia-cs-mini-grid noesia-cs-reorder-status-grid">
			<div class="noesia-cs-reorder-status-card <?php echo ! empty( $runtime['email_engine_ready'] ) ? 'is-ok' : 'is-warn'; ?>"><strong><?php echo ! empty( $runtime['email_engine_ready'] ) ? esc_html__( 'Ready', 'noesia-commerce-suite' ) : esc_html__( 'Off', 'noesia-commerce-suite' ); ?></strong><span><?php esc_html_e( 'Email sending', 'noesia-commerce-suite' ); ?></span></div>
			<div class="noesia-cs-reorder-status-card <?php echo ! empty( $runtime['pending_due'] ) ? 'is-warn' : 'is-muted'; ?>"><strong><?php echo esc_html( absint( $runtime['pending_due'] ?? 0 ) ); ?></strong><span><?php esc_html_e( 'Customer reminders ready today', 'noesia-commerce-suite' ); ?></span></div>
			<div class="noesia-cs-reorder-status-card"><strong><?php echo esc_html( absint( $runtime['pending_future'] ?? 0 ) ); ?></strong><span><?php esc_html_e( 'Customer reminders for later', 'noesia-commerce-suite' ); ?></span></div>
			<div class="noesia-cs-reorder-status-card"><strong><?php echo ! empty( $runtime['next_cron'] ) ? esc_html( date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), absint( $runtime['next_cron'] ) ) ) : esc_html__( 'Automatic system check', 'noesia-commerce-suite' ); ?></strong><span><?php esc_html_e( 'Automatic reminder check', 'noesia-commerce-suite' ); ?></span></div>
		</div>
		<?php if ( ! empty( $runtime['last_message'] ) ) : ?>
			<div class="noesia-cs-reorder-last-result"><strong><?php esc_html_e( 'Last result', 'noesia-commerce-suite' ); ?></strong><span><?php echo esc_html( $runtime['last_message'] ); ?><?php if ( ! empty( $runtime['last_time'] ) ) : ?> · <?php echo esc_html( date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), absint( $runtime['last_time'] ) ) ); ?><?php endif; ?></span></div>
		<?php endif; ?>
		<div class="noesia-cs-reorder-live-test">
			<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
				<?php wp_nonce_field( 'noesia_cs_send_reorder_test_copy' ); ?>
				<input type="hidden" name="action" value="noesia_cs_send_reorder_test_copy">
				<label><span><?php esc_html_e( 'Send test to', 'noesia-commerce-suite' ); ?></span><input type="email" name="noesia_cs_reorder_test_recipient" value="<?php echo esc_attr( $settings['test_email_recipient'] ?? get_option( 'admin_email' ) ); ?>"></label>
				<button class="noesia-cs-primary-button" type="submit"><?php esc_html_e( 'Send Test Email Now', 'noesia-commerce-suite' ); ?></button>
			</form>
		</div>
	</div>
</section>

<section id="noesia-cs-reorder-settings" class="noesia-cs-reorder-section" data-noesia-cs-reorder-section="settings" hidden>
	<?php
	$render_reorder_form(
		__( 'Reminder Settings', 'noesia-commerce-suite' ),
		__( 'safe reminder control', 'noesia-commerce-suite' ),
		__( 'These settings control scheduled reminder emails only. They do not affect the immediate Order Again button inside the public Order Care Hub.', 'noesia-commerce-suite' ),
		array(
			array(
				'icon'         => '↻',
				'title'        => __( 'Reminder Workflow', 'noesia-commerce-suite' ),
				'description'  => __( 'Enable customer reorder reminders and choose the fallback reminder period when no checkout date is selected.', 'noesia-commerce-suite' ),
				'fields'       => array( 'reorder_reminders_enabled', 'reorder_default_days' ),
				'tone'         => 'warranty',
			),
			array(
				'icon'         => '🛒',
				'title'        => __( 'Reorder Destination', 'noesia-commerce-suite' ),
				'description'  => __( 'Choose where reorder links send customers from the public hub and reminder emails.', 'noesia-commerce-suite' ),
				'fields'       => array( 'reorder_button_behavior' ),
				'tone'         => 'documents',
			),
			array(
				'icon'         => '⚙',
				'title'        => __( 'Sending Volume', 'noesia-commerce-suite' ),
				'description'  => __( 'Choose how many reminder emails Noesia handles at once so sending stays smooth on shared hosting and larger stores.', 'noesia-commerce-suite' ),
				'fields'       => array( 'reorder_batch_size' ),
				'tone'         => 'video',
			),
		),
		__( 'Save Reminder Settings', 'noesia-commerce-suite' )
	);
	?>
</section>

<section id="noesia-cs-reorder-checkout" class="noesia-cs-reorder-section" data-noesia-cs-reorder-section="checkout" hidden>
	<?php
	$reorder_reminders_on = 'yes' === ( $settings['reorder_reminders_enabled'] ?? 'no' );
	$checkout_request_on  = 'yes' === ( $settings['reorder_checkout_request_enabled'] ?? 'no' );
	$reorder_email_on     = 'yes' === ( $settings['reorder_email_enabled'] ?? 'no' );
	$field_should_appear   = $reorder_reminders_on && $checkout_request_on;
	$checkout_page_id      = function_exists( 'wc_get_page_id' ) ? absint( wc_get_page_id( 'checkout' ) ) : 0;
	$checkout_type         = __( 'Unknown', 'noesia-commerce-suite' );
	$checkout_note         = __( 'Could not inspect the checkout page automatically. Test checkout after enabling this option.', 'noesia-commerce-suite' );

	if ( $checkout_page_id > 0 ) {
		$checkout_post  = get_post( $checkout_page_id );
		$content        = $checkout_post ? (string) $checkout_post->post_content : '';
		if ( function_exists( 'has_block' ) && $checkout_post && has_block( 'woocommerce/checkout', $checkout_post ) ) {
			$checkout_type  = __( 'Checkout Block detected', 'noesia-commerce-suite' );
			$checkout_note  = __( 'The automatic classic checkout hook will not reliably render inside the Checkout Block. Use a builder-supported shortcode placement inside the real checkout form, or switch the Checkout page to the classic [woocommerce_checkout] shortcode after testing.', 'noesia-commerce-suite' );
		} elseif ( false !== strpos( $content, '[woocommerce_checkout]' ) ) {
			$checkout_type  = __( 'Classic shortcode checkout detected', 'noesia-commerce-suite' );
			$checkout_note  = __( 'The automatic field should appear on the standard checkout if both required settings are enabled.', 'noesia-commerce-suite' );
		} else {
			$checkout_type  = __( 'Custom checkout content', 'noesia-commerce-suite' );
			$checkout_note  = __( 'Your checkout page does not clearly show the classic shortcode or Checkout Block. Test the checkout and use the shortcode only if your builder renders it inside the real checkout form.', 'noesia-commerce-suite' );
		}
	}
	?>
	<section class="noesia-cs-panel noesia-cs-checkout-status-panel">
		<div class="noesia-cs-panel__head"><h2><?php esc_html_e( 'Checkout Field Status', 'noesia-commerce-suite' ); ?></h2><span><?php echo $field_should_appear ? esc_html__( 'ready to display', 'noesia-commerce-suite' ) : esc_html__( 'not ready yet', 'noesia-commerce-suite' ); ?></span></div>
		<p class="noesia-cs-panel__body-note"><?php esc_html_e( 'The checkout reminder field appears only when Reorder Reminders and Checkout Reminder Request are both enabled. Reorder Email controls sending later; it is not required for the checkbox to display.', 'noesia-commerce-suite' ); ?></p>
		<div class="noesia-cs-checkout-status-grid">
			<div class="noesia-cs-checkout-status-card <?php echo $reorder_reminders_on ? 'is-ok' : 'is-warn'; ?>"><strong><?php echo $reorder_reminders_on ? esc_html__( 'ON', 'noesia-commerce-suite' ) : esc_html__( 'OFF', 'noesia-commerce-suite' ); ?></strong><span><?php esc_html_e( 'Enable Reorder Reminders', 'noesia-commerce-suite' ); ?></span></div>
			<div class="noesia-cs-checkout-status-card <?php echo $checkout_request_on ? 'is-ok' : 'is-warn'; ?>"><strong><?php echo $checkout_request_on ? esc_html__( 'ON', 'noesia-commerce-suite' ) : esc_html__( 'OFF', 'noesia-commerce-suite' ); ?></strong><span><?php esc_html_e( 'Checkout Reminder Request', 'noesia-commerce-suite' ); ?></span></div>
			<div class="noesia-cs-checkout-status-card <?php echo $field_should_appear ? 'is-ok' : 'is-warn'; ?>"><strong><?php echo $field_should_appear ? esc_html__( 'YES', 'noesia-commerce-suite' ) : esc_html__( 'NO', 'noesia-commerce-suite' ); ?></strong><span><?php esc_html_e( 'Field should appear', 'noesia-commerce-suite' ); ?></span></div>
			<div class="noesia-cs-checkout-status-card <?php echo $reorder_email_on ? 'is-ok' : 'is-muted'; ?>"><strong><?php echo $reorder_email_on ? esc_html__( 'ON', 'noesia-commerce-suite' ) : esc_html__( 'OFF', 'noesia-commerce-suite' ); ?></strong><span><?php esc_html_e( 'Email sending later', 'noesia-commerce-suite' ); ?></span></div>
		</div>
		<div class="noesia-cs-checkout-detected"><strong><?php echo esc_html( $checkout_type ); ?></strong><span><?php echo esc_html( $checkout_note ); ?></span></div>
		<?php if ( $field_should_appear ) : ?>
			<p class="noesia-cs-panel__body-note"><?php esc_html_e( 'Automatic display uses one standard classic checkout position near Order Notes. This avoids duplicate boxes on classic checkout pages.', 'noesia-commerce-suite' ); ?></p>
		<?php endif; ?>
	</section>
	<?php
	$render_reorder_form(
		__( 'Checkout Reminder Request', 'noesia-commerce-suite' ),
		__( 'customer opt-in', 'noesia-commerce-suite' ),
		__( 'Shows a small optional checkbox in checkout. If the customer ticks it, a date field opens and Noesia saves one future reorder reminder for the selected date after checkout.', 'noesia-commerce-suite' ),
		array(
			array(
				'icon'         => '☑',
				'title'        => __( 'Checkout Field', 'noesia-commerce-suite' ),
				'description'  => __( 'Enable the optional checkout request and customize the text customers see near the order notes area. The info text should tell customers they will receive an email on the date they choose.', 'noesia-commerce-suite' ),
				'fields'       => array( 'reorder_checkout_request_enabled', 'reorder_checkout_style', 'reorder_checkout_badge_label', 'reorder_checkout_label', 'reorder_checkout_info_text', 'reorder_checkout_date_label' ),
				'tone'         => 'warranty',
			),
			array(
				'icon'         => '✓',
				'title'        => __( 'Safety Rule', 'noesia-commerce-suite' ),
				'description'  => __( 'No reminder is created unless the customer explicitly checks the box and chooses a valid future date.', 'noesia-commerce-suite' ),
				'fields'       => array( 'reorder_reminders_enabled', 'reorder_email_enabled' ),
				'tone'         => 'documents',
			),
			array(
				'icon'         => '⌁',
				'title'        => __( 'Manual Shortcode Fallback', 'noesia-commerce-suite' ),
				'description'  => __( 'Use this fallback only if your checkout builder renders shortcodes inside the real WooCommerce checkout form. If placed outside the checkout form, the customer selection may not be submitted.', 'noesia-commerce-suite' ),
				'fields'       => array(),
				'tone'         => 'promo',
			),
		),
		__( 'Save Checkout Request', 'noesia-commerce-suite' )
	);
	?>
		<?php
		$checkout_style         = sanitize_key( $settings['reorder_checkout_style'] ?? 'clean' );
		$checkout_style         = in_array( $checkout_style, array( 'clean', 'dark', 'glamour' ), true ) ? $checkout_style : 'clean';
		$checkout_style_labels  = array(
			'clean'    => __( 'Clean', 'noesia-commerce-suite' ),
			'dark'     => __( 'Dark', 'noesia-commerce-suite' ),
			'glamour'  => __( 'Noesia Glamour', 'noesia-commerce-suite' ),
		);
		$preview_date           = date_i18n( 'Y-m-d', strtotime( '+30 days' ) );
		$checkout_badge_label = ! empty( $settings['reorder_checkout_badge_label'] ) ? $settings['reorder_checkout_badge_label'] : __( 'Reorder Reminder', 'noesia-commerce-suite' );
		?>
		<div class="noesia-cs-panel noesia-cs-checkout-preview-panel" data-noesia-cs-checkout-style-preview>
			<div class="noesia-cs-panel__head"><h2><?php esc_html_e( 'Checkout Reminder Box Preview', 'noesia-commerce-suite' ); ?></h2><span><?php esc_html_e( 'click a style below', 'noesia-commerce-suite' ); ?></span></div>
			<p class="noesia-cs-panel__body-note"><?php esc_html_e( 'Click Clean, Dark or Noesia Glamour to preview the checkout box instantly. The real setting below changes with your choice; press Save Checkout Request to keep it.', 'noesia-commerce-suite' ); ?></p>
			<div class="noesia-cs-checkout-preview-stage noesia-cs-checkout-preview-stage--<?php echo esc_attr( $checkout_style ); ?>" data-noesia-cs-checkout-preview-stage>
				<div class="noesia-cs-checkout-reorder-request noesia-cs-checkout-reorder-request--<?php echo esc_attr( $checkout_style ); ?> noesia-cs-checkout-reorder-request--preview" data-noesia-cs-checkout-preview-box data-noesia-cs-badge-label="<?php echo esc_attr( $checkout_badge_label ); ?>">
					<label class="noesia-cs-checkout-reorder-request__checkbox"><input type="checkbox" checked disabled><span><?php echo esc_html( $settings['reorder_checkout_label'] ?? __( 'Remind me to reorder later', 'noesia-commerce-suite' ) ); ?></span></label>
					<p><?php echo esc_html( $settings['reorder_checkout_info_text'] ?? __( 'Choose this if you want to receive a scheduled reorder reminder email on the date you select.', 'noesia-commerce-suite' ) ); ?></p>
					<div class="noesia-cs-checkout-reorder-request__date"><label><?php echo esc_html( $settings['reorder_checkout_date_label'] ?? __( 'Reminder date', 'noesia-commerce-suite' ) ); ?></label><input type="date" value="<?php echo esc_attr( $preview_date ); ?>" disabled></div>
				</div>
			</div>
			<div class="noesia-cs-checkout-style-gallery" role="radiogroup" aria-label="<?php esc_attr_e( 'Checkout box style preview', 'noesia-commerce-suite' ); ?>">
				<?php foreach ( $checkout_style_labels as $style_key => $style_label ) : ?>
					<button type="button" class="noesia-cs-checkout-style-card noesia-cs-checkout-style-card--<?php echo esc_attr( $style_key ); ?> <?php echo $checkout_style === $style_key ? 'is-active' : ''; ?>" data-noesia-cs-checkout-style-choice="<?php echo esc_attr( $style_key ); ?>" aria-pressed="<?php echo $checkout_style === $style_key ? 'true' : 'false'; ?>"><strong><?php echo esc_html( $style_label ); ?></strong><span><?php echo $checkout_style === $style_key ? esc_html__( 'Selected', 'noesia-commerce-suite' ) : esc_html__( 'Click to preview', 'noesia-commerce-suite' ); ?></span></button>
				<?php endforeach; ?>
			</div>
			<p class="noesia-cs-checkout-style-hint" data-noesia-cs-checkout-style-hint><?php esc_html_e( 'Selected style is also applied to the Checkout Box Style setting. Save to use it on the real checkout.', 'noesia-commerce-suite' ); ?></p>
		</div>
	<div class="noesia-cs-reorder-compat-grid">
		<div class="noesia-cs-panel noesia-cs-reorder-compat-card">
			<div class="noesia-cs-panel__head"><h2><?php esc_html_e( 'Checkout Compatibility', 'noesia-commerce-suite' ); ?></h2><span><?php esc_html_e( 'test after enabling', 'noesia-commerce-suite' ); ?></span></div>
			<p><?php esc_html_e( 'This option is designed for the standard WooCommerce classic checkout. If your store uses the WooCommerce Checkout Block or a third-party checkout builder, test the checkout after enabling it because custom layouts may control where additional fields appear.', 'noesia-commerce-suite' ); ?></p>
			<details class="noesia-cs-checkout-guide">
				<summary><?php esc_html_e( 'Using the WooCommerce Checkout Block? View classic checkout steps', 'noesia-commerce-suite' ); ?></summary>
				<ol>
					<li><?php esc_html_e( 'Open Pages → Checkout in WordPress.', 'noesia-commerce-suite' ); ?></li>
					<li><?php esc_html_e( 'Remove the Checkout block and add a Shortcode block.', 'noesia-commerce-suite' ); ?></li>
					<li><?php esc_html_e( 'Paste [woocommerce_checkout], update the page, then run a live test order before production.', 'noesia-commerce-suite' ); ?></li>
				</ol>
			</details>
		</div>
		<div class="noesia-cs-panel noesia-cs-reorder-compat-card">
			<div class="noesia-cs-panel__head"><h2><?php esc_html_e( 'Manual Shortcode Fallback', 'noesia-commerce-suite' ); ?></h2><span><?php esc_html_e( 'for compatible builders', 'noesia-commerce-suite' ); ?></span></div>
			<p><?php esc_html_e( 'If the automatic checkout hook is hidden by a custom checkout builder, you may test this shortcode only inside the real checkout form/flow. Do not place it in a normal Elementor section outside checkout; the checkbox/date may display but the submitted order may not receive the values. Always run a live test order before production.', 'noesia-commerce-suite' ); ?></p>
			<code class="noesia-cs-inline-code">[noesia_cs_reorder_reminder_request]</code>
		</div>
	</div>
</section>

<section id="noesia-cs-reorder-email" class="noesia-cs-reorder-section" data-noesia-cs-reorder-section="email" hidden>
	<?php
	$render_reorder_form(
		__( 'Reorder Email Template', 'noesia-commerce-suite' ),
		__( 'customer-requested email', 'noesia-commerce-suite' ),
		__( 'Enable and customize the reminder email. It is sent only when a customer has asked for a future reorder reminder.', 'noesia-commerce-suite' ),
		array(
			array(
				'icon'         => '✉',
				'title'        => __( 'Email Sending & Opt-out', 'noesia-commerce-suite' ),
				'description'  => __( 'Allow Noesia to send due reminder emails and include the unsubscribe/opt-out link inside those emails.', 'noesia-commerce-suite' ),
				'fields'       => array( 'reorder_email_enabled', 'reorder_opt_out_enabled', 'unsubscribe_text' ),
				'tone'         => 'promo',
			),
			array(
				'icon'         => '✦',
				'title'        => __( 'Email Copy', 'noesia-commerce-suite' ),
				'description'  => __( 'Use supported email words such as {store_name}, {order_number} and {product_name} so Noesia can fill real details automatically.', 'noesia-commerce-suite' ),
				'fields'       => array( 'reorder_email_subject', 'reorder_email_body', 'reorder_email_button_label' ),
				'tone'         => 'documents',
			),
		),
		__( 'Save Reorder Email', 'noesia-commerce-suite' )
	);
	?>
	<div class="noesia-cs-panel noesia-cs-reorder-optout-explainer">
		<div class="noesia-cs-panel__head"><h2><?php esc_html_e( 'How Opt-out Works', 'noesia-commerce-suite' ); ?></h2><span><?php esc_html_e( 'reorder reminder emails only', 'noesia-commerce-suite' ); ?></span></div>
		<p class="noesia-cs-panel__body-note"><?php esc_html_e( 'When Enable Reminder Opt-out is on, scheduled reorder reminder emails include an unsubscribe link. If the customer clicks it, this order is marked as opted out and future reorder reminder emails for that order are skipped. It does not disable the public Order Again button, the Order Care Hub, or normal WooCommerce emails.', 'noesia-commerce-suite' ); ?></p>
	</div>
	<div class="noesia-cs-panel noesia-cs-reorder-email-preview-panel">
		<div class="noesia-cs-panel__head"><h2><?php esc_html_e( 'Reorder Email Preview', 'noesia-commerce-suite' ); ?></h2><span><?php esc_html_e( 'uses saved settings', 'noesia-commerce-suite' ); ?></span></div>
		<p class="noesia-cs-panel__body-note"><?php esc_html_e( 'This is a layout preview with sample values. Real reminder emails are sent only when a reminder is due. The logo comes from Email Logo URL in Email Settings.', 'noesia-commerce-suite' ); ?></p>
		<div class="noesia-cs-email-preview-frame"><?php echo wp_kses_post( $email_preview ); ?></div>
	</div>
</section>

<section id="noesia-cs-reorder-queue" class="noesia-cs-reorder-section" data-noesia-cs-reorder-section="queue" hidden>
	<div class="noesia-cs-panel">
		<div class="noesia-cs-panel__head">
			<h2><?php esc_html_e( 'Reminder Email History', 'noesia-commerce-suite' ); ?></h2>
			<span><?php esc_html_e( 'customer reminder activity', 'noesia-commerce-suite' ); ?></span>
		</div>
		<p class="noesia-cs-panel__body-note"><?php esc_html_e( 'This area only shows what happened with customer reorder reminders. It does not send emails, change orders or contact customers.', 'noesia-commerce-suite' ); ?></p>
		<p class="noesia-cs-panel__body-note"><?php printf( /* translators: %d: days kept. */ esc_html__( 'Noesia keeps recent email history for %d days so you can check recent sent or failed reminder emails.', 'noesia-commerce-suite' ), absint( $settings['email_log_retention_days'] ?? 15 ) ); ?></p>
	</div>

	<section class="noesia-cs-grid noesia-cs-grid--bottom">
		<div class="noesia-cs-panel">
			<div class="noesia-cs-panel__head"><h2><?php esc_html_e( 'Upcoming Customer Reminders', 'noesia-commerce-suite' ); ?></h2><span><?php esc_html_e( 'saved from checkout', 'noesia-commerce-suite' ); ?></span></div>
			<div class="noesia-cs-log-list">
				<?php foreach ( $queue_items as $entry ) : ?>
					<?php $is_opted_out = ! empty( $entry['opted_out'] ); ?>
					<div class="noesia-cs-log-row is-<?php echo esc_attr( $is_opted_out ? 'opted-out' : $entry['status'] ); ?>">
						<?php
						$entry_order_id    = isset( $entry['order_id'] ) ? absint( $entry['order_id'] ) : 0;
						$entry_due_label   = date_i18n( get_option( 'date_format' ), absint( $entry['due_at'] ) );
						$entry_order       = $entry_order_id && function_exists( 'wc_get_order' ) ? wc_get_order( $entry_order_id ) : null;
						$entry_total_label = '';
						$entry_order_url   = '';

						if ( $entry_order && is_a( $entry_order, 'WC_Order' ) ) {
							$entry_total_label = wp_strip_all_tags( wc_price( $entry_order->get_total(), array( 'currency' => $entry_order->get_currency() ) ) );

							if ( current_user_can( 'edit_shop_orders' ) ) {
								$entry_order_url = method_exists( $entry_order, 'get_edit_order_url' ) ? $entry_order->get_edit_order_url() : admin_url( 'post.php?post=' . $entry_order_id . '&action=edit' );
							}
						}
						?>
						<div>
							<strong><?php echo esc_html( $entry['product_name'] ); ?></strong>
							<small>
								<?php if ( $entry_order_url ) : ?>
									<a href="<?php echo esc_url( $entry_order_url ); ?>" target="_blank" rel="noopener noreferrer"><?php printf( /* translators: %d: numeric order id. */ esc_html__( 'Order #%d', 'noesia-commerce-suite' ), absint( $entry_order_id ) ); ?></a>
								<?php else : ?>
									<?php printf( /* translators: %d: numeric order id. */ esc_html__( 'Order #%d', 'noesia-commerce-suite' ), absint( $entry_order_id ) ); ?>
								<?php endif; ?>
								<?php if ( $entry_total_label ) : ?> · <?php echo esc_html( $entry_total_label ); ?><?php endif; ?>
								 · <?php printf( /* translators: %s: reminder date. */ esc_html__( 'reminder date %s', 'noesia-commerce-suite' ), esc_html( $entry_due_label ) ); ?>
								<?php if ( $is_opted_out ) : ?> · <?php esc_html_e( 'Customer stopped this reminder', 'noesia-commerce-suite' ); ?><?php endif; ?>
							</small>
						</div>
						<em><?php echo esc_html( $is_opted_out ? __( 'Stopped', 'noesia-commerce-suite' ) : ucfirst( $entry['status'] ) ); ?></em>
					</div>
				<?php endforeach; ?>
				<?php if ( empty( $queue_items ) ) : ?><p><?php esc_html_e( 'No customer reminders saved yet. After a test order, the customer must tick the reminder checkbox and choose a future date; then the request appears here.', 'noesia-commerce-suite' ); ?></p><?php endif; ?>
			</div>
		</div>
		<div class="noesia-cs-panel">
			<div class="noesia-cs-panel__head"><h2><?php esc_html_e( 'Stopped Reminder Emails', 'noesia-commerce-suite' ); ?></h2><span><?php esc_html_e( 'customer clicked stop reminders', 'noesia-commerce-suite' ); ?></span></div>
			<p class="noesia-cs-panel__body-note"><?php esc_html_e( 'This list appears only when a customer clicks the stop-reminders link inside a reorder reminder email. It only stops future reorder reminder emails for that order; normal WooCommerce emails and the Order Again button still work.', 'noesia-commerce-suite' ); ?></p>
			<div class="noesia-cs-log-list">
				<?php foreach ( $opt_out_items as $optout ) : ?>
					<div class="noesia-cs-log-row is-opted-out">
						<div><strong><?php echo esc_html( $optout['label'] ); ?></strong><small><?php printf( /* translators: %d: numeric order id. */ esc_html__( 'Order #%d', 'noesia-commerce-suite' ), absint( $optout['order_id'] ) ); ?></small></div>
						<em><?php echo ! empty( $optout['time'] ) ? esc_html( date_i18n( get_option( 'date_format' ), absint( $optout['time'] ) ) ) : esc_html__( 'Stopped', 'noesia-commerce-suite' ); ?></em>
					</div>
				<?php endforeach; ?>
				<?php if ( empty( $opt_out_items ) ) : ?><p><?php esc_html_e( 'No customers have stopped reminder emails yet.', 'noesia-commerce-suite' ); ?></p><?php endif; ?>
			</div>
		</div>
		<div class="noesia-cs-panel">
			<div class="noesia-cs-panel__head"><h2><?php esc_html_e( 'Recent Reminder Email Results', 'noesia-commerce-suite' ); ?></h2><span><?php esc_html_e( 'sent, skipped or failed', 'noesia-commerce-suite' ); ?></span></div>
			<p class="noesia-cs-panel__body-note"><?php esc_html_e( 'Use this list only to see recent reminder email results. Customer reminders are sent automatically when their selected date arrives.', 'noesia-commerce-suite' ); ?></p>
			<div class="noesia-cs-log-list">
				<?php foreach ( $recent_logs as $log ) : ?>
					<div class="noesia-cs-log-row is-<?php echo esc_attr( $log['status'] ); ?>">
						<div><strong><?php echo esc_html( ucfirst( str_replace( '_', ' ', $log['status'] ) ) ); ?></strong><small><?php echo esc_html( $log['message'] ); ?><?php if ( ! empty( $log['order_id'] ) ) : ?> · <?php printf( /* translators: %d: numeric order id. */ esc_html__( 'Order #%d', 'noesia-commerce-suite' ), absint( $log['order_id'] ) ); ?><?php endif; ?></small></div>
						<em><?php echo esc_html( date_i18n( get_option( 'date_format' ), absint( $log['time'] ) ) ); ?></em>
					</div>
				<?php endforeach; ?>
				<?php if ( empty( $recent_logs ) ) : ?><p><?php esc_html_e( 'No reminder email results yet.', 'noesia-commerce-suite' ); ?></p><?php endif; ?>
			</div>
		</div>
	</section>
</section>

<section class="noesia-cs-pro-grid">
	<?php $admin->pro_edition_card( __( 'Branded Reminder Emails', 'noesia-commerce-suite' ), __( 'Make reorder reminder emails look and sound like your brand: subject, body, button text and button color.', 'noesia-commerce-suite' ), 'qr_email_branding' ); ?>
	<?php $admin->pro_edition_card( __( 'Customer QR Studio', 'noesia-commerce-suite' ), __( 'Print reorder cards and package inserts that guide customers back to buy again or request care.', 'noesia-commerce-suite' ), 'customer_qr_studio' ); ?>
</section>
