<?php

/**
 * Registers the plugin's Settings API options and renders the Settings page's Branding, Presentation, and Advanced tabs.
 *
 * @link       https://nudgesells.com
 * @since      1.0.0
 *
 * @package    NudgeSells
 * @subpackage NudgeSells/admin
 */

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

/**
 * NudgeSells_Admin_Settings
 *
 * Handles registration, sanitization, and rendering of the plugin's global Settings page.
 *
 * @since      1.0.0
 * @package    NudgeSells
 * @subpackage NudgeSells/admin
 * @author     NudgeSells <support@nudgesells.com>
 */
class NudgeSells_Admin_Settings
{

	const OPTION_KEY = 'nudgesells_settings';

	/**
	 * Get.
	 *
	 * @param mixed $key
	 * @param mixed $default
	 *
	 * @return mixed
	 * @since 1.0.0
	 */
	public static function get($key, $default = null)
	{
		$settings = get_option(self::OPTION_KEY, array());
		return array_key_exists($key, $settings) ? $settings[$key] : $default;
	}

	/**
	 * Defaults.
	 *
	 * @return array
	 * @since 1.0.0
	 */
	public static function defaults()
	{
		$defaults = array(
			'master_enable'       => 'yes',
			'default_display'     => 'list',
			'thumbnail_image_size' => 'thumbnail',
			'show_savings_badges' => 'yes',
			'ignore_sale_price_for_discounts' => 'yes',
			'addon_count_display' => 'no_discount',
			'addon_count_label' => 'selected',
			'delete_data_on_uninstall' => 'no',
			'color_accent'        => '#1E5F46',
			'color_accent_dark'   => '#164734',
			'color_success'       => '#2a7d2a',
			'color_background'    => '#fafafa',
			'color_border'        => '#e2e2e2',
			'color_card_background' => '#ffffff',
			'color_text'          => '#000000',
			'color_text_muted'    => '#767676',
			'color_price'         => '#000000',
			'color_discount_progress' => '#C56B25',
			'color_countdown'      => '#D63A18',
			'color_selection'      => '#000000',
			'color_selection_border' => '#000000',
			'border_radius_style' => 'rounded',
			'show_qty_stepper'    => 'yes',
			'show_upgrade_thumbnails' => 'no',
			'cascade_remove_addons' => 'yes',
			'fallback_cross_sells' => 'no',
			'fallback_up_sells'    => 'no',
			'product_title_link_mode' => 'none',
			'quick_view_css_class' => '',
		);

		/**
		 * Filters the default settings values, so Pro can register defaults
		 * for its own fields (e.g. flash sale countdown, progress
		 * indicator style, offer suggestions) without core needing any
		 * knowledge of what those fields are.
		 *
		 * @param array $defaults
		 */
		return apply_filters('nudgesells_settings_defaults', $defaults);
	}

	/**
	 * Color fields.
	 *
	 * @return array
	 * @since 1.0.0
	 */
	public static function color_fields()
	{
		return array(
			'color_accent'          => '--nudgesells-accent',
			'color_accent_dark'     => '--nudgesells-accent-dark',
			'color_success'         => '--nudgesells-success',
			'color_background'      => '--nudgesells-bg',
			'color_border'          => '--nudgesells-border',
			'color_card_background' => '--nudgesells-card-bg',
			'color_text'            => '--nudgesells-text',
			'color_text_muted'      => '--nudgesells-text-muted',
			'color_price'           => '--nudgesells-price',
			'color_discount_progress' => '--nudgesells-discount-progress',
			'color_countdown'      => '--nudgesells-countdown',
			'color_selection'      => '--nudgesells-selection',
			'color_selection_border' => '--nudgesells-selection-border',
		);
	}

	/**
	 * Radius values.
	 *
	 * @param mixed $style
	 *
	 * @return array
	 * @since 1.0.0
	 */
	public static function radius_values($style)
	{
		switch ($style) {
			case 'square':
				return array(
					'--nudgesells-radius'    => '4px',
					'--nudgesells-radius-sm' => '2px',
				);
			case 'pill':
				return array(
					'--nudgesells-radius'    => '24px',
					'--nudgesells-radius-sm' => '999px',
				);
			case 'rounded':
			default:
				return array(
					'--nudgesells-radius'    => '10px',
					'--nudgesells-radius-sm' => '6px',
				);
		}
	}

	/**
	 * Register settings.
	 *
	 * @return void
	 * @since 1.0.0
	 */
	public function register_settings()
	{
		register_setting('nudgesells_settings_group', self::OPTION_KEY, array($this, 'sanitize'));
	}

	/**
	 * Sanitize.
	 *
	 * @param mixed $input
	 *
	 * @return mixed
	 * @since 1.0.0
	 */
	public function sanitize($input)
	{
		$defaults = self::defaults();
		$input    = is_array($input) ? $input : array();
		$is_pro   = NudgeSells_Pro_Loader::is_active();

		$clean = array();
		$clean['master_enable']       = ! empty($input['master_enable']) ? 'yes' : 'no';
		$clean['default_display']     = in_array($input['default_display'] ?? '', array('list', 'card'), true) ? $input['default_display'] : $defaults['default_display'];
		$valid_image_sizes = array_merge(get_intermediate_image_sizes(), array('full'));
		$clean['thumbnail_image_size'] = in_array($input['thumbnail_image_size'] ?? '', $valid_image_sizes, true) ? $input['thumbnail_image_size'] : $defaults['thumbnail_image_size'];
		$clean['show_savings_badges'] = ! empty($input['show_savings_badges']) ? 'yes' : 'no';
		$clean['ignore_sale_price_for_discounts'] = ! empty($input['ignore_sale_price_for_discounts']) ? 'yes' : 'no';
		$clean['addon_count_display'] = in_array($input['addon_count_display'] ?? '', array('hidden', 'no_discount', 'always'), true) ? $input['addon_count_display'] : $defaults['addon_count_display'];
		$clean['addon_count_label'] = isset($input['addon_count_label']) && '' !== trim(wp_unslash($input['addon_count_label'])) ? sanitize_text_field(wp_unslash($input['addon_count_label'])) : $defaults['addon_count_label'];
		$clean['delete_data_on_uninstall'] = ! empty($input['delete_data_on_uninstall']) ? 'yes' : 'no';
		$clean['border_radius_style']      = in_array($input['border_radius_style'] ?? '', array('square', 'rounded', 'pill'), true) ? $input['border_radius_style'] : $defaults['border_radius_style'];
		$clean['show_qty_stepper']         = ! empty($input['show_qty_stepper']) ? 'yes' : 'no';
		$clean['show_upgrade_thumbnails']  = ! empty($input['show_upgrade_thumbnails']) ? 'yes' : 'no';
		$clean['cascade_remove_addons']    = ! empty($input['cascade_remove_addons']) ? 'yes' : 'no';
		$clean['fallback_cross_sells']     = ! empty($input['fallback_cross_sells']) ? 'yes' : 'no';
		$clean['fallback_up_sells']        = ($is_pro && ! empty($input['fallback_up_sells'])) ? 'yes' : 'no';
		$clean['product_title_link_mode']  = in_array($input['product_title_link_mode'] ?? '', array('none', 'new_tab', 'quick_view'), true) ? $input['product_title_link_mode'] : $defaults['product_title_link_mode'];
		$clean['quick_view_css_class']     = isset($input['quick_view_css_class']) ? preg_replace('/[^A-Za-z0-9_\- ]/', '', wp_unslash($input['quick_view_css_class'])) : '';

		foreach (self::color_fields() as $key => $css_var) {
			$value = isset($input[$key]) ? sanitize_hex_color(wp_unslash($input[$key])) : '';
			$clean[$key] = $value ? $value : $defaults[$key];
		}

		/**
		 * Filters the sanitized settings array, so Pro can validate and
		 * add its own fields (which never appear in $input at all unless
		 * Pro is active, since core's form doesn't render them). $is_pro
		 * is passed through so Pro's own callback still enforces the
		 * gate server-side rather than trusting the client.
		 *
		 * @param array $clean
		 * @param array $input
		 * @param array $defaults
		 * @param bool  $is_pro
		 */
		$clean = apply_filters('nudgesells_settings_sanitize', $clean, $input, $defaults, $is_pro);

		return $clean;
	}

	/**
	 * Render.
	 *
	 * @return void
	 * @since 1.0.0
	 */
	public static function render()
	{
		if (! current_user_can(NudgeSells_Admin::CAPABILITY)) {
			return;
		}

		$settings = wp_parse_args(get_option(self::OPTION_KEY, array()), self::defaults());
		$defaults = self::defaults();
		$is_pro   = NudgeSells_Pro_Loader::is_active();
		$locked   = $is_pro ? '' : 'disabled';
		$pro_badge = '<span class="nudgesells-badge" style="background:var(--ns-accent-dark);color:#fff;margin-inline-start:6px;">' . esc_html__('Pro', 'nudgesells') . '</span>';
?>
		<div class="wrap nudgesells-wrap">
			<?php NudgeSells_Admin::render_header(__('Settings', 'nudgesells')); ?>
			<p class="nudgesells-subtitle"><?php esc_html_e('Global defaults that apply across every offer.', 'nudgesells'); ?></p>

			<div class="nudgesells-tabs">
				<button type="button" class="nudgesells-tab-btn active" data-tab="branding"><?php esc_html_e('Branding', 'nudgesells'); ?></button>
				<button type="button" class="nudgesells-tab-btn" data-tab="presentation"><?php esc_html_e('Presentation', 'nudgesells'); ?></button>
				<button type="button" class="nudgesells-tab-btn" data-tab="advanced"><?php esc_html_e('Advanced', 'nudgesells'); ?></button>
			</div>

			<div class="nudgesells-card">
				<form method="post" action="options.php">
					<?php settings_fields('nudgesells_settings_group'); ?>

					<div class="nudgesells-tab-panel" data-tab="branding">
						<p class="description"><?php esc_html_e('Colors and corner rounding used by the inline upsell widget on the storefront. Applies everywhere, across every offer.', 'nudgesells'); ?></p>
						<table class="form-table" role="presentation">
							<tr>
								<th scope="row"><?php esc_html_e('Accent color', 'nudgesells'); ?></th>
								<td>
									<input type="text" class="nudgesells-color-field" name="<?php echo esc_attr(self::OPTION_KEY); ?>[color_accent]" value="<?php echo esc_attr($settings['color_accent']); ?>" data-default-color="<?php echo esc_attr($defaults['color_accent']); ?>" />
									<p class="description"><?php esc_html_e('Selected states, focus rings, and "read more" links.', 'nudgesells'); ?></p>
								</td>
							</tr>
							<tr>
								<th scope="row"><?php esc_html_e('Accent color (hover)', 'nudgesells'); ?></th>
								<td>
									<input type="text" class="nudgesells-color-field" name="<?php echo esc_attr(self::OPTION_KEY); ?>[color_accent_dark]" value="<?php echo esc_attr($settings['color_accent_dark']); ?>" data-default-color="<?php echo esc_attr($defaults['color_accent_dark']); ?>" />
									<p class="description"><?php esc_html_e('Used on hover for accent-colored links.', 'nudgesells'); ?></p>
								</td>
							</tr>
							<tr>
								<th scope="row"><?php esc_html_e('Savings color', 'nudgesells'); ?></th>
								<td>
									<input type="text" class="nudgesells-color-field" name="<?php echo esc_attr(self::OPTION_KEY); ?>[color_success]" value="<?php echo esc_attr($settings['color_success']); ?>" data-default-color="<?php echo esc_attr($defaults['color_success']); ?>" />
									<p class="description"><?php esc_html_e('Discounted prices, savings badges, and the summary total note.', 'nudgesells'); ?></p>
								</td>
							</tr>
							<tr>
								<th scope="row"><?php esc_html_e('Widget background', 'nudgesells'); ?></th>
								<td>
									<input type="text" class="nudgesells-color-field" name="<?php echo esc_attr(self::OPTION_KEY); ?>[color_background]" value="<?php echo esc_attr($settings['color_background']); ?>" data-default-color="<?php echo esc_attr($defaults['color_background']); ?>" />
								</td>
							</tr>
							<tr>
								<th scope="row"><?php esc_html_e('Widget border', 'nudgesells'); ?></th>
								<td>
									<input type="text" class="nudgesells-color-field" name="<?php echo esc_attr(self::OPTION_KEY); ?>[color_border]" value="<?php echo esc_attr($settings['color_border']); ?>" data-default-color="<?php echo esc_attr($defaults['color_border']); ?>" />
								</td>
							</tr>
							<tr>
								<th scope="row"><?php esc_html_e('Card background', 'nudgesells'); ?></th>
								<td>
									<input type="text" class="nudgesells-color-field" name="<?php echo esc_attr(self::OPTION_KEY); ?>[color_card_background]" value="<?php echo esc_attr($settings['color_card_background']); ?>" data-default-color="<?php echo esc_attr($defaults['color_card_background']); ?>" />
									<p class="description"><?php esc_html_e('Background of individual upgrade options and, in the Cards display style, each add-on card.', 'nudgesells'); ?></p>
								</td>
							</tr>
							<tr>
								<th scope="row"><?php esc_html_e('Text color', 'nudgesells'); ?></th>
								<td>
									<input type="text" class="nudgesells-color-field" name="<?php echo esc_attr(self::OPTION_KEY); ?>[color_text]" value="<?php echo esc_attr($settings['color_text']); ?>" data-default-color="<?php echo esc_attr($defaults['color_text']); ?>" />
									<p class="description"><?php esc_html_e('General widget text - titles, labels, and option text.', 'nudgesells'); ?></p>
								</td>
							</tr>
							<tr>
								<th scope="row"><?php esc_html_e('Muted text color', 'nudgesells'); ?></th>
								<td>
									<input type="text" class="nudgesells-color-field" name="<?php echo esc_attr(self::OPTION_KEY); ?>[color_text_muted]" value="<?php echo esc_attr($settings['color_text_muted']); ?>" data-default-color="<?php echo esc_attr($defaults['color_text_muted']); ?>" />
									<p class="description"><?php esc_html_e('Sublines, struck-through original prices, and other secondary text.', 'nudgesells'); ?></p>
								</td>
							</tr>
							<tr>
								<th scope="row"><?php esc_html_e('Price color', 'nudgesells'); ?></th>
								<td>
									<input type="text" class="nudgesells-color-field" name="<?php echo esc_attr(self::OPTION_KEY); ?>[color_price]" value="<?php echo esc_attr($settings['color_price']); ?>" data-default-color="<?php echo esc_attr($defaults['color_price']); ?>" />
									<p class="description"><?php esc_html_e('The host product/upgrade prices and cart summary total. Discounted add-ons and badges use the Savings color instead.', 'nudgesells'); ?></p>
								</td>
							</tr>
							<tr>
								<th scope="row"><?php esc_html_e('Discount progress color', 'nudgesells'); ?></th>
								<td>
									<input type="text" class="nudgesells-color-field" name="<?php echo esc_attr(self::OPTION_KEY); ?>[color_discount_progress]" value="<?php echo esc_attr($settings['color_discount_progress']); ?>" data-default-color="<?php echo esc_attr($defaults['color_discount_progress']); ?>" />
									<p class="description"><?php esc_html_e('Background of the bundle/tier progress message (e.g. "Add 2 more to unlock..."); switches to the Savings color once reached.', 'nudgesells'); ?></p>
								</td>
							</tr>
							<tr>
								<th scope="row"><?php esc_html_e('Flash sale countdown color', 'nudgesells'); ?></th>
								<td>
									<input type="text" class="nudgesells-color-field" name="<?php echo esc_attr(self::OPTION_KEY); ?>[color_countdown]" value="<?php echo esc_attr($settings['color_countdown']); ?>" data-default-color="<?php echo esc_attr($defaults['color_countdown']); ?>" />
									<p class="description"><?php esc_html_e('The countdown pill\'s dot, text, and background, shown next to the widget title.', 'nudgesells'); ?></p>
								</td>
							</tr>
							<tr>
								<th scope="row"><?php esc_html_e('Selection color', 'nudgesells'); ?></th>
								<td>
									<input type="text" class="nudgesells-color-field" name="<?php echo esc_attr(self::OPTION_KEY); ?>[color_selection]" value="<?php echo esc_attr($settings['color_selection']); ?>" data-default-color="<?php echo esc_attr($defaults['color_selection']); ?>" />
									<p class="description"><?php esc_html_e('Used for selected checkboxes/radios, filled progress pills, and the section icon badge background.', 'nudgesells'); ?></p>
								</td>
							</tr>
							<tr>
								<th scope="row"><?php esc_html_e('Selection border color', 'nudgesells'); ?></th>
								<td>
									<input type="text" class="nudgesells-color-field" name="<?php echo esc_attr(self::OPTION_KEY); ?>[color_selection_border]" value="<?php echo esc_attr($settings['color_selection_border']); ?>" data-default-color="<?php echo esc_attr($defaults['color_selection_border']); ?>" />
									<p class="description"><?php esc_html_e('The border color of a selected add-on (list or card style) or a selected upgrade option.', 'nudgesells'); ?></p>
								</td>
							</tr>
							<tr>
								<th scope="row"><?php esc_html_e('Border radius', 'nudgesells'); ?></th>
								<td>
									<?php
									$radius_options = array(
										'square'  => __('Square', 'nudgesells'),
										'rounded' => __('Rounded', 'nudgesells'),
										'pill'    => __('Pill', 'nudgesells'),
									);
									foreach ($radius_options as $value => $label) :
									?>
										<label style="margin-right:16px">
											<input type="radio" name="<?php echo esc_attr(self::OPTION_KEY); ?>[border_radius_style]" value="<?php echo esc_attr($value); ?>" <?php checked($settings['border_radius_style'], $value); ?> />
											<?php echo esc_html($label); ?>
										</label>
									<?php endforeach; ?>
									<p class="description"><?php esc_html_e('Corner rounding for the widget, cards, buttons, and inputs.', 'nudgesells'); ?></p>
								</td>
							</tr>
						</table>
					</div>

					<div class="nudgesells-tab-panel" data-tab="presentation" style="display:none;">
						<table class="form-table" role="presentation">
							<tr>
								<th scope="row"><?php esc_html_e('Default display style', 'nudgesells'); ?></th>
								<td>
									<select name="<?php echo esc_attr(self::OPTION_KEY); ?>[default_display]">
										<option value="list" <?php selected('list', $settings['default_display']); ?>><?php esc_html_e('List (compact rows)', 'nudgesells'); ?></option>
										<option value="card" <?php selected('card', $settings['default_display']); ?>><?php esc_html_e('Cards (grid with larger images)', 'nudgesells'); ?></option>
									</select>
									<p class="description"><?php esc_html_e('Used by any offer set to "Inherit from Settings" for its display style - new offers default to this, and stay in sync with it unless overridden individually.', 'nudgesells'); ?></p>
								</td>
							</tr>
							<tr>
								<th scope="row"><?php esc_html_e('Thumbnail image size', 'nudgesells'); ?></th>
								<td>
									<select name="<?php echo esc_attr(self::OPTION_KEY); ?>[thumbnail_image_size]">
										<?php foreach (array_merge(get_intermediate_image_sizes(), array('full')) as $size) : ?>
											<option value="<?php echo esc_attr($size); ?>" <?php selected($size, $settings['thumbnail_image_size']); ?>><?php echo esc_html($size); ?></option>
										<?php endforeach; ?>
									</select>
									<p class="description"><?php esc_html_e('The registered WordPress image size used for add-on and upgrade product thumbnails in the widget.', 'nudgesells'); ?></p>
								</td>
							</tr>
								<?php
								/**
								 * Fires in the Presentation tab, right after the thumbnail
								 * size field, so Pro can render its Flash Sale Countdown and
								 * Countdown Format fields here as additional <tr> rows.
								 *
								 * @param array  $settings
								 * @param array  $defaults
								 * @param bool   $is_pro
								 * @param string $locked    'disabled' attribute string when Pro isn't active.
								 * @param string $pro_badge Pre-rendered "Pro" badge markup.
								 */
								do_action( 'nudgesells_settings_presentation_fields_top', $settings, $defaults, $is_pro, $locked, $pro_badge );
								?>
							<tr>
								<th scope="row"><?php esc_html_e('Savings badges', 'nudgesells'); ?></th>
								<td>
									<label>
										<input type="checkbox" name="<?php echo esc_attr(self::OPTION_KEY); ?>[show_savings_badges]" value="1" <?php checked('yes', $settings['show_savings_badges']); ?> />
										<?php esc_html_e('Show "Save X%" badges next to discounted add-ons and upgrades', 'nudgesells'); ?>
									</label>
								</td>
							</tr>
							<tr>
								<th scope="row"><?php esc_html_e('Selection count', 'nudgesells'); ?></th>
								<td>
									<label style="display:block;margin-bottom:8px;">
										<input type="radio" name="<?php echo esc_attr(self::OPTION_KEY); ?>[addon_count_display]" value="hidden" <?php checked('hidden', $settings['addon_count_display']); ?> />
										<?php esc_html_e('Hidden', 'nudgesells'); ?>
									</label>
									<label style="display:block;margin-bottom:8px;">
										<input type="radio" name="<?php echo esc_attr(self::OPTION_KEY); ?>[addon_count_display]" value="no_discount" <?php checked('no_discount', $settings['addon_count_display']); ?> />
										<?php esc_html_e('Show when no discounts are configured', 'nudgesells'); ?>
									</label>
									<label style="display:block;">
										<input type="radio" name="<?php echo esc_attr(self::OPTION_KEY); ?>[addon_count_display]" value="always" <?php checked('always', $settings['addon_count_display']); ?> />
										<?php
										echo wp_kses(
											__('Show both (e.g. <code>2 selected - you save $12.00</code>)', 'nudgesells'),
											array('code' => array())
										);
										?>
									</label>
									<p class="description"><?php esc_html_e('With exactly one selected, its title is shown instead of a number.', 'nudgesells'); ?></p>
									<p style="margin-top:10px;">
										<label for="nudgesells-addon-count-label"><?php esc_html_e('Label', 'nudgesells'); ?></label><br />
										<input type="text" id="nudgesells-addon-count-label" name="<?php echo esc_attr(self::OPTION_KEY); ?>[addon_count_label]" value="<?php echo esc_attr($settings['addon_count_label']); ?>" style="width:200px" placeholder="<?php esc_attr_e('e.g. selected, items selected', 'nudgesells'); ?>" />
									</p>
								</td>
							</tr>
							<tr>
								<th scope="row"><?php esc_html_e('Product titles', 'nudgesells'); ?></th>
								<td>
									<label style="display:block;margin-bottom:8px;">
										<input type="radio" name="<?php echo esc_attr(self::OPTION_KEY); ?>[product_title_link_mode]" value="none" <?php checked('none', $settings['product_title_link_mode']); ?> />
										<?php esc_html_e('Not clickable (default)', 'nudgesells'); ?>
									</label>
									<label style="display:block;margin-bottom:8px;">
										<input type="radio" name="<?php echo esc_attr(self::OPTION_KEY); ?>[product_title_link_mode]" value="new_tab" <?php checked('new_tab', $settings['product_title_link_mode']); ?> />
										<?php esc_html_e('Open the product page in a new tab', 'nudgesells'); ?>
									</label>
									<label style="display:block;">
										<input type="radio" id="nudgesells-title-link-quick-view" name="<?php echo esc_attr(self::OPTION_KEY); ?>[product_title_link_mode]" value="quick_view" <?php checked('quick_view', $settings['product_title_link_mode']); ?> />
										<?php esc_html_e('Quick View popup', 'nudgesells'); ?>
									</label>
									<div id="nudgesells-quick-view-class-wrap" style="margin:10px 0 0 24px;<?php echo 'quick_view' === $settings['product_title_link_mode'] ? '' : 'display:none;'; ?>">
										<label for="nudgesells-quick-view-css-class"><?php esc_html_e('Custom CSS class for the link', 'nudgesells'); ?></label><br />
										<input type="text" id="nudgesells-quick-view-css-class" name="<?php echo esc_attr(self::OPTION_KEY); ?>[quick_view_css_class]" value="<?php echo esc_attr($settings['quick_view_css_class']); ?>" placeholder="<?php esc_attr_e('e.g. open-quick-view', 'nudgesells'); ?>" style="width:280px" />
										<p class="description"><?php echo wp_kses(__('The CSS class your Quick View plugin binds to (also settable via the <code>nudgesells_quick_view_css_class</code> filter).', 'nudgesells'), array('code' => array())); ?></p>
									</div>
									<p class="description"><?php esc_html_e('Applies to add-on/upgrade product names. Falls back to opening in a new tab if nothing intercepts the click.', 'nudgesells'); ?></p>
								</td>
							</tr>
							<tr>
								<th scope="row"><?php esc_html_e('Quantity stepper', 'nudgesells'); ?></th>
								<td>
									<label>
										<input type="checkbox" name="<?php echo esc_attr(self::OPTION_KEY); ?>[show_qty_stepper]" value="1" <?php checked('yes', $settings['show_qty_stepper']); ?> />
										<?php esc_html_e('Show +/- buttons next to quantity-mode add-on inputs', 'nudgesells'); ?>
									</label>
									<p class="description"><?php esc_html_e('Only affects add-ons configured with "Quantity input" selection mode.', 'nudgesells'); ?></p>
								</td>
							</tr>
							<tr>
								<th scope="row"><?php esc_html_e('Upgrade thumbnails', 'nudgesells'); ?></th>
								<td>
									<label>
										<input type="checkbox" name="<?php echo esc_attr(self::OPTION_KEY); ?>[show_upgrade_thumbnails]" value="1" <?php checked('yes', $settings['show_upgrade_thumbnails']); ?> />
										<?php esc_html_e('Show a product thumbnail next to each upgrade option, same as add-ons', 'nudgesells'); ?>
									</label>
									<p class="description"><?php esc_html_e('For variable upgrades, updates to the picked variation\'s image, or the parent product\'s image otherwise.', 'nudgesells'); ?></p>
								</td>
							</tr>
								<?php
								/**
								 * Fires at the end of the Presentation tab - Pro renders
								 * its Bundle/Tier Progress Indicator field here.
								 *
								 * @param array  $settings
								 * @param array  $defaults
								 * @param bool   $is_pro
								 * @param string $locked    'disabled' attribute string when Pro isn't active.
								 * @param string $pro_badge Pre-rendered "Pro" badge markup.
								 */
								do_action( 'nudgesells_settings_presentation_fields_bottom', $settings, $defaults, $is_pro, $locked, $pro_badge );
								?>
						</table>
					</div>

					<div class="nudgesells-tab-panel" data-tab="advanced" style="display:none;">
						<table class="form-table" role="presentation">
							<tr>
								<th scope="row"><?php esc_html_e('Inline upsells', 'nudgesells'); ?></th>
								<td>
									<label>
										<input type="checkbox" name="<?php echo esc_attr(self::OPTION_KEY); ?>[master_enable]" value="1" <?php checked('yes', $settings['master_enable']); ?> />
										<?php esc_html_e('Enable inline upsells storefront-wide', 'nudgesells'); ?>
									</label>
									<p class="description"><?php esc_html_e('Turning this off hides every offer immediately without touching individual offer settings - a quick kill switch.', 'nudgesells'); ?></p>
								</td>
							</tr>
							<tr>
								<th scope="row"><?php esc_html_e('Removing the main product', 'nudgesells'); ?></th>
								<td>
									<label>
										<input type="checkbox" name="<?php echo esc_attr(self::OPTION_KEY); ?>[cascade_remove_addons]" value="1" <?php checked('yes', $settings['cascade_remove_addons']); ?> />
										<?php esc_html_e('Remove its add-ons/upgrade from the cart too', 'nudgesells'); ?>
									</label>
									<p class="description"><?php esc_html_e('When checked (default), removing the main product removes its add-ons/upgrade too. When unchecked, they stay in the cart at full (non-offer) price.', 'nudgesells'); ?></p>
								</td>
							</tr>
							<tr>
								<th scope="row"><?php esc_html_e('Sale prices', 'nudgesells'); ?></th>
								<td>
									<label>
										<input type="checkbox" name="<?php echo esc_attr(self::OPTION_KEY); ?>[ignore_sale_price_for_discounts]" value="1" <?php checked('yes', $settings['ignore_sale_price_for_discounts']); ?> />
										<?php esc_html_e('Ignore a product\'s own WooCommerce sale price when it has an item-level discount configured as an add-on or upgrade', 'nudgesells'); ?>
									</label>
									<p class="description"><?php esc_html_e('Checked (default): the discount is based on the regular price, so it doesn\'t stack with an active sale price. Unchecked: the two discounts compound. Bundle/tier discounts are unaffected either way.', 'nudgesells'); ?></p>
								</td>
							</tr>
							<tr>
								<th scope="row"><?php esc_html_e('Fallback offers', 'nudgesells'); ?></th>
								<td>
									<label style="display:block;<?php echo $is_pro ? 'margin-bottom:8px;' : ''; ?>">
										<input type="checkbox" name="<?php echo esc_attr(self::OPTION_KEY); ?>[fallback_cross_sells]" value="1" <?php checked('yes', $settings['fallback_cross_sells']); ?> />
										<?php esc_html_e("Use the product's saved Cross-sells as add-ons when a offer has none configured", 'nudgesells'); ?>
									</label>
									<?php if ($is_pro) : ?>
									<label style="display:block;">
										<input type="checkbox" name="<?php echo esc_attr(self::OPTION_KEY); ?>[fallback_up_sells]" value="1" <?php checked('yes', $settings['fallback_up_sells']); ?> />
										<?php esc_html_e("Use the product's saved Up-sells as upgrade offers when a offer has none configured", 'nudgesells'); ?>
									</label>
									<?php endif; ?>
									<p class="description"><?php echo $is_pro ? esc_html__('Only for a matching offer with an empty Add-ons or Upgrades list; fallback items show at full price, with no offer-specific discount.', 'nudgesells') : esc_html__('Only for a matching offer with an empty Add-ons list; fallback items show at full price, with no offer-specific discount.', 'nudgesells'); ?></p>
								</td>
							</tr>
								<?php
								/**
								 * Fires in the Advanced tab, right after Fallback offers --
								 * Pro renders its Offer Suggestions threshold and Order
								 * Statuses fields here as additional <tr> rows.
								 *
								 * @param array  $settings
								 * @param array  $defaults
								 * @param bool   $is_pro
								 * @param string $locked    'disabled' attribute string when Pro isn't active.
								 * @param string $pro_badge Pre-rendered "Pro" badge markup.
								 */
								do_action( 'nudgesells_settings_advanced_fields', $settings, $defaults, $is_pro, $locked, $pro_badge );
								?>
							<tr>
								<th scope="row"><?php esc_html_e('Uninstall', 'nudgesells'); ?></th>
								<td>
									<label>
										<input type="checkbox" name="<?php echo esc_attr(self::OPTION_KEY); ?>[delete_data_on_uninstall]" value="1" <?php checked('yes', $settings['delete_data_on_uninstall']); ?> />
										<?php esc_html_e('Delete all offers and settings when the plugin is deleted', 'nudgesells'); ?>
									</label>
									<p class="description"><?php esc_html_e('Leave unchecked to keep your offers if you reinstall later.', 'nudgesells'); ?></p>
								</td>
							</tr>
						</table>
					</div>

					<?php submit_button(); ?>
				</form>
			</div>
		</div>
<?php
	}
}
