<?php

/**
 * All the white label related features handle from this file
 * Key function name admin_setup
 */

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

function sapmfly_white_label()
{
    // Handle form submission with nonce check
    if (isset($_POST['sapmfly_save_settings'])) {
        if (
            isset($_POST['sapmfly_white_label_nonce']) &&
            check_admin_referer('sapmfly_white_label_action', 'sapmfly_white_label_nonce')
        ) {
            $hide_wp_logo = isset($_POST['sapmfly_hide_wp_logo']) ? 'yes' : 'no';
            update_option('sapmfly_hide_wp_logo', $hide_wp_logo);

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

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

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

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

            $global_toggle_value = isset($_POST['sapmfly_widgets_toggle']) ? 'yes' : 'no';
            update_option('sapmfly_widgets_toggle', $global_toggle_value);
        } else {
            wp_die(esc_html__('Security check failed. Please try again.', 'minifly'));
        }
    }

    // Get saved settings
    $hide_wp_logo = get_option('sapmfly_hide_wp_logo', 'no');
    $hide_wp_help = get_option('sapmfly_hide_wp_help', 'no');
    $hide_wp_version = get_option('sapmfly_hide_wp_version', 'no');
    $hide_screen_options = get_option('sapmfly_screen_options', 'no');
    $hide_footer_credit = get_option('sapmfly_hide_footer_credit', 'no');

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

    <!-- Admin settings page starts here -->
    <div class="sapmfly-white-label-wrap">
        <div class="sapmfly-white-label-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-white-label-page-body">
                <div class="sapmfly-page-heading">
                    <h1><?php echo esc_html__('Custom Branding and White-Label Settings', 'minifly'); ?></h1>
                    <p><?php echo esc_html__('Customize your website’s look and feel with white-label branding options.', 'minifly'); ?></p>
                </div>

                <div class="sapmfly-white-label-settings-area">
                    <form method="post" id="sapmfly-settings-form">
                        <div class="sapmfly-white-label-form">
                            <div class="sapmfly-white-label-header">
                                <div class="sapmfly-white-label-header-title">
                                    <h2><?php echo esc_html__('Enable full rebranding for a seamless client experience', 'minifly'); ?></h2>
                                </div>
                                <div class="sapmfly-white-label-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-white-label-settings-toggle">
                                <?php wp_nonce_field('sapmfly_white_label_action', 'sapmfly_white_label_nonce'); ?>

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

                                    <!-- Free Feature styling -->
                                    <!-- Hide WordPress version number starts here -->
                                    <?php
                                    $sapmfly_hide_version_number = SAPMFLY_TEMPLATES . 'white-label/features/version-number.php';
                                    if (file_exists($sapmfly_hide_version_number)) {
                                        require_once $sapmfly_hide_version_number;
                                    }
                                    ?>

                                    <!-- Hide top top right help option starts here -->
                                    <?php
                                    $sapmfly_hide_top_right_help = SAPMFLY_TEMPLATES . 'white-label/features/top-right-help.php';
                                    if (file_exists($sapmfly_hide_top_right_help)) {
                                        require_once $sapmfly_hide_top_right_help;
                                    }
                                    ?>

                                    <!-- Pro Feature styling -->
                                    <!-- Hide top left WordPress icon code starts here -->
                                    <?php
                                    $sapmfly_hide_top_left_icon = SAPMFLY_TEMPLATES . 'white-label/features/top-left-icon.php';
                                    if (file_exists($sapmfly_hide_top_left_icon)) {
                                        require_once $sapmfly_hide_top_left_icon;
                                    }
                                    ?>

                                    <!-- Hide WordPress version number starts here -->
                                    <?php
                                    $sapmfly_hide_screen_options = SAPMFLY_TEMPLATES . 'white-label/features/option-menu.php';
                                    if (file_exists($sapmfly_hide_screen_options)) {
                                        require_once $sapmfly_hide_screen_options;
                                    }
                                    ?>

                                    <!-- Hide WordPress version number starts here -->
                                    <?php
                                    $sapmfly_hide_footer_credits = SAPMFLY_TEMPLATES . 'white-label/features/footer-credit.php';
                                    if (file_exists($sapmfly_hide_footer_credits)) {
                                        require_once $sapmfly_hide_footer_credits;
                                    }
                                    ?>

                                    <!-- Hidden Modal Popup -->
                                    <div id="minifly-premium-popup" class="minifly-pro-popup">
                                        <p>Unlock the feature with <a href="https://brainywp.com/minifly-pricing/" target="_blank" style="color: #288acb; text-decoration: underline; font-weight: bold;">pro version</a></p>
                                        <button type="button" onclick="miniflyClosePopup()"><?php echo esc_html__('Close', 'minifly'); ?></button>
                                    </div>

                                    <!-- Overlay -->
                                    <div id="minifly-premium-overlay" class="minifly-pro-overlay" onclick="miniflyClosePopup()"></div>

                                </div>
                                <br>
                                <br>
                                <button type="submit" name="sapmfly_save_settings" class="sapmfly-save-btn">
                                    <?php echo esc_html__('Save Changes', 'minifly'); ?>
                                </button>
                                <br>
                                <br>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
<?php
}
