<?php
// Copyright 1999-2021. Plesk International GmbH. All rights reserved.
if ($this->element instanceof Zend_Form_DisplayGroup) {
    $typeElement = $this->element->getElement($this->radio);
    $indentControls = $this->indentControls;
} else {
    $typeElement = $this->element;
    $indentControls = array();
}
$multiOptions = array();
$errorStyle = $typeElement->hasErrors() ? '' : ' style="display:none;"';
$errorsPreformatted = $typeElement->getAttrib('errorsPreformatted');
$error = $typeElement->hasErrors() ? ' error' : '';
?>
<div class="form-row<?php echo $error; ?>" id="<?php echo $this->id ? $this->id : $typeElement->getId() . '-form-row'; ?>">

    <div class="field-name">
        <label for="<?php echo $typeElement->getId(); ?>">
            <?php echo $typeElement->getLabel(); ?>
            <?= $this->requiredMark($typeElement->isRequired()) ?>
        </label>
        <?php if (isset($this->labelBadgeNew) && $this->labelBadgeNew): ?>
            <span class="badge-new"><?php echo $this->lmsg('components.buttons.badgeNew') ?></span>
        <?php endif; ?>
    </div>

    <div class="field-value">
        <div class="text-value">
            <?php foreach ($typeElement->getMultiOptions() as $optionName => $optionTitle): ?>

                <div class="indent-box">
                    <input type="radio"
                        value="<?php echo $optionName; ?>"
                        id="<?php echo $typeElement->getId() . "-$optionName"; ?>"
                        class="radio"
                        name="<?php
                            echo $typeElement->getBelongsTo()
                                ? $typeElement->getBelongsTo() . '[' . $typeElement->getName() . ']'
                                : $typeElement->getName();
                        ?>"
                        <?php echo ($optionName == $typeElement->getValue()) ? 'checked="checked"' : ''; ?>
                        <?php echo ($typeElement->disable === true
                            || (is_array($typeElement->disable) && in_array($optionName, $typeElement->disable)))
                                ? 'disabled="disabled"' : ''; ?>
                    />
                    <div class="indent-box-content">
                        <label for="<?php echo $typeElement->getId() . "-$optionName"; ?>">
                            <?php echo false === $typeElement->escape ? $optionTitle : $this->escape($optionTitle); ?>
                        </label>
                        <?php if (isset($this->optionBadgeNew) && in_array($optionName, $this->optionBadgeNew)): ?>
                            <span class="badge-new"><?php echo $this->lmsg('components.buttons.badgeNew') ?></span>
                        <?php endif ?>
                        <?php if ('' != $typeElement->getDescriptionValue($optionName)): ?>
                            <p class="hint"><?php echo $typeElement->getDescriptionValue($optionName); ?></p>
                        <?php endif; ?>
                        <?php
                            $typeElementId = $typeElement->getId() . "-$optionName";
                            if (isset($indentControls[$optionName])) {
                                $controls = $indentControls[$optionName];
                                foreach ((array)$controls as $ctrl) {
                                    $optionElement = $ctrl instanceof Zend_Form_Element
                                        ? $ctrl
                                        : $this->element->getElement($ctrl);
                                    echo $optionElement->render($this);
                                    $multiOptions[$typeElementId][] = $optionElement->getId();
                                }
                            } else {
                                $multiOptions[$typeElementId] = null;
                            }
                        ?>
                    </div>
                </div>
            <?php endforeach;?>
            <?php echo $this->elementPart($typeElement)->description(); ?>
        </div>
        <span class="field-errors<?=$errorsPreformatted ? ' preformatted' : ''?>"<?php echo $errorStyle; ?>><?php echo $this->elementPart($typeElement)->errors(); ?></span>
    </div>
</div>
<?php if ($this->disableIndentControls || $this->hideIndentControls): ?>
<script type="text/javascript">
//<![CDATA[
(function () {
    var multiOptions = <?php echo Zend_Json::encode($multiOptions); ?>;
    var updateControlsState = function () {
        Object.keys(multiOptions).forEach(function (option) {
            if (!Array.isArray(multiOptions[option])) {
                return;
            }

            multiOptions[option].forEach(function (control) {
                <?php if ($this->disableIndentControls): ?>
                if (document.getElementById(option).checked) {
                    document.getElementById(control).disabled = false;
                } else {
                    document.getElementById(control).disabled = true;
                }
                <?php else: ?>
                document.getElementById(control).parentNode.style.display = document.getElementById(option).checked ? '' : 'none';
                <?php endif ?>
            });
        });
    };

    Object.keys(multiOptions).forEach(function (option) {
        document.getElementById(option).addEventListener('click', updateControlsState);
    });

    updateControlsState();
})();
//]]>
</script>
<?php endif;?>
