<?php
// Copyright 1999-2026. WebPros International GmbH. All rights reserved.
?>
<?php
$selectElement = $this->element->getElement($this->select);
$customElement = $this->element->getElement($this->custom);

$selectElement->setAttrib('aria-describedby', $selectElement->getId() . '-hint ' . $selectElement->getId() . '-error');
$customElement->setAttrib('aria-describedby', $customElement->getId() . '-hint ' . $customElement->getId() . '-error');
?>
<div id='<?php echo $selectElement->getId(); ?>-form-row' class='form-row'>
    <label class='field-name' for='<?php echo $selectElement->getId(); ?>'>
        <?php echo $selectElement->getLabel(); ?>
        <?= $this->requiredMark($selectElement->isRequired()) ?>
    </label>
    <div class='field-value'>
        <span>
            <?php
            echo $this->elementPart($selectElement)->content() . '&nbsp;' .
                $this->elementPart($customElement)->content();
            ?>
        </span>
        <span id="<?php echo $selectElement->getId(); ?>-error" class="field-errors" role="alert" aria-live="polite" style="display: none;"></span>
        <?php if ($selectElement->getDescription()): ?>
            <span id="<?php echo $selectElement->getId(); ?>-hint" class="hint"><?php echo $selectElement->getDescription(); ?></span>
        <?php endif; ?>
        <?php if ($customElement->getDescription()): ?>
            <span id='<?php echo $customElement->getId(); ?>-hint' class="hint" style="display: none;"><?php echo $customElement->getDescription(); ?></span>
        <?php endif; ?>
    </div>
</div>
<script type="text/javascript">
//<![CDATA[
Jsw.onReady(function () {
    var updateState = function () {
        var customId = '<?php echo $customElement->getId(); ?>';
        var customInput = document.getElementById(customId);
        var customHint = document.getElementById(customId + '-hint');
        var isCustomSelected = $F('<?php echo $selectElement->getId(); ?>') === 'custom';

        customInput.disabled = !isCustomSelected;
        customInput.setAttribute('aria-disabled', !isCustomSelected);

        var selectedOption = document.getElementById('<?php echo $selectElement->getId(); ?>').querySelector('[selected]');
        var showHint = selectedOption && $F('<?php echo $selectElement->getId(); ?>') !== selectedOption.value;

        customHint.style.display = showHint ? '' : 'none';
        customHint.setAttribute('aria-hidden', !showHint);
    }

    updateState();
    document.getElementById('<?php echo $selectElement->getId(); ?>').addEventListener('change', updateState);
});
//]]>
</script>
