<?php
// Copyright 1999-2024. WebPros 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);

        <?php if ($this->withDkimSelectors): ?>
        document.getElementById('btn-createSelector').addEventListener(
            'click',
            () => new Jsw.Popup({
                title: <?=$this->jsLmsg('smb.components.forms.mail-settings.createSelectorDialogTitle')?>,
                content: fieldRow,
                buttons: [
                    {
                        class: 'action',
                        title: <?=$this->jsLmsg('smb.components.forms.mail-settings.buttonCreate')?>,
                        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()
                    }
                ]
            })
        );
        <?php endif; ?>

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