<?php
// Copyright 1999-2022. Plesk International GmbH. All rights reserved.
?>
<?php
echo $this->partial('partials/forms/radio-controls.phtml', [
    'radio' => $this->radio,
    'indentControls' => $this->indentControls,
    'element' => $this->element,
    'description' => $this->description,
]);
$predefinedRadioElementId = $this->element->getElement($this->radio)->getId() . '-predefined';
$predefinedInputElementId = isset($this->indentControls['predefined']) ?
    $this->element->getElement($this->indentControls['predefined'])->getId() : null;
$customRadioElementId = $this->element->getElement($this->radio)->getId() . '-custom';
$customInputElementId = $this->element->getElement($this->indentControls['custom'])->getId();
?>
<script type="text/javascript">
//<![CDATA[
(function () {
    var updateControlsState = function() {
        if (document.getElementById('<?php echo $predefinedRadioElementId; ?>').checked) {
            <?php if ($predefinedInputElementId): ?>
                document.getElementById('<?php echo $predefinedInputElementId; ?>').disabled = false;
            <?php endif; ?>
            document.getElementById('<?php echo $customInputElementId; ?>').disabled = true;
        }
        if (document.getElementById('<?php echo $customRadioElementId; ?>').checked) {
            <?php if ($predefinedInputElementId): ?>
                document.getElementById('<?php echo $predefinedInputElementId; ?>').disabled = true;
            <?php endif; ?>
            document.getElementById('<?php echo $customInputElementId; ?>').disabled = false;
        }
    };

    document.getElementById('<?php echo $predefinedRadioElementId; ?>').addEventListener('click', updateControlsState);
    document.getElementById('<?php echo $predefinedRadioElementId; ?>').updateControlsState = updateControlsState;
    document.getElementById('<?php echo $customRadioElementId; ?>').addEventListener('click', updateControlsState);

    if (!document.getElementById('<?php echo $predefinedRadioElementId; ?>').disabled) {
        updateControlsState();
    }
})();
//]]>
</script>
