<?php

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

$g = static function ($k) use ($module) { return $module->get($k); };
$cb = static function ($k) use ($module) { return $module->get($k) === 'yes'; };

$position_options = array(
    'bottom' => __('Bottom of viewport', 'ai-eshop-optimizer'),
    'top'    => __('Top of viewport',    'ai-eshop-optimizer'),
);
$visibility_options = array(
    'all'     => __('Desktop + mobile', 'ai-eshop-optimizer'),
    'desktop' => __('Desktop only',     'ai-eshop-optimizer'),
    'mobile'  => __('Mobile only',      'ai-eshop-optimizer'),
);
$layout_options = array(
    'inline'  => __('Inline (single row)', 'ai-eshop-optimizer'),
    'stacked' => __('Stacked (two rows)',  'ai-eshop-optimizer'),
);
?>
<div class="aieo-dmm-section">
    <h2><?php esc_html_e('Sticky Add to Cart', 'ai-eshop-optimizer'); ?></h2>
    <p class="description">
        <?php esc_html_e('A sticky bar that pins to the top or bottom of the viewport on product pages so the add-to-cart button is always reachable. For variable products with many variations it shows a swatch row inline (with a “+N more” indicator), so most shoppers can buy straight from the bar without scrolling back to the product gallery.', 'ai-eshop-optimizer'); ?>
    </p>

    <div class="aieo-dmm-callout">
        <strong><?php esc_html_e('Important', 'ai-eshop-optimizer'); ?>:</strong>
        <?php esc_html_e('If your theme (Merchant, Botiga Pro, etc.) ships its own sticky add-to-cart, leave “Override theme/Merchant sticky bar” enabled below to suppress the duplicate. Disable om-free-shipping’s sticky bar once this one is live.', 'ai-eshop-optimizer'); ?>
    </div>

    <form id="aieo-dmm-sticky-atc-form" class="aieo-dmm-form">
        <table class="form-table">
            <tr>
                <th><?php esc_html_e('Enabled', 'ai-eshop-optimizer'); ?></th>
                <td>
                    <label class="aieo-dmm-choice">
                        <input type="checkbox" name="satc_enabled" value="1" <?php checked($cb('enabled')); ?>>
                        <?php esc_html_e('Render the sticky add-to-cart bar on the frontend', 'ai-eshop-optimizer'); ?>
                    </label>
                </td>
            </tr>

            <tr>
                <th><?php esc_html_e('Position', 'ai-eshop-optimizer'); ?></th>
                <td>
                    <select name="satc_position">
                        <?php foreach ($position_options as $val => $lbl) : ?>
                            <option value="<?php echo esc_attr($val); ?>" <?php selected($g('position'), $val); ?>><?php echo esc_html($lbl); ?></option>
                        <?php endforeach; ?>
                    </select>
                </td>
            </tr>
            <tr>
                <th><?php esc_html_e('Show on', 'ai-eshop-optimizer'); ?></th>
                <td>
                    <select name="satc_visibility">
                        <?php foreach ($visibility_options as $val => $lbl) : ?>
                            <option value="<?php echo esc_attr($val); ?>" <?php selected($g('visibility'), $val); ?>><?php echo esc_html($lbl); ?></option>
                        <?php endforeach; ?>
                    </select>
                </td>
            </tr>
            <tr>
                <th><?php esc_html_e('Reveal after scrolling (px)', 'ai-eshop-optimizer'); ?></th>
                <td>
                    <input type="number" name="satc_display_after_scroll" min="0" value="<?php echo esc_attr((int) $g('display_after_scroll')); ?>">
                    <p class="description"><?php esc_html_e('How far down the page the shopper has to scroll before the bar appears. Default: 100px.', 'ai-eshop-optimizer'); ?></p>
                </td>
            </tr>
            <tr>
                <th><?php esc_html_e('Hide on scroll up', 'ai-eshop-optimizer'); ?></th>
                <td>
                    <label class="aieo-dmm-choice">
                        <input type="checkbox" name="satc_scroll_hide" value="1" <?php checked($cb('scroll_hide')); ?>>
                        <?php esc_html_e('Auto-hide the bar when the shopper scrolls up the page', 'ai-eshop-optimizer'); ?>
                    </label>
                </td>
            </tr>

            <tr>
                <th colspan="2"><h3 style="margin:18px 0 0;"><?php esc_html_e('Desktop layout', 'ai-eshop-optimizer'); ?></h3></th>
            </tr>
            <tr>
                <th><?php esc_html_e('Show on desktop', 'ai-eshop-optimizer'); ?></th>
                <td>
                    <label class="aieo-dmm-choice"><input type="checkbox" name="satc_desktop_show_image"      value="1" <?php checked($cb('desktop_show_image')); ?>>      <?php esc_html_e('Image',              'ai-eshop-optimizer'); ?></label><br>
                    <label class="aieo-dmm-choice"><input type="checkbox" name="satc_desktop_show_title"      value="1" <?php checked($cb('desktop_show_title')); ?>>      <?php esc_html_e('Product title',      'ai-eshop-optimizer'); ?></label><br>
                    <label class="aieo-dmm-choice"><input type="checkbox" name="satc_desktop_show_price"      value="1" <?php checked($cb('desktop_show_price')); ?>>      <?php esc_html_e('Price',              'ai-eshop-optimizer'); ?></label><br>
                    <label class="aieo-dmm-choice"><input type="checkbox" name="satc_desktop_show_quantity"   value="1" <?php checked($cb('desktop_show_quantity')); ?>>   <?php esc_html_e('Quantity input',     'ai-eshop-optimizer'); ?></label><br>
                    <label class="aieo-dmm-choice"><input type="checkbox" name="satc_desktop_show_variations" value="1" <?php checked($cb('desktop_show_variations')); ?>> <?php esc_html_e('Variation swatches', 'ai-eshop-optimizer'); ?></label>
                </td>
            </tr>
            <tr>
                <th><?php esc_html_e('Desktop layout style', 'ai-eshop-optimizer'); ?></th>
                <td>
                    <select name="satc_desktop_layout">
                        <?php foreach ($layout_options as $val => $lbl) : ?>
                            <option value="<?php echo esc_attr($val); ?>" <?php selected($g('desktop_layout'), $val); ?>><?php echo esc_html($lbl); ?></option>
                        <?php endforeach; ?>
                    </select>
                </td>
            </tr>

            <tr>
                <th colspan="2"><h3 style="margin:18px 0 0;"><?php esc_html_e('Mobile layout', 'ai-eshop-optimizer'); ?></h3></th>
            </tr>
            <tr>
                <th><?php esc_html_e('Show on mobile', 'ai-eshop-optimizer'); ?></th>
                <td>
                    <label class="aieo-dmm-choice"><input type="checkbox" name="satc_mobile_show_image"      value="1" <?php checked($cb('mobile_show_image')); ?>>      <?php esc_html_e('Image',              'ai-eshop-optimizer'); ?></label><br>
                    <label class="aieo-dmm-choice"><input type="checkbox" name="satc_mobile_show_title"      value="1" <?php checked($cb('mobile_show_title')); ?>>      <?php esc_html_e('Product title',      'ai-eshop-optimizer'); ?></label><br>
                    <label class="aieo-dmm-choice"><input type="checkbox" name="satc_mobile_show_price"      value="1" <?php checked($cb('mobile_show_price')); ?>>      <?php esc_html_e('Price',              'ai-eshop-optimizer'); ?></label><br>
                    <label class="aieo-dmm-choice"><input type="checkbox" name="satc_mobile_show_quantity"   value="1" <?php checked($cb('mobile_show_quantity')); ?>>   <?php esc_html_e('Quantity input',     'ai-eshop-optimizer'); ?></label><br>
                    <label class="aieo-dmm-choice"><input type="checkbox" name="satc_mobile_show_variations" value="1" <?php checked($cb('mobile_show_variations')); ?>> <?php esc_html_e('Variation swatches', 'ai-eshop-optimizer'); ?></label>
                </td>
            </tr>
            <tr>
                <th><?php esc_html_e('Mobile layout style', 'ai-eshop-optimizer'); ?></th>
                <td>
                    <select name="satc_mobile_layout">
                        <?php foreach ($layout_options as $val => $lbl) : ?>
                            <option value="<?php echo esc_attr($val); ?>" <?php selected($g('mobile_layout'), $val); ?>><?php echo esc_html($lbl); ?></option>
                        <?php endforeach; ?>
                    </select>
                </td>
            </tr>

            <tr>
                <th colspan="2"><h3 style="margin:18px 0 0;"><?php esc_html_e('Variation swatches', 'ai-eshop-optimizer'); ?></h3></th>
            </tr>
            <tr>
                <th><?php esc_html_e('Smart variation handling', 'ai-eshop-optimizer'); ?></th>
                <td>
                    <label class="aieo-dmm-choice">
                        <input type="checkbox" name="satc_smart_variation_enabled" value="1" <?php checked($cb('smart_variation_enabled')); ?>>
                        <?php esc_html_e('Hide the bar until a variation is selected when a product has more than the threshold below', 'ai-eshop-optimizer'); ?>
                    </label>
                    <p class="description"><?php esc_html_e('Avoids confusing shoppers on products with many variations: the inline swatches are still shown, but the “Add to cart” button only enables once a variation is picked.', 'ai-eshop-optimizer'); ?></p>
                </td>
            </tr>
            <tr>
                <th><?php esc_html_e('Variation threshold', 'ai-eshop-optimizer'); ?></th>
                <td>
                    <input type="number" name="satc_variation_threshold" min="1" value="<?php echo esc_attr((int) $g('variation_threshold')); ?>">
                    <p class="description"><?php esc_html_e('Products with more variations than this are treated as "many-vars". Choose what to render for them below.', 'ai-eshop-optimizer'); ?></p>
                </td>
            </tr>
            <tr>
                <th><?php esc_html_e('Many-vars products show', 'ai-eshop-optimizer'); ?></th>
                <td>
                    <?php $mvm = (string) $g('many_vars_mode'); ?>
                    <select name="satc_many_vars_mode">
                        <option value="dropdown" <?php selected($mvm, 'dropdown'); ?>><?php esc_html_e('Dropdown with proper variation names (recommended)', 'ai-eshop-optimizer'); ?></option>
                        <option value="hide"     <?php selected($mvm, 'hide'); ?>><?php esc_html_e('Hide the sticky bar entirely on these products', 'ai-eshop-optimizer'); ?></option>
                        <option value="swatches" <?php selected($mvm, 'swatches'); ?>><?php esc_html_e('Inline swatches with “+N more” indicator (legacy)', 'ai-eshop-optimizer'); ?></option>
                    </select>
                </td>
            </tr>
            <tr>
                <th><?php esc_html_e('Variation dropdown placeholder', 'ai-eshop-optimizer'); ?></th>
                <td>
                    <input type="text" name="satc_dropdown_placeholder" class="regular-text"
                           value="<?php echo esc_attr((string) $g('dropdown_placeholder')); ?>"
                           placeholder="<?php esc_attr_e('Choose a variation…', 'ai-eshop-optimizer'); ?>">
                    <p class="description"><?php esc_html_e('Empty = use the translated default.', 'ai-eshop-optimizer'); ?></p>
                </td>
            </tr>
            <tr>
                <th><?php esc_html_e('Swatch limit (desktop)', 'ai-eshop-optimizer'); ?></th>
                <td><input type="number" name="satc_swatch_limit_desktop" min="1" value="<?php echo esc_attr((int) $g('swatch_limit_desktop')); ?>"></td>
            </tr>
            <tr>
                <th><?php esc_html_e('Swatch limit (mobile)', 'ai-eshop-optimizer'); ?></th>
                <td><input type="number" name="satc_swatch_limit_mobile" min="1" value="<?php echo esc_attr((int) $g('swatch_limit_mobile')); ?>"></td>
            </tr>
            <tr>
                <th><?php esc_html_e('Show selected attributes', 'ai-eshop-optimizer'); ?></th>
                <td>
                    <label class="aieo-dmm-choice">
                        <input type="checkbox" name="satc_show_selected_attrs" value="1" <?php checked($cb('show_selected_attrs')); ?>>
                        <?php esc_html_e('After a variation is picked, show the chosen attribute(s) inside the bar (e.g. “Red / 30ml”)', 'ai-eshop-optimizer'); ?>
                    </label>
                </td>
            </tr>

            <tr>
                <th colspan="2"><h3 style="margin:18px 0 0;"><?php esc_html_e('Persistent mode', 'ai-eshop-optimizer'); ?></h3></th>
            </tr>
            <tr>
                <th><?php esc_html_e('Enable persistent mode', 'ai-eshop-optimizer'); ?></th>
                <td>
                    <label class="aieo-dmm-choice">
                        <input type="checkbox" name="satc_persistent_enabled" value="1" <?php checked($cb('persistent_enabled')); ?>>
                        <?php esc_html_e('Keep the bar visible across pages until the shopper either buys the product or dismisses it', 'ai-eshop-optimizer'); ?>
                    </label>
                    <p class="description"><?php esc_html_e('When on, the last-viewed product is remembered (cookie) so the bar follows the shopper around the site. Dismiss button + auto-expiry below.', 'ai-eshop-optimizer'); ?></p>
                </td>
            </tr>
            <tr>
                <th><?php esc_html_e('Dismiss button label', 'ai-eshop-optimizer'); ?></th>
                <td><input type="text" name="satc_persistent_dismiss_text" class="regular-text" value="<?php echo esc_attr((string) $g('persistent_dismiss_text')); ?>" placeholder="Dismiss"></td>
            </tr>
            <tr>
                <th><?php esc_html_e('Persistent expiry (hours)', 'ai-eshop-optimizer'); ?></th>
                <td>
                    <input type="number" name="satc_persistent_expire_hours" min="1" value="<?php echo esc_attr((int) $g('persistent_expire_hours')); ?>">
                    <p class="description"><?php esc_html_e('How long the cookie remembers the last-viewed product before the bar stops following the shopper. Default: 24 hours.', 'ai-eshop-optimizer'); ?></p>
                </td>
            </tr>

            <tr>
                <th colspan="2"><h3 style="margin:18px 0 0;"><?php esc_html_e('Appearance', 'ai-eshop-optimizer'); ?></h3></th>
            </tr>
            <tr>
                <th><?php esc_html_e('Use theme button styles', 'ai-eshop-optimizer'); ?></th>
                <td>
                    <label class="aieo-dmm-choice">
                        <input type="checkbox" name="satc_use_theme_button" value="1" <?php checked($cb('use_theme_button')); ?>>
                        <?php esc_html_e('Inherit the add-to-cart button look from the active theme (recommended)', 'ai-eshop-optimizer'); ?>
                    </label>
                    <p class="description"><?php esc_html_e('When off, the custom button colors below are used.', 'ai-eshop-optimizer'); ?></p>
                </td>
            </tr>
            <tr>
                <th><?php esc_html_e('Add-to-cart button text', 'ai-eshop-optimizer'); ?></th>
                <td>
                    <input type="text" name="satc_button_text" class="regular-text" value="<?php echo esc_attr((string) $g('button_text')); ?>" placeholder="<?php esc_attr_e('Add to Cart', 'ai-eshop-optimizer'); ?>">
                    <p class="description"><?php esc_html_e('Leave empty to use WooCommerce’s default per-product label.', 'ai-eshop-optimizer'); ?></p>
                </td>
            </tr>
            <tr>
                <th><?php esc_html_e('Bar background', 'ai-eshop-optimizer'); ?></th>
                <td><input type="color" name="satc_background_color" value="<?php echo esc_attr((string) $g('background_color')); ?>"></td>
            </tr>
            <tr>
                <th><?php esc_html_e('Bar border', 'ai-eshop-optimizer'); ?></th>
                <td>
                    <input type="number" name="satc_border_width" min="0" value="<?php echo esc_attr((int) $g('border_width')); ?>" style="width:80px;">
                    <input type="text"   class="aieo-dmm-color" name="satc_border_color" value="<?php echo esc_attr((string) $g('border_color')); ?>">
                </td>
            </tr>
            <tr>
                <th><?php esc_html_e('Text colors', 'ai-eshop-optimizer'); ?></th>
                <td>
                    <label><?php esc_html_e('Text', 'ai-eshop-optimizer'); ?>  <input type="color" name="satc_text_color"  value="<?php echo esc_attr((string) $g('text_color')); ?>"></label>
                    <label><?php esc_html_e('Title','ai-eshop-optimizer'); ?>  <input type="color" name="satc_title_color" value="<?php echo esc_attr((string) $g('title_color')); ?>"></label>
                    <label><?php esc_html_e('Price','ai-eshop-optimizer'); ?>  <input type="color" name="satc_price_color" value="<?php echo esc_attr((string) $g('price_color')); ?>"></label>
                </td>
            </tr>
            <tr>
                <th><?php esc_html_e('Custom button colors', 'ai-eshop-optimizer'); ?></th>
                <td>
                    <p class="description" style="margin:0 0 6px;"><?php esc_html_e('Used only when "Use theme button styles" is off. Leave a colour empty (clear it) to inherit the master Button Styles (Tools → Button Styles); set one to override the master for the sticky button.', 'ai-eshop-optimizer'); ?></p>
                    <label style="display:block;margin:3px 0;"><?php esc_html_e('Background',       'ai-eshop-optimizer'); ?> <input type="text" class="aieo-dmm-satc-color" data-default-color="#212121" name="satc_button_bg_color"         value="<?php echo esc_attr((string) $g('button_bg_color')); ?>"></label>
                    <label style="display:block;margin:3px 0;"><?php esc_html_e('Background (hover)','ai-eshop-optimizer'); ?> <input type="text" class="aieo-dmm-satc-color" data-default-color="#404040" name="satc_button_bg_color_hover"  value="<?php echo esc_attr((string) $g('button_bg_color_hover')); ?>"></label>
                    <label style="display:block;margin:3px 0;"><?php esc_html_e('Text',             'ai-eshop-optimizer'); ?> <input type="text" class="aieo-dmm-satc-color" data-default-color="#ffffff" name="satc_button_text_color"        value="<?php echo esc_attr((string) $g('button_text_color')); ?>"></label>
                    <label style="display:block;margin:3px 0;"><?php esc_html_e('Text (hover)',     'ai-eshop-optimizer'); ?> <input type="text" class="aieo-dmm-satc-color" data-default-color="#ffffff" name="satc_button_text_color_hover" value="<?php echo esc_attr((string) $g('button_text_color_hover')); ?>"></label>
                    <label><?php esc_html_e('Radius (px)',      'ai-eshop-optimizer'); ?> <input type="number" name="satc_button_border_radius" min="0" value="<?php echo esc_attr((int) $g('button_border_radius')); ?>" style="width:80px;"></label>
                </td>
            </tr>
            <tr>
                <th><?php esc_html_e('Spacing', 'ai-eshop-optimizer'); ?></th>
                <td>
                    <label><?php esc_html_e('Bar padding (px)', 'ai-eshop-optimizer'); ?>      <input type="number" name="satc_bar_padding"     min="0" value="<?php echo esc_attr((int) $g('bar_padding')); ?>"     style="width:80px;"></label>
                    <label><?php esc_html_e('Element gap (px)', 'ai-eshop-optimizer'); ?>      <input type="number" name="satc_elements_spacing" min="0" value="<?php echo esc_attr((int) $g('elements_spacing')); ?>" style="width:80px;"></label>
                    <label><?php esc_html_e('Mobile padding (px)','ai-eshop-optimizer'); ?>    <input type="number" name="satc_mobile_bar_padding" min="0" value="<?php echo esc_attr((int) $g('mobile_bar_padding')); ?>" style="width:80px;"></label>
                    <label><?php esc_html_e('Mobile font size (px)','ai-eshop-optimizer'); ?>  <input type="number" name="satc_mobile_font_size"   min="0" value="<?php echo esc_attr((int) $g('mobile_font_size')); ?>"   style="width:80px;"></label>
                </td>
            </tr>

            <tr>
                <th colspan="2"><h3 style="margin:18px 0 0;"><?php esc_html_e('Compatibility', 'ai-eshop-optimizer'); ?></h3></th>
            </tr>
            <tr>
                <th><?php esc_html_e('Override theme/Merchant sticky bar', 'ai-eshop-optimizer'); ?></th>
                <td>
                    <label class="aieo-dmm-choice">
                        <input type="checkbox" name="satc_override_merchant" value="1" <?php checked($cb('override_merchant')); ?>>
                        <?php esc_html_e('Suppress Merchant Pro / Merchant sticky add-to-cart so this one renders alone', 'ai-eshop-optimizer'); ?>
                    </label>
                </td>
            </tr>
            <tr>
                <th><?php esc_html_e('Allow third-party hooks', 'ai-eshop-optimizer'); ?></th>
                <td>
                    <label class="aieo-dmm-choice">
                        <input type="checkbox" name="satc_allow_third_party" value="1" <?php checked($cb('allow_third_party')); ?>>
                        <?php esc_html_e('Run other plugins’ "woocommerce_*" cart/variation hooks inside the sticky bar', 'ai-eshop-optimizer'); ?>
                    </label>
                    <p class="description"><?php esc_html_e('Off by default to avoid double-firing add-to-cart redirects from analytics plugins.', 'ai-eshop-optimizer'); ?></p>
                </td>
            </tr>
        </table>

        <p class="submit">
            <button type="submit" class="button button-primary" data-action="aieo_dmm_sticky_atc_save_settings">
                <?php esc_html_e('Save sticky bar settings', 'ai-eshop-optimizer'); ?>
            </button>
        </p>
    </form>
</div>

<div class="aieo-dmm-section">
    <h2><?php esc_html_e('How it works', 'ai-eshop-optimizer'); ?></h2>
    <ul class="aieo-dmm-bullets">
        <li>
            <strong><?php esc_html_e('Loaded only where needed.', 'ai-eshop-optimizer'); ?></strong>
            <?php esc_html_e('CSS + JS enqueue on single-product pages — or every page when persistent mode is on.', 'ai-eshop-optimizer'); ?>
        </li>
        <li>
            <strong><?php esc_html_e('Inline variation swatches.', 'ai-eshop-optimizer'); ?></strong>
            <?php esc_html_e('Color and image swatches are pulled from the OM Product Swatches term meta so they match the rest of the catalogue. The bar shows up to the desktop/mobile limits with a “+N more” pill linking to the full picker.', 'ai-eshop-optimizer'); ?>
        </li>
        <li>
            <strong><?php esc_html_e('Persistent mode.', 'ai-eshop-optimizer'); ?></strong>
            <?php esc_html_e('When the shopper leaves the product page, an AJAX endpoint re-renders the bar contents from the last-viewed product so it follows them around the store until they buy or dismiss.', 'ai-eshop-optimizer'); ?>
        </li>
        <li>
            <strong><?php esc_html_e('Theme override.', 'ai-eshop-optimizer'); ?></strong>
            <?php esc_html_e('Merchant Pro and Merchant’s built-in sticky bar are removed at init when “Override theme/Merchant sticky bar” is on, so you never end up with two bars stacked on each other.', 'ai-eshop-optimizer'); ?>
        </li>
    </ul>
</div>
