<?php
// Copyright 1999-2024. WebPros International GmbH. All rights reserved.
?>
<?php echo $this->form; ?>
<script type="text/javascript">
    //<![CDATA[
    Jsw.onReady(function () {
        document.getElementById('smb-form-final-filemanager-show').submit = function () {
            var href = this.getAttribute('action') || location.href;
            var formParams = this.serialize(true);
            var noRedirect = this.noRedirect;

            var btnSaveAs = document.getElementById('btn-buttonSaveAs');
            var btnOk = document.getElementById('btn-send');
            var btnApply = document.getElementById('btn-apply');
            var btnCancel = document.getElementById('btn-cancel');

            btnSaveAs.classList.add('disabled');
            btnOk.classList.add('disabled');
            btnApply.classList.add('disabled');
            btnCancel.classList.add('disabled');

            var applyHTML = btnApply.innerHTML;
            btnApply.innerHTML = '<span class="wait">' + <?php echo $this->jsLmsg('waitButtonTitle'); ?> + '</span>';

            new Ajax.Request(href, {
                parameters: {
                    eol: formParams.eol,
                    loadCodepage: formParams.loadCodepage,
                    saveAs: formParams.saveAsFile,
                    saveCodepage: formParams.saveCodepage,
                    spaw2: formParams.spaw2,
                },
                onSuccess: function (data) {
                    if (data.responseJSON && data.responseJSON.status === 'confirmationRequired') {
                        Jsw.addStatusMessage(
                            'error',
                            '<?= $this->lmsg('smb.components.forms.file-manager.show.fileExists') ?>'.replace(
                                '%%fileName%%', formParams.saveAsFile
                            ),
                        );
                        btnSaveAs.classList.remove('disabled');
                        btnOk.classList.remove('disabled');
                        btnApply.classList.remove('disabled');
                        btnCancel.classList.remove('disabled');
                        btnApply.innerHTML = applyHTML;
                        document.getElementById('saveAsFile').value = '';
                        return;
                    }

                    if (data.responseJSON && data.responseJSON.redirect) {
                        window.location = Jsw.prepareUrl(data.responseJSON.redirect);
                        return;
                    }

                    if (data.responseJSON && data.responseJSON.message) {
                        if (noRedirect) {
                            btnSaveAs.classList.remove('disabled');
                            btnOk.classList.remove('disabled');
                            btnApply.classList.remove('disabled');
                            btnCancel.classList.remove('disabled');
                            var status = new Jsw.StatusMessage({ type: 'info', message: data.responseJSON.message });
                            setTimeout(function () { status.hide(); }, 3000);
                        } else {
                            btnCancel.click();
                        }

                        btnApply.innerHTML = applyHTML;
                    }
                },
            });

            return false;
        };

        var spaw2editor = document.getElementById('spaw2_rEdit');
        var spaw2textarea = document.getElementById('spaw2');
        var onkeydown = function (event) {
            var ctrlOrMetaKeyPressed = (event.ctrlKey && !event.metaKey) || (event.metaKey && !event.ctrlKey);

            if (event.key.toLowerCase() === 's'
                && ctrlOrMetaKeyPressed
                && !event.shiftKey
                && !event.altKey
            ) {
                event.preventDefault();

                var form = document.getElementById('smb-form-final-filemanager-show');
                form.noRedirect = true;
                form.submit();
            }
        };
        if (spaw2editor && spaw2editor.contentWindow) {
            spaw2editor.contentWindow.onkeydown = onkeydown;
        }
        if (spaw2textarea) {
            spaw2textarea.addEventListener('keydown', onkeydown);
        }


        var checkSpaw2Height = function () {
            var buttons = document.querySelector('.btns-box');
            if (!buttons) {
                window.removeEventListener('resize', checkSpaw2Height, false);
                return;
            }

            var height = document.viewport.getHeight();
            height -= Element.getHeight(document.querySelector('.pul-layout__footer'));
            height -= parseFloat(Element.getStyle(document.querySelector('.pul-layout__main-inner'), 'padding-bottom'));
            height -= Element.getHeight(buttons);
            height -= Element.cumulativeOffset(buttons).top;

            var adjustEditor = height + Element.getHeight(spaw2editor);
            var adjustTextarea = height + Element.getHeight(spaw2textarea);

            spaw2editor.style.height = (adjustEditor > 300 ? adjustEditor : 300) + 'px';
            spaw2textarea.style.height = (adjustTextarea > 300 ? adjustTextarea : 300) + 'px';
        };
        window.addEventListener('resize', checkSpaw2Height);
        checkSpaw2Height();
    });
    //]]>
</script>
