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

{% block meta_title %}{{ 'Client Area' |trans }}{% endblock %}
{% import "macro_functions.phtml" as mf %}

{% block content %}
<div class="dashboard">
    {% set tickets = client.support_ticket_get_list({"status":'on_hold'}) %}
    {% if tickets.total > 0 %}
        <div class="widget">
            <div class="head">
                <h2 class="dark-icon i-support">{{ 'Tickets waiting your reply'|trans }}</h2>
            </div>
            <table>
                <thead>
                    <tr>
                        <th>{{ 'Id'|trans }}</th>
                        <th>{{ 'Subject'|trans }}</th>
                        <th>{{ 'Help desk'|trans }}</th>
                        <th>{{ 'Status'|trans }}</th>
                        <th>{{ 'Submitted'|trans }}</th>
                    </tr>
                </thead>

                <tbody>
                    {% for i, ticket in tickets.list %}
                        <tr class="{{ cycle(['odd', 'even'], i) }}">
                            <td>{{ ticket.id }}</td>
                            <td>
                                <a href="{{ 'support/ticket'|link }}/{{ticket.id}}">{{ ticket.subject }}</a>
                            </td>
                            <td>{{ ticket.helpdesk.name }}</td>
                            <td>{{ mf.status_name(ticket.status) }}</td>
                            <td>{{ ticket.created_at|bb_date }}</td>
                        </tr>
                    {% else %}
                        <tr>
                            <td colspan="5">{{ 'The list is empty'|trans }}</td>
                        </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    {% endif %}

    {% set profile = client.client_get %}
    <div class="grid_6 alpha">
        <div class="widget">
            <div class="head">
                <h2 class="dark-icon i-profile">{{ 'Profile'|trans }}</h2>
            </div>

            <table>
                <tbody>
                    <tr>
                        <td>{{ 'ID'|trans }}</td>
                        <td>#{{ profile.id }}</td>
                    </tr>
                    <tr>
                        <td>{{ 'Email'|trans }}</td>
                        <td>{{ profile.email }}</td>
                    </tr>
                    <tr>
                        <td>{{ 'Balance'|trans }}</td>
                        <td>{{ profile.balance | money(profile.currency) }}</td>
                    </tr>
                </tbody>

                <tfoot>
                    <tr>
                        <td colspan="2">
                            <a class="bb-button" href="{{ 'client/me'|link }}">{{ 'Manage profile'|trans }}</a>
                        </td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>

    <div class="grid_6 omega">
        <div class="widget">
            <div class="head">
                <h2 class="dark-icon i-invoice">{{ 'Invoices'|trans }}</h2>
            </div>
            <table>
                <tbody>
                    <tr>
                        <td>{{ 'Total'|trans }}</td>
                        <td>{{ client.invoice_get_list().total }}</td>
                    </tr>
                    <tr>
                        <td>{{ 'Paid'|trans }}</td>
                        <td>{{ client.invoice_get_list({ "status": "paid" }).total }}</td>
                    </tr>
                    <tr>
                        <td>{{ 'Unpaid'|trans }}</td>
                        <td>{{ client.invoice_get_list({ "status": "unpaid" }).total }}</td>
                    </tr>
                </tbody>

                <tfoot>
                    <tr>
                        <td colspan="2">
                            <a class="bb-button" href="{{ 'invoice'|link }}">{{ 'All Invoices'|trans }}</a>
                        </td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>

    <div class="grid_6 alpha">
        <div class="widget">
            <div class="head">
                <h2 class="dark-icon i-services">{{ 'Services'|trans }}</h2>
            </div>
            <table>
                <tbody>
                    <tr>
                        <td>{{ 'Total'|trans }}</td>
                        <td>{{ client.order_get_list({"hide_addons":1}).total }}</td>
                    </tr>
                    <tr>
                        <td>{{ 'Active'|trans }}</td>
                        <td>{{ client.order_get_list({"hide_addons":1, "status":"active"}).total }}</td>
                    </tr>
                    <tr>
                        <td>{{ 'Soon expires'|trans }}</td>
                        <td>{{ client.order_get_list({"expiring":1}).total }}</td>
                    </tr>
                </tbody>

                <tfoot>
                    <tr>
                        <td colspan="2">
                            <a class="bb-button" href="{{ 'order/service'|link }}">{{ 'All services'|trans }}</a>
                            <a class="bb-button bb-button-submit" href="{{ 'order'|link }}">{{ 'New order'|trans }}</a>
                        </td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>

    <div class="grid_6 omega">
        <div class="widget">
            <div class="head">
                <h2 class="dark-icon i-support">{{ 'Tickets'|trans }}</h2>
            </div>
            <table>
                <tbody>
                    <tr>
                        <td>{{ 'Total'|trans }}</td>
                        <td>{{ client.support_ticket_get_list().total }}</td>
                    </tr>
                    <tr>
                        <td>{{ 'Open'|trans }}</td>
                        <td>{{ client.support_ticket_get_list({ "status": 'open' }).total }}</td>
                    </tr>
                    <tr>
                        <td>{{ 'On Hold'|trans }}</td>
                        <td>{{ client.support_ticket_get_list({ "status": 'on_hold' }).total }}</td>
                    </tr>
                </tbody>

                <tfoot>
                    <tr>
                        <td colspan="2">
                            <a class="bb-button" href="{{ 'support'|link }}">{{ 'All tickets'|trans }}</a>
                            <a class="bb-button bb-button-submit" href="{{ 'support'|link({'ticket' : 1}) }}">{{ 'New ticket'|trans }}</a>
                        </td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>

    <div class="grid_6 alpha">
        <div class="widget">
            <div class="head">
                <h2 class="dark-icon i-order">{{ 'Recent orders'|trans }}</h2>
            </div>
            <table>
                <tbody>
                    {% for i, order in client.order_get_list({"per_page":5, "page":request.page, "hide_addons":1}).list %}
                    <tr class="{{ cycle(['odd', 'even'], i) }}">
                        <td><a href="{{ 'order/service/manage'|link }}/{{order.id}}">{{ order.title|truncate(30) }}</a></td>
                        <td>{{ mf.status_name(order.status) }}</td>
                    </tr>
                    {% else %}
                    <tr>
                        <td colspan="3">{{ 'The list is empty'|trans }}</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>

    <div class="grid_6 omega">
        <div class="widget">
            <div class="head">
                <h2 class="dark-icon i-email">{{ 'Recent emails'|trans }}</h2>
            </div>
            <table>
                <tbody>
                    {% for i, email in client.email_get_list({"per_page":5}).list %}
                    <tr class="{{ cycle(['odd', 'even'], i) }}">
                        <td><a href="{{ 'email'|link }}/{{email.id}}">{{email.subject|truncate(30)}}</a></td>
                        <td title="{{ email.created_at|bb_date }}">{{ email.created_at|timeago }} {{ 'ago'|trans }}</td>
                    </tr>
                    {% else %}
                    <tr>
                        <td colspan="2">{{ 'The list is empty'|trans }}</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
    <div class="clear"></div>
    
</div>
{% endblock %}


{% block js %}
<script type="text/javascript">
$(function() {

    $('#client-login').bind('submit',function(event){
        bb.post(
            'guest/client/login',
            $(this).serialize(),
            function(result) {
                bb.redirect();
            }
        );
        return false;
    });

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