<?php

    // prevent direct file access
	if( !defined( 'ABSPATH' ) ) exit;
    
    function pixe_plugin_settings_tools_script(){
        pixe_enqueue_admin_styles(array('utility', 'settings'));
        
        $active_tab = empty($_GET['type'])? 'system-status': sanitize_text_field(wp_unslash($_GET['type'])); // phpcs:disable WordPress.Security.NonceVerification.Recommended -- Nonce not required: request performs read-only query.
        if( $active_tab == 'maintenance' ){
            pixe_enqueue_admin_scripts(array('jquery', 'utility', 'plugin-option'));
            wp_localize_script('pixe-plugin-option', 'pixeData', pixe_plugin_settings_script_data(array('post-type', 'page', 'user-role')));
        }else if( $active_tab == 'replace-url' ){
            pixe_enqueue_admin_scripts(array('jquery', 'utility', 'plugin-option'));
            wp_localize_script('pixe-plugin-option', 'pixeData', pixe_plugin_settings_script_data());
        }else if( $active_tab == 'performance' ){
            pixe_enqueue_admin_scripts(array('jquery', 'utility', 'plugin-option'));
            wp_localize_script('pixe-plugin-option', 'pixeData', pixe_plugin_settings_script_data(array('script-handle')));
        }
    }

    // content area
    function pixe_plugin_settings_tools(){
        pixe_plugin_settings_head('tools');

        $active_tab = empty($_GET['type'])? 'system-status': sanitize_text_field(wp_unslash($_GET['type'])); // phpcs:disable WordPress.Security.NonceVerification.Recommended -- Nonce not required: request performs read-only query.
?>
<div class="pixe-plugin-settings-content" >
    <div class="pixe-plugin-settings-tab" >
        <div class="pixe-head" ><?php esc_html_e('Tools', 'pixebuilder'); ?></div>
        <?php
            // system status
            echo '<a ';
            if( $active_tab == 'system-status' ){
                echo 'href="#" class="pixe-active" ';
            }else{
                echo 'href="' . esc_attr(add_query_arg(array('type' => 'system-status'))) . '" ';
            }
            echo '>';
            echo '<span class="pixe-admin-icon pixe-icon pixe-type-svg" >';
            echo '<svg width="15" height="15" viewBox="0 0 15 15" fill="none"><path d="M11.875 2.5H3.125C2.43464 2.5 1.875 3.05964 1.875 3.75V5.625C1.875 6.31536 2.43464 6.875 3.125 6.875H11.875C12.5654 6.875 13.125 6.31536 13.125 5.625V3.75C13.125 3.05964 12.5654 2.5 11.875 2.5Z" stroke="var(--pixe--svg--color, white)" stroke-width="1.0625"/><path d="M11.875 8.125H3.125C2.43464 8.125 1.875 8.68464 1.875 9.375V11.25C1.875 11.9404 2.43464 12.5 3.125 12.5H11.875C12.5654 12.5 13.125 11.9404 13.125 11.25V9.375C13.125 8.68464 12.5654 8.125 11.875 8.125Z" stroke="var(--pixe--svg--color, white)" stroke-width="1.0625"/><path d="M4.375 4.6875H4.38125M4.375 10.3125H4.38125" stroke="var(--pixe--svg--color, white)" stroke-width="1.375" stroke-linecap="round"/></svg>';
            echo '</span>';
            esc_html_e('System Status', 'pixebuilder'); 
            echo '</a>';

            // maintenance
            echo '<a ';
            if( $active_tab == 'maintenance' ){
                echo 'href="#" class="pixe-active" ';
            }else{
                echo 'href="' . esc_attr(add_query_arg(array('type' => 'maintenance'))) . '" ';
            }
            echo '>';
            echo '<span class="pixe-admin-icon pixe-icon pixe-type-svg" >';
            echo '<svg width="12" height="13" viewBox="0 0 12 13" fill="none"><path d="M5.53125 0.53125V6.15625M2.40625 2.71875C1.59759 3.36619 1.01 4.24882 0.724662 5.24466C0.439329 6.2405 0.470347 7.30038 0.813432 8.27783C1.15652 9.25528 1.79473 10.102 2.63986 10.7011C3.485 11.3001 4.49534 11.6219 5.53125 11.6219C6.56716 11.6219 7.5775 11.3001 8.42264 10.7011C9.26777 10.102 9.90598 9.25528 10.2491 8.27783C10.5922 7.30038 10.6232 6.2405 10.3378 5.24466C10.0525 4.24882 9.46491 3.36619 8.65625 2.71875" stroke="var(--pixe--svg--color, #5B5970)" stroke-width="1.0625" stroke-linecap="round"/></svg>';
            echo '</span>';
            esc_html_e('Maintenance', 'pixebuilder'); 
            echo '</a>';

            // performance
            echo '<a ';
            if( $active_tab == 'performance' ){
                echo 'href="#" class="pixe-active" ';
            }else{
                echo 'href="' . esc_attr(add_query_arg(array('type' => 'performance'))) . '" ';
            }
            echo '>';
            echo '<span class="pixe-admin-icon pixe-icon pixe-type-svg" >';
            echo '<svg width="15" height="15" viewBox="0 0 15 15" fill="none"><path d="M2.5 11.875C2.5 10.5489 3.02678 9.27715 3.96447 8.33947C4.90215 7.40178 6.17392 6.875 7.5 6.875C8.82608 6.875 10.0979 7.40178 11.0355 8.33947C11.9732 9.27715 12.5 10.5489 12.5 11.875" stroke="var(--pixe--svg--color, #5B5970)" stroke-width="1.0625" stroke-linecap="round"/><path d="M7.5 11.875L10 8.125" stroke="var(--pixe--svg--color, #5B5970)" stroke-width="1.0625" stroke-linecap="round"/></svg>';
            echo '</span>';
            esc_html_e('Performance', 'pixebuilder'); 
            echo '</a>';
        ?>
    </div>
    <?php
        if( $active_tab == 'system-status' ){
            echo '<div class="pixe-plugin-settings-tab-content">';
            pixe_plugin_settings_system_status();
            echo '</div>';
        }else if( function_exists('pixe_plugin_settings_tools_content') ){
            pixe_plugin_settings_tools_content($active_tab);
        }else{
            pixe_plugin_license_no_pro();
        }
    ?>
</div>
<?php
    }

    // system status conteent
    function pixe_plugin_settings_system_status(){
?>
<div class="pixe-plugin-settings-tab-inner" >
    <div class="pixe-plugin-settings-tab-head" >
        <div class="pixe-title" ><?php esc_html_e('Server Status', 'pixebuilder'); ?></div>
        <div class="pixe-description" ><?php esc_html_e('Overview of your current server environment and recommended PHP configurations.', 'pixebuilder'); ?></div>
    </div>
    <div class="pixe-plugin-settings-info" >
        <div class="pixe-plugin-info pixe-type-system" >
            <div class="pixe-head" ><?php esc_html_e('Debug Mode', 'pixebuilder'); ?></div>
            <div class="pixe-tail" ><?php
                if( defined('WP_DEBUG') && WP_DEBUG ){
                    esc_html_e('On', 'pixebuilder');
                }else{
                    esc_html_e('Off', 'pixebuilder');
                }
            ?></div>
        </div>
        <div class="pixe-plugin-info pixe-type-system" >
            <div class="pixe-head" ><?php esc_html_e('PHP Version', 'pixebuilder'); ?></div>
            <div class="pixe-tail" ><?php
                if( function_exists('phpversion') ){
                    echo esc_html(phpversion());
                }else{
                    echo '-';
                }
            ?></div>
        </div>
        <div class="pixe-plugin-info pixe-type-system" >
            <div class="pixe-head" >wp_max_upload_size</div>
            <div class="pixe-tail" ><?php
                echo intval(wp_max_upload_size() / 1048576) . 'M';
            ?></div>
        </div>
        <?php if( function_exists('ini_get') ){ ?>
            <div class="pixe-plugin-info pixe-type-system" >
                <div class="pixe-head" >upload_max_filesize</div>
                <div class="pixe-tail" ><?php
                    echo esc_html(ini_get('upload_max_filesize'));
                ?></div>
            </div>
            <div class="pixe-plugin-info pixe-type-system" >
                <div class="pixe-head" >post_max_size</div>
                <div class="pixe-tail" ><?php
                    echo esc_html(ini_get('post_max_size'));
                ?></div>
            </div>
            <div class="pixe-plugin-info pixe-type-system" >
                <div class="pixe-head" >max_execution_time</div>
                <div class="pixe-tail" ><?php
                    $max_execution_time = ini_get('max_execution_time');
                    echo esc_html($max_execution_time);
                    if( intval($max_execution_time) < 300 ){
                        echo '<span class="pixe-info">' . esc_html__('300 Recommended', 'pixebuilder') . '</span>';
                    }
                ?></div>
            </div>
            <div class="pixe-plugin-info pixe-type-system" >
                <div class="pixe-head" >memory_limit</div>
                <div class="pixe-tail" ><?php
                    $memory_limit = ini_get('memory_limit');
                    echo esc_html($memory_limit);
                    if( intval($memory_limit) < 512 ){
                        echo '<span class="pixe-info">' . esc_html__('512M Recommended', 'pixebuilder') . '</span>';
                    }
                ?></div>
            </div>
            <div class="pixe-plugin-info pixe-type-system" >
                <div class="pixe-head" >max_input_vars</div>
                <div class="pixe-tail" ><?php
                    $max_input_vars = ini_get('max_input_vars');
                    echo esc_html($max_input_vars);
                    if( intval($max_input_vars) < 512 ){
                        echo '<span class="pixe-info">' . esc_html__('512M Recommended', 'pixebuilder') . '</div>';
                    }
                ?></div>
            </div>
            <div class="pixe-plugin-info pixe-type-system" >
                <div class="pixe-head" >default_socket_timeout</div>
                <div class="pixe-tail" ><?php
                    echo esc_html(ini_get('default_socket_timeout'));
                ?></div>
            </div>
        <?php } ?>
        <div class="pixe-plugin-info pixe-type-system" >
            <div class="pixe-head" >suhosin</div>
            <div class="pixe-tail" ><?php
                if( extension_loaded( 'suhosin' ) ){
                    echo esc_html__('On', 'pixebuilder');
                }else{
                    echo esc_html__('Off', 'pixebuilder');
                }
            ?></div>
        </div>
    </div>

    <div class="pixe-detail" >
        <svg width="13" height="13" viewBox="0 0 13 13" fill="none" ><path d="M6.41667 0L8.06667 4.76667L12.8333 6.41667L8.06667 8.06667L6.41667 12.8333L4.76667 8.06667L0 6.41667L4.76667 4.76667L6.41667 0Z" fill="#1F1BA5"/></svg>
        <?php
            echo wp_kses(
                __('For smooth page/block import, set <strong>max_execution_time</strong> to at least 300s and <strong>memory_limit</strong> to at least 512MB.', 'pixebuilder'),
                array(
                    'strong' => array(),
                )
            );
        ?>
    </div>
</div>
<?php
    }
