<?php
// Copyright 1999-2022. Plesk International GmbH. All rights reserved.
?>
<div class='form-row'>
    <div class="field-name">
        <label for="<?php echo $this->element->getId(); ?>">
            <?php echo $this->element->getLabel(); ?>
            <?= $this->requiredMark(!$this->readOnly) ?>
        </label>
    </div>

    <div class='field-value'>
        <span>
            <img src="<?php echo $this->skinUrl('/icons/16/plesk/website.png'); ?>"/>
            /
            <?php if ($this->readOnly): ?>
                <?php echo $this->escape($this->element->getValue()); ?>
                <?php if ($this->readOnlyHint): ?>
                    <?php echo $this->elementPart($this->element)->description(); ?>
                <?php endif; ?>
            <?php else: ?>
                <?php echo $this->elementPart($this->element)->content(); ?>
                <?php echo $this->elementPart($this->element)->description(); ?>
            <?php endif; ?>
        </span>
        <span class="field-errors" style="display: none;"></span>
    </div>
</div>

<script type="text/javascript">
//<![CDATA[
(() => {
    const getDocRootPath = domainName => /^[a-zA-Z0-9\.\-\*]+$/.match(domainName) ? domainName.replace('*', '_') : '';

    const generateDocRootPath = e => {
        const element = document.getElementById('hostingSettings-root');
        const docrootPattern = document.getElementById('hostingSettings-rootPattern').value;
        let docroot = getDocRootPath(e.detail);

        if (-1 !== docrootPattern.indexOf('<domain>')) {
            docroot = docrootPattern.replace('<domain>', docroot);
        }

        element.value = docroot;
        Jsw.fireCustomEvent(element, 'value:change');
    };

    const docrootEl = document.getElementById('<?= $this->element->getId() ?>');
    if (docrootEl && !docrootEl.value) {
        const formEl = docrootEl.closest('form');
        formEl.addEventListener('domainNameChange', generateDocRootPath);
        ['change', 'keyup'].forEach(eventName => {
            docrootEl.addEventListener(eventName, () => {
                formEl.removeEventListener('domainNameChange', generateDocRootPath);
            });
        });
    }
})();
//]]>
</script>
