<?php

/**
 * If this file is called directly, abort.
 *
 * @package    Extra_Product_Options_For_WooCommerce
 * @subpackage Extra_Product_Options_For_WooCommerce/settings
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
    // Exit if accessed directly.
}
$epofw_gs = ( isset( $_POST['epofw_gs'] ) ? sanitize_text_field( wp_unslash( $_POST['epofw_gs'] ) ) : '' );
if ( $epofw_gs ) {
    $epofw_general_save_method_nonce = ( isset( $_POST['epofw_general_save_method_nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['epofw_general_save_method_nonce'] ) ) : '' );
    if ( empty( $epofw_general_save_method_nonce ) || !wp_verify_nonce( sanitize_text_field( $epofw_general_save_method_nonce ), 'epofw_general_save_method' ) ) {
        esc_html_e( 'Error with security check.', 'extra-product-options-for-woocommerce' );
        return false;
    }
    $epofw_general_data = filter_input(
        INPUT_POST,
        'epofw_general_data',
        FILTER_DEFAULT,
        FILTER_REQUIRE_ARRAY
    );
    if ( !empty( $epofw_general_data ) ) {
        $epofw_sanitized_general_data = array_map( 'sanitize_text_field', $epofw_general_data );
        update_option( 'epofw_general_data', $epofw_sanitized_general_data );
        // Clear the general settings cache after saving.
        epofw_clear_general_settings_cache();
    }
}
$epofw_addon_details_title = epofw_get_addon_details_title( 'epofw_addon_details_title' );
$epofw_addon_details_subtotal_title = epofw_get_addon_subtitle_title( 'epofw_addon_details_subtotal_title' );
// Check if user is premium.
$epofw_is_premium_user = false;
// Premium settings - get values for premium users, set defaults for free users.
if ( $epofw_is_premium_user ) {
    $epofw_addon_details_display = epofw_general_settings( 'epofw_addon_details_display' );
    $epofw_addon_details_position = epofw_general_settings( 'epofw_addon_details_position' );
    $epofw_addon_on_shop = epofw_general_settings( 'epofw_addon_on_shop' );
    $epofw_shop_addon_details_display = epofw_general_settings( 'epofw_shop_addon_details_display' );
    $epofw_custom_css = epofw_general_settings( 'epofw_custom_css' );
} else {
    // Default values for preview to free users.
    $epofw_addon_details_display = '';
    $epofw_addon_details_position = 'before_add_to_cart';
    $epofw_addon_on_shop = '';
    $epofw_shop_addon_details_display = '';
    $epofw_custom_css = '';
}
// Premium field styling for free users.
$epofw_premium_disabled_class = ( !$epofw_is_premium_user ? ' epofw-premium-field-disabled' : '' );
$epofw_premium_pro_badge = ( !$epofw_is_premium_user ? '<span class="epofw-pro-badge-inline">' . esc_html__( 'PRO', 'extra-product-options-for-woocommerce' ) . '</span>' : '' );
$epofw_premium_disabled_attr = ( !$epofw_is_premium_user ? ' disabled' : '' );
?>
	<div class="epofw_main_form_section epofw-general-settings-container">
		<div class="epofw_main_left_section epofw-configuration-box">
			<div class="epofw-box-header">
				<h3><?php 
echo esc_html__( 'General Settings', 'extra-product-options-for-woocommerce' );
?></h3>
				<p class="epofw-box-description"><?php 
echo esc_html__( 'Configure the global settings that control how your product addons are displayed and behave across your entire store. These settings affect all addon fields and provide a consistent experience for your customers.', 'extra-product-options-for-woocommerce' );
?></p>
			</div>
			<div class="epofw_section epofw_field_info epofw-box-content">
				<?php 
/**
 * Fire action before addon details title.
 *
 * @since 1.0.0
 */
do_action( 'epofw_before_addon_details_title' );
?>
				<div class="epofw-setting-field">
					<div class="epofw-setting-label">
						<label for="epofw_addon_status"><?php 
echo esc_html__( 'Addon Details Title', 'extra-product-options-for-woocommerce' );
?></label>
						<p class="epofw-field-description"><?php 
echo esc_html__( 'Set the main heading that displays above your addon options and their prices. This title appears before the list of addon items and their individual costs, helping customers understand what additional options are available.', 'extra-product-options-for-woocommerce' );
?></p>
					</div>
					<div class="epofw-setting-input">
						<input type="text" name="epofw_general_data[epofw_addon_details_title]" id="epofw_addon_details_title" value="<?php 
echo esc_attr( $epofw_addon_details_title );
?>" class="epofw-form-control">
					</div>
				</div>
				<?php 
/**
 * Fire action after addon details title.
 *
 * @since 1.0.0
 */
do_action( 'epofw_after_addon_details_title' );
?>
				<div class="epofw-setting-field">
					<div class="epofw-setting-label">
						<label for="epofw_addon_status"><?php 
echo esc_html__( 'Subtotal Title', 'extra-product-options-for-woocommerce' );
?></label>
						<p class="epofw-field-description"><?php 
echo esc_html__( 'Set the label for the subtotal section that displays the total cost of selected addon options. This helps customers see the additional cost before adding to cart.', 'extra-product-options-for-woocommerce' );
?></p>
					</div>
					<div class="epofw-setting-input">
						<input type="text" name="epofw_general_data[epofw_addon_details_subtotal_title]" id="epofw_addon_details_subtotal_title" value="<?php 
echo esc_attr( $epofw_addon_details_subtotal_title );
?>" class="epofw-form-control">
					</div>
				</div>
				<?php 
/**
 * Fire action before display structure.
 *
 * @since      1.0.0
 * @deprecated 4.4.0 Display Structure Format setting has been removed. All addons now use div
 *             structure.
 */
do_action( 'epofw_before_display_structure' );
/**
 * Fire action after display structure.
 *
 * @since      1.0.0
 * @deprecated 4.4.0 Display Structure Format setting has been removed. All addons now use div
 *             structure.
 */
do_action( 'epofw_after_display_structure' );
/**
 * Fire action before addon details position.
 *
 * @since 1.0.0
 */
do_action( 'epofw_before_addon_details_position' );
?>
				<div class="epofw-setting-field<?php 
echo esc_attr( $epofw_premium_disabled_class );
?>">
					<div class="epofw-setting-label">
						<label for="epofw_addon_details_position_display"><?php 
echo esc_html__( 'Display Addon Details', 'extra-product-options-for-woocommerce' );
echo wp_kses( $epofw_premium_pro_badge, epofw_get_badge_allowed_html() );
?></label>
						<p class="epofw-field-description"><?php 
echo esc_html__( 'Control where the addon fields appear on your product page. Display before the Add to Cart button for better visibility, or after for a cleaner product description layout.', 'extra-product-options-for-woocommerce' );
?></p>
					</div>
					<div class="epofw-setting-input">
						<select 
						<?php 
?>
						id="epofw_addon_details_position_display" class="epofw-form-control"<?php 
echo esc_attr( $epofw_premium_disabled_attr );
?>>
							<option value=""><?php 
echo esc_html__( 'Select Position', 'extra-product-options-for-woocommerce' );
?></option>
							<option 
							<?php 
?>
							>
								<?php 
echo esc_html__( 'Before Add to Cart', 'extra-product-options-for-woocommerce' );
?>
							</option>
							<option 
							<?php 
?>
							>
								<?php 
echo esc_html__( 'After Add to Cart', 'extra-product-options-for-woocommerce' );
?>
							</option>
						</select>
					</div>
				</div>
				<?php 
/**
 * Fire action after addon details position.
 *
 * @since 1.0.0
 */
do_action( 'epofw_after_addon_details_position' );
/**
 * Fire action before hide addon details.
 *
 * @since 1.0.0
 */
do_action( 'epofw_before_hide_addon_details' );
?>
				<div class="epofw-setting-field<?php 
echo esc_attr( $epofw_premium_disabled_class );
?>">
					<div class="epofw-setting-label">
						<label for="epofw_addon_details_display"><?php 
echo esc_html__( 'Hide Addon Details', 'extra-product-options-for-woocommerce' );
echo wp_kses( $epofw_premium_pro_badge, epofw_get_badge_allowed_html() );
?></label>
						<p class="epofw-field-description"><?php 
echo esc_html__( 'Enable this option to completely hide all addon details with their prices.', 'extra-product-options-for-woocommerce' );
?></p>
					</div>
					<div class="epofw-setting-input">
						<input type="checkbox" 
						<?php 
?>
						id="epofw_addon_details_display" value="on" <?php 
checked( 'on', $epofw_addon_details_display );
?> class="epofw-form-control"<?php 
echo esc_attr( $epofw_premium_disabled_attr );
?>>
						<label for="epofw_addon_details_display" class="toggle-switch"></label>
					</div>
				</div>
				<?php 
/**
 * Fire action after hide addon details.
 *
 * @since 1.0.0
 */
do_action( 'epofw_after_hide_addon_details' );
/**
 * Fire action before show shop page addon.
 *
 * @since 1.0.0
 */
do_action( 'epofw_before_shop_addon_display' );
?>
				<div class="epofw-setting-field<?php 
echo esc_attr( $epofw_premium_disabled_class );
?>">
					<div class="epofw-setting-label">
						<label for="epofw_addon_on_shop"><?php 
echo esc_html__( 'Show Addon in Shop Page', 'extra-product-options-for-woocommerce' );
echo wp_kses( $epofw_premium_pro_badge, epofw_get_badge_allowed_html() );
?></label>
						<p class="epofw-field-description"><?php 
echo esc_html__( 'Display addon options directly on the shop page, allowing customers to select addons without visiting individual product pages. This can improve conversion rates and user experience.', 'extra-product-options-for-woocommerce' );
?></p>
					</div>
					<div class="epofw-setting-input">
						<input type="checkbox" 
						<?php 
?>
						id="epofw_addon_on_shop" value="on" <?php 
checked( 'on', $epofw_addon_on_shop );
?> class="epofw-form-control"<?php 
echo esc_attr( $epofw_premium_disabled_attr );
?>>
						<label for="epofw_addon_on_shop" class="toggle-switch"></label>
					</div>
				</div>
				<?php 
/**
 * Fire action after show shop page addon.
 *
 * @since 1.0.0
 */
do_action( 'epofw_after_shop_addon_display' );
/**
 * Fire action before shop addon details display.
 *
 * @since 1.0.0
 */
do_action( 'epofw_before_shop_addon_details_display' );
?>
				<div class="epofw-setting-field<?php 
echo esc_attr( $epofw_premium_disabled_class );
?>">
					<div class="epofw-setting-label">
						<label for="epofw_shop_addon_details_display"><?php 
echo esc_html__( 'Hide Addon Details in Shop Page', 'extra-product-options-for-woocommerce' );
echo wp_kses( $epofw_premium_pro_badge, epofw_get_badge_allowed_html() );
?></label>
						<p class="epofw-field-description"><?php 
echo esc_html__( 'Enable this option to completely hide all addon details with their prices on the shop page.', 'extra-product-options-for-woocommerce' );
?></p>
					</div>
					<div class="epofw-setting-input">
						<input type="checkbox" 
						<?php 
?>
						id="epofw_shop_addon_details_display" value="on" <?php 
checked( 'on', $epofw_shop_addon_details_display );
?> class="epofw-form-control"<?php 
echo esc_attr( $epofw_premium_disabled_attr );
?>>
						<label for="epofw_shop_addon_details_display" class="toggle-switch"></label>
					</div>
				</div>
				<?php 
/**
 * Fire action after shop addon details display.
 *
 * @since 1.0.0
 */
do_action( 'epofw_after_shop_addon_details_display' );
/**
 * Fire action before custom css display.
 *
 * @since 1.0.0
 */
do_action( 'epofw_before_custom_css_display' );
?>
				<div class="epofw-setting-field epofw-custom-css-setting-field<?php 
echo esc_attr( $epofw_premium_disabled_class );
?>">
					<div class="epofw-setting-label">
						<label for="epofw_custom_css"><?php 
echo esc_html__( 'Custom CSS', 'extra-product-options-for-woocommerce' );
echo wp_kses( $epofw_premium_pro_badge, epofw_get_badge_allowed_html() );
?></label>
						<p class="epofw-field-description"><?php 
echo esc_html__( 'Add your custom CSS styles to personalize the appearance of addon fields. This CSS will be applied globally to all addon elements across your store. Use this to match your theme\'s design or create unique styling.', 'extra-product-options-for-woocommerce' );
?></p>
					</div>
					<div class="epofw-setting-input">
						<textarea 
						<?php 
?>
						id="epofw_custom_css" data-mode="text/css" class="epofw-form-control"<?php 
echo esc_attr( $epofw_premium_disabled_attr );
?>><?php 
echo esc_attr( $epofw_custom_css );
?></textarea>
					</div>
				</div>
				<?php 
/**
 * Fire action after custom css display.
 *
 * @since 1.0.0
 */
do_action( 'epofw_after_custom_css_display' );
?>
			</div>
		</div>
		<p class="submit">
			<input type="submit"
			class="button button-primary"
			name="epofw_gs"
			value="<?php 
esc_attr_e( 'Save Changes', 'extra-product-options-for-woocommerce' );
?>">
		</p>
	</div>
<?php 
wp_nonce_field( 'epofw_general_save_method', 'epofw_general_save_method_nonce' );