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

{% block meta_title %}{{ 'Emails'|trans }}{% endblock %}
{% block breadcrumb %} <li class="active">{{ 'Emails'|trans }}</li>{% endblock %}

{% set emails = client.email_get_list({"per_page":10, "page":request.page}) %}

{% block content %}
<div class="row">
    <article class="span12 data-block">
        <div class="data-container">
            <header>
                <h1>{{ 'Emails'|trans }}</h1>
                <p>{{ 'Here you can find all the emails we sent you. Please click on email topic in left column and it will be displayed in right side.'|trans }}</p>
            </header>
            <section>
                {% if emails.list is empty  %}
                <div class="alert alert-info" id="information-block">{{ 'There are no emails to display'|trans }}</div>
                {% else %}
                <div class="tabbable tabs-left">
                    <ul class="nav nav-tabs" >
                        {% for i, email in emails.list %}
                        <li class="email-title {% if loop.first%}active{% endif %}" style="line-height: 50%">
                            <a href="#tab{{email.id}}" data-toggle="tab" style="padding-left: 0px; padding-bottom: 0px">
                                <p><strong>{{email.subject |slice(0,50) }}{% if email.subject | length > 50%}...{% endif%}</strong></p>
                                <p><small>{{email.created_at|bb_date}}</small></p>
                            </a>
                        </li>
                        {% endfor %}
                    </ul>
                    <div class="tab-content">
                        {% for i, email in emails.list %}
                        <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>
                        {% endfor %}
                    </div>
                </div>
                {% include "partial_pagination.phtml" with {'list': emails} %}
                {% endif %}
            </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 %}