{% extends request.ajax ? "layout_blank.phtml" : "layout_default.phtml" %}

{% block meta_title %}{{ 'Email'|trans }}{% endblock %}

{% block breadcrumb %}
<li><a href="{{ 'email' | link}}">{{ 'Emails'|trans }}</a><span class="divider">/</span></li>
{{ email.subject }}
{% endblock %}

{% block content %}
<div class="row">
    <article class="span12 data-block">
        <div class="data-container">
            <header>
                <h1>{{ email.subject }}</h1>
                <div class="data-header-actions">
                    <ul>
                        <li>
                            <a class="btn btn-small" href="{{ '/email'|link }}">{{ 'Back to emails list'|trans }}</a>
                        </li>
                    </ul>
                </div>
            </header>
            <section>
                <div class="tab-pane {% if loop.first%}active{% endif %}" id="tab{{email.id}}">
                    <div class="well well-small">
                        <p><strong>{{ 'From:'|trans }}</strong> {{email.sender}}</p>
                        <p><strong>{{ 'To:'|trans }}</strong> {{email.recipients}}</p>
                        <p><strong>{{ 'Created at:'|trans }}</strong> {{email.created_at|bb_date}}</p>
                    </div>
                    <div class="well">
                        <h3>{{ email.subject }}</h3>
                        <p> {{email.content_html | raw}}</p>
                    </div>
                    <a class="btn btn-inverse email-resend" href="#" mail-id="{{email.id}}">{{ 'Resend'|trans }}</a>
                    <a class="btn btn-danger email-delete" href="#"  mail-id="{{email.id}}">{{ 'Delete'|trans }}</a>
                </div>
            </section>
        </div>
    </article>
</div>
{% endblock %}

{% block js %}
{% autoescape "js" %}
<script type="text/javascript">
    $(function() {
        $('.email-resend').click(function(e) {
            e.preventDefault();
            $('.wait').show();

            var email_id = $(this).attr('mail-id');

            bb.post(
                'client/email/resend',
                { id: email_id },
                function(result) {
                    $('.wait').hide();
                    bb.msg('Email resent');

                    return false;
                }
            );
        });
        $('.email-delete').click(function(e){
            e.preventDefault();
            
            if (confirm('Are you sure?')){
                $('.wait').show();
                
                var email_id = $(this).attr('mail-id');
                
                bb.post(
                    'client/email/delete',
                    { id: email_id },
                    function(result) {
                        bb.msg('Email deleted');
                        bb.redirect('{{ 'email'|link }}');

                        return false;
                    }
                );
            };
        });

    });
</script>
{% endautoescape %}
{% endblock %}
