<?php
// Copyright 1999-2022. Plesk International GmbH. All rights reserved.
?>
<?php echo $this->form ?>
<div id="cloud-backup-restriction"></div>
<script type="text/javascript">
//<![CDATA[
Jsw.onReady(function () {
    var fullBackupOptions = <?php echo $this->fullBackupOptions ?>;
    var backupPeriod = document.getElementById('<?php echo $this->form->getSubForm('schedule')->getElement('backupPeriod')->getId() ?>');
    var fullBackupPeriod = document.getElementById('<?php echo $this->form->getSubForm('schedule')->getElement('fullBackupPeriod')->getId() ?>');
    var useIncremental = document.getElementById('<?php echo $this->form->getSubForm('schedule')->getElement('useIncremental')->getId() ?>');
    var repositories = document.querySelectorAll('input[id^=backup-content-repository]');
    var mailContent = document.getElementById('backup-content-content_type-backup_content_mail_only');
    var vhostContent = document.getElementById('backup-content-content_type-backup_content_vhost_only');
    var remoteStorage = document.getElementById('<?php echo $this->form->getSubForm('backup')->getSubForm('content')->getElement('remote_storage')->getId() ?>');

    var changeControlState = function () {
        fullBackupPeriod.closest('.form-row').style.display = (useIncremental.checked && !useIncremental.disabled) ? '' : 'none';

        var selected = fullBackupPeriod.value;
        fullBackupPeriod.innerHTML = Object.keys(fullBackupOptions)
            .filter(option => parseInt(option) > parseInt(backupPeriod.value))
            .map(option => '<option value="' + option + '"' + (selected === option ? ' selected="selected"' : '') + '>' + fullBackupOptions[option]['period'] + '</option>')
            .join('');
    };
    backupPeriod.addEventListener('change', changeControlState);
    fullBackupPeriod.addEventListener('change', changeControlState);
    useIncremental.addEventListener('click', changeControlState);
    changeControlState();

    var incrementalTypeControlState = function () {
        if ((mailContent && mailContent.checked) || (vhostContent && vhostContent.checked)) {
            useIncremental.disabled = false;
        } else {
            useIncremental.disabled = true;
        }
        changeControlState();
    };
    mailContent && mailContent.addEventListener('change', incrementalTypeControlState);
    vhostContent.addEventListener('change', incrementalTypeControlState);
    incrementalTypeControlState();

    <?php if (!$this->form->isBackupToCloudAllowed()): ?>
    var cloudBackupRestrictionHint = null;
    var showCloudBackupRestrictionHint = function (el) {
        hideCloudBackupRestrictionHint();
        if (!el) {
            return;
        }

        Jsw.render(el, (
            '<span class="hint">' +
            <?php if ($this->cloudBackupBuyUrl): ?>
                '<div class="b-indent">' +
                    '<span class="b-indent-icon"><i class="icon"><img src="' + Jsw.skinUrl + '/icons/16/plesk/dollar.png" alt="" /></i></span>' +
                    <?= $this->jsLmsg('cloudBackupRestrictionHintWithBuy'); ?> +
                    ' <a href="<?= $this->escape($this->cloudBackupBuyUrl) ?>"' +
                    <?php if ($this->cloudBackupBuyUrlNewWindow): ?>
                        ' target="_blank"' +
                    <?php endif; ?>
                    '>' + <?= $this->jsLmsg('cloudBackupRestrictionBuyButton'); ?> + '</a>' +
                '</div>' +
            <?php else: ?>
                '<div class="b-indent">' +
                    '<span class="b-indent-icon"><i class="icon"><img src="' + Jsw.skinUrl + '/icons/16/plesk/att-tr.png" alt="" /></i></span>' +
                    <?= $this->jsLmsg('cloudBackupRestrictionHint'); ?> +
                '</div>' +
            <?php endif; ?>
            '</span>'
        ), 'after');
        cloudBackupRestrictionHint = el.nextElementSibling;

        <?php if (Plesk\UserActivityTracking\Helper::isActive()): ?>
        Jsw.UAT.dispatchAction('SHOWPROMO', {
            promo: 'backup_scheduler',
            destination: remoteStorage.value,
        });
        <?php endif; ?>
    };
    var hideCloudBackupRestrictionHint = function () {
        if (cloudBackupRestrictionHint) {
            cloudBackupRestrictionHint.parentNode.removeChild(cloudBackupRestrictionHint);
            cloudBackupRestrictionHint = null;
        }
    };
    <?php if ($this->retrieveKeysTaskId): ?>
    Jsw.Observer.append(function (item) {
        if (item.id === <?= $this->jsEscape($this->retrieveKeysTaskId) ?> && item.status === 'done') {
            showCloudBackupRestrictionHint = function () {
                // skip showing hint after retrieving keys
            };
            hideCloudBackupRestrictionHint();
        }
    }, 'plesk:taskComplete');
    <?php endif; ?>
    <?php endif; ?>

    var repositoriesControlState = function() {
        var repository;
        if (1 == repositories.length) {
            repository = repositories[0].value;
        } else {
            repositories.forEach(function (item){
                if (item.checked) {
                    repository = item.value;
                }
            });
        }
        document.querySelectorAll('div[id^=backup-content-split_backup_size]').forEach(function (el) {
            el.style.display = 'local' === repository ? 'none' : '';
        });
        if (remoteStorage.closest('.form-row')) {
            remoteStorage.closest('.form-row').style.display = 'local' === repository ? 'none' : '';
        }

        <?php if (!$this->form->isBackupToCloudAllowed()): ?>
        if (repository === 'local' || remoteStorage.value === 'ftp') {
            hideCloudBackupRestrictionHint();
            return;
        }

        if (remoteStorage.closest('.form-row')) {
            showCloudBackupRestrictionHint(remoteStorage);
        } else {
            showCloudBackupRestrictionHint(document.querySelector('label[for="backup-content-repository-' + repository + '"]'));
        }
        <?php endif; ?>
    };
    repositories.forEach(function (el) {
        el.addEventListener('click', repositoriesControlState)
    });
    remoteStorage.addEventListener('change', repositoriesControlState);
    repositoriesControlState();

    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 = vhostContent.checked ? '' : 'none';
        });
    };

    vhostContent.addEventListener('change', excludeFilesState);
    excludeFilesState();

    <?php if (!$this->form->isBackupToCloudAllowed()): ?>
    Jsw.namespace('Backup.Schedule');
    Backup.Schedule.checkCloudBackupRestriction = function () {
        if (cloudBackupRestrictionHint) {
            Plesk.require('app/cloud-backup-restriction', function (renderDialog) {
                renderDialog({
                    container: document.getElementById('cloud-backup-restriction'),
                    <?php if ($this->cloudBackupBuyUrl): ?>
                    buyUrl: '<?= $this->cloudBackupBuyUrl ?>',
                    buyUrlNewWindow: <?= $this->cloudBackupBuyUrlNewWindow ? 'true' : 'false' ?>,
                    <?php endif; ?>
                });
                <?php if (Plesk\UserActivityTracking\Helper::isActive()): ?>
                Jsw.UAT.dispatchAction('SHOWPROMO', {
                    promo: 'backup_scheduler',
                    destination: remoteStorage.value,
                });
                <?php endif; ?>
            });
            return false;
        }

        return true;
    };
    <?php endif; ?>
});
//]]>
</script>
