<?php

if (!defined('ABSPATH')) {
    exit; // Pas d'accès direct !
}
if (!class_exists('hpixl_securicheck_screen_reglages')) {

    class hpixl_securicheck_ScreenReglages
    {

        public function __construct($id = null) {}

        function render_screen()
        {
?>
            <?php settings_errors(); ?>
            <div class='wrap'>
                <h1 style="display:none;"></h1>
                <div class="hpixl-securicheck-admin-audit-header">
                    <div class="hpixl-securicheck-admin-audit-header-panneau-logo">
                        <img src="<?php echo esc_html(HPIXL_SECURICHECK_PLUGIN_URL . "images/securicheck.png"); ?>" alt="logo hpixl-securicheck" />
                        <h1><?php esc_html_e('Securicheck - RÉGLAGES', 'securicheck'); ?></h1>
                    </div>
                    <span>V<?php echo esc_html(HPIXL_SECURICHECK_PLUGIN_VERSION); ?></span>
                </div>

                <h2 class="nav-tab-wrapper">
                    <a href="?page=<?php echo esc_html(HPIXL_SECURICHECK_SETTINGS_SLUG . '-reglages'); ?>" class="nav-tab nav-tab-active"><?php esc_html_e('Fonctionnalités', 'securicheck'); ?></a>
                    <div class="hpixl-securicheck-admin-reglages-boutons-export">
                        <form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>">
                            <?php wp_nonce_field('hpixl_securicheck_export_reglages', 'hpixl_securicheck_export_reglages_nonce'); ?>
                            <input type="hidden" name="action" value="hpixl_securicheck_export_reglages_json">
                            <button type="submit">
                                <img src="<?php echo esc_url(HPIXL_SECURICHECK_PLUGIN_URL); ?>images/json.png" alt="exporter en json" />
                            </button>
                        </form>
                    </div>
                </h2>



                <div class='hpixl-securicheck-settings-form-panel'>
                    <form action='options.php' method='POST'>
                        <?php
                        $active_tab = isset($_GET['tab']) ? sanitize_text_field($_GET['tab']) : 'fonctionnalites';
                        submit_button();
                        if ($active_tab == 'fonctionnalites') {
                            settings_fields('hpixl_securicheck_settings_fonctionnalites_groupe');
                            do_settings_sections('hpixl_securicheck_settings_fonctionnalites_groupe');
                            hpixl_securicheck_render_reglages_fonctionnalites();
                        }
                        ?>
                    </form>
                </div>

            </div>
<?php
        }
    }

    //Ajout d'un settings pour notre plugin 
    add_action('admin_init', 'hpixl_securicheck_settings');

    /**
     * Enregistre le setting
     */
    function hpixl_securicheck_settings()
    {
        register_setting('hpixl_securicheck_settings_fonctionnalites_groupe', 'hpixl_securicheck_hotlinking_image_url');
        register_setting('hpixl_securicheck_settings_fonctionnalites_groupe', 'hpixl_securicheck_destinataire_email_notifications');
        register_setting('hpixl_securicheck_settings_fonctionnalites_groupe', 'hpixl_securicheck_toggle_notifications');
        register_setting('hpixl_securicheck_settings_fonctionnalites_groupe', 'hpixl_securicheck_toggle_notifications_scan');
        register_setting('hpixl_securicheck_settings_fonctionnalites_groupe', 'hpixl_securicheck_toggle_notifications_compte_admin');
        register_setting('hpixl_securicheck_settings_fonctionnalites_groupe', 'hpixl_securicheck_toggle_notifications_ip_bloquee');
        register_setting('hpixl_securicheck_settings_fonctionnalites_groupe', 'hpixl_securicheck_toggle_notifications_ip_debloquee');
        register_setting('hpixl_securicheck_settings_fonctionnalites_groupe', 'hpixl_securicheck_toggle_limite_nombre_audit');
        register_setting('hpixl_securicheck_settings_fonctionnalites_groupe', 'hpixl_securicheck_text_limite_nombre_audit');
        register_setting('hpixl_securicheck_settings_fonctionnalites_groupe', 'hpixl_securicheck_toggle_delete_audits_after_uninstall');
        register_setting('hpixl_securicheck_settings_fonctionnalites_groupe', 'hpixl_securicheck_toggle_delete_reglages_after_uninstall');

        //valeurs par défaut
        add_option('hpixl_securicheck_hotlinking_image_url', '');
        add_option('hpixl_securicheck_destinataire_email_notifications', get_option('admin_email'));
        add_option('hpixl_securicheck_toggle_notifications', '0');
        add_option('hpixl_securicheck_toggle_notifications_scan', '0');
        add_option('hpixl_securicheck_toggle_notifications_compte_admin', '0');
        add_option('hpixl_securicheck_toggle_notifications_ip_bloquee', '1');
        add_option('hpixl_securicheck_toggle_notifications_ip_debloquee', '1');
        add_option('hpixl_securicheck_toggle_limite_nombre_audit', '0');
        add_option('hpixl_securicheck_text_limite_nombre_audit', '');
        add_option('hpixl_securicheck_toggle_delete_audits_after_uninstall', '0');
        add_option('hpixl_securicheck_toggle_delete_reglages_after_uninstall', '0');
    }

    function hpixl_securicheck_render_reglages_fonctionnalites()
    {
        require plugin_dir_path(dirname(__FILE__)) . 'admin/Screen_reglages_onglet_fonctionnalites.php';
    }

    function hpixl_securicheck_render_page_securite_onglet_connexion()
    {
        require plugin_dir_path(dirname(__FILE__)) . 'admin/Screen_protection_login_onglet_url_connexion.php';
    }

    function hpixl_securicheck_render_page_securite_onglet_brute_force()
    {
        require plugin_dir_path(dirname(__FILE__)) . 'admin/Screen_protection_login_onglet_brute_force.php';
    }
}
