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

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- View templates receive local variables from their renderer.
$fields = array( 'passport_email_enabled', 'email_integration', 'email_logo', 'email_button_color', 'email_sender_name', 'test_email_recipient', 'passport_email_subject', 'passport_email_body', 'passport_email_button_label', 'email_footer_text', 'email_log_retention_days' );
$admin->render_settings_group( 'emails', __( 'Order Care Hub Email & Branding', 'noesia-commerce-suite' ), __( 'Controls the Order Care Hub block added to WooCommerce customer emails, verification emails and shared email branding. Reorder reminder email copy lives only inside Reorder Reminders.', 'noesia-commerce-suite' ), $fields, $settings, 'emails' );
?>
<section class="noesia-cs-panel noesia-cs-email-role-panel">
	<div class="noesia-cs-panel__head"><h2><?php esc_html_e( 'What this tab controls', 'noesia-commerce-suite' ); ?></h2><span><?php esc_html_e( 'Order Care Hub email access', 'noesia-commerce-suite' ); ?></span></div>
	<p class="noesia-cs-panel__body-note"><?php esc_html_e( 'Use this tab for the Order Care Hub block that can be inserted into WooCommerce customer emails and for shared email branding/testing. This does not create a separate standalone order email; WooCommerce must still send the normal customer email. Scheduled reorder reminder emails are managed separately under Reorder Reminders → Email Template.', 'noesia-commerce-suite' ); ?></p>
	<div class="noesia-cs-email-role-grid">
		<div class="noesia-cs-email-role-card is-hub">
			<strong><?php esc_html_e( 'Order Care Hub Email', 'noesia-commerce-suite' ); ?></strong>
			<span><?php esc_html_e( 'Controls the Order Care Hub access block inside WooCommerce customer emails when email access is enabled.', 'noesia-commerce-suite' ); ?></span>
		</div>
		<div class="noesia-cs-email-role-card is-brand">
			<strong><?php esc_html_e( 'Branding & Delivery Test', 'noesia-commerce-suite' ); ?></strong>
			<span><?php esc_html_e( 'Controls shared logo/color/sender details and sends one verification email to an admin-controlled inbox.', 'noesia-commerce-suite' ); ?></span>
		</div>
		<div class="noesia-cs-email-role-card is-reorder">
			<strong><?php esc_html_e( 'Not Reorder Reminders', 'noesia-commerce-suite' ); ?></strong>
			<span><?php esc_html_e( 'Scheduled reorder reminder copy, opt-out text and reminder sending controls live only in the Reorder Reminders tab. The opt-out link appears only in those reorder reminder emails.', 'noesia-commerce-suite' ); ?></span>
		</div>
	</div>
</section>

<section class="noesia-cs-panel noesia-cs-email-requirements-panel">
	<div class="noesia-cs-panel__head"><h2><?php esc_html_e( 'Order Email Requirements', 'noesia-commerce-suite' ); ?></h2><span><?php esc_html_e( 'WooCommerce email block', 'noesia-commerce-suite' ); ?></span></div>
	<p class="noesia-cs-panel__body-note"><?php esc_html_e( 'The Order Care Hub access block is added inside WooCommerce customer emails only when all related email access options are enabled. If no WooCommerce order email is delivered at all, check WooCommerce email settings, order status, SMTP and hosting mail delivery.', 'noesia-commerce-suite' ); ?></p>
	<div class="noesia-cs-email-role-grid">
		<div class="noesia-cs-email-role-card is-hub"><strong><?php esc_html_e( 'Order Care Hub Email', 'noesia-commerce-suite' ); ?></strong><span><?php echo 'yes' === ( $settings['passport_email_enabled'] ?? 'no' ) ? esc_html__( 'Enabled', 'noesia-commerce-suite' ) : esc_html__( 'Disabled', 'noesia-commerce-suite' ); ?></span></div>
		<div class="noesia-cs-email-role-card is-brand"><strong><?php esc_html_e( 'Email Integration', 'noesia-commerce-suite' ); ?></strong><span><?php echo 'yes' === ( $settings['email_integration'] ?? 'no' ) ? esc_html__( 'Enabled', 'noesia-commerce-suite' ) : esc_html__( 'Disabled', 'noesia-commerce-suite' ); ?></span></div>
		<div class="noesia-cs-email-role-card is-reorder"><strong><?php esc_html_e( 'Show in Woo Emails', 'noesia-commerce-suite' ); ?></strong><span><?php echo 'yes' === ( $settings['show_qr_emails'] ?? 'no' ) ? esc_html__( 'Enabled', 'noesia-commerce-suite' ) : esc_html__( 'Disabled', 'noesia-commerce-suite' ); ?></span></div>
	</div>
	<p class="noesia-cs-example-note"><?php esc_html_e( 'Opt-out links are not part of this Order Care Hub access email. They appear only inside scheduled reorder reminder emails when Reminder Opt-out is enabled.', 'noesia-commerce-suite' ); ?></p>
</section>

<div class="noesia-cs-panel noesia-cs-email-preview-panel">
	<div class="noesia-cs-panel__head"><h2><?php esc_html_e( 'Email Preview', 'noesia-commerce-suite' ); ?></h2><span><?php esc_html_e( 'Uses saved settings only', 'noesia-commerce-suite' ); ?></span></div>
	<p class="noesia-cs-panel__body-note"><?php esc_html_e( 'This preview shows the Order Care Hub access email layout with sample values. It is not a reorder reminder email and it does not count as a sent email.', 'noesia-commerce-suite' ); ?></p>
	<div class="noesia-cs-email-preview-grid noesia-cs-email-preview-grid--single">
		<div>
			<h3><?php esc_html_e( 'Order Care Hub Email', 'noesia-commerce-suite' ); ?></h3>
			<div class="noesia-cs-email-preview-frame"><?php echo wp_kses_post( $email_previews['passport'] ?? '' ); ?></div>
		</div>
	</div>
</div>
<div class="noesia-cs-panel">
	<div class="noesia-cs-panel__head"><h2><?php esc_html_e( 'Send Verification Email', 'noesia-commerce-suite' ); ?></h2><span><?php esc_html_e( 'Uses current saved email settings', 'noesia-commerce-suite' ); ?></span></div>
	<p class="noesia-cs-panel__body-note"><?php esc_html_e( 'This sends one real WordPress verification email to the chosen recipient. It contains no customer/order data and does not simulate campaigns or reminder performance.', 'noesia-commerce-suite' ); ?></p>
	<form class="noesia-cs-email-test-form" method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
		<?php wp_nonce_field( 'noesia_cs_send_test_email' ); ?>
		<input type="hidden" name="action" value="noesia_cs_send_test_email">
		<label><span><?php esc_html_e( 'Recipient', 'noesia-commerce-suite' ); ?></span><input type="email" name="noesia_cs_test_email_recipient" value="<?php echo esc_attr( $settings['test_email_recipient'] ); ?>"><small class="noesia-cs-field__help"><?php esc_html_e( 'Use an inbox you control, usually the store owner or admin email. This field is only for verification delivery tests; inbox placement depends on SMTP and domain authentication, not on this recipient value.', 'noesia-commerce-suite' ); ?></small></label>
		<button class="noesia-cs-primary-button" type="submit"><?php esc_html_e( 'Send Verification Email', 'noesia-commerce-suite' ); ?></button>
	</form>
</div>
<div class="noesia-cs-panel noesia-cs-deliverability-panel">
	<div class="noesia-cs-panel__head"><h2><?php esc_html_e( 'Deliverability', 'noesia-commerce-suite' ); ?></h2><span><?php esc_html_e( 'Inbox placement depends on mail transport', 'noesia-commerce-suite' ); ?></span></div>
	<p class="noesia-cs-panel__body-note"><?php esc_html_e( 'The module is self-hosted and reliable at the application layer, but inbox placement still depends on your SMTP transport and domain authentication records such as SPF, DKIM and DMARC.', 'noesia-commerce-suite' ); ?></p>
	<div class="noesia-cs-deliverability-grid">
		<div><strong><?php esc_html_e( 'Use authenticated SMTP', 'noesia-commerce-suite' ); ?></strong><span><?php esc_html_e( 'Prefer a trusted SMTP/mail provider instead of unauthenticated PHP mail when stores send customer emails.', 'noesia-commerce-suite' ); ?></span></div>
		<div><strong><?php esc_html_e( 'Align DNS records', 'noesia-commerce-suite' ); ?></strong><span><?php esc_html_e( 'Check that the From domain matches SPF, DKIM and DMARC expectations for your hosting or mail platform.', 'noesia-commerce-suite' ); ?></span></div>
	</div>
	<p class="noesia-cs-example-note"><?php esc_html_e( 'The examples below are not universal DNS records. Use the exact values from your hosting provider or mail platform.', 'noesia-commerce-suite' ); ?></p>
	<div class="noesia-cs-code-row"><span><?php esc_html_e( 'Example SPF', 'noesia-commerce-suite' ); ?></span><code>v=spf1 include:_spf.your-host.com ~all</code></div>
	<div class="noesia-cs-code-row"><span><?php esc_html_e( 'Example DMARC', 'noesia-commerce-suite' ); ?></span><code>v=DMARC1; p=none;</code></div>
</div>
<div class="noesia-cs-panel noesia-cs-email-smart-words-panel">
	<div class="noesia-cs-panel__head"><h2><?php esc_html_e( 'Smart Email Words', 'noesia-commerce-suite' ); ?></h2><span><?php esc_html_e( 'Automatic order details', 'noesia-commerce-suite' ); ?></span></div>
	<p class="noesia-cs-panel__body-note"><?php esc_html_e( 'Use these short words inside supported email fields and Noesia fills the real details automatically. Example: write {store_name} in the message, and the real email shows your store name. This helps every email feel personal without typing order details by hand.', 'noesia-commerce-suite' ); ?></p>
	<div class="noesia-cs-smart-words-example">
		<strong><?php esc_html_e( 'Simple example', 'noesia-commerce-suite' ); ?></strong>
		<span><?php esc_html_e( '“Thank you for shopping with {store_name}. Your order {order_number} is ready.”', 'noesia-commerce-suite' ); ?></span>
	</div>
	<div class="noesia-cs-table-wrap">
		<table class="noesia-cs-table">
			<thead><tr><th><?php esc_html_e( 'Text you type', 'noesia-commerce-suite' ); ?></th><th><?php esc_html_e( 'What Noesia fills automatically', 'noesia-commerce-suite' ); ?></th></tr></thead>
			<tbody>
			<?php foreach ( $email_placeholders as $placeholder => $description ) : ?>
				<tr><td><code><?php echo esc_html( $placeholder ); ?></code></td><td><?php echo esc_html( $description ); ?></td></tr>
			<?php endforeach; ?>
			</tbody>
		</table>
	</div>
</div>
<div class="noesia-cs-panel">
	<div class="noesia-cs-panel__head"><h2><?php esc_html_e( 'Email Delivery Log', 'noesia-commerce-suite' ); ?></h2><span><?php esc_html_e( 'No email addresses are stored here', 'noesia-commerce-suite' ); ?></span></div>
	<p class="noesia-cs-panel__body-note"><?php printf( /* translators: %d: numeric value. */ esc_html__( 'Showing 20 logs per page. Noesia keeps recent email history for %d days.', 'noesia-commerce-suite' ), absint( $settings['email_log_retention_days'] ?? 15 ) ); ?></p>
	<div class="noesia-cs-table-wrap">
		<table class="noesia-cs-table">
			<thead><tr><th><?php esc_html_e( 'Time', 'noesia-commerce-suite' ); ?></th><th><?php esc_html_e( 'Type', 'noesia-commerce-suite' ); ?></th><th><?php esc_html_e( 'Status', 'noesia-commerce-suite' ); ?></th><th><?php esc_html_e( 'Order', 'noesia-commerce-suite' ); ?></th><th><?php esc_html_e( 'Message', 'noesia-commerce-suite' ); ?></th></tr></thead>
			<tbody>
			<?php
			$email_log_page       = isset( $email_log_page ) ? absint( $email_log_page ) : 1;
			$email_logs_per_page  = isset( $email_logs_per_page ) ? max( 1, absint( $email_logs_per_page ) ) : 20;
			$email_log_count      = isset( $email_log_count ) ? absint( $email_log_count ) : 0;
			$email_log_pages      = max( 1, (int) ceil( $email_log_count / $email_logs_per_page ) );
			$email_log_page       = min( max( 1, $email_log_page ), $email_log_pages );
			$logs                 = isset( $logs ) && is_array( $logs ) ? $logs : array();
			?>
			<?php foreach ( $logs as $log ) : ?>
				<tr>
					<td><?php echo esc_html( ! empty( $log['time'] ) ? wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), absint( $log['time'] ) ) : '—' ); ?></td>
					<td><?php echo esc_html( ucfirst( sanitize_key( $log['type'] ?? '' ) ) ); ?></td>
					<td><span class="noesia-cs-status-pill is-<?php echo esc_attr( sanitize_key( $log['status'] ?? 'neutral' ) ); ?>"><?php echo esc_html( ucfirst( sanitize_key( $log['status'] ?? 'neutral' ) ) ); ?></span></td>
					<td><?php echo ! empty( $log['order_id'] ) ? esc_html( '#' . absint( $log['order_id'] ) ) : esc_html__( 'Not stored', 'noesia-commerce-suite' ); ?></td>
					<td><?php echo esc_html( $log['message'] ?? '' ); ?></td>
				</tr>
			<?php endforeach; ?>
			<?php if ( empty( $logs ) ) : ?>
				<tr><td colspan="5"><?php esc_html_e( 'No email delivery events yet.', 'noesia-commerce-suite' ); ?></td></tr>
			<?php endif; ?>
			</tbody>
		</table>
	</div>
	<?php if ( $email_log_pages > 1 ) : ?>
		<div class="noesia-cs-pagination">
			<?php for ( $page_index = 1; $page_index <= $email_log_pages; $page_index++ ) : ?>
				<a class="<?php echo $page_index === $email_log_page ? 'is-active' : ''; ?>" href="<?php echo esc_url( add_query_arg( array(
					'page'                      => 'noesia-commerce-suite',
					'tab'                       => 'emails',
					'noesia_cs_email_log_page'  => $page_index,
				), admin_url( 'admin.php' ) ) ); ?>"><?php echo esc_html( number_format_i18n( $page_index ) ); ?></a>
			<?php endfor; ?>
		</div>
	<?php endif; ?>
</div>
<section class="noesia-cs-pro-grid">
	<?php $admin->pro_edition_card( __( 'Branded QR & Care Emails', 'noesia-commerce-suite' ), __( 'Make QR labels, care emails, access cards and reminder buttons look like your own store brand.', 'noesia-commerce-suite' ), 'qr_email_branding' ); ?>
	<?php $admin->pro_edition_card( __( 'Smart Recovery Contacts', 'noesia-commerce-suite' ), __( 'Capture recovery emails and phone numbers from checkout flows with Pro.', 'noesia-commerce-suite' ), 'smart_recovery_contacts' ); ?>
</section>
