<?php

/**
 * Slider field template
 *
 * @package Extra_Product_Options_For_WooCommerce
 */
defined( 'ABSPATH' ) || exit;
$epofw_args = array();
$epofw_field_type = ( isset( $args['epofwtwp_args']['attr_data']['type'] ) ? $args['epofwtwp_args']['attr_data']['type'] : 'text' );
$epofw_sanitize_attr_data = array();
if ( !empty( $args['epofwtwp_args']['attr_data'] ) ) {
    foreach ( $args['epofwtwp_args']['attr_data'] as $epofw_attr_name => $epofw_attr_value ) {
        $epofw_sanitize_attr_data['input'][$epofw_attr_name] = true;
        if ( 'name' === $epofw_attr_name ) {
            if ( strpos( $epofw_attr_value, 'epofw_field_' ) === false ) {
                $epofw_attr_value = 'epofw_field_' . $epofw_attr_value;
            }
            /**
             * Apply filters for text name property.
             *
             * @since 2.5
             */
            $epofw_args['name'] = apply_filters( 'epofw_field_property_' . $epofw_field_type . '_' . $epofw_attr_name, $epofw_attr_value . '[value]', $args );
        } elseif ( 'type' === $epofw_attr_name ) {
            /**
             * Apply filters for radiogroup type field property.
             *
             * @since 2.5
             */
            $epofw_args[$epofw_attr_name] = apply_filters( 'epofw_field_property_' . $epofw_field_type . '_' . $epofw_attr_name, 'hidden', $args );
        } elseif ( !empty( $epofw_attr_value ) ) {
            /**
             * Apply filters for text field property.
             *
             * @since 2.5
             */
            $epofw_args[$epofw_attr_name] = apply_filters( 'epofw_field_property_' . $epofw_field_type . '_' . $epofw_attr_name, $epofw_attr_value, $args );
        }
    }
}
$epofw_sanitize_attr_data['div'] = array(
    'class'                       => array(),
    'id'                          => array(),
    'data-min'                    => array(),
    'data-max'                    => array(),
    'data-step'                   => array(),
    'data-default'                => array(),
    'data-mode'                   => array(),
    'data-default-min'            => array(),
    'data-default-max'            => array(),
    'data-range-pricing-strategy' => array(),
);
// Allow span for the current value display.
$epofw_sanitize_attr_data['span'] = array(
    'class' => array(),
);
// Add data-default to input for repeater field support.
$epofw_sanitize_attr_data['input']['data-default'] = true;
$epofw_sanitize_attr_data['input']['class'] = true;
/**
 * Fire action before text addon field.
 *
 * @since 2.5
 */
do_action( 'epofw_before_addon_field_' . $epofw_field_type );
if ( !empty( $epofw_args ) ) {
    $epofw_args['min_value'] = ( !empty( $epofw_args['min_value'] ) ? $epofw_args['min_value'] : 0 );
    $epofw_args['max_value'] = ( !empty( $epofw_args['max_value'] ) ? $epofw_args['max_value'] : 100 );
    $epofw_args['step_value'] = ( !empty( $epofw_args['step_value'] ) ? $epofw_args['step_value'] : 1 );
    $epofw_args['default_value'] = ( !empty( $epofw_args['default_value'] ) ? $epofw_args['default_value'] : (( isset( $epofw_args['value'] ) && '' !== (string) $epofw_args['value'] ? $epofw_args['value'] : 0 )) );
    $slider_mode = 'single';
    if ( !empty( $epofw_args['slider_mode'] ) && 'range' === $epofw_args['slider_mode'] ) {
    }
    $default_min_value = ( isset( $epofw_args['default_min_value'] ) && '' !== (string) $epofw_args['default_min_value'] ? (float) $epofw_args['default_min_value'] : (float) $epofw_args['min_value'] );
    $default_max_value = ( isset( $epofw_args['default_max_value'] ) && '' !== (string) $epofw_args['default_max_value'] ? (float) $epofw_args['default_max_value'] : (float) $epofw_args['min_value'] );
    /**
     * Filter resolved slider display mode (single vs range).
     *
     * @since 4.4.7
     */
    $slider_mode = apply_filters(
        'epofw_slider_mode',
        $slider_mode,
        $args,
        $epofw_args
    );
    $slider_range_pricing_strategy = ( !empty( $epofw_args['slider_range_pricing_strategy'] ) ? sanitize_key( (string) $epofw_args['slider_range_pricing_strategy'] ) : 'max' );
    $allowed_range_pricing_strategies = array('max', 'min', 'spread');
    if ( !in_array( $slider_range_pricing_strategy, $allowed_range_pricing_strategies, true ) ) {
        $slider_range_pricing_strategy = 'max';
    }
    if ( 'range' !== $slider_mode ) {
        $slider_range_pricing_strategy = 'max';
    }
    /**
     * Filter pricing strategy for dual-handle (range) sliders.
     *
     * @since 4.4.7
     */
    $slider_range_pricing_strategy = apply_filters(
        'epofw_slider_range_pricing_strategy',
        $slider_range_pricing_strategy,
        $args,
        $epofw_args
    );
    $min_limit = (float) $epofw_args['min_value'];
    $max_limit = (float) $epofw_args['max_value'];
    if ( $default_min_value < $min_limit ) {
        $default_min_value = $min_limit;
    }
    if ( $default_max_value > $max_limit ) {
        $default_max_value = $max_limit;
    }
    if ( $default_min_value > $default_max_value ) {
        $default_min_value = $min_limit;
        $default_max_value = $max_limit;
    }
    /**
     * Filter default min/max for range slider handles before clamping.
     *
     * @since 4.4.7
     */
    $defaults = apply_filters(
        'epofw_slider_range_defaults',
        array(
            'min' => $default_min_value,
            'max' => $default_max_value,
        ),
        $args,
        $epofw_args
    );
    if ( is_array( $defaults ) ) {
        if ( isset( $defaults['min'] ) ) {
            $default_min_value = (float) $defaults['min'];
        }
        if ( isset( $defaults['max'] ) ) {
            $default_max_value = (float) $defaults['max'];
        }
    }
    if ( $default_min_value < $min_limit ) {
        $default_min_value = $min_limit;
    }
    if ( $default_max_value > $max_limit ) {
        $default_max_value = $max_limit;
    }
    if ( $default_min_value > $default_max_value ) {
        $default_min_value = $min_limit;
        $default_max_value = $max_limit;
    }
    if ( 'range' === $slider_mode ) {
        // Keep primary submitted slider value numeric (max handle) for backward compatibility.
        $epofw_args['default_value'] = $default_max_value;
    }
    // Add data-default for repeater field support.
    $epofw_args['data-default'] = $epofw_args['default_value'];
    // Ensure input has an ID for form submission.
    // Get fields_key to match label's ID for aria-labelledby.
    $epofw_fields_key = ( isset( $args['epofwtwp_args']['fields_key'] ) ? $args['epofwtwp_args']['fields_key'] : '' );
    if ( empty( $epofw_args['id'] ) ) {
        if ( !empty( $epofw_fields_key ) ) {
            $epofw_args['id'] = $epofw_fields_key;
        } else {
            $epofw_args['id'] = 'epofw_slider_' . wp_rand();
        }
    }
    // Generate label ID for aria-labelledby (accessibility).
    // The label template creates an ID like 'epofw_label_{field_id}'.
    $epofw_label_id = ( !empty( $epofw_fields_key ) ? 'epofw_label_' . esc_attr( $epofw_fields_key ) : 'epofw_label_' . esc_attr( $epofw_args['id'] ) );
    $epofw_html = '<div class="epofw-field-wrapper epofw-slider-wrapper">';
    $epofw_html .= '<div class="epofw-slider-container">';
    $epofw_html .= '<div id="slider_' . esc_attr( $epofw_args['id'] ) . '" class="epofw-slider-control ' . esc_attr( $epofw_args['class'] ) . '" data-min="' . esc_attr( $epofw_args['min_value'] ) . '" data-max="' . esc_attr( $epofw_args['max_value'] ) . '" data-step="' . esc_attr( $epofw_args['step_value'] ) . '" data-default="' . esc_attr( $epofw_args['default_value'] ) . '" data-mode="' . esc_attr( $slider_mode ) . '" data-range-pricing-strategy="' . esc_attr( $slider_range_pricing_strategy ) . '" data-default-min="' . esc_attr( $default_min_value ) . '" data-default-max="' . esc_attr( $default_max_value ) . '" role="slider" aria-labelledby="' . $epofw_label_id . '" aria-valuemin="' . esc_attr( $epofw_args['min_value'] ) . '" aria-valuemax="' . esc_attr( $epofw_args['max_value'] ) . '" aria-valuenow="' . esc_attr( $epofw_args['default_value'] ) . '"></div>';
    $epofw_input_html = '<input ';
    foreach ( $epofw_args as $epofw_attr_name => $epofw_attr_value ) {
        if ( !isset( $epofw_sanitize_attr_data['input'][$epofw_attr_name] ) ) {
            continue;
        }
        $epofw_input_html .= ' ' . sanitize_key( $epofw_attr_name ) . EPOFW_EQUAL . '"' . esc_attr( sanitize_text_field( $epofw_attr_value ) ) . '"';
    }
    $epofw_input_html .= '/>';
    if ( 'range' === $slider_mode ) {
        $range_min_name = str_replace( '[value]', '[range_min]', $epofw_args['name'] );
        $epofw_input_html .= '<input type="hidden" class="epofw-slider-range-min" name="' . esc_attr( sanitize_text_field( $range_min_name ) ) . '" value="' . esc_attr( $default_min_value ) . '" />';
    }
    $epofw_html .= $epofw_input_html;
    $epofw_html .= '<div class="epofw-slider-value">';
    if ( 'range' === $slider_mode ) {
        $epofw_html .= '<span class="epofw-slider-current-value">' . esc_html( $default_min_value . ' - ' . $default_max_value ) . '</span>';
    } else {
        $epofw_html .= '<span class="epofw-slider-current-value">' . esc_html( $epofw_args['default_value'] ) . '</span>';
    }
    $epofw_html .= '</div>';
    $epofw_html .= '</div>';
    $epofw_html .= '</div>';
    echo wp_kses( $epofw_html, $epofw_sanitize_attr_data );
}
/**
 * Fire action after text addon field.
 *
 * @since 2.5
 */
do_action( 'epofw_after_addon_field_' . $epofw_field_type );