<?php
// Copyright 1999-2024. WebPros International GmbH. All rights reserved.
?>
<?php echo $this->form ?>

<script type="text/javascript">
//<![CDATA[
(function () {
    var dbList = JSON.parse(document.getElementById('dbList').value);

    var initDbListSelector = function () {
        var selectedDbs = dbList[document.getElementById('dbServer').value][document.getElementById('subscriptionsList').value];

        if (Array.isArray(selectedDbs) && !selectedDbs.length) {
            document.getElementById('actionType-new').checked = true;
            document.getElementById('actionType-existing').disabled = true;
            Jsw.render(document.getElementById('existingDatabase'), '<input type="hidden" name="existingDatabase" id="existingDatabase" />', 'replace');
        } else {
            Jsw.render(document.getElementById('existingDatabase'), (
                '<select id="existingDatabase" name="existingDatabase">' +
                    Object.keys(selectedDbs).map(function (value) {
                        return '<option value="' + value + '">' + selectedDbs[value] + '</option>';
                    }).join('') +
                '</select>'
            ), 'replace');
            document.getElementById('actionType-existing').disabled = false;
        }
    }

    var onServerChange = function () {
        // if external database is selected
        if ('0' === document.getElementById('dbServer').value) {
            document.getElementById('databasePrefix').style.display = 'none';
            document.getElementById('externalDbHost-form-row').style.display = '';
            document.getElementById('externalDbLogin-form-row').style.display = '';
            document.getElementById('externalDbPassword-form-row').style.display = '';
            if (document.getElementById('subscriptionsList-form-row')) {
                document.getElementById('subscriptionsList-form-row').style.display = 'none';
            }
            document.getElementById('actionType-existing').disabled = false;

            Jsw.render(document.getElementById('existingDatabase'), '<input type="text" class="input-text" id="existingDatabase" name="existingDatabase" />', 'replace');
        } else {
            document.getElementById('databasePrefix').style.display = '';
            document.getElementById('externalDbHost-form-row').style.display = 'none';
            document.getElementById('externalDbLogin-form-row').style.display = 'none';
            document.getElementById('externalDbPassword-form-row').style.display = 'none';
            if (document.getElementById('subscriptionsList-form-row')) {
                document.getElementById('subscriptionsList-form-row').style.display = '';
            }

            initDbListSelector();
        }

        onActionTypeChange();
    }

    var onActionTypeChange = function () {
        if (document.getElementById('actionType-new').checked) {
            document.getElementById('newName').disabled = false;
            if (document.getElementById('existingDatabase')) {
                document.getElementById('existingDatabase').disabled = true;
            }
        } else {
            document.getElementById('newName').disabled = true;
            if (document.getElementById('existingDatabase')) {
                document.getElementById('existingDatabase').disabled = false;
            }
        }
    }

    document.getElementById('dbServer').addEventListener('change', onServerChange);
    onServerChange();

    if (document.getElementById('subscriptionsList')) {
        document.getElementById('subscriptionsList').addEventListener('change', initDbListSelector);
    }

    document.getElementById('actionType-new').addEventListener('click', onActionTypeChange);
    document.getElementById('actionType-existing').addEventListener('click', onActionTypeChange);
    onActionTypeChange();
})();
//]]>
</script>
