{{ header }}{{ column_left }}
<div id="content">
    <div class="page-header">
        <div class="container-fluid">
            <div class="pull-right">
                <button id="button-send" data-loading-text="{{ text_loading }}" data-toggle="tooltip" title="{{ button_send }}" class="btn btn-primary" onclick="send('index.php?route=plaza/newsletter/sendMail&user_token={{ user_token }}');"><i class="fa fa-envelope"></i></button>
                <a href="{{ cancel }}" data-toggle="tooltip" title="{{ button_cancel }}" class="btn btn-default"><i class="fa fa-reply"></i></a></div>
            <h1>{{ heading_title }}</h1>
            <ul class="breadcrumb">
                {% for breadcrumb in breadcrumbs %}
                    <li><a href="{{ breadcrumb.href }}">{{ breadcrumb.text }}</a></li>
                {% endfor %}
            </ul>
        </div>
    </div>
    <div class="container-fluid">
        <div class="theme-option-container">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title"><i class="fa fa-envelope"></i> {{ text_mail_form }}</h3>
                </div>
                <div class="panel-body">
                    <form class="form-horizontal">
                        <div class="form-group">
                            <label class="col-sm-2 control-label" for="input-store">{{ entry_store }}</label>
                            <div class="col-sm-10">
                                <select name="store_id" id="input-store" class="form-control">
                                    <option value="0">{{ text_default }}</option>
                                    {% for store in stores %}
                                        <option value="{{ store.store_id }}">{{ store.name }}</option>
                                    {% endfor %}
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label" for="input-to">{{ entry_to }}</label>
                            <div class="col-sm-10">
                                <select name="to" id="input-to" class="form-control">
                                    <option value="all">{{ text_all }}</option>
                                    <option value="specified">{{ text_specified }}</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group to" id="to-specified">
                            <label class="col-sm-2 control-label" for="input-subscribers"><span data-toggle="tooltip" title="{{ help_subscribers }}">{{ entry_subscribers }}</span></label>
                            <div class="col-sm-10">
                                <input type="text" name="subscribers" value="" placeholder="{{ entry_subscribers }}" id="input-subscribers" class="form-control" />
                                <div class="well well-sm" style="height: 150px; overflow: auto;"></div>
                            </div>
                        </div>
                        <div class="form-group required">
                            <label class="col-sm-2 control-label" for="input-subject">{{ entry_subject }}</label>
                            <div class="col-sm-10">
                                <input type="text" name="subject" value="" placeholder="{{ entry_subject }}" id="input-subject" class="form-control" />
                            </div>
                        </div>
                        <div class="form-group required">
                            <label class="col-sm-2 control-label" for="input-message">{{ entry_message }}</label>
                            <div class="col-sm-10">
                                <textarea name="message" placeholder="{{ entry_message }}" id="input-message" data-toggle="summernote" data-lang="{{ summernote }}" class="form-control"></textarea>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    <link href="view/javascript/codemirror/lib/codemirror.css" rel="stylesheet" />
    <link href="view/javascript/codemirror/theme/monokai.css" rel="stylesheet" />
    <script type="text/javascript" src="view/javascript/codemirror/lib/codemirror.js"></script>
    <script type="text/javascript" src="view/javascript/codemirror/lib/xml.js"></script>
    <script type="text/javascript" src="view/javascript/codemirror/lib/formatting.js"></script>
    <script type="text/javascript" src="view/javascript/summernote/summernote.js"></script>
    <link href="view/javascript/summernote/summernote.css" rel="stylesheet" />
    <script type="text/javascript" src="view/javascript/summernote/summernote-image-attributes.js"></script>
    <script type="text/javascript" src="view/javascript/summernote/opencart.js"></script>
    <script type="text/javascript"><!--
        $('select[name=\'to\']').on('change', function() {
            $('.to').hide();

            $('#to-' + this.value.replace('_', '-')).show();
        });

        $('select[name=\'to\']').trigger('change');
        //--></script>
    <script type="text/javascript"><!--
        // Subscribers
        $('input[name=\'subscribers\']').autocomplete({
            'source': function(request, response) {
                $.ajax({
                    url: 'index.php?route=plaza/newsletter/autocomplete&user_token={{ user_token }}&filter_mail=' +  encodeURIComponent(request),
                    dataType: 'json',
                    success: function(json) {
                        response($.map(json, function(item) {
                            return {
                                label: item['mail'],
                                value: item['newsletter_id']
                            }
                        }));
                    },
                    error: function(xhr, ajaxOptions, thrownError) {
                        alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                    }
                });
            },
            'select': function(item) {
                $('input[name=\'subscribers\']').val('');

                $('#input-subscribers' + item['value']).remove();

                $('#input-subscribers').parent().find('.well').append('<div id="subscribers' + item['value'] + '"><i class="fa fa-minus-circle"></i> ' + item['label'] + '<input type="hidden" name="subscribers[]" value="' + item['value'] + '" /></div>');
            }
        });

        $('#input-subscribers').parent().find('.well').delegate('.fa-minus-circle', 'click', function() {
            $(this).parent().remove();
        });

        function send(url) {
            $.ajax({
                url: url,
                type: 'post',
                data: $('#content select, #content input, #content textarea'),
                dataType: 'json',
                beforeSend: function() {
                    $('#button-send').button('loading');
                },
                complete: function() {
                    $('#button-send').button('reset');
                },
                success: function(json) {
                    $('.alert-dismissible, .text-danger').remove();

                    if (json['error']) {
                        if (json['error']['warning']) {
                            $('#content > .container-fluid').prepend('<div class="alert alert-danger alert-dismissible"><i class="fa fa-exclamation-circle"></i> ' + json['error']['warning'] + ' <button type="button" class="close" data-dismiss="alert">&times;</button></div>');
                        }

                        if (json['error']['email']) {
                            $('#content > .container-fluid').prepend('<div class="alert alert-danger alert-dismissible"><i class="fa fa-exclamation-circle"></i> ' + json['error']['email'] + ' <button type="button" class="close" data-dismiss="alert">&times;</button></div>');
                        }

                        if (json['error']['subject']) {
                            $('input[name=\'subject\']').after('<div class="text-danger">' + json['error']['subject'] + '</div>');
                        }

                        if (json['error']['message']) {
                            $('textarea[name=\'message\']').parent().append('<div class="text-danger">' + json['error']['message'] + '</div>');
                        }
                    }

                    if (json['success']) {
                        $('#content > .container-fluid').prepend('<div class="alert alert-success alert-dismissible"><i class="fa fa-check-circle"></i>  ' + json['success'] + ' <button type="button" class="close" data-dismiss="alert">&times;</button></div>');
                    }

                    if (json['next']) {
                        send(json['next']);
                    }
                },
                error: function(xhr, ajaxOptions, thrownError) {
                    alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                }
            });
        }
        //--></script></div>
{{ footer }}