<?php
// Copyright 1999-2022. Plesk International GmbH. All rights reserved.
?>
<div class="form-row">
    <div class="field-name">
        <input type="hidden" value="none" name="<?php echo $this->escape($this->element->getFullyQualifiedName()) ?>">
        <input type="checkbox" class="checkbox"
               <?php echo 'none' == $this->element->getValue() ? '' : 'checked="checked"' ?>
               id="<?php echo $this->escape($this->element->getId()) ?>-enabled">
        <label for="<?php echo $this->escape($this->element->getId()) ?>-enabled">
            <?php echo $this->element->getLabel() ?>
        </label>
    </div>
    <div class="field-value">
        <select name="<?php echo $this->escape($this->element->getFullyQualifiedName()) ?>"
            id="<?php echo $this->escape($this->element->getId()) ?>">
            <?php foreach ($this->element->getMultiOptions() as $value => $label): ?>
                <?php if ('none' == $value) continue ?>
                <option label="<?php echo $this->escape($label) ?>"
                    <?php echo $value == $this->element->getValue() || ('none' == $this->element->getValue() && 'weekly' == $value) ? 'selected="selected"' : '' ?>
                    value="<?php echo $this->escape($value) ?>">
                    <?php echo $this->escape($label) ?>
                </option>
            <?php endforeach ?>
        </select>
        <p>
            <?php
            $notificationLink = null;
            if ($this->showSettingsLink) {
                $notificationLink = <<<LINK
                    <a href="{$this->baseUrl('/admin/notifications/panel-notifications-settings')}">
                        {$this->lmsg("{$this->localeSection}.notificationLink")}
                    </a>
                    LINK;
            }

            echo $this->lmsg(
                "{$this->localeSection}.{$this->notificationRuleMsgKey}",
                [
                    'notificationLink' => $notificationLink
                ]
            );
            ?>
        </p>
    </div>
</div>
<script type="text/javascript">
//<![CDATA[
Jsw.onReady(function () {
    var updateControls = function () {
        var updateSupported = ['tortix', 'atomic', 'comodo', 'comodo_free'].some(function (id) {
            var ruleset = document.getElementById('<?php echo $this->escape($this->form->getElement('ruleSet')->getId()) ?>-' + id);
            return ruleset && ruleset.checked;
        });

        var updateElement = document.getElementById('<?php echo $this->escape($this->element->getId()) ?>');
        updateElement.closest('.form-row').style.display = updateSupported ? '' : 'none';
        if (document.getElementById('<?php echo $this->escape($this->element->getId()) ?>-enabled').checked) {
            updateElement.disabled = false;
        } else {
            updateElement.disabled = true;
        }
    };
    updateControls();

    <?php foreach (array_keys($this->form->getElement('ruleSet')->getMultiOptions()) as $option): ?>
        document.getElementById('<?php echo $this->escape($this->form->getElement('ruleSet')->getId() . '-' . $option) ?>').addEventListener('click', updateControls);
    <?php endforeach ?>

    document.getElementById('<?php echo $this->escape($this->element->getId()) ?>-enabled').addEventListener('click', updateControls);
});
//]]>
</script>
