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

{% block title %}Branch: {{ branch.name }} - SwiftPOS{% endblock %}

{% block topbar_title %}
Branch Detail
{% 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>

    <!-- Header -->
    <div class="d-flex flex-wrap justify-content-between align-items-center mb-3 gap-2">
        <div>
            <h5 class="mb-0">
                {{ branch.name }}
                {% if not branch.is_active %}
                    <span class="badge bg-danger-subtle text-danger">Inactive</span>
                {% endif %}
            </h5>
            <div class="text-muted small">
                {{ branch.address|default:"No address provided" }}
                {% if branch.phone %}
                    · {{ branch.phone }}
                {% endif %}
            </div>
        </div>
    </div>

    <!-- Metrics -->
    <div class="row g-3 mb-3">
        <div class="col-12 col-sm-6 col-xl-3">
            <div class="metric-card sp-animate-card">
                <div class="metric-card-title">Today Sales</div>
                <div class="metric-card-value">{{ currency_symbol }}{{ today_sales|floatformat:2 }}</div>
                <div class="metric-card-sub">
                    {{ total_orders_today }} order{{ total_orders_today|pluralize }}
                </div>
            </div>
        </div>

        <div class="col-12 col-sm-6 col-xl-3">
            <div class="metric-card sp-animate-card">
                <div class="metric-card-title">This Month Sales</div>
                <div class="metric-card-value">{{ currency_symbol }}{{ month_sales|floatformat:2 }}</div>
                <div class="metric-card-sub">
                    {{ total_orders_month }} order{{ total_orders_month|pluralize }}
                </div>
            </div>
        </div>

        <div class="col-12 col-sm-6 col-xl-3">
            <div class="metric-card sp-animate-card">
                <div class="metric-card-title">Users in Branch</div>
                <div class="metric-card-value">{{ users|length }}</div>
                <div class="metric-card-sub">
                    Admins, managers & cashiers
                </div>
            </div>
        </div>
    </div>

    <!-- Two-column layout: Users + Today Orders -->
    <div class="row g-3">
        <!-- Users table -->
        <div class="col-lg-5">
            <div class="card sp-animate-card h-100">
                <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-users"></i> Branch Users
                    </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>User</th>
                                    <th>Role</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% if users %}
                                    {% for u in users %}
                                        <tr>
                                            <td>
                                                <div class="fw-semibold small">
                                                    {{ u.get_full_name|default:u.username }}
                                                </div>
                                                <div class="small text-muted">
                                                    {{ u.email|default:"" }}
                                                </div>
                                            </td>
                                            <td>
                                                <span class="badge bg-info-subtle">
                                                    {{ u.role|title }}
                                                </span>
                                            </td>
                                        </tr>
                                    {% endfor %}
                                {% else %}
                                    <tr>
                                        <td colspan="2" class="text-center text-muted small py-3">
                                            No users assigned to this branch yet.
                                        </td>
                                    </tr>
                                {% endif %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        <!-- Today orders -->
        <div class="col-lg-7">
            <div class="card sp-animate-card h-100">
                <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> Today Orders
                    </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>Order</th>
                                    <th>Customer</th>
                                    <th>Amount</th>
                                    <th>Cashier</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% if orders_today %}
                                    {% for order in orders_today %}
                                        <tr>
                                            <td>
                                                <div class="fw-semibold small">
                                                    <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>
                                            </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 recorded today for this branch.
                                        </td>
                                    </tr>
                                {% endif %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

</div>
{% endblock %}
