<?php
// Copyright 1999-2025. WebPros International GmbH. All rights reserved.
?>

<script type="text/javascript">
    //<![CDATA[
    Jsw.onReady(function () {
        const hintContainer = document.createElement('span');
        hintContainer.id = 'dkimPopupHint';
        Jsw.render(document.querySelector('#domainKeys-form-row label'), hintContainer, 'after');

        Plesk.require('app/helper/mail-settings-dkim-hint', function (module) {
            module({
                renderTo: 'dkimPopupHint',
                locale: <?= json_encode(Zend_Registry::get('translate')->getSection('smb.components.forms.mail-settings')) ?>,
                enabled: <?= $this->enabled ? 'true' : 'false' ?>,
                dnsRecords: <?= json_encode($this->dnsRecords) ?>,
            })
        });

        <?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>
