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

{% block title %}Daily Dashboard - SwiftPOS{% endblock %}

{% block topbar_title %}
Daily Sales
{% 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">Today's Sales Overview</h5>
            <div class="text-muted small">
                Branch-based daily metrics.
            </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">
                    <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>

    <!-- Metric cards -->
    <div class="row g-3 mb-3">
        <!-- Total Revenue -->
        <div class="col-12 col-sm-6 col-xl-3">
            <div class="card h-100 shadow-sm sp-animate-card">
                <div class="card-body d-flex justify-content-between align-items-center">
                    <div>
                        <div class="small text-muted text-uppercase mb-1">Revenue (Today)</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 class="rounded-circle d-flex align-items-center justify-content-center"
                         style="width:44px;height:44px;background:rgba(79,70,229,.10);color:var(--sp-primary);">
                        <i class="fas fa-sack-dollar"></i>
                    </div>
                </div>
            </div>
        </div>

        <!-- Net Profit -->
        <div class="col-12 col-sm-6 col-xl-3">
            <div class="card h-100 shadow-sm sp-animate-card">
                <div class="card-body d-flex justify-content-between align-items-center">
                    <div>
                        <div class="small text-muted text-uppercase mb-1">Net Profit</div>
                        <div class="h4 mb-1 fw-semibold text-success">
                            {{ currency_symbol }}{{ summary.total_profit|default:0|floatformat:2 }}
                        </div>
                        <div class="small text-muted">
                            Based on cost price × quantity
                        </div>
                    </div>
                    <div class="rounded-circle d-flex align-items-center justify-content-center"
                         style="width:44px;height:44px;background:rgba(16,185,129,.10);color:var(--sp-secondary);">
                        <i class="fas fa-chart-line"></i>
                    </div>
                </div>
            </div>
        </div>

        <!-- Payment Mix -->
        <div class="col-12 col-sm-6 col-xl-3">
            <div class="card h-100 shadow-sm sp-animate-card">
                <div class="card-body">
                    <div class="small text-muted text-uppercase mb-1">Payment Mix</div>
                    <div class="d-flex justify-content-between small mb-1">
                        <span>Cash</span>
                        <span>{{ currency_symbol }}{{ summary.cash_revenue|floatformat:2 }}</span>
                    </div>
                    <div class="d-flex justify-content-between small mb-1">
                        <span>Transfer</span>
                        <span>{{ currency_symbol }}{{ summary.transfer_revenue|floatformat:2 }}</span>
                    </div>
                    <div class="d-flex justify-content-between small mb-1">
                        <span>POS</span>
                        <span>{{ currency_symbol }}{{ summary.pos_revenue|floatformat:2 }}</span>
                    </div>
                    <div class="d-flex justify-content-between small">
                        <span>Mobile</span>
                        <span>{{ currency_symbol }}{{ summary.mobile_money_revenue|floatformat:2 }}</span>
                    </div>
                </div>
            </div>
        </div>

        <!-- Credit outstanding -->
        <div class="col-12 col-sm-6 col-xl-3">
            <div class="card h-100 shadow-sm sp-animate-card">
                <div class="card-body d-flex justify-content-between align-items-center">
                    <div>
                        <div class="small text-muted text-uppercase mb-1">Today's Credit</div>
                        <div class="h4 mb-1 fw-semibold text-danger">
                            {{ currency_symbol }}{{ summary.credit_outstanding|default:0|floatformat:2 }}
                        </div>
                        <div class="small text-muted">
                            Balance from today’s credit sales
                        </div>
                    </div>
                    <div class="rounded-circle d-flex align-items-center justify-content-center"
                         style="width:44px;height:44px;background:rgba(220,38,38,.12);color:#DC2626;">
                        <i class="fas fa-hand-holding"></i>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Payment method breakdown -->
    <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-chart-pie"></i> Payment Method Breakdown
            </h6>
            <div class="d-flex gap-2">
                <button type="button" class="btn btn-outline-secondary btn-sm" onclick="location.reload();">
                    <i class="fas fa-rotate"></i> Refresh
                </button>
                <button type="button" class="btn btn-outline-secondary btn-sm" onclick="window.print();">
                    <i class="fas fa-print"></i> Print
                </button>
            </div>
        </div>
        <div class="card-body">
            <div class="row g-3">
                <div class="col-md-3 col-6">
                    <div class="p-3 rounded text-center" style="background:rgba(22,163,74,.12);">
                        <i class="fas fa-money-bill-wave mb-2" style="color:var(--sp-success);"></i>
                        <div class="fw-semibold">{{ currency_symbol }}{{ summary.cash_revenue|floatformat:2 }}</div>
                        <div class="small text-muted">
                            Cash ({{ summary.cash_transactions }})
                        </div>
                    </div>
                </div>
                <div class="col-md-3 col-6">
                    <div class="p-3 rounded text-center" style="background:rgba(59,130,246,.12);">
                        <i class="fas fa-university mb-2" style="color:var(--sp-info);"></i>
                        <div class="fw-semibold">{{ currency_symbol }}{{ summary.transfer_revenue|floatformat:2 }}</div>
                        <div class="small text-muted">
                            Transfer ({{ summary.transfer_transactions }})
                        </div>
                    </div>
                </div>
                <div class="col-md-3 col-6">
                    <div class="p-3 rounded text-center" style="background:rgba(96,165,250,.12);">
                        <i class="fas fa-credit-card mb-2" style="color:var(--sp-primary-soft);"></i>
                        <div class="fw-semibold">{{ currency_symbol }}{{ summary.pos_revenue|floatformat:2 }}</div>
                        <div class="small text-muted">
                            POS ({{ summary.pos_transactions }})
                        </div>
                    </div>
                </div>
                <div class="col-md-3 col-6">
                    <div class="p-3 rounded text-center" style="background:rgba(16,185,129,.12);">
                        <i class="fas fa-mobile-screen mb-2" style="color:var(--sp-secondary);"></i>
                        <div class="fw-semibold">{{ currency_symbol }}{{ summary.mobile_money_revenue|floatformat:2 }}</div>
                        <div class="small text-muted">
                            Mobile ({{ summary.mobile_money_transactions }})
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Top selling items & low stock -->
    <div class="row g-3">
        <div class="col-lg-6">
            <div class="card h-100 sp-animate-card">
                <div class="card-header">
                    <h6 class="card-title mb-0 d-flex align-items-center gap-2">
                        <i class="fas fa-chart-line"></i> Top Selling Items (Today)
                    </h6>
                </div>
                <div class="card-body">
                    {% if top_selling_items %}
                        {% for item in top_selling_items %}
                            <div class="d-flex justify-content-between align-items-center mb-2">
                                <div>
                                    <div class="fw-semibold small">{{ item.product__name }}</div>
                                    <div class="text-muted small">{{ item.product__sku }}</div>
                                </div>
                                <div class="text-end">
                                    <div class="text-success small">{{ currency_symbol }}{{ item.total_revenue|floatformat:2 }}</div>
                                    <div class="text-muted small">{{ item.total_quantity }} sold</div>
                                </div>
                            </div>
                        {% endfor %}
                    {% else %}
                        <p class="text-muted small mb-0 text-center">No sales recorded for this branch today.</p>
                    {% endif %}
                </div>
            </div>
        </div>

        <!-- Low stock quick view -->
        <div class="col-lg-6">
            <div class="card h-100 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-box-open"></i> Low Stock (Quick View)
                    </h6>
                    <a href="{% url 'inventory:product_list' %}"
                       class="small text-muted text-decoration-none">
                        Manage stock
                    </a>
                </div>
                <div class="card-body">
                    {% if low_stock_products %}
                        {% for product in low_stock_products %}
                            <div class="d-flex justify-content-between align-items-center mb-2">
                                <div>
                                    <div class="fw-semibold small">{{ product.name }}</div>
                                    <div class="text-muted small">{{ product.sku }}</div>
                                </div>
                                <div class="text-end">
                                    <span class="badge bg-warning text-dark">
                                        {{ product.stock_quantity }}
                                    </span>
                                </div>
                            </div>
                        {% endfor %}
                    {% else %}
                        <p class="text-muted small mb-0 text-center">
                            No low stock alerts.
                        </p>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>

</div>
{% endblock %}
