<?php
// Copyright 1999-2023. Plesk International GmbH. All rights reserved.
?>

<div id="howToConfigureExternalDns" style="display: none;">
    <p data-name="extDnsHowtoIntro"><?php echo $this->lmsg('smb.components.forms.mail-settings.howToConfigureExternalDnsAlways'); ?></p>
    <?php if ($this->enabled): ?>
        <span data-name="extDnsHowtoOn"><?php echo $this->lmsg('smb.components.forms.mail-settings.howToConfigureExternalDnsEnabled'); ?></span>
        <ul>
            <?php foreach ($this->dnsRecords as $dnsRecord): ?>
                <?php echo '<li data-name="extDnsHowtoRecord" style="overflow-wrap: break-word;  word-break: break-word;">' . $this->escape($dnsRecord) . '</li>'; ?>
            <?php endforeach; ?>
        </ul>
        <span data-name="extDnsHowtoAliases"><?php echo $this->lmsg('smb.components.forms.mail-settings.howToConfigureExternalDnsEnabledAliases'); ?></span>
    <?php else: ?>
        <p data-name="extDnsHowtoOff"><?php echo $this->lmsg('smb.components.forms.mail-settings.howToConfigureExternalDnsDisabled', ['option' => $this->lmsg('smb.components.forms.mail-settings.domainKeys')]); ?></p>
    <?php endif; ?>
</div>

<script type="text/javascript">
    //<![CDATA[
    Jsw.onReady(function () {
        var hintContainer = document.createElement('span');
        hintContainer.className = 'hint-inline hint-info';
        hintContainer.innerHTML = '&nbsp;&nbsp;';
        var hint = document.createElement('a');
        hint.className = 's-btn sb-help';
        hint.setAttribute('data-name', 'extDnsHowtoLink');
        hint.innerHTML = <?php echo $this->jsLmsg('smb.components.forms.mail-settings.howToConfigureExternalDns'); ?>;
        Jsw.render(hintContainer, hint);

        Jsw.render(document.querySelector('#domainKeys-form-row label'), hintContainer, 'after');
        new Jsw.DynamicPopupHint.Instance({
            title: <?php echo $this->jsLmsg('smb.components.forms.mail-settings.howToConfigureExternalDns'); ?>,
            placement: 'top',
            target: hint,
            content: document.getElementById('howToConfigureExternalDns').innerHTML
        });
        document.getElementById('howToConfigureExternalDns').innerHTML = '';

        <?php if ($this->domainId): ?>
        const fieldName = document.createElement('div');
        fieldName.className = 'field-name';
        fieldName.appendChild(document.createTextNode(<?=$this->jsLmsg('smb.components.forms.mail-settings.dkimSelectorName')?>));

        const nameField = document.createElement('input');
        nameField.type = 'text';
        nameField.id = 'signing-keys-selector';

        const fieldValue = document.createElement('div');
        fieldValue.className = 'field-value';
        fieldValue.appendChild(nameField);

        const errorText = document.createElement('span');
        errorText.className = 'error-hint';

        const errorDesc = document.createElement('span');
        errorDesc.className = 'field-errors';
        errorDesc.appendChild(errorText);
        fieldValue.appendChild(errorDesc);

        const fieldRow = document.createElement('div');
        fieldRow.className = 'form-row';
        fieldRow.appendChild(fieldName);
        fieldRow.appendChild(fieldValue);

        document.getElementById('btn-generateSelector').addEventListener(
            'click',
            () => new Jsw.Popup({
                title: <?=$this->jsLmsg('smb.components.forms.mail-settings.generateDialogTitle')?>,
                content: fieldRow,
                buttons: [
                    {
                        class: 'action',
                        title: <?=$this->jsLmsg('smb.components.forms.mail-settings.buttonGenerate')?>,
                        id: 'signing-keys-generate-submit',
                        handler: () => {
                            errorDesc.style.display = 'none';
                            errorText.innerHTML = '';
                            fieldRow.className = fieldRow.className.replace('error', '').trim();
                            new Ajax.Request(
                                '/smb/mail-settings/generate-domain-keys/id/<?=$this->domainId?>/domainId/<?=$this->domainId?>',
                                {
                                    method: 'post',
                                    parameters: {selector: nameField.value.trim()},
                                    onSuccess: (response) => {
                                        if (response.responseJSON.status === 'error') {
                                            fieldRow.className += ' error';
                                            errorText.appendChild(document.createTextNode(response.responseJSON.message));
                                            errorDesc.style.display = '';
                                        } else {
                                            window.location.reload();
                                        }
                                    },
                                }
                            );
                        }
                    },
                    {
                        class: 'action',
                        title: <?=$this->jsLmsg('smb.components.forms.mail-settings.buttonCancel')?>,
                        handler: (event, popup) => popup.hide()
                    }
                ]
            })
        );

        const options = document.forms[0].elements['selector'];
        const activeDkimSelector = options.value;
        const wrapperClass = 'selector-remove';
        const wrapperActiveClass = 'selector-active';
        let context
        if (options.length > 1) {
            options.forEach(option => {
                const wrapper = document.createElement('span');
                const label = option.labels[0];
                if (activeDkimSelector !== option.value) {
                    wrapper.className = wrapperClass;
                    context = document.createElement('a');
                    context.dataset['remove'] = option.value;
                    context.appendChild(document.createTextNode(<?=$this->jsLmsg('smb.components.forms.mail-settings.labelRemoveSelector')?>));
                    context.addEventListener('click', (event) => {
                        event.stopPropagation();
                        event.preventDefault();
                        new Jsw.Popup({
                            title: <?=$this->jsLmsg('smb.components.forms.mail-settings.removeDialogTitle')?>,
                            content: <?=$this->jsLmsg('smb.components.forms.mail-settings.removeConfirmation')?>.replace('%%selector%%', option.value),
                            buttons: [
                                {
                                    class: 'action',
                                    id: 'selector-remove-confirm',
                                    title: <?=$this->jsLmsg('smb.components.forms.mail-settings.buttonConfirm')?>,
                                    handler: () => {
                                        new Ajax.Request(
                                            '/smb/mail-settings/remove-selector/id/<?=$this->domainId?>/domainId/<?=$this->domainId?>',
                                            {
                                                method: 'post',
                                                parameters: {selector: option.value.trim()},
                                                onSuccess: () => window.location.reload()
                                            }
                                        );
                                    }
                                },
                                {
                                    class: 'action',
                                    title: <?=$this->jsLmsg('smb.components.forms.mail-settings.buttonCancel')?>,
                                    handler: (event, popup) => popup.hide()
                                }
                            ],
                        })
                    });
                } else {
                    wrapper.className = wrapperActiveClass;
                    context = document.createTextNode(<?=$this->jsLmsg('smb.components.forms.mail-settings.labelActiveSelector')?>);
                }
                wrapper.appendChild(document.createTextNode(' ('))
                wrapper.appendChild(context)
                wrapper.appendChild(document.createTextNode(')'))
                label.appendChild(wrapper)
            });
        }
        <?php endif; ?>
    });
    //]]>
</script>
