{% extends 'base.html' %}
{% load i18n %}

{% block title %}{% trans "Expense Report" %} | {{ settings.business_name }}{% endblock %}

{% block content %}
<div class="container-fluid py-4">
    <!-- Print Header (Visible only when printing) -->
    <div class="d-none d-print-block mb-4">
        <div class="row align-items-center">
            <div class="col-6">
                {% if settings.logo %}
                    <img src="{{ settings.logo.url }}" alt="{{ settings.business_name }}" style="max-height: 80px;">
                {% else %}
                    <h2 class="fw-bold mb-0">{{ settings.business_name }}</h2>
                {% endif %}
            </div>
            <div class="col-6 text-end">
                <h1 class="h3 fw-bold mb-0">{% trans "Expense Report" %}</h1>
                <p class="text-muted mb-0">
                    {% if start_date %}{{ start_date }}{% else %}{% trans "Start" %}{% endif %} - 
                    {% if end_date %}{{ end_date }}{% else %}{% trans "Today" %}{% endif %}
                </p>
            </div>
        </div>
        <hr>
    </div>

    <!-- Screen Header -->
    <div class="row mb-4 align-items-center d-print-none">
        <div class="col-md-6">
            <h1 class="h3 fw-bold mb-0">{% trans "Expense Report" %}</h1>
            <p class="text-muted">{% trans "Detailed summary of business expenses." %}</p>
        </div>
        <div class="col-md-6 text-md-end">
            <div class="btn-group">
                <button onclick="window.print()" class="btn btn-outline-primary">
                    <i class="bi bi-printer"></i> {% trans "Print / PDF" %}
                </button>
                <a href="{% url 'export_expenses_excel' %}?{{ request.GET.urlencode }}" class="btn btn-outline-success">
                    <i class="bi bi-file-earmark-excel"></i> {% trans "Export to Excel" %}
                </a>
            </div>
        </div>
    </div>

    <!-- Filters -->
    <div class="card border-0 shadow-sm mb-4 d-print-none">
        <div class="card-body">
            <form method="get" class="row g-3">
                <div class="col-md-3">
                    <label class="form-label small fw-bold">{% trans "Start Date" %}</label>
                    <input type="date" name="start_date" class="form-control" value="{{ start_date|default:'' }}">
                </div>
                <div class="col-md-3">
                    <label class="form-label small fw-bold">{% trans "End Date" %}</label>
                    <input type="date" name="end_date" class="form-control" value="{{ end_date|default:'' }}">
                </div>
                <div class="col-md-4">
                    <label class="form-label small fw-bold">{% trans "Category" %}</label>
                    <select name="category" class="form-select">
                        <option value="">{% trans "All Categories" %}</option>
                        {% for cat in categories %}
                        <option value="{{ cat.id }}" {% if category_id == cat.id %}selected{% endif %}>
                            {{ cat.name_en }} / {{ cat.name_ar }}
                        </option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-2 d-flex align-items-end">
                    <button type="submit" class="btn btn-primary w-100">
                        <i class="bi bi-filter"></i> {% trans "Filter" %}
                    </button>
                </div>
            </form>
        </div>
    </div>

    <!-- Summary Card -->
    <div class="row mb-4">
        <div class="col-md-4 ms-auto">
            <div class="card border-0 shadow-sm bg-danger text-white">
                <div class="card-body p-4 text-center">
                    <h6 class="text-uppercase small fw-bold opacity-75">{% trans "Total Expenses" %}</h6>
                    <h2 class="fw-bold mb-0">{{ settings.currency_symbol }} {{ total_amount|floatformat:settings.decimal_places }}</h2>
                </div>
            </div>
        </div>
    </div>

    <!-- Detailed Transactions -->
    <div class="card border-0 shadow-sm">
        <div class="card-header bg-white py-3 d-print-none">
            <h5 class="card-title mb-0 fw-bold">{% trans "Expense Details" %}</h5>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead class="bg-light">
                        <tr>
                            <th class="ps-4">{% trans "Date" %}</th>
                            <th>{% trans "Category" %}</th>
                            <th>{% trans "Description" %}</th>
                            <th>{% trans "Payment Method" %}</th>
                            <th>{% trans "Recorded By" %}</th>
                            <th class="text-end pe-4">{% trans "Amount" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for expense in expenses %}
                        <tr>
                            <td class="ps-4">{{ expense.date|date:"d M Y" }}</td>
                            <td>
                                <span class="badge bg-secondary-soft text-dark rounded-pill">
                                    {{ expense.category.name_en }}
                                </span>
                            </td>
                            <td>
                                {% if expense.description %}
                                    {{ expense.description }}
                                {% else %}
                                    <span class="text-muted fst-italic">{% trans "No description" %}</span>
                                {% endif %}
                                {% if expense.attachment %}
                                    <a href="{{ expense.attachment.url }}" target="_blank" class="ms-2 d-print-none">
                                        <i class="bi bi-paperclip"></i>
                                    </a>
                                {% endif %}
                            </td>
                            <td>{{ expense.payment_method.name_en|default:"-" }}</td>
                            <td><small class="text-muted">{{ expense.created_by.username }}</small></td>
                            <td class="text-end fw-bold pe-4">
                                {{ settings.currency_symbol }} {{ expense.amount|floatformat:settings.decimal_places }}
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="6" class="text-center py-5 text-muted">
                                <i class="bi bi-inbox fs-1 d-block mb-3"></i>
                                {% trans "No expenses found for this period." %}
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<style>
    .bg-secondary-soft { background-color: rgba(108, 117, 125, 0.1); }
    
    @media print {
        .d-print-none { display: none !important; }
        .d-print-block { display: block !important; }
        .card { border: none !important; box-shadow: none !important; }
        body { background: white !important; font-size: 12px; }
        .container-fluid { padding: 0 !important; }
        .table { border: 1px solid #dee2e6 !important; }
        .table thead th { background-color: #f8f9fa !important; border-bottom: 2px solid #dee2e6 !important; -webkit-print-color-adjust: exact; }
        .badge { border: 1px solid #ccc !important; color: black !important; background: none !important; }
        .bg-danger { background-color: transparent !important; color: #dc3545 !important; border: 2px solid #dc3545 !important; }
        .text-white { color: inherit !important; }
        hr { border-top: 1px solid #000 !important; opacity: 1 !important; }
    }
</style>
{% endblock %}