<?php
// Copyright 1999-2023. Plesk International GmbH. All rights reserved.
?>
<div class='form-row'>
    <div class='single-row'>
        <?php echo $this->content ?>
        <?php if ($this->fpmDedicatedHandlerHint) : ?>
            <span class='hint' style="display:none" id="fpmDedicatedHandlerHint">
                <i class="icon-attention"></i> <?php echo $this->fpmDedicatedHandlerHint ?>
            </span>
        <?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() {
        const phpHandlerId = '<?= $this->phpHandlerId ?>';
        if (!phpHandlerId) {
            return;
        }
        const phpHandler = document.getElementById(phpHandlerId);
        if (!phpHandler) {
            return;
        }

        phpHandler.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 ($this->fpmDedicatedHandlerHint) : ?>

            var phpHandlerElements = document.querySelectorAll(`[id^=${phpHandlerId}]`);
            phpHandlerElements.forEach(function (el) {
                el.addEventListener('change', function () {
                    const phpHandlerType = document.getElementById(phpHandlerId + 'Id' + phpHandler.value);
                    <?php if ($this->fpmDedicatedHandlerHint) : ?>
                    document.getElementById('fpmDedicatedHandlerHint').style.display = phpHandlerType.value.includes('fpm-dedicated') ? '' : 'none';
                    <?php endif; ?>
                });
            });
        <?php endif; ?>
    });
//]]>
</script>
