<?php
// Copyright 1999-2021. Plesk International GmbH. All rights reserved.
?>

<script type="text/javascript">
    //<![CDATA[
    Jsw.namespace('AdminPanel.WebAppFirewall');
    AdminPanel.WebAppFirewall.confirmation = function () {
        const data = <?php echo json_encode($this->confirmationData) ?>;
        const {currentWebServer, currentRuleSet, isCustomized} = data;
        const messages = [];
        let showResetBox = false;

        const webServerId = '<?php echo $this->form->getSubForm('ruleSets')->getElement('webServer')->getId() ?>';
        const webServer = document.getElementById(webServerId).value;
        if ('nginx' === webServer && currentWebServer !== webServer && isCustomized) {
            messages.push(<?php echo $this->jsLmsg("{$this->localeSection}.nginxIsIncompatible") ?>);
            showResetBox = true;
        }

        const crsRulesetId = '<?php echo $this->escape($this->form->getSubForm('ruleSets')->getElement('ruleSet')->getId() . '-crs') ?>';
        const crsRuleset = document.getElementById(crsRulesetId);
        if (crsRuleset && crsRuleset.checked && 'crs' !== currentRuleSet) {
            messages.push(<?php echo $this->jsLmsg("{$this->localeSection}.ruleSetCrsConfirmDescription") ?>);
        }

        if (0 === messages.length) {
            return true;
        }

        let description = messages[0];
        let descriptionWrapperTag = 'p';
        let descriptionDecorator = content => content;
        if (1 < messages.length) {
            description = messages.map(description => `<li>${description}</li>`).join('');
            descriptionWrapperTag = 'ul';
        }
        if (showResetBox) {
            const resetDescription = <?php echo $this->jsLmsg("{$this->localeSection}.nginxResetCustomizations") ?>;
            descriptionDecorator = (content) => {
                return content + (
                    '<div class="indent-box">' +
                        '<input type="checkbox" class="checkbox" id="resetBox" value="true" >' +
                            '<div class="indent-box-content">' +
                                `<label for="resetBox">${resetDescription}</label>` +
                            '</div>' +
                    '</div>'
                );
            };
        }

        Jsw.messageBox.show({
            type: Jsw.messageBox.TYPE_YESNO,
            subtype: 'confirm',
            text: <?php echo $this->jsLmsg("{$this->localeSection}.ruleSetCrsConfirmTitle") ?>,
            description: description,
            descriptionWrapperTag: descriptionWrapperTag,
            descriptionDecorator: descriptionDecorator,
            onYesClick: function () {
                const confirmation = AdminPanel.WebAppFirewall.confirmation;
                AdminPanel.WebAppFirewall.confirmation = function () {
                    AdminPanel.WebAppFirewall.confirmation = confirmation;
                    return true;
                };
                const form = document.getElementById('<?php echo $this->escape($this->form->getId()) ?>').closest('form');
                const resetBox = document.getElementById('resetBox');
                if (resetBox && resetBox.checked) {
                    const resetCustomizationsId = '<?php echo $this->form->getSubForm('ruleSets')->getElement('resetCustomizations')->getId() ?>';
                    document.getElementById(resetCustomizationsId).value = 'true';
                }
                Jsw.submit(form, form.noRedirect);
            },
            buttonTitles: {
                yes: <?php echo $this->jsLmsg('components.buttons.yes') ?>,
                no: <?php echo $this->jsLmsg('components.buttons.no') ?>
            },
        });
        return false;
    };
    //]]>
</script>
