<?php
/**
 * Radio Button Input View for Pixelio Cart Abandon Recovery More plugin.
 *
 * This file defines a reusable class for rendering radio button inputs
 * in the admin pages of the Pixelio Cart Abandon Recovery More plugin.
 *
 * PHP version 8.0+
 *
 * @package PixelioCartAbandonRecoveryMore
 */

namespace PixelioCartAbandonRecoveryMore\Admin\Views\Reusable;

use PixelioCartAbandonRecoveryMore\Admin\Wrapper\Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper;
use PixelioCartAbandonRecoveryMore\Includes\Utils\Pixelio_Cart_Abandon_Recovery_More_Util;

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

/**
 * Class Pixelio_Cart_Abandon_Recovery_More_Radio_Button
 *
 * Reusable class for displaying a radio button input in admin pages.
 *
 * @package PixelioCartAbandonRecoveryMore\Admin\Views\Reusable
 */
class Pixelio_Cart_Abandon_Recovery_More_Radio_Button extends Pixelio_Cart_Abandon_Recovery_More_All_Input {

	/**
	 * Array of radio button values.
	 *
	 * @var array
	 */
	private $values;

	/**
	 * Counter for generating unique IDs (if needed).
	 *
	 * @var int
	 */
	private $i = 0;

	private Pixelio_Cart_Abandon_Recovery_More_Util $pixelio_util;

	/**
	 * Constructor.
	 *
	 * Initializes the radio button with label, description, name, pro flag, values, and dependency.
	 *
	 * @param string $label      The label of the input.
	 * @param string $description The description of the input.
	 * @param string $name       The option name in WP options table.
	 * @param bool   $pro        Whether this input is for a pro feature. Default false.
	 * @param array  $values     Array of values for the radio buttons. Default empty array.
	 * @param string $dependency Optional dependency field name. Default empty string.
	 */
	public function __construct( $label, $description, $name, $pro = false, $values = array(), $dependency = '' ) {
        $this->pixelio_util=new Pixelio_Cart_Abandon_Recovery_More_Util(new Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper());
		[$this->name, $this->label, $this->pixelio_cart_abandon_recovery_more_saved_value, $this->error] = $this->pixelio_util->pixelio_cart_abandon_recovery_more_check_nonce( $name, $label );
		$this->description     = $description;
		$this->pro             = false;
		$this->dependency_name = $dependency;
		$this->values          = $values;
		$this->pixelio_cart_abandon_recovery_more_saved_value = $this->pixelio_util->pixelio_cart_abandon_recovery_more_dependency_is_checked($this->name);
	}

	/**
	 * Outputs the HTML for the radio button input.
	 *
	 * This method includes the input header and footer templates
	 * and loops through all provided values to render radio buttons.
	 *
	 * @return void
	 */
	public function pixelio_cart_abandon_recovery_more_get_views(): void {

		?>

		<?php
		include plugin_dir_path( __FILE__ ) . Pixelio_Cart_Abandon_Recovery_More_Util::$pixelio_plugin_class_slug . '-input-header.php';
		?>


		<div class="pixelio-cart-abandon-recovery-more-opt-in-radio-button-container">
			<?php foreach ( $this->values as $value ) : ?>
				<label class="pixelio-cart-abandon-recovery-more-radio-button-label">
					<input type="radio" name="<?php echo esc_attr( $this->name ); ?>"
							value="<?php echo esc_attr( $value ); ?>"
						<?php
						if ( $value === get_option( $this->name ) ) {
							esc_attr_e( 'checked', 'rivio-ai-cart-recovery-marketing-email-automation' );
						}
						?>
					>
					<?php echo esc_attr( $value ); ?>
					<span class="pixelio-cart-abandon-recovery-more-radio-span"></span>
				</label>
				<?php
				++$this->i;
			endforeach;
			?>
		</div>
		<p class="<?php echo ( '' !== $this->error ) ? esc_attr('pixelio-cart-abandon-recovery-more-error') : esc_attr('pixelio-cart-abandon-recovery-more-hide-it'); ?>"><?php esc_html_e( "An error occurred, be sure, it's correctly filled", 'rivio-ai-cart-recovery-marketing-email-automation' ); ?></p>
		<?php include plugin_dir_path( __FILE__ ) . Pixelio_Cart_Abandon_Recovery_More_Util::$pixelio_plugin_class_slug . '-input-footer.php'; ?>
		<?php
	}
}

