<?php

/**
 * Pricing Settings.
 *
 * @since   4.4.0
 * @package Extra Product Options for WooCommerce
 *
 * Available Variables (from element-form.php):
 * @var array  $epofw_field_data        The field/element data array.
 * @var string $epofw_field_type        The field type (text, select, etc.).
 * @var array  $epofw_options_data      The field settings array.
 * @var string $epofw_field_name_prefix The form field name prefix.
 * @var string $epofw_field_id          The field ID/key.
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
// Check if this is a premium field type and user is not premium.
$epofw_is_premium_field_type = epofw_is_premium_field_type( $epofw_field_type );
$epofw_is_premium_user = false;
$epofw_show_premium_preview = $epofw_is_premium_field_type && !$epofw_is_premium_user;
// Get premium field data for disabled state when showing premium field types to free users.
$epofw_prem_data_ps = ( $epofw_show_premium_preview ? epofw_get_premium_field_data() : array(
    'class'      => '',
    'badge'      => '',
    'disabled'   => '',
    'is_premium' => true,
) );
$epofw_enable_price_extra = epofw_check_array_key_exists( 'enable_price_extra', $epofw_options_data );
$epofw_addon_price_type = epofw_check_array_key_exists( 'addon_price_type', $epofw_options_data );
$epofw_addon_price = epofw_check_array_key_exists( 'addon_price', $epofw_options_data );
// Product Group: pricing is per-product in Options tab; field-level price is disabled.
$epofw_is_productgroup = 'productgroup' === $epofw_field_type;
?>
<div class="epofw-tab-content" data-tab="pricing-settings">
	<?php 
if ( $epofw_is_productgroup ) {
    ?>
		<p class="epofw-field-description"><?php 
    esc_html_e( 'Pricing for Product Group is set per product in the Options tab (Fixed, Original price, or % of product price).', 'extra-product-options-for-woocommerce' );
    ?></p>
	<?php 
}
?>
	<div class="epofw-form-group<?php 
echo esc_attr( $epofw_prem_data_ps['class'] );
?>">
		<div class="epofw-field-label">
			<label for="addon_price_type"><?php 
esc_html_e( 'Enable Pricing', 'extra-product-options-for-woocommerce' );
echo wp_kses( ( $epofw_show_premium_preview ? $epofw_prem_data_ps['badge'] : '' ), epofw_get_badge_allowed_html() );
?></label>
			<div class="epofw-field-description">
				<?php 
esc_html_e( 'Enable pricing for this field.', 'extra-product-options-for-woocommerce' );
?>
			</div>
		</div>
		<div class="epofw-field-input">
			<input type="checkbox" 
			<?php 
if ( !$epofw_is_premium_field_type && !$epofw_is_productgroup ) {
    echo 'name="' . esc_attr( $epofw_field_name_prefix ) . '[epofw_field_settings][enable_price_extra]"';
} else {
}
?>
		id="enable_price_extra" value="on" <?php 
checked( $epofw_enable_price_extra, 'on' );
?> class="epofw-has-sub" data-nextfe="price_extra" data-crntfe="enable_price_extra"<?php 
echo esc_attr( ( $epofw_is_productgroup ? ' disabled="disabled"' : $epofw_prem_data_ps['disabled'] ) );
?>>
			<label for="enable_price_extra" class="toggle-switch"></label>
		</div>
	</div>

	<div class="epofw-price-extra-fields-container" id="price_extra" style="display: <?php 
echo ( 'on' === $epofw_enable_price_extra ? 'block' : 'none' );
?>;">
		<div class="epofw-form-group<?php 
echo esc_attr( $epofw_prem_data_ps['class'] );
?>">
			<div class="epofw-field-label">
				<label for="addon_price_type"><?php 
esc_html_e( 'Price Type', 'extra-product-options-for-woocommerce' );
echo wp_kses( ( $epofw_show_premium_preview ? $epofw_prem_data_ps['badge'] : '' ), epofw_get_badge_allowed_html() );
?></label>
				<div class="epofw-field-description">
					<?php 
esc_html_e( 'Choose whether the price is a fixed amount or a percentage of the base product price.', 'extra-product-options-for-woocommerce' );
?>
				</div>
			</div>
			<div class="epofw-field-input">
				<?php 
$epofw_price_type_all = epofw_price_type_data_all( $epofw_field_type );
?>
				<select 
				<?php 
if ( !$epofw_is_premium_field_type ) {
    echo 'name="' . esc_attr( $epofw_field_name_prefix ) . '[epofw_field_settings][addon_price_type]"';
} else {
}
?>
			id="addon_price_type" class="epofw-form-control epofw-update-code-example epofw_addon_price_type"<?php 
echo esc_attr( ( $epofw_is_productgroup ? ' disabled="disabled"' : $epofw_prem_data_ps['disabled'] ) );
?>>
					<?php 
foreach ( $epofw_price_type_all as $epofw_key => $epofw_option_data ) {
    $epofw_is_pro_option = $epofw_option_data['is_pro'];
    $epofw_option_label = $epofw_option_data['label'];
    $epofw_option_disabled = '';
    // For free users, disable PRO options and remove value attribute completely.
    if ( $epofw_is_pro_option && !$epofw_is_premium_user ) {
        $epofw_option_label .= ' (PRO)';
        $epofw_option_disabled = ' disabled';
    }
    // Only output value attribute if not a PRO option OR using premium code.
    $epofw_value_attr = '';
    if ( !$epofw_is_pro_option ) {
        // Free options always get value attribute.
        $epofw_value_attr = 'value="' . esc_attr( $epofw_key ) . '"';
    } else {
    }
    ?>
						<?php 
    // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $epofw_value_attr is built with esc_attr() above.
    ?>
						<option <?php 
    echo $epofw_value_attr;
    ?> <?php 
    selected( $epofw_addon_price_type, $epofw_key );
    echo esc_attr( $epofw_option_disabled );
    ?>><?php 
    echo esc_html( $epofw_option_label );
    ?></option>
						<?php 
}
?>
				</select>
			</div>
		</div>

		<?php 
// Hide Base Price field when Custom pricing type is selected (user enters price directly).
$epofw_hide_base_price = 'custom' === $epofw_addon_price_type;
?>
		<div class="epofw-form-group epofw-base-price-field<?php 
echo esc_attr( $epofw_prem_data_ps['class'] );
?>"<?php 
echo ( $epofw_hide_base_price ? ' style="display: none !important;"' : '' );
?>>
			<div class="epofw-field-label">
				<label for="addon_price"><?php 
esc_html_e( 'Base Price', 'extra-product-options-for-woocommerce' );
echo wp_kses( ( $epofw_show_premium_preview ? $epofw_prem_data_ps['badge'] : '' ), epofw_get_badge_allowed_html() );
?></label>
				<div class="epofw-field-description">
					<?php 
esc_html_e( 'Set a fixed price for this addon field. This amount will be added to the total product price.', 'extra-product-options-for-woocommerce' );
?>
				</div>
			</div>
			<div class="epofw-field-input">
				<input type="text" 
				<?php 
if ( !$epofw_is_premium_field_type ) {
    echo 'name="' . esc_attr( $epofw_field_name_prefix ) . '[epofw_field_settings][addon_price]"';
} else {
}
?>
			id="addon_price" value="<?php 
echo esc_attr( ( isset( $epofw_options_data['addon_price'] ) ? $epofw_options_data['addon_price'] : '' ) );
?>" class="epofw-form-control"<?php 
echo esc_attr( ( $epofw_is_productgroup ? ' disabled="disabled"' : $epofw_prem_data_ps['disabled'] ) );
?>>
			</div>
		</div>

		<?php 
?>

		<!-- Custom Price Formula Description - Only for Text Fields -->
		<?php 
?>
	</div>

	<?php 
if ( 'datepicker' === $epofw_field_type ) {
}
?>
</div>
