<?php

/**
 * This template displaying field input.
 *
 * This template can be overridden by copying it to yourtheme/eopfw-templates/epofw-field-input-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;
$epofw_fields_data = ( isset( $args['epofwtwp_args']['fields_data'] ) ? $args['epofwtwp_args']['fields_data'] : array() );
$epofw_product_data = ( isset( $args['epofwtwp_args']['product_data'] ) ? $args['epofwtwp_args']['product_data'] : array() );
$epofw_action = ( isset( $args['epofwtwp_args']['epofw_action'] ) ? $args['epofwtwp_args']['epofw_action'] : array() );
$epofw_get_exftpoc = EPOFW_Front::instance()->exclude_field_type_on_cart_page();
$epofw_attr_data = epofw_get_field_input_property( $epofw_fields_data );
$epofw_check_field_inp_type = epofw_get_field_type( $epofw_fields_data );
// This ensures unique field names and IDs for proper total calculation on frontend.
$epofw_field_id = ( isset( $args['epofwtwp_args']['fields_key'] ) ? $args['epofwtwp_args']['fields_key'] : '' );
if ( !empty( $epofw_field_id ) ) {
    // For repeater fields (identified by _instance_ in field_id), always override name/id
    // to ensure each instance has unique identifiers for pricing calculation.
    $epofw_is_repeater_child = strpos( $epofw_field_id, '_instance_' ) !== false;
    if ( empty( $epofw_attr_data['name'] ) || $epofw_is_repeater_child ) {
        $epofw_attr_data['name'] = $epofw_field_id;
    }
    if ( empty( $epofw_attr_data['id'] ) || $epofw_is_repeater_child ) {
        $epofw_attr_data['id'] = $epofw_field_id;
    }
}
$epofw_check_field_common = array();
$epofw_check_epofw_field_settings = epofw_get_field_settings( $epofw_fields_data );
$epofw_check_field_title_position = epofw_get_field_label_title_position( $epofw_fields_data );
if ( !in_array( $epofw_check_field_inp_type, $epofw_get_exftpoc, true ) ) {
    $epofw_check_field_title_position = epofw_get_field_label_title_position( $epofw_fields_data );
    /**
     * Filter for input start tag.
     *
     * @since 1.0.0
     * @since 4.4.0 Removed epofw_get_tag_based_on_structure() param, now uses 'div' directly.
     */
    $epofw_tag = apply_filters( 'epofw_field_input_start_tag_display', 'div', $args );
    /**
     * Filter for td class.
     *
     * @since 1.0.0
     */
    $epofw_input_class = apply_filters( 'epofw_field_td_input_class', 'value epofw_td_value epofw_' . $epofw_check_field_title_position, $args );
    // Input Container styling has been removed - use Field Container styling instead.
    // Input container now uses default theme styles or can be styled via Field Container Custom CSS.
    ?>
	<<?php 
    echo esc_html( $epofw_tag );
    ?> class="<?php 
    echo esc_attr( $epofw_input_class );
    ?>"
	<?php 
    if ( !empty( $args ) ) {
        echo 'data-epofw_sa="' . esc_attr( wc_esc_json( epofw_all_fields_attr( $args ) ) ) . '"';
    }
    ?>
	>
	<?php 
    if ( 'above' === $epofw_check_field_title_position ) {
        /**
         * Before title.
         *
         * @since 2.5
         *
         * @param array    $epofwtwp_args  {
         *                                 The array of data.
         *
         * @type object    $product_data   Data of the current product.
         * @type int|float $product_price  Product price.
         * @type string    $opt_price_type Price type - Fixed, Percentage of product price or etc.
         * @type string    $epofw_action   Page action - shop|product page.
         * @type array     $fields_data    Array of addon data.
         * }
         */
        do_action( 'epofw_field_before_title', $args );
        /**
         * Display title html.
         *
         * @since 2.5
         *
         * @param array    $epofwtwp_args  {
         *                                 The array of data.
         *
         * @type object    $product_data   Data of the current product.
         * @type int|float $product_price  Product price.
         * @type string    $opt_price_type Price type - Fixed, Percentage of product price or etc.
         * @type string    $epofw_action   Page action - shop|product page.
         * @type array     $fields_data    Array of addon data.
         * }
         */
        do_action( 'epofw_field_title', $args );
        /**
         * After title.
         *
         * @since 2.5
         *
         * @param array    $epofwtwp_args  {
         *                                 The array of data.
         *
         * @type object    $product_data   Data of the current product.
         * @type int|float $product_price  Product price.
         * @type string    $opt_price_type Price type - Fixed, Percentage of product price or etc.
         * @type string    $epofw_action   Page action - shop|product page.
         * @type array     $fields_data    Array of addon data.
         * }
         */
        do_action( 'epofw_field_after_title', $args );
        /**
         * Before subtitle.
         *
         * @since 2.5
         *
         * @param array    $epofwtwp_args  {
         *                                 The array of data.
         *
         * @type object    $product_data   Data of the current product.
         * @type int|float $product_price  Product price.
         * @type string    $opt_price_type Price type - Fixed, Percentage of product price or etc.
         * @type string    $epofw_action   Page action - shop|product page.
         * @type array     $fields_data    Array of addon data.
         * }
         */
        do_action( 'epofw_field_before_subtitle', $args );
        /**
         * Display subtitle html.
         *
         * @since 2.5
         *
         * @param array    $epofwtwp_args  {
         *                                 The array of data.
         *
         * @type object    $product_data   Data of the current product.
         * @type int|float $product_price  Product price.
         * @type string    $opt_price_type Price type - Fixed, Percentage of product price or etc.
         * @type string    $epofw_action   Page action - shop|product page.
         * @type array     $fields_data    Array of addon data.
         * }
         */
        do_action( 'epofw_field_subtitle', $args );
        /**
         * After subtitle.
         *
         * @since 2.5
         *
         * @param array    $epofwtwp_args  {
         *                                 The array of data.
         *
         * @type object    $product_data   Data of the current product.
         * @type int|float $product_price  Product price.
         * @type string    $opt_price_type Price type - Fixed, Percentage of product price or etc.
         * @type string    $epofw_action   Page action - shop|product page.
         * @type array     $fields_data    Array of addon data.
         * }
         */
        do_action( 'epofw_field_after_subtitle', $args );
    }
    echo wp_kses_post( epofw_render_fields_front( $epofw_attr_data, $epofw_check_field_inp_type, $args ) );
    /**
     * End td.
     *
     * @since 2.5
     *
     * @param array    $epofwtwp_args  {
     *                                 The array of data.
     *
     * @type object    $product_data   Data of the current product.
     * @type int|float $product_price  Product price.
     * @type string    $opt_price_type Price type - Fixed, Percentage of product price or etc.
     * @type string    $epofw_action   Page action - shop|product page.
     * @type array     $fields_data    Array of addon data.
     * }
     */
    do_action( 'epofw_field_input_end_td', $args );
}