{% extends "layout_default.phtml" %}

{% block meta_title %}{% trans 'Dashboard' %}{% endblock %}

{% block content %}

{% for msg in admin.system_messages({"type":"info"}) %}
<div class="nNote nInformation hideit {% if loop.first %}first{% endif %}">
    <p><strong>{% trans 'INFORMATION' %}: </strong>{{ msg }}</p>
</div>
{% endfor %}

<div class="widget nomargin">
    <div class="head"><h5 class="iChart8">{% trans 'Website statistic' %}</h5></div>
    {% set stats = admin.stats_get_summary %}
    {% set income = admin.stats_get_summary_income %}
    <table cellpadding="0" cellspacing="0" width="100%" class="tableStatic">
        <thead>
            <tr>
                <td>{% trans 'Metric' %}</td>
                <td width="27%">{% trans 'Today' %}</td>
                <td width="27%">{% trans 'Yesterday' %}</td>
                <td width="27%">{% trans 'This month so far' %}</td>
                <td width="27%">{% trans 'Total' %}</td>
            </tr>
        </thead>

        <tbody>
            <tr>
                <td>{% trans 'Income' %}</td>
                <td align="center"><a href="{{ 'invoice'|alink }}?paid_at={{ "now"|date('Y-m-d') }}" title="" class="webStatsLink">{{ mf.currency_format(income.today) }}</a></td>
                <td align="center"><a href="{{ 'invoice'|alink }}?paid_at={{ "yesterday"|date('Y-m-d') }}" title="" class="webStatsLink">{{ mf.currency_format(income.yesterday) }}</a></td>
                <td align="center"><a href="{{ 'invoice'|alink }}" title="" class="webStatsLink">{{ mf.currency_format(income.this_month) }}</a></td>
                <td align="center"><a href="{{ 'invoice'|alink }}" title="" class="webStatsLink">{{ mf.currency_format(income.total) }}</a></td>
            </tr>
            <tr>
                <td>{% trans 'Clients' %}</td>
                <td align="center"><a href="{{ 'client'|alink }}?created_at={{ "now"|date('Y-m-d') }}" title="" class="webStatsLink">{{ stats.clients_today }}</a></td>
                <td align="center"><a href="{{ 'client'|alink }}?created_at={{ "yesterday"|date('Y-m-d') }}" title="" class="webStatsLink">{{ stats.clients_yesterday }}</a></td>
                <td align="center"><a href="{{ 'client'|alink }}" title="" class="webStatsLink">{{ stats.clients_this_month }}</a></td>
                <td align="center"><a href="{{ 'client'|alink }}" title="" class="webStatsLink">{{ stats.clients_total }}</a></td>
            </tr>
            <tr>
                <td>{% trans 'Orders' %}</td>
                <td align="center"><a href="{{ 'order'|alink }}?created_at={{ "now"|date('Y-m-d') }}" title="" class="webStatsLink">{{ stats.orders_today }}</a></td>
                <td align="center"><a href="{{ 'order'|alink }}?created_at={{ "yesterday"|date('Y-m-d') }}" title="" class="webStatsLink">{{ stats.orders_yesterday }}</a></td>
                <td align="center"><a href="{{ 'order'|alink }}" title="" class="webStatsLink">{{ stats.orders_this_month }}</a></td>
                <td align="center"><a href="{{ 'order'|alink }}" title="" class="webStatsLink">{{ stats.orders_total }}</a></td>
            </tr>
            <tr>
                <td>{% trans 'Invoices' %}</td>
                <td align="center"><a href="{{ 'invoice'|alink }}?created_at={{ "now"|date('Y-m-d') }}" title="" class="webStatsLink">{{ stats.invoices_today }}</a></td>
                <td align="center"><a href="{{ 'invoice'|alink }}?created_at={{ "yesterday"|date('Y-m-d') }}" title="" class="webStatsLink">{{ stats.invoices_yesterday }}</a></td>
                <td align="center"><a href="{{ 'invoice'|alink }}" title="" class="webStatsLink">{{ stats.invoices_this_month }}</a></td>
                <td align="center"><a href="{{ 'invoice'|alink }}" title="" class="webStatsLink">{{ stats.invoices_total }}</a></td>
            </tr>
            <tr>
                <td>{% trans 'Tickets' %}</td>
                <td align="center"><a href="{{ 'support'|alink }}?created_at={{ "now"|date('Y-m-d') }}" title="" class="webStatsLink">{{ stats.tickets_today }}</a></td>
                <td align="center"><a href="{{ 'support'|alink }}?created_at={{ "yesterday"|date('Y-m-d') }}" title="" class="webStatsLink">{{ stats.tickets_yesterday }}</a></td>
                <td align="center"><a href="{{ 'support'|alink }}" title="" class="webStatsLink">{{ stats.tickets_this_month }}</a></td>
                <td align="center"><a href="{{ 'support'|alink }}" title="" class="webStatsLink">{{ stats.tickets_total }}</a></td>
            </tr>
        </tbody>
    </table>
</div>

{% set orders = admin.order_get_list({"per_page":"5", "hide_addons":1, "status":"active"}) %}
{% if orders.list|length > 0 %}
<div class="widgets">
    <div class="left">
        <div class="widget">
            <div class="head">
                <h5 class="iMoney"><a href="{{ 'order'|alink }}">{% trans 'Latest orders' %}</a></h5>
                <div class="num"><a href="{{ 'order'|alink }}" class="greenNum">+{{ orders.total }}</a></div>
            </div>
            <div style="height: 221px; overflow: auto;">
                <table class="tableStatic wide">
                    <thead>
                        <tr>
                            <td>{% trans 'Order' %}</td>
                            <td>{% trans 'Client' %}</td>
                        </tr>
                    </thead>
                    <tbody>
                    {% for order in orders.list %}
                        <tr title="{{order.created_at|timeago}} ago">
                            <td><a href="{{ 'order/manage'|alink }}/{{ order.id }}">{{ order.title|truncate(35) }}</a></td>
                            <td align="center"><a href="{{ 'client/manage'|alink }}/{{ order.client_id }}" title="">{{ order.client.first_name|truncate(1, null, '.') }} {{ order.client.last_name }}</a></td>
                        </tr>
                    {% else %}
                    <tr>
                        <td colspan="2" align="center">{% trans 'The list is empty' %}</td>
                    </tr>
                    {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <div class="right">
        <div class="widget">
            <div class="head">
                <h5 class="iGraph">{% trans 'Product sales' %}</h5>
            </div>
            <div style="height: 221px; overflow: auto;">
                <table class="tableStatic wide">
                    <thead>
                        <tr>
                            <td>{% trans 'Product/Service' %}</td>
                            <td>{% trans 'Orders' %}</td>
                        </tr>
                    </thead>
                    <tbody>
                    {% for p in admin.stats_get_product_summary %}
                        <tr>
                            <td><a href="{{ 'product/manage'|alink }}/{{p.id}}" title="{{ p.title }}">{{ p.title|truncate(35) }}</a></td>
                            <td align="center"><a href="{{ 'order'|alink }}?product_id={{p.id}}" title="" class="webStatsLink">{{ p.orders }}</a></td>
                        </tr>
                    {% else %}
                    <tr>
                        <td colspan="2" align="center">{% trans 'No active orders available' %}</td>
                    </tr>
                    {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <div class="fix"></div>
</div>
{% endif %}

<div class="widget">
    <div class="head">
        <h5 class="iDropper">{% trans 'Define date interval for graphs' %}</h5>
    </div>
    <form method="get" action="" class="mainForm">
        <fieldset>
            <div class="rowElem noborder">
                <div class="moreFields">
                    <ul>
                        <li style="width: 100px"><input type="text" name="date_from" value="{% if request.date_from %}{{ request.date_from|date('Y-m-d') }}{%endif%}" placeholder="{{ now|date('Y-m-d') }}" class="datepicker"/></li>
                        <li class="sep">-</li>
                        <li style="width: 100px"><input type="text" name="date_to" value="{% if request.date_to %}{{ request.date_to|date('Y-m-d') }}{%endif%}" placeholder="{{ now|date('Y-m-d') }}" class="datepicker"/></li>
                        <li class="sep" style="padding-top: 0px"><input type="submit" value="{% trans 'Update graphs' %}" class="greyishBtn" /></li>
                    </ul>
                </div>
                <div class="fix"></div>
            </div>
        </fieldset>
    </form>
</div>

<div class="widget">
    <div class="head">
        <h5 class="iGraph">{% trans 'Income' %}</h5>
    </div>
    <div class="body">
        <div id="graph-income" style="width: 700px; height: 200px;"></div>
    </div>
</div>

<div class="widget">
    <div class="head">
        <h5 class="iGraph">{% trans 'Orders' %}</h5>
    </div>
    <div class="body">
        <div id="graph-orders" style="width: 700px; height: 200px;"></div>
    </div>
</div>

<div class="widget">
    <div class="head">
        <h5 class="iGraph">{% trans 'Invoices' %}</h5>
    </div>
    <div class="body">
        <div id="graph-invoice" style="width: 700px; height: 200px;"></div>
    </div>
</div>

<div class="widget">
    <div class="head">
        <h5 class="iGraph">{% trans 'Clients' %}</h5>
    </div>
    <div class="body">
        <div id="graph-clients" style="width: 700px; height: 200px;"></div>
    </div>
</div>

<div class="widget">
    <div class="head">
        <h5 class="iGraph">{% trans 'Support tickets' %}</h5>
    </div>
    <div class="body">
        <div id="graph-tickets" style="width: 700px; height: 200px;"></div>
    </div>
</div>

<div class="widget simpleTabs">
    <ul class="tabs">
        <li><a href="#tab-index">{% trans 'Recent clients activity' %}</a></li>
        <li><a href="#tab-staff">{% trans 'Recent staff activity' %}</a></li>
    </ul>

    <div class="tabs_container">
        <div class="fix"></div>

        <div class="tab_content nopadding" id="tab-index">
            <table class="tableStatic wide">
                <tbody>
                {% set events = admin.activity_log_get_list({"per_page":10,"only_clients":1}) %}
                {% for i, event in events.list %}
                <tr {% if loop.first%}class="noborder"{% endif %}>
                    <td style="width: 5%"><a href="{{ 'client/manage'|alink }}/{{ event.client.id }}" title="{{ event.client.name }}"><img src="{{ event.client.email|gravatar }}?size=20" alt="{{ event.client.name }}" /></a></td>
                    <td>{{ event.client.name|truncate(40) }}</td>
                    <td><a href="{{ 'client/manage'|alink }}/{{ event.client.id }}">{{ event.message|truncate(50) }}</a></td>
                    <td>{{ event.created_at|timeago }} ago</td>
                </tr>
                </tbody>

                {% else %}
                <tbody>
                    <tr class="noborder">
                        <td colspan="4">
                            {% trans 'The list is empty' %}
                        </td>
                    </tr>
                </tbody>
                {% endfor %}
            </table>
        </div>
        
        <div class="tab_content nopadding" id="tab-staff">
            <table class="tableStatic wide">
                <tbody>
                {% set events = admin.activity_log_get_list({"per_page":10,"only_staff":1}) %}
                {% for i, event in events.list %}
                <tr {% if loop.first%}class="noborder"{% endif %}>
                    <td style="width: 5%"><a href="{{ 'staff/manage'|alink }}/{{ event.staff.id }}" title="{{ event.staff.name }}"><img src="{{ event.staff.email|gravatar }}?size=20" alt="{{ event.staff.name }}" /></a></td>
                    <td>{{ event.staff.name }}</td>
                    <td><a href="{{ 'staff/manage'|alink }}/{{ event.staff.id }}">{{ event.message|truncate(50) }}</a></td>
                    <td>{{ event.created_at|timeago }} ago</td>
                </tr>
                </tbody>

                {% else %}
                <tbody>
                    <tr class="noborder">
                        <td colspan="4">
                            {% trans 'The list is empty' %}
                        </td>
                    </tr>
                </tbody>
                {% endfor %}
            </table>
        </div>

    </div>
    
    <div class="fix"></div>
</div>

{% endblock %}

{% block js %}
<script type="text/javascript" src="js/flot/jquery.flot.js"></script>
<script type="text/javascript" src="js/flot/jquery.flot.pie.js"></script>
<script type="text/javascript" src="js/flot/excanvas.min.js"></script>
<script type="text/javascript">

$(function() {
    setPlotData('graph-income', 'admin/stats/get_income');
    setPlotData('graph-orders', 'admin/stats/get_orders');
    setPlotData('graph-invoice', 'admin/stats/get_invoices');
    setPlotData('graph-clients', 'admin/stats/get_clients');
    setPlotData('graph-tickets', 'admin/stats/get_tickets');
});

function setPlotData(id, url) {
    api.get(url, {date_from:'{{request.date_from}}', date_to:'{{request.date_to}}'}, function(result){
        $.plot($("#"+id), [ result ] , {
            yaxis: {
                min: 0,
                tickDecimals: false
            },
            xaxis: {
                mode: "time",
                tickDecimals: false,
                timeformat: "%y-%m-%d"
            },
            clickable: true,
            colors: ["#afd8f8"],
            series: {
                   lines: {
                        lineWidth: 2,
                        fill: true,
                        fillColor: { colors: [ { opacity: 0.6 }, { opacity: 0.2 } ] },
                        steps: false
                   }
                }
        });
    });
}

function setPieData(id, url) {
    api.get(url, {}, function(result){
        var d = [];
        $.each(result, function(title, value) {
            d.push({ label: title, data: [[1, value]]});
        });
        $.plot($("#"+id), d,
        {
			series: {
				pie: {
					show: true,
					radius:300,
					label: {
						show: true,
						formatter: function(label, series){
							return '<div style="font-size:11px;text-align:center;padding:2px;color:white;">'+label+'<br/>'+Math.round(series.percent)+'%</div>';
						},
						threshold: 0.1
					}
				}
			},
			legend: {
				show: false
			}
        });
    });
}
</script>
{% endblock %}