<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/* @var $block \Magento\Newsletter\Block\Adminhtml\Queue\Edit */
?>
<div data-mage-init='{"floatingHeader": {}}' class="page-actions">
    <?php echo $block->getBackButtonHtml() ?>
    <?php echo $block->getPreviewButtonHtml(); ?>
    <?php if (!$block->getIsPreview()): ?>
        <?php echo $block->getResetButtonHtml() ?>
        <?php echo $block->getSaveButtonHtml() ?>
    <?php endif ?>
    <?php if ($block->getCanResume()): ?>
        <?php echo $block->getResumeButtonHtml() ?>
    <?php endif ?>
</div>

<form action="<?php /* @escapeNotVerified */ echo $block->getSaveUrl() ?>" method="post" id="queue_edit_form">
    <?php echo $block->getBlockHtml('formkey')?>
    <?php echo $block->getChildHtml('form') ?>
</form>
<form action="<?php /* @escapeNotVerified */ echo $block->getPreviewUrl() ?>" method="post" id="newsletter_queue_preview_form" target="_blank">
    <?php echo $block->getBlockHtml('formkey')?>
    <div class="no-display">
        <input type="hidden" id="preview_type" name="type" value="<?php /* @escapeNotVerified */ echo $block->getIsTextType() ? 1 : 2 ?>" />
        <input type="hidden" id="preview_text" name="text" value="" />
        <input type="hidden" id="preview_styles" name="styles" value="" />
        <input type="hidden" id="preview_id" name="id" value="" />
    </div>
</form>
<script>
require([
    'jquery',
    'tinymce',
    'mage/mage',
    'prototype'
], function(jQuery, tinyMCE){

//<![CDATA[
jQuery('#queue_edit_form').mage('form').mage('validation');
jQuery('#newsletter_queue_preview_form').mage('form').mage('validation');

queueControl = {
    id: 'text',
    preview: function() {
        if (this.isEditor() && tinyMCE.get(this.id)) {
            tinyMCE.triggerSave();
            $('preview_text').value = tinyMCE.get(this.id).getContent();
            tinyMCE.triggerSave();
        } else {
            $('preview_text').value = $(this.id).value;
        }
        if ($('styles') != undefined) {
            $('preview_styles').value = $('styles').value;
        }
        if ($('id') != undefined) {
            $('preview_id').value = $('id').value;
        }
        // Temporary solution will be replaced after refactoring of newsletter queue functionality
        jQuery('#newsletter_queue_preview_form').trigger('save');
        return false;
    },
    isEditor: function() {
        return (typeof tinyMceEditors != 'undefined' && tinyMceEditors.get(this.id) != undefined)
    }
};
//]]>

});
</script>
