{% extends 'core/base.html' %}
{% load static %}

{% block title %}Yesterday Summary - SwiftPOS{% endblock %}

{% block topbar_title %}
Yesterday Summary
{% endblock %}

{% block content %}
<div class="container-fluid px-0">

    <!-- Toast Messages -->
    <div class="swiftpos-toast-container">
        {% if messages %}
            {% for message in messages %}
                <div class="swiftpos-toast swiftpos-toast-{{ message.tags }}">
                    <div class="toast-icon">
                        {% if message.tags == 'success' %}
                            <i class="fas fa-check-circle"></i>
                        {% elif message.tags == 'error' or message.tags == 'danger' %}
                            <i class="fas fa-exclamation-circle"></i>
                        {% elif message.tags == 'warning' %}
                            <i class="fas fa-exclamation-triangle"></i>
                        {% else %}
                            <i class="fas fa-info-circle"></i>
                        {% endif %}
                    </div>
                    <div class="toast-text">
                        {{ message }}
                    </div>
                </div>
            {% endfor %}
        {% endif %}
    </div>

    <!-- Branch selector (Option B) -->
    <div class="d-flex flex-wrap justify-content-between align-items-center mb-3 gap-2">
        <div>
            <h5 class="mb-0">Yesterday's Performance</h5>
            <div class="text-muted small">
                Branch-based breakdown for yesterday.
            </div>
        </div>

        <div>
            {% if user_role == 'admin' or user_role == 'superadmin' or user_role == 'super_admin' %}
                <form method="get" class="d-flex gap-2 align-items-center">
                    <!-- Keep filters when switching branch -->
                    {% if payment_method %}
                        <input type="hidden" name="payment_method" value="{{ payment_method }}">
                    {% endif %}
                    {% if min_amount %}
                        <input type="hidden" name="min_amount" value="{{ min_amount }}">
                    {% endif %}

                    <label class="small text-muted me-1 mb-0">Branch:</label>
                    <select name="branch" class="form-select form-select-sm" onchange="this.form.submit()">
                        <option value="all" {% if selected_branch_id == 'all' %}selected{% endif %}>
                            All branches
                        </option>
                        {% for branch in branches %}
                            <option value="{{ branch.id }}" {% if selected_branch_id|default:'' == branch.id|stringformat:'s' %}selected{% endif %}>
                                {{ branch.name }}
                            </option>
                        {% endfor %}
                    </select>
                </form>
            {% else %}
                <div class="small text-muted">
                    Branch: <strong>{{ request.user.branch.name|default:"Not assigned" }}</strong>
                </div>
            {% endif %}
        </div>
    </div>

    <!-- Top summary cards -->
    <div class="row g-3 mb-3">
        <!-- Yesterday Revenue -->
        <div class="col-12 col-sm-6 col-lg-3">
            <div class="card h-100 shadow-sm sp-animate-card">
                <div class="card-body">
                    <div class="small text-muted text-uppercase mb-1">Revenue (Yesterday)</div>
                    <div class="h4 mb-1 fw-semibold">{{ currency_symbol }}{{ summary.total_revenue|floatformat:2 }}</div>
                    <div class="small text-muted">
                        {{ summary.total_transactions }} transaction{{ summary.total_transactions|pluralize }}
                    </div>
                </div>
            </div>
        </div>

        <!-- Cash -->
        <div class="col-12 col-sm-6 col-lg-3">
            <div class="card h-100 shadow-sm sp-animate-card">
                <div class="card-body">
                    <div class="small text-muted text-uppercase mb-1">Cash</div>
                    <div class="h5 mb-1">{{ currency_symbol }}{{ summary.cash_revenue|floatformat:2 }}</div>
                    <div class="small text-muted">
                        {{ summary.cash_transactions }} cash txn
                    </div>
                </div>
            </div>
        </div>

        <!-- Transfers -->
        <div class="col-12 col-sm-6 col-lg-3">
            <div class="card h-100 shadow-sm sp-animate-card">
                <div class="card-body">
                    <div class="small text-muted text-uppercase mb-1">Transfers</div>
                    <div class="h5 mb-1">{{ currency_symbol }}{{ summary.transfer_revenue|floatformat:2 }}</div>
                    <div class="small text-muted">
                        {{ summary.transfer_transactions }} transfer txn
                    </div>
                </div>
            </div>
        </div>

        <!-- POS + Mobile combined -->
        <div class="col-12 col-sm-6 col-lg-3">
            <div class="card h-100 shadow-sm sp-animate-card">
                <div class="card-body">
                    <div class="small text-muted text-uppercase mb-1">Card + Mobile</div>
                    <div class="h5 mb-1">
                        {{ currency_symbol }}{{ summary.pos_revenue|floatformat:2|add:summary.mobile_money_revenue|default:0 }}
                    </div>
                    <div class="small text-muted">
                        POS: {{ currency_symbol }}{{ summary.pos_revenue|floatformat:2 }} · Mobile: {{ currency_symbol }}{{ summary.mobile_money_revenue|floatformat:2 }}
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Unusual transactions -->
    <div class="card mb-3 sp-animate-card">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h6 class="card-title mb-0 d-flex align-items-center gap-2">
                <i class="fas fa-triangle-exclamation"></i> Unusual Transactions
            </h6>
            <span class="small text-muted">
                Orders above {{ currency_symbol }}{{ min_amount|default:"0"|floatformat:2 }} or flagged manually.
            </span>
        </div>
        <div class="card-body p-0">
            <div class="sp-table-container" style="font-size: 8px !important">
                <table class="table align-middle mb-0 table-hover" style="font-size: 8px !important">
                    <thead>
                    <tr>
                        <th>Order Details</th>
                        <th>Amount</th>
                        <th>Status</th>
                        <th>Actions</th>
                    </tr>
                    </thead>
                    <tbody>
                    {% if unusual_transactions %}
                        {% for unusual in unusual_transactions %}
                            <tr>
                                <td>
                                    <div class="fw-semibold small">
                                        <a href="{% url 'sales:print_receipt' unusual.order.id %}"
                                           class="text-decoration-none text-primary">
                                            {{ unusual.order.order_number }}
                                        </a>
                                    </div>
                                    <div class="text-muted small">
                                        {{ unusual.order.created_at|date:"Y-m-d H:i" }}
                                    </div>
                                    <div class="small text-muted">
                                        Payment: {{ unusual.order.get_payment_method_display }}
                                    </div>
                                    <div class="small text-muted">
                                        Flagged By: {{ unusual.flagged_by|default:"-" }}
                                    </div>
                                </td>
                                <td>
                                    {{ currency_symbol }}{{ unusual.order.final_amount|floatformat:2 }}
                                </td>
                                <td>
                                    {% if unusual.resolved %}
                                        <span class="badge bg-success-subtle text-success border border-success-subtle">
                                            Resolved
                                        </span>
                                    {% else %}
                                        <span class="badge bg-warning text-dark">
                                            Pending
                                        </span>
                                    {% endif %}
                                </td>
                                <td>
                                    <button class="btn btn-sm btn-outline-secondary">
                                        View Details
                                    </button>
                                </td>
                            </tr>
                        {% endfor %}
                    {% else %}
                        <tr>
                            <td colspan="4" class="text-center text-muted small py-3">
                                No unusual transactions flagged for this branch yesterday.
                            </td>
                        </tr>
                    {% endif %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Yesterday orders list -->
    <div class="card sp-animate-card">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h6 class="card-title mb-0 d-flex align-items-center gap-2">
                <i class="fas fa-list"></i> Yesterday Orders
            </h6>
            <div class="d-flex gap-2">
                <form method="get" class="d-flex gap-2 align-items-center">
                    <!-- keep branch + filters for export -->
                    <input type="hidden" name="export" value="csv">
                    {% if selected_branch_id %}
                        <input type="hidden" name="branch" value="{{ selected_branch_id }}">
                    {% endif %}
                    {% if payment_method %}
                        <input type="hidden" name="payment_method" value="{{ payment_method }}">
                    {% endif %}
                    {% if min_amount %}
                        <input type="hidden" name="min_amount" value="{{ min_amount }}">
                    {% endif %}
                    <button type="submit" class="btn btn-outline-secondary btn-sm">
                        <i class="fas fa-file-export me-1"></i> Export CSV
                    </button>
                </form>

                <button type="button" class="btn btn-outline-secondary btn-sm" onclick="window.print();">
                    <i class="fas fa-print me-1"></i> Print
                </button>
            </div>
        </div>
        <div class="card-body p-0">
            <div class="sp-table-container">
                <table class="table align-middle mb-0 table-hover">
                    <thead>
                    <tr>
                        <th>Order Details</th>
                        <th>Customer</th>
                        <th>Amount</th>
                        <th>Cashier</th>
                    </tr>
                    </thead>
                    <tbody>
                    {% if yesterday_orders %}
                        {% for order in yesterday_orders %}
                            <tr>
                                <td>
                                    <div class="fw-semibold">
                                        <a href="{% url 'sales:print_receipt' order.id %}"
                                           class="text-decoration-none text-primary">
                                            {{ order.order_number }}
                                        </a>
                                    </div>
                                    <div class="text-muted small">
                                        {{ order.created_at|date:"Y-m-d H:i" }}
                                    </div>
                                    <div class="text-muted small">
                                        Payment: {{ order.get_payment_method_display }}
                                    </div>
                                </td>
                                <td>
                                    {{ order.customer_name|default:"Walk-in" }}
                                </td>
                                <td class="text-end">
                                    {{ currency_symbol }}{{ order.final_amount|floatformat:2 }}
                                </td>
                                <td>
                                    {{ order.cashier }}
                                </td>
                            </tr>
                        {% endfor %}
                    {% else %}
                        <tr>
                            <td colspan="4" class="text-center text-muted small py-3">
                                No orders were found for this branch yesterday.
                            </td>
                        </tr>
                    {% endif %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>

</div>
{% endblock %}
