<?php
// Copyright 1999-2021. Plesk International GmbH. All rights reserved.
?>
<script type="text/javascript">
    //<![CDATA[
    Jsw.onReady(function () {
        var databaseControlsState = function () {
            if (0 == document.getElementById('database').value) {
                document.getElementById('default').closest('.form-row').style.display = 'none';
            } else {
                document.getElementById('default').closest('.form-row').style.display = '';
            }
        };

        var dbServerControlState = function (event) {
            var dbServerElement = document.getElementById('dbServer');
            var dbElement = document.getElementById('database');
            var dbList = JSON.parse(document.getElementById('dbList').value);

            dbElement.innerHTML = (
                Object.keys(dbList[dbServerElement.value]).map(function (value) {
                    return '<option value="' + value + '">' + dbList[dbServerElement.value][value] + '</option>';
                }).join('')
            );

            var dbWebAdmins = JSON.parse(document.getElementById('dbWebAdmins').value);
            var dbDefaultLabel = <?php echo $this->jsLmsg('smb.components.forms.database.user.default') ?>;
            document.querySelectorAll('label[for="default"]').forEach(function (el) {
                el.innerHTML = dbDefaultLabel.replace('%%webadmin%%', dbWebAdmins[dbServerElement.value]);
            });

            databaseControlsState();
            Jsw.Observer.notify(event, 'dbServer');
        };

        document.getElementById('database').addEventListener('change', databaseControlsState);
        document.getElementById('dbServer').addEventListener('change', dbServerControlState);
        databaseControlsState();
    });
    //]]>
</script>
