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

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

{% block topbar_title %}
Payment Summary
{% endblock %}

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

    <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>
    <!-- Page Header -->
    <div class="sp-page-header">
        <div>
            <h1 class="sp-page-title"><i class="fas fa-chart-pie"></i>Payment Summary</h1>
            <p class="sp-page-subtitle">Today's sales totals and payment method breakdown.</p>
        </div>
        <button type="button" class="btn btn-outline-secondary btn-sm" onclick="window.print()">
            <i class="fas fa-print me-1"></i> Print
        </button>
    </div>

    <!-- Top summary cards -->
    <div class="row g-3 mb-3">
    <!-- Today Total -->
    <div class="col-12 col-md-4">
        <div class="card h-100 shadow-sm">
            <div class="card-body">
                <div class="d-flex justify-content-between align-items-center mb-1">
                    <div class="small text-muted text-uppercase">
                        Today’s Sales
                    </div>
                    <span class="badge bg-primary-subtle text-primary">
                        {{ today_summary.total_transactions|default:0 }} txn
                    </span>
                </div>
                <div class="h4 fw-semibold mb-1">
                    {{ currency_symbol }}{{ today_summary.total_sales|default:0|floatformat:2 }}
                </div>
                <div class="small text-muted">
                    Across all payment methods
                </div>
            </div>
        </div>
    </div>

    <!-- Cash / Transfer -->
    <div class="col-12 col-md-4">
        <div class="card h-100 shadow-sm">
            <div class="card-body">
                <div class="small text-muted text-uppercase mb-2">Cash & Transfer</div>
                <div class="d-flex justify-content-between small mb-1">
                    <span>Cash</span>
                    <span>{{ currency_symbol }}{{ today_summary.cash_sales|default:0|floatformat:2 }}</span>
                </div>
                <div class="d-flex justify-content-between small">
                    <span>Transfer</span>
                    <span>{{ currency_symbol }}{{ today_summary.transfer_sales|default:0|floatformat:2 }}</span>
                </div>
            </div>
        </div>
    </div>

    <!-- POS / Mobile -->
    <div class="col-12 col-md-4">
        <div class="card h-100 shadow-sm">
            <div class="card-body">
                <div class="small text-muted text-uppercase mb-2">POS & Mobile</div>
                <div class="d-flex justify-content-between small mb-1">
                    <span>POS</span>
                    <span>{{ currency_symbol }}{{ today_summary.pos_sales|default:0|floatformat:2 }}</span>
                </div>
                <div class="d-flex justify-content-between small">
                    <span>Mobile Money</span>
                    <span>{{ currency_symbol }}{{ today_summary.mobile_money_sales|default:0|floatformat:2 }}</span>
                </div>
            </div>
        </div>
    </div>
</div>


    <!-- Today highlight -->
    {% if today_summary %}
    <div class="card mb-3 shadow-sm">
        <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-calendar-day"></i> Today Overview
            </h6>
            <span class="small text-muted">
                {{ today_summary.total_transactions|default:0 }} transaction{{ today_summary.total_transactions|default:0|pluralize }}
            </span>
        </div>
        <div class="card-body">
            <div class="row g-3">
                <div class="col-6 col-md-3">
                    <div class="p-3 rounded-3 text-center" style="background:var(--sp-surface-muted);">
                        <div class="small text-muted mb-1">Total Sales</div>
                        <div class="fw-semibold">{{ currency_symbol }}{{ today_summary.total_sales|default:0|floatformat:2 }}</div>
                    </div>
                </div>
                <div class="col-6 col-md-3">
                    <div class="p-3 rounded-3 text-center" style="background:var(--sp-surface-muted);">
                        <div class="small text-muted mb-1">Cash</div>
                        <div class="fw-semibold">{{ today_summary.cash_transactions|default:0 }}</div>
                    </div>
                </div>
                <div class="col-6 col-md-3">
                    <div class="p-3 rounded-3 text-center" style="background:var(--sp-surface-muted);">
                        <div class="small text-muted mb-1">POS</div>
                        <div class="fw-semibold">{{ today_summary.pos_transactions|default:0 }}</div>
                    </div>
                </div>
                <div class="col-6 col-md-3">
                    <div class="p-3 rounded-3 text-center" style="background:var(--sp-surface-muted);">
                        <div class="small text-muted mb-1">Transactions</div>
                        <div class="fw-semibold">{{ today_summary.total_transactions|default:0 }}</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    {% else %}
        <div class="card mb-3 shadow-sm">
            <div class="card-body text-center">
                <p class="text-muted small mb-0">No summary available for today.</p>
            </div>
        </div>
    {% endif %}

    <!-- Payment method distribution -->
    <div class="card mb-3 shadow-sm">
        <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-chart-pie"></i> Payment Method Distribution
            </h6>
            <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 class="card-body">
            {% if payment_methods %}
                <div class="row g-3">
                    {% for method in payment_methods %}
                        <div class="col-12 col-sm-6 col-lg-3">
                            <div class="p-3 rounded-3 h-100 text-center" style="background:var(--sp-surface-muted);">
                                <div class="fw-semibold mb-1">
                                    {{ method.display_name }}
                                </div>
                                <div class="small text-muted mb-1">
                                    {{ method.count|default:0 }} transaction{{ method.count|default:0|pluralize }}
                                </div>
                                <div class="h6 mb-1">{{ currency_symbol }}{{ method.total|default:0|floatformat:2 }}</div>
                                <div class="small text-muted">
                                    {{ method.percentage|default:0 }}% of total
                                </div>
                            </div>
                        </div>
                    {% endfor %}
                </div>
            {% else %}
                <p class="text-muted small mb-0">No payment data available for this period.</p>
            {% endif %}
        </div>
    </div>

    <!-- Recent daily summaries -->
    <div class="card shadow-sm">
        <div class="card-header">
            <h6 class="card-title mb-0 d-flex align-items-center gap-2">
                <i class="fas fa-history"></i> Recent Daily Summaries
            </h6>
        </div>
        <div class="card-body p-0">
            <div class="sp-table-container">
                <table class="table align-middle mb-0 table-hover">
                    <thead>
                        <tr>
                            <th>Date</th>
                            <th>Total Sales</th>
                            <th>Breakdown</th>
                            <th>Transactions</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% if recent_summaries %}
                            {% for summary in recent_summaries %}
                                <tr>
                                    <td>{{ summary.date|date:"M d, Y" }}</td>
                                    <td class="text-success">{{ currency_symbol }}{{ summary.total_sales|default:0|floatformat:2 }}</td>
                                    <td>
                                        <div class="small text-muted">
                                            Cash: {{ currency_symbol }}{{ summary.cash_sales|default:0|floatformat:2 }},
                                            POS: {{ currency_symbol }}{{ summary.pos_sales|default:0|floatformat:2 }},
                                            Transfer: {{ currency_symbol }}{{ summary.transfer_sales|default:0|floatformat:2 }},
                                            Mobile: {{ currency_symbol }}{{ summary.mobile_money_sales|default:0|floatformat:2 }}
                                        </div>
                                    </td>
                                    <td>{{ summary.total_transactions|default:0 }}</td>
                                </tr>
                            {% endfor %}
                        {% else %}
                            <tr>
                                <td colspan="4" class="text-center text-muted small py-3">
                                    No recent summaries to display.
                                </td>
                            </tr>
                        {% endif %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>

</div>
{% endblock %}