<?php

/**
 * Template for displaying field label.
 *
 * This template can be overridden by copying it to yourtheme/eopfw-templates/epofw-field-label-html.php
 *
 * NOTE: On occasion Extra Product Options for WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @package Extra_Product_Options_For_WooCommerce/Templates
 * @version 2.5
 */
defined( 'ABSPATH' ) || exit;
// Get field data from arguments.
$epofw_fields_data = ( isset( $args['epofwtwp_args']['fields_data'] ) ? $args['epofwtwp_args']['fields_data'] : array() );
// Get field ID for label association (accessibility).
$epofw_field_id = ( isset( $args['epofwtwp_args']['fields_key'] ) ? $args['epofwtwp_args']['fields_key'] : '' );
if ( empty( $epofw_field_id ) ) {
    $epofw_attr_data = epofw_get_field_input_property( $epofw_fields_data );
    $epofw_field_id = ( isset( $epofw_attr_data['id'] ) ? $epofw_attr_data['id'] : '' );
}
// Get label styling settings (PREMIUM feature).
$epofw_label_styling_settings = array();
// Generate label text style class (typography: font-size, font-weight, etc.).
// Label containers no longer support styling (simplified approach).
$epofw_label_text_style_class = '';
if ( !empty( $epofw_label_styling_settings ) ) {
    $epofw_label_text_style_class = epofw_generate_label_text_styles( $epofw_label_styling_settings );
    // Output custom CSS for label if available.
    $epofw_label_custom_css = ( !empty( $epofw_label_styling_settings['custom_css'] ) ? $epofw_label_styling_settings['custom_css'] : '' );
    $epofw_label_selector = 'label[for="epofw_field_' . esc_attr( $epofw_field_id ) . '"], .epofw_td_label label';
    if ( !empty( $epofw_label_custom_css ) ) {
        echo epofw_output_custom_css( $epofw_label_custom_css, $epofw_label_selector );
        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
    }
}
// Get field title position setting.
$epofw_field_title_position = epofw_get_field_label_title_position( $epofw_fields_data );
// Handle different title positions.
// Note: CSS classes are added to help with styling different positions.
switch ( $epofw_field_title_position ) {
    case 'disable':
        // Don't display label at all - exit early.
        return;
    case 'above':
        // Display label above the field (full width, block-level element).
        // Uses div wrapper instead of table cell for better layout control.
        $epofw_display_above = true;
        break;
    case 'right':
        // Display label to the right of the field.
        // Uses table cell structure with 'epofw_label_right' class for styling.
        $epofw_display_right = true;
        break;
    case 'left':
    default:
        // Display label to the left of the field (default behavior).
        // Uses table cell structure with 'epofw_label_left' class for styling.
        $epofw_display_left = true;
        break;
}
// Get field type for conditional logic.
$epofw_field_type = epofw_get_field_type( $epofw_fields_data );
// Determine the appropriate HTML structure based on position.
if ( isset( $epofw_display_above ) ) {
    // For 'above' position, use a div wrapper with full width.
    /**
     * Filter the label class for above position.
     *
     * @since 1.0.0
     *
     * @param string $label_class The label class.
     * @param array  $args        The template arguments.
     */
    $epofw_label_class = apply_filters( 'epofw_field_td_label_class', 'label epofw_label_above', $args );
    $epofw_colspan_attr = '';
} elseif ( isset( $epofw_display_right ) ) {
    // For 'right' position, use table cell structure with right alignment.
    /**
     * Filter the label class for right position.
     *
     * @since 1.0.0
     *
     * @param string $label_class The label class.
     * @param array  $args        The template arguments.
     */
    $epofw_label_class = apply_filters( 'epofw_field_td_label_class', 'label epofw_td_label epofw_label_right', $args );
    // Determine if colspan should be applied for heading/paragraph/separator fields.
    $epofw_colspan_attr = '';
    if ( in_array( $epofw_field_type, array('heading', 'paragraph', 'separator'), true ) ) {
        $epofw_colspan_attr = 'colspan="2"';
    }
} else {
    // For 'left' position (default), use table cell structure.
    /**
     * Filter the label class for left position.
     *
     * @since 1.0.0
     *
     * @param string $label_class The label class.
     * @param array  $args        The template arguments.
     */
    $epofw_label_class = apply_filters( 'epofw_field_td_label_class', 'label epofw_td_label epofw_label_left', $args );
    // Determine if colspan should be applied for heading/paragraph/separator fields.
    $epofw_colspan_attr = '';
    if ( in_array( $epofw_field_type, array('heading', 'paragraph', 'separator'), true ) ) {
        $epofw_colspan_attr = 'colspan="2"';
    }
}
// Build the label container attributes.
$epofw_label_attributes = array(
    'class' => $epofw_label_class,
);
if ( !empty( $epofw_colspan_attr ) ) {
    $epofw_label_attributes['colspan'] = '2';
}
// Convert attributes array to string.
$epofw_label_attr_string = '';
foreach ( $epofw_label_attributes as $epofw_attr => $epofw_value ) {
    $epofw_label_attr_string .= ' ' . esc_attr( $epofw_attr ) . '="' . esc_attr( $epofw_value ) . '"';
}
// Label containers no longer support styling (simplified approach).
// Handle separator field type - render full-width <hr> without label structure.
if ( 'separator' === $epofw_field_type ) {
    ?>
	<div <?php 
    echo wp_kses_post( $epofw_label_attr_string );
    ?>>
		<hr class="epofw-separator" />
	</div>
	<?php 
    return;
}
?>
<div <?php 
echo wp_kses_post( $epofw_label_attr_string );
?>>
	<div class="label_div">
		<?php 
// Get field label properties.
$epofw_field_label_title_type = epofw_get_field_label_title_type( $epofw_fields_data );
$epofw_field_label_title = epofw_get_field_label_title( $epofw_fields_data );
$epofw_field_label_class = epofw_get_field_label_class( $epofw_fields_data );
// Prepare data attribute for label name.
$epofw_data_label_name = '';
if ( !empty( $epofw_field_label_title ) ) {
    $epofw_data_label_name = 'data-label-name="' . esc_attr( $epofw_field_label_title ) . '"';
}
// Initialize variables for title/content display.
$epofw_heading_type = $epofw_field_label_title_type;
$epofw_title_or_content = $epofw_field_label_title;
$epofw_title_styles = '';
$epofw_color_attr = '';
// Process label class with filter.
$epofw_processed_label_class = '';
if ( !empty( $epofw_field_label_class ) ) {
    /**
     * Filter for title label class.
     *
     * @since 1.0.0
     * @param string $class The CSS class for the title label.
     * @param array  $args  Template arguments.
     */
    $epofw_filtered_class = apply_filters( 'epofw_field_title_lbl_class', $epofw_field_label_class, $args );
    $epofw_processed_label_class = 'class="' . esc_attr( $epofw_filtered_class ) . '"';
}
// Check if price should be hidden on product page.
$epofw_hide_price_on_page = '';
// Add price to title if enabled and not hidden.
// Skip price label for 'custom' pricing type - user enters the price directly.
$epofw_addon_price_type = epofw_get_addon_price_type( $epofw_fields_data );
$epofw_skip_price_label = in_array( $epofw_addon_price_type, array('custom', 'dynamic', 'dynamic_excl'), true );
if ( 'on' !== $epofw_hide_price_on_page && !$epofw_skip_price_label && isset( $epofw_fields_data['epofw_field_settings']['enable_price_extra'] ) && 'on' === $epofw_fields_data['epofw_field_settings']['enable_price_extra'] ) {
    $epofw_price_label_text = epofw_title_with_price( $args['epofwtwp_args'], $epofw_fields_data );
    $epofw_title_or_content = $epofw_title_or_content . $epofw_price_label_text;
}
// Handle heading field type.
if ( 'heading' === $epofw_field_type ) {
    $epofw_heading_type = epofw_get_field_heading_type( $epofw_fields_data );
    $epofw_title_or_content = epofw_get_field_label_title( $epofw_fields_data );
}
// Handle paragraph field type.
if ( 'paragraph' === $epofw_field_type ) {
    $epofw_heading_type = epofw_get_field_content_type( $epofw_fields_data );
    $epofw_title_or_content = epofw_get_field_content( $epofw_fields_data );
}
// Add label text style class to processed label class (typography).
if ( !empty( $epofw_label_text_style_class ) ) {
    if ( !empty( $epofw_processed_label_class ) ) {
        // Extract class value and add label text style class.
        $epofw_processed_label_class = str_replace( 'class="', 'class="' . esc_attr( $epofw_label_text_style_class ) . ' ', $epofw_processed_label_class );
    } else {
        $epofw_processed_label_class = 'class="' . esc_attr( $epofw_label_text_style_class ) . '"';
    }
}
// Merge existing title styles (color) with label text class.
$epofw_merged_title_styles = $epofw_title_styles;
// Build the title HTML.
// Field types that contain multiple inputs should not have a 'for' attribute on the main label.
// Individual inputs within these groups have their own labels.
// Note: 'select' and 'multiselect' are single <select> elements, so they CAN have 'for' attributes.
// Note: 'slider' uses a hidden input, so we use aria-labelledby instead of 'for' attribute.
$epofw_multi_input_field_types = array('checkboxgroup', 'radiogroup');
$epofw_aria_labelledby_field_types = array('slider');
// For form fields (not heading/paragraph), render based on Title Type setting.
if ( !in_array( $epofw_field_type, array('heading', 'paragraph'), true ) && !empty( $epofw_field_id ) ) {
    // Check if Title Type is set to 'label' (default) or another element type (span, h1, h2, etc.).
    $epofw_use_label_element = 'label' === $epofw_heading_type;
    if ( $epofw_use_label_element ) {
        // Use <label> element for accessibility when Title Type is 'label'.
        if ( in_array( $epofw_field_type, $epofw_multi_input_field_types, true ) ) {
            // For multi-input field types, don't use 'for' attribute (they contain multiple inputs with their own labels).
            $epofw_title_html = sprintf(
                '<label %2$s %3$s %4$s %5$s>%6$s</label>',
                esc_html( $epofw_heading_type ),
                $epofw_processed_label_class,
                $epofw_color_attr,
                $epofw_data_label_name,
                $epofw_merged_title_styles,
                wp_kses_post( $epofw_title_or_content )
            );
        } elseif ( in_array( $epofw_field_type, $epofw_aria_labelledby_field_types, true ) ) {
            // For slider fields, use label with ID (for aria-labelledby) but no 'for' attribute.
            $epofw_label_id = 'epofw_label_' . esc_attr( $epofw_field_id );
            $epofw_title_html = sprintf(
                '<label id="%8$s" %2$s %3$s %4$s %5$s>%6$s</label>',
                esc_html( $epofw_heading_type ),
                $epofw_processed_label_class,
                $epofw_color_attr,
                $epofw_data_label_name,
                $epofw_merged_title_styles,
                wp_kses_post( $epofw_title_or_content ),
                esc_attr( $epofw_field_id ),
                $epofw_label_id
            );
        } else {
            // Use label element with 'for' attribute for single-input fields.
            $epofw_title_html = sprintf(
                '<label for="epofw_field_%7$s" %2$s %3$s %4$s %5$s>%6$s</label>',
                esc_html( $epofw_heading_type ),
                $epofw_processed_label_class,
                $epofw_color_attr,
                $epofw_data_label_name,
                $epofw_merged_title_styles,
                wp_kses_post( $epofw_title_or_content ),
                esc_attr( $epofw_field_id )
            );
        }
    } else {
        // Use the specified element type (span, h1, h2, etc.) when Title Type is not 'label'.
        $epofw_title_html = sprintf(
            '<%1$s %2$s %3$s %4$s %5$s>%6$s</%1$s>',
            esc_html( $epofw_heading_type ),
            $epofw_processed_label_class,
            $epofw_color_attr,
            $epofw_data_label_name,
            $epofw_merged_title_styles,
            wp_kses_post( $epofw_title_or_content )
        );
    }
} else {
    // For heading/paragraph or when no field ID, use heading element.
    $epofw_title_html = sprintf(
        '<%1$s %2$s %3$s %4$s %5$s>%6$s</%1$s>',
        esc_html( $epofw_heading_type ),
        $epofw_processed_label_class,
        $epofw_color_attr,
        $epofw_data_label_name,
        $epofw_merged_title_styles,
        wp_kses_post( $epofw_title_or_content )
    );
}
/**
 * Filter for title action.
 *
 * @since 1.0.0
 * @param string $html The title HTML.
 * @param array  $args Template arguments.
 */
echo wp_kses_post( apply_filters( 'epofw_field_title_action', $epofw_title_html, $args ) );
// Handle subtitle display.
$epofw_field_label_subtitle = epofw_get_field_label_subtitle( $epofw_fields_data );
$epofw_field_label_subtitle_type = epofw_get_field_label_subtitle_type( $epofw_fields_data );
$epofw_field_label_subtitle_class = epofw_get_field_label_subtitle_class( $epofw_fields_data );
// Process subtitle class with filter.
$epofw_processed_subtitle_class = '';
if ( !empty( $epofw_field_label_subtitle_class ) ) {
    /**
     * Filter for subtitle label class.
     *
     * @since 1.0.0
     * @param string $class The CSS class for the subtitle label.
     * @param array  $args  Template arguments.
     */
    $epofw_filtered_subtitle_class = apply_filters( 'epofw_field_subtitle_lbl_subtitle', $epofw_field_label_subtitle_class, $args );
    $epofw_processed_subtitle_class = 'class="' . esc_attr( $epofw_filtered_subtitle_class ) . '"';
}
// Handle premium subtitle color styling.
$epofw_subtitle_styles = '';
// Add label text style class to subtitle (typography applies to subtitle too).
if ( !empty( $epofw_label_text_style_class ) ) {
    if ( !empty( $epofw_processed_subtitle_class ) ) {
        // Extract class value and add label text style class.
        $epofw_processed_subtitle_class = str_replace( 'class="', 'class="' . esc_attr( $epofw_label_text_style_class ) . ' ', $epofw_processed_subtitle_class );
    } else {
        $epofw_processed_subtitle_class = 'class="' . esc_attr( $epofw_label_text_style_class ) . '"';
    }
}
// Merge existing subtitle styles (color).
$epofw_merged_subtitle_styles = $epofw_subtitle_styles;
// Build subtitle HTML if subtitle exists.
$epofw_subtitle_html = '';
if ( !empty( $epofw_field_label_subtitle ) ) {
    $epofw_subtitle_html = sprintf(
        '<%1$s %2$s %3$s>%4$s</%1$s>',
        esc_html( $epofw_field_label_subtitle_type ),
        $epofw_processed_subtitle_class,
        $epofw_merged_subtitle_styles,
        wp_kses_post( $epofw_field_label_subtitle )
    );
}
/**
 * Filter for subtitle action.
 *
 * @since 1.0.0
 * @param string $html The subtitle HTML.
 * @param array  $args Template arguments.
 */
echo wp_kses_post( apply_filters( 'epofw_field_subtitle_action', $epofw_subtitle_html, $args ) );
?>
	</div>
	<?php 
/**
 * Filter for end label tag.
 *
 * @since 1.0.0
 * @since 4.4.0 Removed epofw_get_tag_based_on_structure() param, now uses 'div' directly.
 */
$epofw_label_end_tag = apply_filters( 'epofw_field_label_end_tag_display', 'div', $args );
?>
</div>
