<?php

/**
 * All the eCommerce related features handle from this file
 * Key function name eCommerce
 */

if (!defined('ABSPATH')) {
    exit;
}

function sapmfly_ecommerce_setup()
{
    // Nonce check for secure form submission 
    if (isset($_POST['sapmfly_save_settings'])) {
        if (
            isset($_POST['sapmfly_ecommerce_nonce']) &&
            check_admin_referer('sapmfly_ecommerce_save_settings', 'sapmfly_ecommerce_nonce')
        ) {
            $hide_product_price = isset($_POST['sapmfly_hide_product_price']) ? 'yes' : 'no';
            update_option('sapmfly_hide_product_price', $hide_product_price);

            $sapmfly_change_cart_text = isset($_POST['sapmfly_change_cart_text']) ? 'yes' : 'no';
            update_option('sapmfly_change_cart_text', $sapmfly_change_cart_text);

            $global_toggle_value = isset($_POST['sapmfly_widgets_toggle']) ? 'yes' : 'no';
            update_option('sapmfly_widgets_toggle', $global_toggle_value);

            // Flush rewrite rules after settings change
            flush_rewrite_rules();
        } else {
            wp_die(esc_html__('Security check failed. Please try again.', 'minifly'));
        }
    }

    // Get saved settings
    $hide_product_price = get_option('sapmfly_hide_product_price', 'no');
    $sapmfly_change_cart_text = get_option('sapmfly_change_cart_text', 'no');

    // Save global save option
    $global_toggle_value = get_option('sapmfly_widgets_toggle', 'no');
?>

    <!-- eCommerce settings page starts here -->
    <div class="sapmfly-ecommerce-wrap">
        <div class="sapmfly-ecommerce-page sapmfly-fonts">
            <div class="sapmfly-plugin-global-headings">
                <?php
                // Include global header
                $sapmfly_promotional_header_template = SAPMFLY_TEMPLATES . 'global-header-design.php';
                if (file_exists($sapmfly_promotional_header_template)) {
                    require_once $sapmfly_promotional_header_template;
                }
                ?>
            </div>
            <div class="sapmfly-ecommerce-page-body">
                <div class="sapmfly-page-heading">
                    <h1><?php echo esc_html__('eCommerce and Store Enhancement Setup', 'minifly'); ?></h1>
                    <p><?php echo esc_html__('Optimize your eCommerce experience with powerful store setup and enhancement features.', 'minifly'); ?></p>
                    <?php if (!class_exists('WooCommerce')) : ?>
                        <p class="sapmfly-woocommerce-not-activated" style=""><?php echo esc_html__('Important: Looks like WooCommerce isn’t active. These settings will start working once it’s installed and enabled!', 'minifly'); ?></p>
                    <?php endif; ?>
                </div>

                <div class="sapmfly-ecommerce-settings-area">
                    <form method="post" id="sapmfly-settings-form">
                        <div class="sapmfly-ecommerce-setup-form">
                            <div class="sapmfly-ecommerce-setup-header">
                                <div class="sapmfly-ecommerce-setup-header-title">
                                    <h2><?php echo esc_html__('Setup user end options here', 'minifly'); ?></h2>
                                </div>
                                <div class="sapmfly-ecommerce-setup-header-controls">
                                    <span class="sapmfly-toggle-label sapmfly-disable-all"><?php echo esc_html__('Disable All', 'minifly'); ?></span>

                                    <!-- Switch Toggle -->
                                    <label class="sapmfly-switch">
                                        <input type="checkbox" id="sapmfly-global-toggle" name="sapmfly_widgets_toggle" <?php checked($global_toggle_value, 'yes'); ?> />
                                        <span class="sapmfly-slider round"></span>
                                    </label>

                                    <span class="sapmfly-toggle-label sapmfly-enable-all"><?php echo esc_html__('Enable All', 'minifly'); ?></span>

                                    <!-- Save Button -->
                                    <button type="submit" name="sapmfly_save_settings" class="sapmfly-save-btn"><?php echo esc_html__('Save Changes', 'minifly'); ?></button>
                                </div>
                            </div>
                            <hr />
                            <div class="sapmfly-ecommerce-setup-settings-toggle">
                                <?php wp_nonce_field('sapmfly_ecommerce_save_settings', 'sapmfly_ecommerce_nonce'); ?>

                                <div class="sapmfly-toggle-wrapper">

                                    <!-- hide all product price code goes here -->
                                    <?php
                                    $sapmfly_hide_all_product_price = SAPMFLY_TEMPLATES . 'ecommerce/features/hide-all-product-price.php';
                                    if (file_exists($sapmfly_hide_all_product_price)) {
                                        require_once $sapmfly_hide_all_product_price;
                                    }
                                    ?>

                                    <!-- hide all product price code goes here, remove up in release production -->
                                    <?php
                                    $sapmfly_add_to_cart_text = SAPMFLY_TEMPLATES . 'ecommerce/features/change-add-to-cart.php';
                                    if (file_exists($sapmfly_add_to_cart_text)) {
                                        require_once $sapmfly_add_to_cart_text;
                                    }
                                    ?>

                                </div>

                                <br /><br />

                                <button type="submit" name="sapmfly_save_settings" class="button button-primary">
                                    <?php echo esc_html__('Save Changes', 'minifly'); ?>
                                </button>

                                <br /><br />

                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
<?php
}
