<?php
/**
 * Product Card Images Tweaks handler.
 *
 * @package FoodOrd
 */

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

/**
 * Handles the Product Card Images settings.
 */
class Foodord_Product_Card_Images {
	/**
	 * Process form submissions for Product Card Images.
	 *
	 * @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;
		}

		$show_images = isset( $_POST['foodord_show_product_card_images'] ) ? 'yes' : 'no';
		$old_value   = get_option( 'foodord_show_product_card_images', 'yes' );
		// Update and return whether value changed.
		if ( $old_value !== $show_images ) {
			update_option( 'foodord_show_product_card_images', $show_images );
			return true;
		}

		return false;
	}

	/**
	 * Render Product Card Images section.
	 */
	public function render_section() {
		$show_images = get_option( 'foodord_show_product_card_images', 'yes' );
		$checked     = ( 'yes' === $show_images ) ? 'checked' : '';
		?>
		<div class="wrc-product-images-container">
			<div class="wrc-product-images-header">
				<h2 class="wrc-section-title"><?php echo esc_html__( 'Product Images', 'foodord' ); ?></h2>
				<p class="wrc-card-sub"><?php echo esc_html__( 'Configure product card display options.', 'foodord' ); ?></p>
			</div>
			
			<div class="wrc-product-images-content">
				<div class="wrc-form">
					<div class="wrc-field">
					<label class="wrc-label" for="foodord_show_product_card_images">
						<?php echo esc_html__( 'Show product images', 'foodord' ); ?>
						</label>
						<div class="wrc-control">
							<div class="foodord-toggle-row">
								<label class="foodord-toggle-switch" for="foodord_show_product_card_images">
									<input type="checkbox" id="foodord_show_product_card_images" name="foodord_show_product_card_images" value="yes" <?php echo esc_attr( $checked ); ?> />
									<span class="foodord-toggle-slider"></span>
								</label>
							<span class="foodord-toggle-label"><?php echo esc_html__( 'Display product images on cards', 'foodord' ); ?></span>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<?php
	}
}
