/* disparition du loader de page quand la page est chargée*/
jQuery(window).load(function () {
    jQuery('.hpixl-securicheck-pre-con').fadeOut('slow');
});

function openOnglet(evt, onglet) {
    // Declare all variables
    var i, tabcontent, tablinks;

    // Get all elements with class="tabcontent" and hide them
    tabcontent = document.getElementsByClassName("hpixl-securicheck-admin-audit-tabcontent");

    if (!tabcontent)
        return;

    for (i = 0; i < tabcontent.length; i++) {
        tabcontent[i].style.display = "none";
    }

    // Get all elements with class="tablinks" and remove the class "active"
    tablinks = document.getElementsByClassName("tablinks");
    for (i = 0; i < tablinks.length; i++) {
        tablinks[i].className = tablinks[i].className.replace(" active", "");
    }

    // Show the current tab, and add an "active" class to the link that opened the tab
    document.getElementById(onglet).style.display = "block";
    evt.currentTarget.className += " active";
}

/** 
 * permet de lancer l'animation sur le bouton aprés lancement de l'audit 
 * permet de ne plus pouvoir cliquer sur le bouton quand on lance un audit
 * evite d'en lancer 2 en paralelle
 * */
function lancerAudit(evt) {
    // si on clique sur le bouton de lancement d'un audit
    const btn = document.getElementsByName('btn-submit-audit')
    btn[0].classList.add('activeLoading');
    btn[0].style.cursor = 'progress';
    btn[0].style.pointerEvents = "none";
}

var pageConnexion = document.getElementById("hpixl_securicheck_toggle_page_connexion_url");
if (pageConnexion) {
    pageConnexion.addEventListener("change", function () {
        var checkbox = this;
        var textInputContainer = document.getElementById("hpixl_securicheck_panneau_page_connexion_url");
        var panneau_redirection_url_backoffice = document.getElementById("hpixl_securicheck_panneau_redirection_url_backoffice");
        if (checkbox.checked) {
            textInputContainer.style.display = "flex";
            panneau_redirection_url_backoffice.style.display = "table-row";
        } else {
            textInputContainer.style.display = "none";
            panneau_redirection_url_backoffice.style.display = "none";

        }
    });
}

var pageConnexionRedirection = document.getElementById("hpixl_securicheck_toggle_page_connexion_redirection");
if (pageConnexionRedirection) {
    pageConnexionRedirection.addEventListener("change", function () {
        var checkbox = this;
        var textInputContainer = document.getElementById("hpixl_securicheck_panneau_page_connexion_redirection");
        if (checkbox.checked) {
            textInputContainer.style.display = "flex";
        } else {
            textInputContainer.style.display = "none";
        }
    });
}

/*var notifications = document.getElementById("hpixl_securicheck_toggle_notifications");
if (notifications) {
    notifications.addEventListener("change", function () {
        var checkbox = this;
        var textInputContainer = document.getElementById("hpixl_securicheck_panneau_notifications");
        if (checkbox.checked) {
            textInputContainer.style.display = "flex";
        } else {
            textInputContainer.style.display = "none";
        }
    });
}*/

var limiteaudit = document.getElementById("hpixl_securicheck_toggle_limite_nombre_audit");
if (limiteaudit) {
    limiteaudit.addEventListener("change", function () {
        var checkbox = this;
        var textInputContainer = document.getElementById("hpixl_securicheck_panneau_limitations");
        if (checkbox.checked) {
            textInputContainer.style.display = "flex";
        } else {
            textInputContainer.style.display = "none";
        }
    });
}


var bruteForce = document.getElementById("hpixl_securicheck_toggle_brute_force");
if (bruteForce) {
    bruteForce.addEventListener("change", function () {
        var checkbox = this;
        var textInputContainer = document.getElementById("hpixl_securicheck_panneau_brute_force_protection");
        if (checkbox.checked) {
            textInputContainer.style.display = "flex";
        } else {
            textInputContainer.style.display = "none";
        }
    });
}

/**
 * Securicheck – Promo banner dismiss
 */
document.addEventListener('DOMContentLoaded', function () {

    const banner = document.querySelector('.hpixl-securicheck-promo-banner');
    if (!banner) {
        return;
    }

    const dismissBtn = banner.querySelector('.hpixl-securicheck-promo-dismiss');
    if (!dismissBtn) {
        return;
    }

    dismissBtn.addEventListener('click', function (e) {
        e.preventDefault();

        // Animation de fermeture
        banner.style.transition = 'opacity 0.3s ease, transform 0.3s ease';
        banner.style.opacity = '0';
        banner.style.transform = 'translateY(-20px)';

        setTimeout(function () {
            banner.remove();
        }, 300);

        // Sécurité : ajaxurl toujours dispo en admin
        if (typeof ajaxurl === 'undefined') {
            return;
        }

        // Appel AJAX
        fetch(ajaxurl, {
            method: 'POST',
            credentials: 'same-origin',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
            },
            body: new URLSearchParams({
                action: 'securicheck_hide_promo_banner',
                nonce: banner.dataset.nonce || ''
            })
        });
    });
});
