<?php
// Copyright 1999-2021. Plesk International GmbH. All rights reserved.
?>
<div class='form-row'>
    <div class='single-row'>
        <?php echo $this->content ?>
        <?php if (isset($this->phpInfoUrl) && '' != $this->phpInfoUrl): ?>
            <a class="i-link" href="<?php echo $this->phpInfoUrl ?>" target="_blank" id="show-php-info"><i class="icon-info"></i> <span><?php echo $this->lmsg('components.forms.php-settings.showPhpInfo') ?></span></a>
        <?php endif; ?>
        <?php if ($this->handlerChangeHint) : ?>
            <span class='hint' style="display:none" id="phpHandlerChangeHint">
                <i class="icon-warning"></i> <?php echo $this->handlerChangeHint ?>
            </span>
        <?php endif; ?>
        <?php if (isset($this->hint)): ?>
            <span class='hint'><?php echo $this->hint ?></span>
        <?php endif; ?>
        <?php if (isset($this->helpLink)): ?>
            <a id='phpSettingsHelpLink' href="<?php echo isset($this->helpLink['href']) ? $this->baseUrl($this->helpLink['href']) : '#' ?>" <?php echo isset($this->helpLink['onclick']) ? "onclick='{$this->helpLink['onclick']}'" : '' ?> target='_blank'><?php echo $this->helpLink['title'] ?></a>
        <?php endif; ?>
        <span class='field-errors'></span>
    </div>
</div>
<script type='text/javascript'>
//<![CDATA[
    function getCurrentPhpHandlerType() {
        var phpHandlerTypeIds = <?php echo json_encode($this->phpHandlerTypeIds) ?>;
        for (var i = 0; i < phpHandlerTypeIds.length; i++) {
            var phpHandlerTypeSelect = document.getElementById(phpHandlerTypeIds[i]);
            if (phpHandlerTypeSelect.style.display !== 'none') {
                return phpHandlerTypeSelect.options[phpHandlerTypeSelect.selectedIndex].text;
            }
        }
        return false;
    }
    function onAfterPhpHandlerChange() {
        var phpHandlerTypeIds = <?php echo json_encode($this->phpHandlerTypeIds) ?>;
        for (var i = 0; i < phpHandlerTypeIds.length; i++) {
            document.getElementById(phpHandlerTypeIds[i]).style.display = 'none';
        }
        var phpHandler = document.getElementById('<?php echo $this->phpHandlerId ?>').value;
        document.getElementById('<?php echo $this->phpHandlerId ?>').style.display = '';
        document.getElementById('<?php echo $this->phpHandlerId ?>Id' + phpHandler).style.display = '';
    }
    Jsw.onReady(function() {
        if (!'<?php echo $this->phpHandlerId ?>' || !document.getElementById('<?php echo $this->phpHandlerId ?>')) {
            return;
        }
        document.getElementById('<?php echo $this->phpHandlerId ?>').addEventListener('change', function (e) {
            var currentHandlerType = getCurrentPhpHandlerType();
            onAfterPhpHandlerChange();
            if (!currentHandlerType) {
                return;
            }
            var phpHandler = document.getElementById('<?php echo $this->phpHandlerId ?>').value;
            var handlerTypeSelect = document.getElementById('<?php echo $this->phpHandlerId ?>Id' + phpHandler);
            for (var j = 0; j < handlerTypeSelect.options.length; j++) {
                if (currentHandlerType === handlerTypeSelect.options[j].text) {
                    handlerTypeSelect.selectedIndex = j;
                    break;
                }
            }
        });
        onAfterPhpHandlerChange();

        <?php if (isset($this->phpInfoUrl) && '' != $this->phpInfoUrl): ?>
        var formValues = new Jsw.form.Values({ selector: '*[name*=php],*[name*=mainSettings]' });
        var showButton = document.getElementById('show-php-info');
        if (showButton) {
            showButton.addEventListener('click', function (e){
                if (!formValues.isChanged()) {
                    return;
                }
                e.preventDefault();
                var linkElement = e.target.closest('a');
                new Jsw.Popup({
                    title: <?php echo $this->jsLmsg('components.forms.php-settings.showPhpInfoTitle') ?>,
                    content: <?php echo $this->jsLmsg('components.forms.php-settings.showPhpInfoDescription') ?>,
                    buttons: [
                        {
                            title: <?php echo $this->jsLmsg('components.buttons.apply') ?>,
                            handler: function (e2) {
                                Jsw.submit(linkElement, true);
                                e2.target.innerHTML = '<span class="wait">' + <?php echo $this->jsLmsg('components.buttons.wait') ?> + '</span>';
                            }
                        },
                        {
                            title: <?php echo $this->jsLmsg('components.forms.php-settings.showPhpInfoButton') ?>,
                            handler: function (e2, popup) {
                                Jsw.redirect(linkElement.getAttribute('href'), {}, '_blank');
                                popup._onCloseClick();
                            }
                        },
                        {
                            title: <?php echo $this->jsLmsg('components.buttons.cancel') ?>,
                            handler: function (e2, popup) {
                                popup._onCloseClick();
                            }
                        }
                    ]
                });
            });
        }
        <?php endif; ?>
    });
//]]>
</script>
