<?php
/**
 * Scroll Navigation Tweaks handler (admin section).
 *
 * @package FoodOrd
 */

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

/**
 * Handles the Scroll Navigation toggle in Tweaks settings.
 */
class Foodord_Scroll_Navigation {
	/**
	 * Process form submissions for Scroll Navigation.
	 *
	 * @return bool Whether the option was updated.
	 */
	public function handle_form_submission() {
		if ( ! isset( $_POST['foodord_tweaks_nonce'] ) ) {
			return false;
		}

		if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['foodord_tweaks_nonce'] ) ), 'foodord_tweaks_save' ) ) {
			return false;
		}

		if ( ! isset( $_POST['foodord_tweaks_submit'] ) ) {
			return false;
		}

		if ( ! current_user_can( 'manage_options' ) ) {
			return false;
		}

		$enabled   = isset( $_POST['foodord_enable_scroll_nav'] ) ? 'yes' : 'no';
		$old_value = get_option( 'foodord_enable_scroll_nav', 'yes' );
		if ( $old_value !== $enabled ) {
			update_option( 'foodord_enable_scroll_nav', $enabled );
			return true;
		}

		return false;
	}

	/**
	 * Render Scroll Navigation section card.
	 */
	public function render_section() {
		$enabled = get_option( 'foodord_enable_scroll_nav', 'yes' );
		$checked = ( 'yes' === $enabled ) ? 'checked' : '';
		?>
		<div class="wrc-scroll-navigation-container">
			<div class="wrc-scroll-navigation-header">
				<h2 class="wrc-section-title"><?php echo esc_html__( 'Scroll Navigation', 'foodord' ); ?></h2>
				<p class="wrc-card-sub"><?php echo esc_html__( 'Enable or disable product categories scroll navigation under the banner.', 'foodord' ); ?></p>
			</div>
			
			<div class="wrc-scroll-navigation-content">
				<div class="wrc-form">
					<div class="wrc-field wrc-field--toggle">
					<label class="wrc-label" for="foodord_enable_scroll_nav">
						<?php echo esc_html__( 'Enable scroll navigation', 'foodord' ); ?>
						</label>
						<div class="wrc-control">
							<div class="foodord-toggle-row">
								<label class="foodord-toggle-switch" for="foodord_enable_scroll_nav">
									<input type="checkbox" id="foodord_enable_scroll_nav" name="foodord_enable_scroll_nav" value="yes" <?php echo esc_attr( $checked ); ?> />
									<span class="foodord-toggle-slider"></span>
								</label>
							<span class="foodord-toggle-label"><?php echo esc_html__( 'Display scroll navigation on pages with products list', 'foodord' ); ?></span>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<?php
	}
}
