const validerEntete = (path, arrayData = []) => {
    activeLoader();

    $.ajax({
        type: 'POST',
        url: path,
        data: arrayData,
        success: (data) => {
            window.location = data.redirectRoute
        },
        error: (data) => {
            const response = data.responseJSON;

            activePopup(response.popupTitle, response.popupHtml, 'modal_small');
            document.querySelector('[data-popupwarning-close]').addEventListener('click',  () => {
                desactivePopup();
            });

            if (response.type === 'nonBloquant') {
                document.querySelector('[data-popupwarning-ignore]').addEventListener('click', () => {
                    validerEntete(path, [{ name: 'confirm', value: true }]);
                    desactivePopup();
                });
            }

            desactiveLoader();
        }
    });
}

const popupConfirmValiderEntete = (popupTitle, popupHtml, path, arrayData = []) => {
    activePopup(
        popupTitle,
        `<div class="modal-body bs-row flex-y-center">
            <div class="icone-modal interrogation"></div>
            <div class="bs-col p-base">${popupHtml}</div>
        </div>
        <div class="modal-footer">
            <button type="button" class="button secondaire" 
                data-dismiss="modal" 
                onclick="desactivePopup();"
            >${window.oxaproc.trad['Annuler']}</button>
            <button type="button" class="button principal" data-popupconfirm-btnconfirm>
                ${window.oxaproc.trad['Confirmer']}
            </button>
        </div>`,
        'modal_small'
    );

    document.querySelector('[data-popupconfirm-btnconfirm]').addEventListener('click',  () => {
        validerEntete(path, arrayData);
    });
}

const onSubmitFormSuccessWithValidation = (data) => {
    if (data.canValidate) {
        activePopup(
            data.popupTitle,
            `<div class="modal-body bs-row flex-y-center">
                    <div class="icone-modal interrogation"></div>
                    <div class="bs-col p-base">${data.popupTextContent}</div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="button secondaire"
                        data-dismiss="modal"
                        onclick="desactivePopup();"
                    >${window.oxaproc.trad['Annuler']}</button>
                    <button type="button" class="button principal" data-popupconfirm-btnconfirm>
                        ${window.oxaproc.trad['Confirmer']}
                    </button>
                </div>`,
            'modal_small'
        );

        document.querySelector('[data-popupconfirm-btnconfirm]').addEventListener('click', () => {
            validerEntete(data.validatePath);
            desactivePopup();
        });
        if (data.redirectRoute) {
            document.querySelector('[data-dismiss]').addEventListener('click', () => {
                window.location = data.redirectRoute;
            });
        }
    } else {
        window.location = data.redirectRoute
    }

    desactiveLoader();
}