<?php
// Copyright 1999-2024. Plesk International GmbH. All rights reserved.
?>
<?php echo $this->form ?>
<script type="text/javascript">
//<![CDATA[
Jsw.onReady(function () {
    var repositories = document.querySelectorAll('input[id^=backup-content-repository]');
    var confAndContent = document.getElementById('backup-content-content-configuration_and_content');
    var mailContent = document.getElementById('backup-content-content_type-backup_content_mail');
    var fileContent = document.getElementById('backup-content-content_type-backup_content_file');
    var databaseContent = document.getElementById('backup-content-content_type-backup_content_database');

    var incrementalTypeControlState = function () {
        var repository;
        if (1 === repositories.length) {
            repository = repositories[0].value;
        } else {
            repositories.forEach(function (item){
                if (item.checked) {
                    repository = item.value;
                }
            });
        }
        if ((confAndContent && confAndContent.checked)
            || ((mailContent && mailContent.checked) || (fileContent && fileContent.checked))
        ) {
            if (document.getElementById('backup-content-incrementalTypeRadio-incremental').checked) {
                document.getElementById('backup-content-incremental').value = 1;
            }
            document.querySelectorAll('div[id^=backup-content-incrementalTypeRadio]').forEach(function (el) {
                el.style.display = '';
            });
            document.querySelectorAll('div[id^=backup-content-incrementalTypeText]').forEach(function (el) {
                el.style.display = 'none';
            });
        } else {
            document.querySelectorAll('div[id^=backup-content-incrementalTypeRadio]').forEach(function (el) {
                el.style.display = 'none';
            });
            document.querySelectorAll('div[id^=backup-content-incrementalTypeText]').forEach(function (el) {
                el.style.display = '';
            });
            document.getElementById('backup-content-incremental').value = 0;
        }

        document.querySelectorAll('div[id^=backup-content-split_backup_size]').forEach(function (el) {
            el.style.display = 'local' === repository ? 'none' : '';
        });
        document.querySelectorAll('div[id^=backup-content-remote_storage-form-row]').forEach(function (el) {
            el.style.display = 'local' === repository ? 'none' : '';
        });
    };

    var contentControlState = function() {
        var comment = document.getElementById('backup-settings-comment');

        var conf = document.getElementById('backup-content-content-configuration');
        if (conf && conf.checked) {
            comment.innerHTML = conf.parentNode.querySelector('label').innerHTML.trim();
        } else if (confAndContent && confAndContent.checked) {
            comment.innerHTML = confAndContent.parentNode.querySelector('label').innerHTML.trim();
        } else if (mailContent && mailContent.checked && fileContent && fileContent.checked && databaseContent && databaseContent.checked) {
            comment.innerHTML = <?= $this->jsLmsg('components.forms.backup.subform.backupAll'); ?>;
        } else if (mailContent && mailContent.checked && fileContent && !fileContent.checked && databaseContent && !databaseContent.checked) {
            comment.innerHTML = <?= $this->jsLmsg('components.forms.backup.subform.backupMail'); ?>;
        } else if (fileContent && fileContent.checked && databaseContent && databaseContent.checked && mailContent && !mailContent.checked) {
            comment.innerHTML = <?= $this->jsLmsg('components.forms.backup.subform.backupVhost'); ?>;
        } else if (fileContent && fileContent.checked && databaseContent && !databaseContent.checked && mailContent && !mailContent.checked) {
            comment.innerHTML = <?= $this->jsLmsg('components.forms.backup.subform.backupFile'); ?>;
        } else if (fileContent && fileContent.checked && databaseContent && !databaseContent.checked && mailContent && mailContent.checked) {
            comment.innerHTML = <?= $this->jsLmsg('components.forms.backup.subform.backupMailFile'); ?>;
        } else if (fileContent && !fileContent.checked && databaseContent && databaseContent.checked && mailContent && !mailContent.checked) {
            comment.innerHTML = <?= $this->jsLmsg('components.forms.backup.subform.backupDatabase'); ?>;
        } else if (fileContent && !fileContent.checked && databaseContent && databaseContent.checked && mailContent && mailContent.checked) {
            comment.innerHTML = <?= $this->jsLmsg('components.forms.backup.subform.backupMailDatabase'); ?>;
        } else {
            comment.innerHTML = <?= $this->jsLmsg('components.forms.backup.subform.backupConfiguration'); ?>;
        }
    };

    var excludeFilesState = function () {
        var checkbox = document.getElementById('backup-settings-excludeFilesMasterCheckbox');
        var rows = [
            checkbox.closest('.form-row'),
            document.getElementById('backup-settings-exclude_logs').closest('.form-row')
        ];
        if (checkbox.checked) {
            rows.push(document.getElementById('backup-settings-exclude_files-form-row'));
        }

        rows.forEach(function (el) {
            el.style.display = fileContent.checked ? '' : 'none';
        });
    };

    document.querySelectorAll('input[id^=backup-content-incrementalTypeRadio]').forEach(function (el) {
        el.addEventListener('click', function () {
            document.getElementById('backup-content-incremental').value = 'incremental' === this.value ? 1 : 0;
        });
    });

    document.querySelectorAll('input[id^=backup-content-content]').forEach(function (el) {
        el.addEventListener('change', incrementalTypeControlState);
        el.addEventListener('change', contentControlState);
        el.addEventListener('change', excludeFilesState);
    });

    repositories.forEach(function (el) {
        el.addEventListener('click', incrementalTypeControlState);
    });

    incrementalTypeControlState();
    contentControlState();
    excludeFilesState();
});
//]]>
</script>
