<?php
// Copyright 1999-2025. WebPros International GmbH. All rights reserved.
?>
<?php
    echo $this->partial('partials/forms/radio-controls.phtml', [
        'id' => $this->id,
        'radio' => $this->radio,
        'indentControls' => $this->indentControls,
        'element' => $this->element,
        'hideIndentControls' => true,
    ]);
    $radioElementId = $this->element->getElement($this->radio)->getId();
?>
<script type="text/javascript">
//<![CDATA[
Jsw.onReady(function () {
    var supportedDatabases = <?php echo Zend_Json::encode($this->supportedDatabases) ?>;
    var supportedServers = <?php echo Zend_Json::encode($this->supportedServers) ?>;
    var databaseElement = <?php echo $this->databaseControl ? 'document.getElementById("' . $this->databaseControl->getId() . '")' : 'null' ?>;
    var serverElement = <?php echo $this->serverControl ? 'document.getElementById("' . $this->serverControl->getId() . '")' : 'null' ?>;
    var createUserElement = <?php echo $this->createUserControl ? 'document.getElementById("' . $this->createUserControl->getId() . '")' : 'null' ?>;
    var databaseServerId = <?php echo $this->databaseServerId ? $this->databaseServerId : 'null' ?>;

    var getServerId = function () {
        var serverId;
        if ('select' === serverElement.tagName.toLowerCase()) {
            serverId = serverElement.value;
        } else {
            serverId = serverElement.dataset.value;
        }
        return parseInt(serverId);
    };

    var updateControlsState = function () {
        var isSupported = false;
        var database = databaseElement ? parseInt(databaseElement.value) : 0;
        if (database) {
            isSupported = -1 < supportedDatabases.indexOf(database);
        } else if (databaseServerId || serverElement) {
            var server = databaseServerId || getServerId();
            isSupported = -1 < supportedServers.indexOf(server);
        }

        document.getElementById('<?php echo $this->id ?>').style.display = (isSupported && (null === createUserElement || createUserElement.checked)) ? '' : 'none';
        if (document.getElementById('<?php echo $this->remoteAccessId ?>')) {
            document.getElementById('<?php echo $this->remoteAccessId ?>').style.display = isSupported ? 'none' : '';
            document.getElementById('<?php echo $this->remoteAccessId ?>-hint').style.display = isSupported ? '' : 'none';
        }
    };
    <?php foreach ($this->element->getElement($this->radio)->getMultiOptions() as $option => $optionTitle): ?>
    document.getElementById('<?php echo "$radioElementId-$option" ?>').addEventListener('click', updateControlsState);
    <?php endforeach ?>
    Jsw.Observer.appendAndCall(updateControlsState, 'dbServer');
    // Temporary workaround for custom prototype's event '<?php echo $this->id ?>:update'
    document.getElementById('<?php echo $this->id ?>').addEventListener('dataavailable', updateControlsState);
});
//]]>
</script>
