<?php
// Copyright 1999-2021. Plesk International GmbH. All rights reserved.
?>
<script type="text/javascript">
//<![CDATA[

Jsw.namespace('Smb.Views.Web');

Smb.Views.Web.showHostingWarning = function (formId) {
    if (!document.getElementById('hostingSettings-root') || (document.getElementById('hosting').value !== 'forwarding' && document.getElementById('hosting').value !== 'none')) {
        return true;
    }

    if (!Smb.Views.Web.warning) {
        Smb.Views.Web.warning = Jsw.messageBox.show({
            type: Jsw.messageBox.TYPE_YESNO,
            subtype: Jsw.messageBox.SUBTYPE_DELETE,
            isWebspace: <?php if ($this->isWebspace): ?>true<?php else: ?>false<?php endif ?>,
            text: <?php echo $this->jsEscape($this->form->lmsg('removeWebsiteHostingTitle')) ?>,
            description: <?php echo $this->jsEscape($this->form->lmsg('removeWebsiteHostingDescription')) ?>,
            successDescription: <?php echo $this->jsEscape($this->form->lmsg('removeWebsiteHostingDescriptionFolders')) ?>,
            failureDescription: <?php echo $this->jsEscape($this->form->lmsg('removeWebsiteHostingDescriptionAllFolders')) ?>,
            requestUrl: '/file-manager/list-data' +
                '?currentDir=' + encodeURIComponent('/') +
                '&domainId=' + <?=$this->jsEscape($this->domainId)?>,
            onYesClick: function () {
                Smb.Views.Web.showHostingWarning = function () {
                    return true;
                };
                var form = document.getElementById(formId);
                Jsw.submit(form);
            },
        });
    }
    Smb.Views.Web.warning.show();

    return false;
};

Jsw.onReady(function () {

    new Jsw.Panel({
        cls: 'tools-box',
        renderTo: 'main',
        renderMode: 'top',
        items: [{
            componentType: 'Jsw.Hint',
            hint: '<?php echo Plesk_Base_Utils_String::safeForJs($this->form->lmsg('formGeneralHint')); ?>'
        }]
    });

    // show/hide primitives
    function hideHosting() {
        ['ipSelector-ipv4', 'ipSelector-ipv6', 'root', 'userName', 'password', 'passwordConfirmation'].forEach(function (elementSuffix) {
            if (document.getElementById('hostingSettings-' + elementSuffix)) {
                document.getElementById('hostingSettings-' + elementSuffix).disabled = true;
            }
        });
        document.getElementById('hostingSettings').style.display = 'none';
    }

    function showHosting() {
        ['ipSelector-ipv4', 'ipSelector-ipv6', 'root', 'userName', 'password', 'passwordConfirmation'].forEach(function (elementSuffix) {
            if (document.getElementById('hostingSettings-' + elementSuffix)) {
                document.getElementById('hostingSettings-' + elementSuffix).disabled = false;
            }
        });
        document.getElementById('hostingSettings').style.display = '';
    }

    function hideForwarding() {
        ['ipSelector-ipv4', 'ipSelector-ipv6', 'forwardTo'].forEach(function (elementSuffix) {
            if (document.getElementById('forwardingSettings-' + elementSuffix)) {
                document.getElementById('forwardingSettings-' + elementSuffix).disabled = true;
            }
        });
        document.getElementById('forwardingSettings').style.display = 'none';
    }

    function showForwarding() {
        ['ipSelector-ipv4', 'ipSelector-ipv6', 'forwardTo'].forEach(function (elementSuffix) {
            if (document.getElementById('forwardingSettings-' + elementSuffix)) {
                document.getElementById('forwardingSettings-' + elementSuffix).disabled = false;
            }
        });
        document.getElementById('forwardingSettings').style.display = '';
    }

    // handlers
    function hostingActive() {
        hideForwarding();
        showHosting();
    }

    function forwardingActive() {
        hideHosting();
        showForwarding();
    }

    function noneActive() {
        hideHosting();
        hideForwarding();
    }

    function onHostingTypeChange() {
        switch (document.getElementById('hosting').value) {
            case 'hosting':
                hostingActive();
                break;
            case 'forwarding':
                forwardingActive();
                break;
            case 'none':
                noneActive();
                break;
        }
    }

    document.getElementById('hosting').addEventListener('change', onHostingTypeChange);
    onHostingTypeChange();
});

//]]>
</script>

<?php echo $this->form; ?>
