<?php

/**
 * Element Form.
 *
 * @since   4.4.0
 * @package Extra Product Options for WooCommerce
 *
 * Available Variables (from field-element.php):
 * @var array  $epofw_field_data   The field/element data array.
 * @var array  $epofw_section      The section data array.
 * @var int    $epofw_get_post_id  The addon post ID.
 * @var string $epofw_field_id     The field ID/key.
 * @var string $epofw_section_id   The section ID.
 * @var string $epofw_group_id     The row/group ID.
 * @var string $epofw_row_id       The column/row ID.
 * @var string $epofw_get_action   The current action (add/edit).
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
// Field Data.
$epofw_field_data = ( isset( $epofw_field_data ) ? $epofw_field_data : array() );
$epofw_general_data = epofw_check_array_key_exists( 'field', $epofw_field_data );
// Get field status - default to 'on' for new fields.
// Check if field_status key exists - if not set at all, default to 'on'.
if ( isset( $epofw_field_data['field_status'] ) ) {
    $epofw_field_status = $epofw_field_data['field_status'];
} else {
    $epofw_field_status = 'on';
    // Default to enabled for new fields.
}
$epofw_field_type = epofw_check_array_key_exists( 'type', $epofw_general_data );
$epofw_field_name = epofw_check_array_key_exists( 'name', $epofw_general_data );
$epofw_input_field_id = epofw_check_array_key_exists( 'id', $epofw_general_data );
$epofw_field_class = epofw_check_array_key_exists( 'class', $epofw_general_data );
$epofw_field_required = ( !empty( $epofw_general_data['required'] ) ? 'on' : 'off' );
// Label Data.
$epofw_label_data = epofw_check_array_key_exists( 'label', $epofw_field_data );
$epofw_label_data = ( isset( $epofw_label_data ) ? $epofw_label_data : array() );
$epofw_label_title = epofw_check_array_key_exists( 'title', $epofw_label_data );
$epofw_label_class = epofw_check_array_key_exists( 'class', $epofw_label_data );
$epofw_label_title_type = epofw_check_array_key_exists( 'title_type', $epofw_label_data );
$epofw_label_title_color = epofw_check_array_key_exists( 'title_color', $epofw_label_data );
$epofw_label_subtitle = epofw_check_array_key_exists( 'subtitle', $epofw_label_data );
$epofw_label_subtitle_class = epofw_check_array_key_exists( 'subtitle_class', $epofw_label_data );
$epofw_label_subtitle_type = epofw_check_array_key_exists( 'subtitle_type', $epofw_label_data );
$epofw_label_subtitle_color = epofw_check_array_key_exists( 'subtitle_color', $epofw_label_data );
$epofw_label_title_position = epofw_get_field_label_title_position( $epofw_field_data );
// Field Settings.
$epofw_options_data = epofw_check_array_key_exists( 'epofw_field_settings', $epofw_field_data );
$epofw_options_data = ( isset( $epofw_options_data ) ? $epofw_options_data : array() );
$epofw_field_options = epofw_check_array_key_exists( 'options', $epofw_options_data );
// Check if options are in the field array (imported data structure).
if ( empty( $epofw_field_options ) ) {
    $epofw_general_data = epofw_check_array_key_exists( 'field', $epofw_field_data );
    $epofw_field_options = epofw_check_array_key_exists( 'options', $epofw_general_data );
}
// Ensure required variables are set.
$epofw_section = ( isset( $epofw_section ) ? $epofw_section : array(
    'section_id' => 'section_' . wp_rand(),
) );
$epofw_field_id = ( isset( $epofw_field_id ) ? $epofw_field_id : wp_rand() );
$epofw_get_post_id = ( isset( $epofw_get_post_id ) && !empty( $epofw_get_post_id ) ? intval( $epofw_get_post_id ) : 0 );
// Field name prefix for form inputs (optimized flat structure).
$epofw_field_name_prefix = 'epofw_field[' . $epofw_field_id . ']';
$epofw_fields_list = epofw_get_fields_list( $epofw_get_post_id, $epofw_field_id );
// Free users can preview premium fields, but saved config values should not be exposed.
$epofw_mask_preview_data = false;
if ( epofw_is_premium_field_type( $epofw_field_type ) ) {
    $epofw_mask_preview_data = true;
}
if ( $epofw_mask_preview_data ) {
    $epofw_field_name = '';
    $epofw_input_field_id = '';
    $epofw_field_class = '';
    $epofw_field_required = 'off';
    $epofw_label_title = '';
    $epofw_label_class = '';
    $epofw_label_subtitle = '';
    $epofw_label_subtitle_class = '';
    $epofw_field_options = array();
    $epofw_options_data = array();
}
$epofw_tabs = array();
$epofw_tabs['general-settings'] = array(
    'label' => esc_html__( 'General Settings', 'extra-product-options-for-woocommerce' ),
    'icon'  => 'dashicons-heading',
);
$epofw_tabs['label-settings'] = array(
    'label' => esc_html__( 'Label Settings', 'extra-product-options-for-woocommerce' ),
    'icon'  => 'dashicons-admin-generic',
);
$epofw_options_for_field_type = array(
    'checkboxgroup',
    'radiogroup',
    'select',
    'multiselect',
    'checkbox'
);
/**
 * Allow addons to register field types that show the Options tab (e.g. Product Group).
 *
 * @since 4.4.6
 * @param array $epofw_options_for_field_type Field types with options.
 */
$epofw_options_for_field_type = apply_filters( 'epofw_builder_options_for_field_type', $epofw_options_for_field_type );
$epofw_tabs['options-settings'] = array(
    'label' => sprintf( 
        /* translators: %s: Field type label */
        esc_html__( '%s Options', 'extra-product-options-for-woocommerce' ),
        ucfirst( $epofw_field_type )
     ),
    'icon'  => 'dashicons-list-view',
);
// Always show premium tabs - they will show PRO badge and preview for free users.
$epofw_tabs['quantity-settings'] = array(
    'label'      => esc_html__( 'Quantity Options', 'extra-product-options-for-woocommerce' ),
    'icon'       => 'dashicons-calculator',
    'is_premium' => true,
);
// Show Pricing tab for all field types except option-based; Product Group shows it with disabled field-level price.
if ( !in_array( $epofw_field_type, $epofw_options_for_field_type, true ) || 'productgroup' === $epofw_field_type ) {
    $epofw_tabs['pricing-settings'] = array(
        'label' => esc_html__( 'Pricing Options', 'extra-product-options-for-woocommerce' ),
        'icon'  => 'dashicons-money-alt',
    );
}
$epofw_tabs['conditional-logic'] = array(
    'label'      => esc_html__( 'Conditional Logic', 'extra-product-options-for-woocommerce' ),
    'icon'       => 'dashicons-filter',
    'is_premium' => true,
);
$epofw_tabs['woocommerce-settings'] = array(
    'label'      => esc_html__( 'WooCommerce Settings', 'extra-product-options-for-woocommerce' ),
    'icon'       => 'dashicons-cart',
    'is_premium' => true,
);
$epofw_tabs['field-container-settings'] = array(
    'label' => esc_html__( 'Field Container Settings', 'extra-product-options-for-woocommerce' ),
    'icon'  => 'dashicons-admin-appearance',
);
$epofw_remove_fields_for_heading_paragraph = true;
if ( 'heading' === $epofw_field_type || 'paragraph' === $epofw_field_type ) {
    $epofw_remove_fields_for_heading_paragraph = false;
    unset($epofw_tabs['quantity-settings']);
    unset($epofw_tabs['pricing-settings']);
    unset($epofw_tabs['conditional-logic']);
    unset($epofw_tabs['woocommerce-settings']);
}
// Separator field - only needs general settings (with separator options).
if ( 'separator' === $epofw_field_type ) {
    unset($epofw_tabs['label-settings']);
    unset($epofw_tabs['options-settings']);
    unset($epofw_tabs['quantity-settings']);
    unset($epofw_tabs['pricing-settings']);
    unset($epofw_tabs['conditional-logic']);
    unset($epofw_tabs['woocommerce-settings']);
    unset($epofw_tabs['field-container-settings']);
}
$epofw_hide_fields_for_hidden = true;
if ( 'hidden' === $epofw_field_type ) {
    $epofw_hide_fields_for_hidden = false;
    unset($epofw_tabs['quantity-settings']);
    unset($epofw_tabs['pricing-settings']);
    unset($epofw_tabs['conditional-logic']);
    unset($epofw_tabs['woocommerce-settings']);
}
$epofw_remove_fields_for_password = true;
if ( 'password' === $epofw_field_type ) {
    $epofw_remove_fields_for_password = false;
    unset($epofw_tabs['quantity-settings']);
    unset($epofw_tabs['pricing-settings']);
    unset($epofw_tabs['conditional-logic']);
    unset($epofw_tabs['woocommerce-settings']);
}
/**
 * Filter field settings tabs.
 *
 * Allows modules to add custom tabs to field settings modal.
 *
 * @since 4.4.5
 *
 * @param array  $epofw_tabs Array of tabs with 'label', 'icon', and optional 'is_premium' keys.
 * @param string $epofw_field_type Current field type.
 * @param string $epofw_field_id   Field ID.
 * @param array  $epofw_field_data Full field data array.
 */
$epofw_tabs = apply_filters(
    'epofw_field_settings_tabs',
    $epofw_tabs,
    $epofw_field_type,
    $epofw_field_id,
    $epofw_field_data
);
$epofw_row_data = epofw_check_array_key_exists( 'row_association', $epofw_field_data );
if ( !empty( $epofw_row_data ) ) {
    $epofw_row_id = epofw_check_array_key_exists( 'row_id', $epofw_row_data );
    $epofw_group_id = epofw_check_array_key_exists( 'group_id', $epofw_row_data );
    $epofw_layout = epofw_check_array_key_exists( 'layout', $epofw_row_data );
    $epofw_columns = epofw_check_array_key_exists( 'columns', $epofw_row_data );
}
// Get section_id - check multiple sources for compatibility.
// Priority: 1) $section array, 2) row_association, 3) field_data direct.
$epofw_field_section_id = '';
if ( isset( $section['section_id'] ) && !empty( $section['section_id'] ) ) {
    $epofw_field_section_id = $section['section_id'];
} elseif ( isset( $epofw_row_data['section_id'] ) && !empty( $epofw_row_data['section_id'] ) ) {
    $epofw_field_section_id = $epofw_row_data['section_id'];
} elseif ( isset( $epofw_field_data['section_id'] ) && !empty( $epofw_field_data['section_id'] ) ) {
    $epofw_field_section_id = $epofw_field_data['section_id'];
}
?>

	<div class="epofw-settings-tabs">
		<?php 
// NOTE: Field association hidden inputs (section_id, row_id, group_id, layout, columns).
// Are now rendered OUTSIDE the modal in addon-builder.php to ensure they're inside the form.
// The modal is appended to body by JavaScript, which would move inputs outside the form.
$epofw_default_tab = 'general-settings';
// Check if the field type itself is premium.
// If a field type is premium, don't show PRO badges on tabs (entire field is premium).
$epofw_is_premium_field_type = epofw_is_premium_field_type( $epofw_field_type );
$epofw_is_user_premium = false;
foreach ( $epofw_tabs as $epofw_tab_key => $epofw_tab_data ) {
    // Only show PRO badge on tab if: tab is premium AND field type is NOT premium AND user is NOT premium.
    // When a field type is premium, all tabs are part of that premium feature, so no need for individual badges.
    $epofw_show_pro_badge_on_tab = !empty( $epofw_tab_data['is_premium'] ) && !$epofw_is_premium_field_type && !$epofw_is_user_premium;
    $epofw_tab_class = ( $epofw_default_tab === $epofw_tab_key ? 'active' : '' );
    if ( $epofw_show_pro_badge_on_tab ) {
        $epofw_tab_class .= ' epofw-premium-tab';
    }
    ?>
			<button type="button" class="epofw-tab-btn <?php 
    echo esc_attr( trim( $epofw_tab_class ) );
    ?>" data-tab="<?php 
    echo esc_attr( $epofw_tab_key );
    ?>">
				<i class="dashicons <?php 
    echo esc_attr( $epofw_tab_data['icon'] );
    ?>"></i>
				<?php 
    echo esc_html( $epofw_tab_data['label'] );
    ?>
				<?php 
    if ( $epofw_show_pro_badge_on_tab ) {
        ?>
					<span class="epofw-pro-badge-small">PRO</span>
				<?php 
    }
    ?>
			</button>
			<?php 
}
?>
	</div>

<?php 
foreach ( $epofw_tabs as $epofw_tab_key => $epofw_tab_label ) {
    // Check if tab file exists in builder directory (core tabs).
    $epofw_tab_file = EPOFW_PLUGIN_DIR . '/builder/' . $epofw_tab_key . '.php';
    if ( file_exists( $epofw_tab_file ) ) {
        include $epofw_tab_file;
    }
}
/**
 * Fires after all field settings tabs content.
 *
 * Allows modules to add custom tab content for field settings.
 * Use with 'epofw_field_settings_tabs' filter to add the tab button.
 *
 * @since 4.4.5
 *
 * @param string $epofw_field_type        Field type.
 * @param string $epofw_field_id          Field ID.
 * @param array  $epofw_field_data        Full field data array.
 * @param string $epofw_field_name_prefix Form field name prefix for inputs.
 * @param array  $section                    Section data containing section_id.
 */
do_action(
    'epofw_field_settings_tab_content',
    $epofw_field_type,
    $epofw_field_id,
    $epofw_field_data,
    $epofw_field_name_prefix,
    $epofw_section
);