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

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- View templates receive local variables from their renderer.
$sections = isset( $sections ) && is_array( $sections ) ? $sections : array();
?>
<div class="noesia-cs-settings-layout">
	<aside class="noesia-cs-settings-anchor-nav" aria-label="<?php esc_attr_e( 'Settings sections', 'noesia-commerce-suite' ); ?>">
		<div class="noesia-cs-settings-anchor-nav__head">
			<strong><?php esc_html_e( 'Settings Menu', 'noesia-commerce-suite' ); ?></strong>
			<span><?php esc_html_e( 'Quick jump', 'noesia-commerce-suite' ); ?></span>
		</div>
		<div class="noesia-cs-settings-anchor-nav__links">
			<?php foreach ( $sections as $section_key => $section ) : ?>
				<a href="#noesia-cs-settings-<?php echo esc_attr( sanitize_key( $section_key ) ); ?>" data-noesia-cs-settings-anchor data-noesia-cs-no-ajax="1" data-noesia-cs-settings-jump="noesia-cs-settings-<?php echo esc_attr( sanitize_key( $section_key ) ); ?>"><?php echo esc_html( $section['title'] ); ?></a>
			<?php endforeach; ?>
		</div>
	</aside>
	<div class="noesia-cs-settings-content">
		<?php do_action( 'noesia_cs_settings_top', $settings, $admin ); ?>
		<?php $admin->render_system_status_panel(); ?>
		<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="noesia-cs-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="settings">
			<section class="noesia-cs-settings-grid noesia-cs-settings-grid--control-center">
				<?php foreach ( $sections as $section_key => $section ) : ?>
					<?php $admin->settings_card( $section['title'], $section['fields'], $settings, $section['description'] ?? '', $section_key ); ?>
				<?php endforeach; ?>
			</section>
			<div class="noesia-cs-sticky-save"><button class="noesia-cs-primary-button" type="submit"><?php esc_html_e( 'Save Settings', 'noesia-commerce-suite' ); ?></button></div>
		</form>
		<?php $admin->render_upgrade_summary(); ?>
		<?php $cleanup_summary = isset( $cleanup_summary ) && is_array( $cleanup_summary ) ? $cleanup_summary : array(); ?>
		<div class="noesia-cs-panel noesia-cs-maintenance-panel">
			<div class="noesia-cs-panel__head"><h2><?php esc_html_e( 'Safe Noesia Log Cleanup', 'noesia-commerce-suite' ); ?></h2><span><?php esc_html_e( 'Old plugin activity only', 'noesia-commerce-suite' ); ?></span></div>
			<p class="noesia-cs-panel__body-note"><?php esc_html_e( 'Use this only when you want to clear old Noesia activity logs and completed internal queues. It never deletes WooCommerce orders, products, customers, payments, coupons or active customer care links.', 'noesia-commerce-suite' ); ?></p>
			<?php if ( ! empty( $cleanup_summary['time'] ) ) : ?>
				<div class="noesia-cs-maintenance-summary">
					<strong><?php esc_html_e( 'Last cleanup', 'noesia-commerce-suite' ); ?>: <?php echo esc_html( wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), absint( $cleanup_summary['time'] ) ) ); ?></strong>
					<span><?php printf( /* translators: %d: numeric value. */ esc_html__( 'Email logs removed: %d', 'noesia-commerce-suite' ), absint( $cleanup_summary['email_logs_removed'] ?? 0 ) ); ?></span>
					<span><?php printf( /* translators: %d: numeric value. */ esc_html__( 'Care action logs removed: %d', 'noesia-commerce-suite' ), absint( $cleanup_summary['care_action_logs_removed'] ?? 0 ) ); ?></span>
					<span><?php printf( /* translators: %d: numeric value. */ esc_html__( 'Reorder logs removed: %d', 'noesia-commerce-suite' ), absint( $cleanup_summary['reorder_logs_removed'] ?? 0 ) ); ?></span>
					<span><?php printf( /* translators: %d: numeric value. */ esc_html__( 'Queue rows removed: %d', 'noesia-commerce-suite' ), absint( $cleanup_summary['reorder_queue_removed'] ?? 0 ) ); ?></span>
					<span><?php printf( /* translators: %d: numeric value. */ esc_html__( 'Sent keys removed: %d', 'noesia-commerce-suite' ), absint( $cleanup_summary['sent_keys_removed'] ?? 0 ) ); ?></span>
					<span><?php printf( /* translators: %d: numeric value. */ esc_html__( 'Doctor report removed: %d', 'noesia-commerce-suite' ), absint( $cleanup_summary['doctor_report_removed'] ?? 0 ) ); ?></span>
				</div>
			<?php endif; ?>
			<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
				<?php wp_nonce_field( 'noesia_cs_cleanup_data' ); ?>
				<input type="hidden" name="action" value="noesia_cs_cleanup_data">
				<button class="noesia-cs-primary-button" type="submit"><?php esc_html_e( 'Clean Old Noesia Logs', 'noesia-commerce-suite' ); ?></button>
			</form>
		</div>
		<?php $admin->render_settings_binding_panel(); ?>
		<?php $admin->render_service_boundary_panel(); ?>

	</div>
</div>
