<?php
/**
 * Quantity Selector Template Partial
 * 
 * Plus/minus buttons for quantity input
 * 
 * @param int $product_id Product ID
 * @param int $quantity Current quantity (default 1)
 * @param int $min Minimum quantity (default 1)
 * @param int $max Maximum quantity (default 999)
 * @param int $step Step value (default 1)
 */

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

$instinctor_active_addons = \Instinctor\Core\Plugin::getActiveAddons();
$instinctor_has_qty_buttons = in_array('quantity_buttons', $instinctor_active_addons) || in_array('all_access', $instinctor_active_addons);

// Fallback to simple input if addon not active
if (!$instinctor_has_qty_buttons):
    ?>
    <input type="number" 
           name="quantity" 
           value="<?php echo esc_attr($quantity ?? 1); ?>" 
           min="<?php echo esc_attr($min ?? 1); ?>" 
           max="<?php echo esc_attr($max ?? 999); ?>"
           step="<?php echo esc_attr($step ?? 1); ?>"
           class="instinctor-quantity-input">
    <?php
else:
    $instinctor_product_id = $product_id ?? 0;
    $instinctor_quantity = $quantity ?? 1;
    $instinctor_min = $min ?? 1;
    $instinctor_max = $max ?? 999;
    $instinctor_step = $step ?? 1;
?>

<div class="instinctor-qty-selector" data-product-id="<?php echo esc_attr($instinctor_product_id); ?>">
    <button type="button" class="instinctor-qty-btn instinctor-qty-minus" aria-label="<?php esc_attr_e('Decrease quantity', 'instinctor'); ?>">-</button>
    <input type="number" 
           name="quantity" 
           value="<?php echo esc_attr($instinctor_quantity); ?>" 
           min="<?php echo esc_attr($instinctor_min); ?>" 
           max="<?php echo esc_attr($instinctor_max); ?>"
           step="<?php echo esc_attr($instinctor_step); ?>"
           class="instinctor-qty-input"
           data-product-id="<?php echo esc_attr($instinctor_product_id); ?>">
    <button type="button" class="instinctor-qty-btn instinctor-qty-plus" aria-label="<?php esc_attr_e('Increase quantity', 'instinctor'); ?>">+</button>
</div>

<?php ob_start(); ?>
(function() {
    document.addEventListener('click', function(e) {
        if (e.target.classList.contains('instinctor-qty-minus') || e.target.classList.contains('instinctor-qty-plus')) {
            var container = e.target.closest('.instinctor-qty-selector');
            var input = container.querySelector('.instinctor-qty-input');
            var step = parseInt(input.step) || 1;
            var min = parseInt(input.min) || 1;
            var max = parseInt(input.max) || 999;
            var value = parseInt(input.value) || min;
            
            if (e.target.classList.contains('instinctor-qty-minus')) {
                value = Math.max(min, value - step);
            } else {
                value = Math.min(max, value + step);
            }
            
            input.value = value;
            input.dispatchEvent(new Event('change', { bubbles: true }));
        }
    });
})();
<?php $_inst_ljs = ob_get_clean(); wp_add_inline_script('instinctor-frontend-js', $_inst_ljs, 'after'); ?>
<?php endif; ?>
