{% extends '@CpCreationVitiCore/admin/base.html.twig' %}

{% block title %}Statistiques commandes{% endblock %}

{% block stylesheets %}
    <style>
        .order-stats-card {
            min-height: 138px;
        }

        .order-stats-value {
            font-size: 30px;
            font-weight: 700;
            line-height: 1.15;
            margin-top: 8px;
        }

        .order-stats-label {
            color: #6b7280;
            font-size: 12px;
            letter-spacing: 0.04em;
            text-transform: uppercase;
        }

        #order-stats-chart {
            height: 420px;
        }

        .order-stats-subtle {
            color: #6b7280;
            font-size: 12px;
        }
    </style>
{% endblock %}

{% block content %}
    <div class="row">
        <div class="col-lg-12">
            <h1 class="page-header">Statistiques commandes</h1>
        </div>
    </div>

    <div class="panel panel-default">
        <div class="panel-heading">
            Filtres
        </div>
        <div class="panel-body">
            <form method="get" action="{{ path('admin_order_stats') }}">
                <div class="row">
                    <div class="col-md-3">
                        <div class="form-group">
                            <label for="payment_method_id">Mode de paiement</label>
                            <select name="payment_method_id" id="payment_method_id" class="form-control">
                                <option value="">Tous</option>
                                {% for payment_method in payment_methods %}
                                    <option value="{{ payment_method.id }}" {% if filters.payment_method_id == payment_method.id.toString %}selected{% endif %}>
                                        {{ payment_method.method|trans }}
                                    </option>
                                {% endfor %}
                            </select>
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="form-group">
                            <label for="paid_from">Payée du</label>
                            <input type="date" name="paid_from" id="paid_from" class="form-control" value="{{ filters.paid_from_input }}">
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="form-group">
                            <label for="paid_to">au</label>
                            <input type="date" name="paid_to" id="paid_to" class="form-control" value="{{ filters.paid_to_input }}">
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="form-group">
                            <label for="group_by">Regroupement</label>
                            <select name="group_by" id="group_by" class="form-control">
                                <option value="day" {% if filters.group_by == 'day' %}selected{% endif %}>Jour</option>
                                <option value="week" {% if filters.group_by == 'week' %}selected{% endif %}>Semaine</option>
                                <option value="month" {% if filters.group_by == 'month' %}selected{% endif %}>Mois</option>
                                <option value="year" {% if filters.group_by == 'year' %}selected{% endif %}>Année</option>
                            </select>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <label>&nbsp;</label>
                        <div>
                            <button type="submit" class="btn btn-primary">Filtrer</button>
                            <a href="{{ path('admin_order_stats') }}" class="btn btn-default">Reinitialiser</a>
                            <a href="{{ path('admin_order') }}" class="btn btn-info">Voir les commandes</a>
                        </div>
                    </div>
                </div>
            </form>

            <p class="order-stats-subtle" style="margin-bottom: 0;">
                Donnees calculees uniquement sur les commandes payees, a partir des montants historises dans les JSON de commande.
            </p>
        </div>
    </div>

    <div class="row">
        <div class="col-md-3">
            <div class="panel panel-default order-stats-card">
                <div class="panel-body">
                    <div class="order-stats-label">Chiffre d'affaire TTC</div>
                    <div class="order-stats-value">{{ (stats.summary.revenue_ttc / 100)|number_format(2, ',', ' ') }} €</div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="panel panel-default order-stats-card">
                <div class="panel-body">
                    <div class="order-stats-label">Commandes payees</div>
                    <div class="order-stats-value">{{ stats.summary.order_count }}</div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="panel panel-default order-stats-card">
                <div class="panel-body">
                    <div class="order-stats-label">Panier moyen TTC</div>
                    <div class="order-stats-value">{{ (stats.summary.average_basket_ttc / 100)|number_format(2, ',', ' ') }} €</div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="panel panel-default order-stats-card">
                <div class="panel-body">
                    <div class="order-stats-label">Equivalence moyenne</div>
                    <div class="order-stats-value">{{ stats.summary.average_equivalence|number_format(2, ',', ' ') }}</div>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-md-3">
            <div class="panel panel-default order-stats-card">
                <div class="panel-body">
                    <div class="order-stats-label">Remises tarifaires produits</div>
                    <div class="order-stats-value">{{ (stats.summary.discount_ttc / 100)|number_format(2, ',', ' ') }} €</div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="panel panel-default order-stats-card">
                <div class="panel-body">
                    <div class="order-stats-label">Montant degressif</div>
                    <div class="order-stats-value">{{ (stats.summary.degressive_ttc / 100)|number_format(2, ',', ' ') }} €</div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="panel panel-default order-stats-card">
                <div class="panel-body">
                    <div class="order-stats-label">Promos produits</div>
                    <div class="order-stats-value">{{ (stats.summary.promo_items_ttc / 100)|number_format(2, ',', ' ') }} €</div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="panel panel-default order-stats-card">
                <div class="panel-body">
                    <div class="order-stats-label">Promos frais de port</div>
                    <div class="order-stats-value">{{ (stats.summary.promo_shipping_ttc / 100)|number_format(2, ',', ' ') }} €</div>
                </div>
            </div>
        </div>
    </div>

    <div class="panel panel-default">
        <div class="panel-heading">
            Evolution des commandes et du chiffre d'affaire
        </div>
        <div class="panel-body">
            {% if stats.summary.order_count > 0 %}
                <div id="order-stats-chart" data-chart="{{ stats.chart|json_encode|e('html_attr') }}"></div>
            {% else %}
                <div class="alert alert-info" style="margin-bottom: 0;">
                    Aucune commande payee ne correspond aux filtres selectionnes.
                </div>
            {% endif %}
        </div>
    </div>

    <div class="row">
        <div class="col-md-8">
            <div class="panel panel-default">
                <div class="panel-heading">
                    Top produits commandes
                </div>
                <div class="table-responsive">
                    <table class="table table-bordered table-striped" style="margin-bottom: 0;">
                        <thead>
                        <tr>
                            <th>#</th>
                            <th>Produit</th>
                            <th>Quantite</th>
                            <th>CA net TTC</th>
                        </tr>
                        </thead>
                        <tbody>
                        {% for product in stats.top_products %}
                            <tr>
                                <td>{{ loop.index }}</td>
                                <td>{{ product.name ? product.name|upper : '-' }}</td>
                                <td>{{ product.quantity }}</td>
                                <td>{{ (product.revenue_ttc / 100)|number_format(2, ',', ' ') }} €</td>
                            </tr>
                        {% else %}
                            <tr>
                                <td colspan="4" class="text-center text-muted">Aucune donnee produit.</td>
                            </tr>
                        {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <div class="col-md-4">
            <div class="panel panel-default">
                <div class="panel-heading">
                    Lecture rapide
                </div>
                <table class="table table-bordered" style="margin-bottom: 0;">
                    <tbody>
                    <tr>
                        <th>Periode analysee</th>
                        <td>{{ filters.paid_from_input }} au {{ filters.paid_to_input }}</td>
                    </tr>
                    <tr>
                        <th>Regroupement</th>
                        <td>
                            {% if filters.group_by == 'day' %}
                                Jour
                            {% elseif filters.group_by == 'week' %}
                                Semaine
                            {% elseif filters.group_by == 'year' %}
                                Annee
                            {% else %}
                                Mois
                            {% endif %}
                        </td>
                    </tr>
                    <tr>
                        <th>Base de calcul</th>
                        <td>Date de paiement</td>
                    </tr>
                    <tr>
                        <th>Source montants</th>
                        <td>JSON `cartPricing` et `cartPromos`</td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
{% endblock %}

{% block javascripts %}
    <script src="{{ asset('bundles/cpcreationviticore/js/admin/lib/echarts.min.js') }}"></script>
    <script src="{{ asset('bundles/cpcreationviticore/js/admin/order-stats.js') }}"></script>
{% endblock %}
