<?php

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

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

function sapmfly_admin_setup()
{
    // Handle form submission with nonce check
    if (isset($_POST['sapmfly_save_settings'])) {
        if (
            isset($_POST['sapmfly_settings_nonce']) &&
            check_admin_referer('sapmfly_save_settings_action', 'sapmfly_settings_nonce')
        ) {
            $disable_notices = isset($_POST['sapmfly_disable_notices']) ? 'yes' : 'no';
            update_option('sapmfly_disable_admin_notices', $disable_notices);

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

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

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

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

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

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

            if (current_user_can('administrator')) {
                $enable_svg_images = isset($_POST['sapmfly_enable_svg_images']) ? 'yes' : 'no';
                update_option('sapmfly_enable_svg_images', $enable_svg_images);
            }

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

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

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

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

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

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

            $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
    $disable_notices = get_option('sapmfly_disable_admin_notices', 'no');
    $duplicate_page_post = get_option('sapmfly_duplicate_enable', 'no');
    $enable_page_category_tag = get_option('sapmfly_enable_page_category_tag', 'no');
    $disable_gutenberg_editor = get_option('sapmfly_disable_gutenberg', 'no');
    $disable_restapi = get_option('sapmfly_disable_call_restapi', 'no');
    $dark_mood_for_admin = get_option('sapmfly_dark_mood_for_admin_button', 'no');
    $sapmfly_hide_admin_toolbar = get_option('sapmfly_hide_admin_toolbar', 'no');
    $enable_svg_images = get_option('sapmfly_enable_svg_images', 'no');
    $gravatar_img_change = get_option('sapmfly_gravatar_img_change', 'no');
    $external_link = get_option('sapmfly_external_link', 'no');
    $disable_theme_update = get_option('sapmfly_disable_theme_update', 'no');
    $disable_plugin_update = get_option('sapmfly_disable_plugin_update', 'no');
    $limit_revision_number = get_option('sapmfly_limit_revision_number', 'no');
    $enable_user_switching = get_option('sapmfly_user_switching', 'no');

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

    <!-- Admin settings page starts here -->
    <div class="sapmfly-admin-wrap">
        <div class="sapmfly-admin-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-admin-page-body">
                <div class="sapmfly-page-heading">
                    <h1><?php echo esc_html__('Admin Tools and Dashboard Features', 'minifly'); ?></h1>
                    <p><?php echo esc_html__('Powerful backend enhancements and admin-focused tools to streamline and optimize website administration.', 'minifly'); ?></p>
                </div>
                <div class="sapmfly-admin-setup-settings-area">
                    <form method="post" id="sapmfly-admin-settings-form">
                        <div class="sapmfly-admin-setup-header">
                            <div class="sapmfly-admin-setup-header-title">
                                <h2><?php echo esc_html__('Setup admin panel options here', 'minifly'); ?></h2>
                            </div>
                            <div class="sapmfly-admin-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-admin-setup-form">

                            <?php wp_nonce_field('sapmfly_save_settings_action', 'sapmfly_settings_nonce'); ?>

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

                                <!-- Disable admin notice code starts here -->
                                <?php
                                $sapmfly_disable_admin_notice = SAPMFLY_TEMPLATES . 'admin/features/disable-admin-notice.php';
                                if (file_exists($sapmfly_disable_admin_notice)) {
                                    require_once $sapmfly_disable_admin_notice;
                                }
                                ?>

                                <!-- duplicate page and post code starts here -->
                                <?php
                                $sapmfly_duplicate_page_post = SAPMFLY_TEMPLATES . 'admin/features/duplicate-page-post.php';
                                if (file_exists($sapmfly_duplicate_page_post)) {
                                    require_once $sapmfly_duplicate_page_post;
                                }
                                ?>

                                <!-- Enable category and tags for page starts here -->
                                <?php
                                $sapmfly_enable_page_category_tag = SAPMFLY_TEMPLATES . 'admin/features/enable-page-category-tag.php';
                                if (file_exists($sapmfly_enable_page_category_tag)) {
                                    require_once $sapmfly_enable_page_category_tag;
                                }
                                ?>

                                <!-- Disable Gutenberg editor starts here -->
                                <?php
                                $sapmfly_disable_gutenberg = SAPMFLY_TEMPLATES . 'admin/features/disable-gutenberg.php';
                                if (file_exists($sapmfly_disable_gutenberg)) {
                                    require_once $sapmfly_disable_gutenberg;
                                }
                                ?>

                                <!-- Disable Gutenberg editor starts here -->
                                <?php
                                $sapmfly_disable_restapi = SAPMFLY_TEMPLATES . 'admin/features/disable-restapi.php';
                                if (file_exists($sapmfly_disable_restapi)) {
                                    require_once $sapmfly_disable_restapi;
                                }
                                ?>

                                <!-- Enable dark mood for back end -->
                                <?php
                                $sapmfly_dark_mood = SAPMFLY_TEMPLATES . 'admin/features/dark-mood-admin.php';
                                if (file_exists($sapmfly_dark_mood)) {
                                    require_once $sapmfly_dark_mood;
                                }
                                ?>

                                <!-- Enable dark mood for back end -->
                                <?php
                                $sapmfly_admin_toolbar_file = SAPMFLY_TEMPLATES . 'admin/features/hide-toolbar-admin.php';
                                if (file_exists($sapmfly_admin_toolbar_file)) {
                                    require_once $sapmfly_admin_toolbar_file;
                                }
                                ?>

                                <!-- Enable SVG images -->
                                <?php
                                $sapmfly_svg_images = SAPMFLY_TEMPLATES . 'admin/features/svg-upload.php';
                                if (file_exists($sapmfly_svg_images)) {
                                    require_once $sapmfly_svg_images;
                                }
                                ?>

                                <!-- Gravatar Upload -->
                                <?php
                                $sapmfly_gravatar_images = SAPMFLY_TEMPLATES . 'admin/features/gravatar-upload.php';
                                if (file_exists($sapmfly_gravatar_images)) {
                                    require_once $sapmfly_gravatar_images;
                                }
                                ?>

                                <!-- Link share -->
                                <?php
                                $sapmfly_external_link_file = SAPMFLY_TEMPLATES . 'admin/features/external-link.php';
                                if (file_exists($sapmfly_external_link_file)) {
                                    require_once $sapmfly_external_link_file;
                                }
                                ?>

                                <!-- Disable all plugin update -->
                                <?php
                                $sapmfly_disable_all_plugin_update = SAPMFLY_TEMPLATES . 'admin/features/disable-plugin-update.php';
                                if (file_exists($sapmfly_disable_all_plugin_update)) {
                                    require_once $sapmfly_disable_all_plugin_update;
                                }
                                ?>

                                <!-- Limited revision number -->
                                <?php
                                $sapmfly_limit_revision_number = SAPMFLY_TEMPLATES . 'admin/features/limited-revision-number.php';
                                if (file_exists($sapmfly_limit_revision_number)) {
                                    require_once $sapmfly_limit_revision_number;
                                }
                                ?>

                                <!-- Enable user switch -->
                                <?php
                                $sapmfly_enable_user_switch = SAPMFLY_TEMPLATES . 'admin/features/enable-user-switcher.php';
                                if (file_exists($sapmfly_enable_user_switch)) {
                                    require_once $sapmfly_enable_user_switch;
                                }
                                ?>

                                <!-- 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>
                    </form>
                </div>
            </div>
        </div>
    </div>
<?php
}
