<?php
// Copyright 1999-2023. Plesk International GmbH. All rights reserved.
?>
<?php $prefixElement = $this->element->getElement('installationPrefix'); ?>
<div class='form-row<?php echo $prefixElement && $prefixElement->hasErrors() ? ' error' : ''?>'>
    <div class='field-name'>
        <?php echo $this->elementPart($this->element->getElement('domain'))->label(); ?>
    </div>
    <div class='field-value'>
        <span>
            <?php echo $this->elementPart($this->element->getElement('protocol'))->content(); ?>
            <span></span>
            ://
            <?php echo $this->elementPart($this->element->getElement('domain'))->content(); ?>
            <?php
                if ($prefixElement) {
                    echo ' / ';
                    echo $this->elementPart($prefixElement)->content();
                }
            ?>
            <span class="ajax-loading" style="display: none;"><?php echo $this->lmsg('loading');?></span>
            <span class="hint"><?php echo $this->lmsg('httpsContentHint');?></span>
        </span>
        <span id="object-switcher"></span>
        <?php if ($prefixElement && $prefixElement->hasErrors()): ?>
            <span class='field-errors'><?php echo $this->elementPart($prefixElement)->errors() ?></span>
        <?php else: ?>
            <span class='field-errors' style="display: none;"></span>
        <?php endif; ?>
    </div>
</div>
<script type="text/javascript">
//<![CDATA[
(function () {
    var contextsOptions = <?php echo json_encode($this->contextsOptions) ?>;
    var protocolSelect = document.getElementById('<?php echo $this->element->getElement('protocol')->getId(); ?>');
    var domainSelect = document.getElementById('<?php echo $this->element->getElement('domain')->getId(); ?>');

    var onContextChange = function (doNotChangePrefix) {
        if ('select' === domainSelect.tagName.toLowerCase()) {
            document.getElementById('<?php echo $this->contextIdElement->getId(); ?>').value = domainSelect.value;
        } else if ('select' === protocolSelect.tagName.toLowerCase()) {
            document.getElementById('<?php echo $this->contextIdElement->getId(); ?>').value = Object.keys(contextsOptions[protocolSelect.value])[0];
        }
        var value = document.getElementById('<?php echo $this->contextIdElement->getId();?>').value.split("-");
        if ('select' === protocolSelect.tagName.toLowerCase()) {
            var hideSelector = Object.keys(contextsOptions).every(function (protocol) {
                return protocol === protocolSelect.value || 'undefined' === typeof contextsOptions[protocol][value[0] + '-' + value[1] + '-' + protocol];
            });

            if (hideSelector) {
                protocolSelect.style.display = 'none';
                protocolSelect.nextElementSibling.innerHTML = protocolSelect.value;
            } else {
                protocolSelect.style.display = '';
                protocolSelect.nextElementSibling.innerHTML = '';
            }
        }
        if (true === doNotChangePrefix) {
            return;
        }
<?php if ($prefixElement): ?>
        new Ajax.Request(Jsw.prepareUrl('/app/get-install-prefix/domainId/' + value[1] + '/metaId/' + '<?php echo $this->metaId; ?>'), {
            method: 'get',
            onCreate: function () {
                document.querySelector('.ajax-loading').style.display = '';
                var sendButtonWrapper = document.getElementById('btn-send');
                if (sendButtonWrapper) {
                    sendButtonWrapper.disabled = true;
                    sendButtonWrapper.classList.add('disabled');
                }
            },
            onSuccess: function (response) {
                document.querySelector('.ajax-loading').style.display = 'none';
                var sendButtonWrapper = document.getElementById('btn-send');
                if (sendButtonWrapper) {
                    sendButtonWrapper.disabled = false;
                    sendButtonWrapper.classList.remove('disabled');
                }

                var response = JSON.parse(response.responseText);
                if (response.redirect) {
                    Jsw.redirect(response.redirect);
                    return;
                }
                var prefix = (response.prefix) ? response.prefix : '';
                document.getElementById('<?php echo $this->element->getElement('installationPrefix')->getId();?>').value = prefix;
            }
        });
<?php endif; ?>
    };

    var onProtocolChange = function () {
        if ('select' === domainSelect.tagName.toLowerCase()) {
            var newOptions = contextsOptions[protocolSelect.value];
            var currentValue = document.getElementById('<?php echo $this->contextIdElement->getId();?>').value.split("-");

            domainSelect.innerHTML = Object.keys(newOptions)
                .map(contextId => '<option value="' + contextId + '">' + newOptions[contextId] + '</option>')
                .join('');

            var newValue = currentValue[0] + '-' + currentValue[1] + '-' + protocolSelect.value;
            if ('undefined' !== typeof newOptions[newValue]) {
                domainSelect.value = newValue;
            }
        }
        onContextChange(true);
    };

    protocolSelect.addEventListener('change', onProtocolChange);
    domainSelect.addEventListener('change', onContextChange);
    onContextChange(true);
})();
//]]>
</script>
