<?php
// Copyright 1999-2021. Plesk International GmbH. All rights reserved.
?>
<div class='form-row'>
    <div class='field-name'>
        <label for="<?php echo $this->element->getId(); ?>">
            <?php echo $this->element->getLabel(); ?>
        </label>
    </div>
    <div class='field-value'>
        <?php echo $this->elementPart($this->element)->content(); ?>
        <span class="field-errors" style="display: none;"></span>
        <?php echo $this->element->getView()->elementPart($this->element)->description()?>
    </div>
</div>
<script type="text/javascript">
//<![CDATA[
Jsw.onReady(function () {
    var dbUserRoles = <?php echo Zend_Json::encode($this->dbUserRoles); ?>;
    var dbPrivilegesSections = ['dataAccess', 'structureAccess1', 'structureAccess2'];
    var sectionsToObserve = ['dbPrivileges-dataAccess-form-row', 'dbPrivileges-structureAccess-form-row'];

    function onDbRoleChange() {
        var element = document.getElementById('<?php echo $this->element->getId(); ?>');
        if (!dbUserRoles[element.value]) {
            return;
        }

        for (var index = 0; index < dbPrivilegesSections.length; index++) {
            document.querySelectorAll('[name="dbPrivileges[' + dbPrivilegesSections[index] + '][]"]').forEach(function (el) {
                el.checked = false;
            });
        }

        var rolePrivileges = dbUserRoles[element.value];
        for (var index = 0; index < rolePrivileges.length; index++) {
            var roleName = rolePrivileges[index].replace(/\s+/g, '');
            for(var sectionIndex in dbPrivilegesSections) {
                var privilegeElement = document.getElementById('dbPrivileges-' + dbPrivilegesSections[sectionIndex] + '-' + roleName);
                if (!privilegeElement) {
                    continue;
                }
                privilegeElement.checked = true;
            }
        }
    }

    function onDbPrivilegeChange() {
        var currentPrivileges = [];
        for (var dbSectionIndex = 0; dbSectionIndex < sectionsToObserve.length; dbSectionIndex++) {
            document.getElementById(sectionsToObserve[dbSectionIndex]).querySelectorAll('input[type="checkbox"]').forEach(function (element) {
                if (element.checked) {
                    currentPrivileges.push(element.value);
                }
            });
        }

        var dbRoleValue = 'custom';
        var currentPrivilegesCount = currentPrivileges.length;
        for (var dbRoleIndex in dbUserRoles) {
            var rolePrivilegesCount = dbUserRoles[dbRoleIndex].length;
            if (currentPrivilegesCount != rolePrivilegesCount) {
                continue;
            }
            var comparedPrivilegesCount = 0;
            for (var priviligeIndex = 0; priviligeIndex < rolePrivilegesCount; priviligeIndex++) {
                if (-1 != currentPrivileges.indexOf(dbUserRoles[dbRoleIndex][priviligeIndex])) {
                    comparedPrivilegesCount++;
                }
            }
            if (comparedPrivilegesCount === rolePrivilegesCount) {
                dbRoleValue = dbRoleIndex;
            }
        }
        document.getElementById('<?php echo $this->element->getId(); ?>').value = dbRoleValue;
    }

    document.getElementById('<?php echo $this->element->getId(); ?>').addEventListener('change', onDbRoleChange);
    onDbRoleChange();

    for (var dbSectionIndex = 0; dbSectionIndex < sectionsToObserve.length; dbSectionIndex++) {
        document.getElementById(sectionsToObserve[dbSectionIndex]).querySelectorAll('input[type="checkbox"]').forEach(function (element) {
            element.addEventListener('change', onDbPrivilegeChange);
        });
    }
});
//]]>
</script>
