<?php
// Copyright 1999-2021. Plesk International GmbH. All rights reserved.
$typeElement = $this->element->getElement('type');
$indentControls = $this->indentControls;
?>
<div class="form-row">

  <div class="field-name">
    <label for="<?php echo $typeElement->getId(); ?>">
      <?php echo $typeElement->getLabel(); ?>
    </label>
  </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"' : ''; ?>
        />
        <div class="indent-box-content">
            <label for="<?php echo $typeElement->getId() . "-$optionName"; ?>">
              <?php echo $optionTitle; ?>
            </label>
            <?php if ('' != $typeElement->getDescriptionValue($optionName)): ?>
                <p class="hint"><?php echo $typeElement->getDescriptionValue($optionName); ?></p>
            <?php endif; ?>
            <?php if(isset($indentControls[$optionName])):?>
                  <?php
                  $controls = $indentControls[$optionName];
                  foreach((array)$controls as $ctrl) {
                      echo $this->element->getElement($ctrl)->render($this);
                  }
                  ?>
            <?php endif;?>
        </div>
      </div>
    <?php endforeach;?>
    </div>
  </div>
</div>

<script type="text/javascript">
//<![CDATA[
Jsw.onReady(function () {
    var updateDestinationProperties = function () {
        <?php foreach ($typeElement->getMultiOptions() as $optionName => $optionTitle): ?>
            <?php if (!isset($indentControls[$optionName])) continue;?>
            var checked = document.getElementById('<?php echo $typeElement->getId() . "-$optionName"; ?>').checked;
            document.getElementById('<?php echo $typeElement->getId() . "-$optionName"; ?>').closest('.indent-box').querySelector('.indent-box-content').querySelectorAll('select', 'input').forEach(function (element) {
                if (checked) {
                    element.disabled = false;
                } else {
                    element.disabled = true;
                }
            });
        <?php endforeach;?>
    }
    if (0 === document.getElementById('destination-site').querySelectorAll('option').length) {
        document.getElementById('destination-type-ftp').checked=true;
        document.getElementById('destination-type-site').disabled=true;
    }
    updateDestinationProperties();
    <?php foreach ($typeElement->getMultiOptions() as $optionName => $optionTitle): ?>
    document.getElementById('destination-type-<?php echo $optionName ?>').addEventListener('click', updateDestinationProperties);
    <?php endforeach;?>
});
//]]>
</script>
