<?php
// Copyright 1999-2021. Plesk International GmbH. All rights reserved.
?>
<?php
$elementIds = [];
foreach (['exec', 'http', 'php', 'arguments', 'phpArguments', 'phpHandlerId', 'subscription', 'sysUserLogin'] as $id) {
    if ($this->form->getElement($id)) {
        $elementIds[$id] = $this->form->getElement($id)->getId();
    }
}
?>
<script type="text/javascript">
//<![CDATA[
Jsw.namespace('Jsw.ScheduledTasks');
Jsw.ScheduledTasks.TaskForm = {
    runTask: function (event) {
        event.preventDefault();
        var elementIds = <?=json_encode($elementIds)?>;

        var checkedRadio = document.querySelectorAll('input:checked[name=taskType]');
        var checkedType = checkedRadio ? checkedRadio[0].value : 'exec';
        var row = document.getElementById(elementIds[checkedType] + '-form-row');
        if (document.getElementById(elementIds[checkedType]).value.length) {
            row.classList.remove('error');
            row.querySelector('.field-errors').style.display = 'none';
        } else {
            row.classList.add('error');
            row.querySelector('.field-errors').innerHTML = '<span class="error-hint">' + '<?php echo Plesk_Base_Utils_String::safeForJs($this->lmsg('components.validators.isEmpty')) ?>' + '</span>';
            row.querySelector('.field-errors').style.display = '';
            return;
        }

        var subscription = document.getElementById(elementIds.subscription);
        var sysUserLogin = document.getElementById(elementIds.sysUserLogin);
        var taskName = document.getElementById(elementIds[checkedType]).value;
        var taskLabel = '<?php echo Plesk_Base_Utils_String::safeForJs($this->form->lmsg('runningTask')) ?>';
        var progressBar = Jsw.getComponent('asyncProgressBarWrapper');
        var itemId = progressBar.fly(Element.cumulativeOffset(event.target),
            taskLabel.replace('%%task%%', Jsw.escapeHtml(taskName)),
            function () {
                var parameters = {
                    id: 0,
                    type: checkedType,
                    command: document.getElementById(elementIds[checkedType]).value,
                    arguments: '',
                    subscription: subscription ? subscription.value : null,
                    sysUserLogin: sysUserLogin ? sysUserLogin.value : null
                };
                if ('exec' === checkedType && elementIds.arguments) {
                    parameters.arguments = document.getElementById(elementIds.arguments).value;
                } else if ('php' === checkedType) {
                    parameters.arguments = document.getElementById(elementIds.phpArguments).value;
                    parameters.phpHandlerId = document.getElementById(elementIds.phpHandlerId).value;
                }
                new Ajax.Request(Jsw.prepareUrl('/scheduler/run-task'), {
                    method: 'post',
                    parameters: parameters,
                    onSuccess: function (response) {
                        progressBar.removePreparingItem(itemId);
                        progressBar.update();
                    }
                });
            });
    }
};

Jsw.onReady(function () {
    var taskTypeControlState = function () {
        var checkedRadio = document.querySelectorAll('input:checked[name=taskType]');
        var checkedType = checkedRadio ? checkedRadio[0].value : 'exec';
        ['exec', 'http', 'php', 'phpHandlerId'].map(function (id) {
            return document.getElementById(id);
        }).filter(function (element) {
            return !!element;
        }).forEach(function (item) {
            item.closest('div.form-row').style.display = item.id.indexOf(checkedType) === 0 ? '' : 'none';
        });
    };

    var notificationControlState = function () {
        document.getElementById('<?php echo $this->form->getElement('emailType')->getId() ?>-form-row').style.display = document.getElementById('<?php echo $this->form->getElement('notify')->getId() ?>-ignore').checked ? 'none' : '';
    };

    document.querySelectorAll('input[name=taskType]').forEach(function (el) {
        el.addEventListener('change', taskTypeControlState);
    });
    taskTypeControlState();
    document.querySelectorAll('input[name=<?php echo $this->form->getElement('notify')->getId() ?>]').forEach(function (el) {
        el.addEventListener('change', notificationControlState);
    });
    notificationControlState();
});
//]]>
</script>
