<?php
// Copyright 1999-2025. WebPros International GmbH. All rights reserved.
?>
<?php echo $this->element->getElement('selector')->render($this); ?>

<script type="text/javascript">
    //<![CDATA[
    Jsw.onReady(function () {
        const optionsLength = <?php echo $this->optionsLength; ?>;

        if (optionsLength > 1) {
            const options = document.forms[0].elements['selector'];
            const activeDkimSelector = options.value;
            const wrapperClass = 'selector-remove';
            const wrapperActiveClass = 'selector-active';

            options.forEach(option => {
                const wrapper = document.createElement('span');
                const label = option.labels[0];
                let context;

                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.removeSelectorDialogTitle')?>,
                            content: <?=$this->jsLmsg('smb.components.forms.mail-settings.removeSelectorConfirmation')?>.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)
            });
        }
    });
    //]]>
</script>
