{% extends "base.html" %}
{% load static %}

{% block title %}Income Statement{% endblock %}

{% block content %}
<div class="container mt-4">
    <div class="card">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h2 class="mb-0">Income Statement</h2>
            <div>
                <form method="get" class="d-inline me-2">
                    <div class="row g-2">
                        <div class="col-md-5">
                            <label class="form-label">From</label>
                            <input type="date" name="start_date" 
                                   value="{{ start_date|date:'Y-m-d' }}" 
                                   class="form-control" required>
                        </div>
                        <div class="col-md-5">
                            <label class="form-label">To</label>
                            <input type="date" name="end_date" 
                                   value="{{ end_date|date:'Y-m-d' }}" 
                                   class="form-control" required>
                        </div>
                        <div class="col-md-2 d-flex align-items-end">
                            <button type="submit" class="btn btn-primary">
                                <i class="bi bi-filter"></i> Filter
                            </button>
                        </div>
                    </div>
                </form>
                <a href="?start_date={{ start_date|date:'Y-m-d' }}&end_date={{ end_date|date:'Y-m-d' }}&export=pdf" 
                   class="btn btn-danger">
                    <i class="bi bi-file-earmark-pdf"></i> Export PDF
                </a>
            </div>
        </div>
        
        <div class="card-body">
            <div class="d-flex justify-content-between mb-3">
                <h5>For Period {{ start_date|date:"F j, Y" }} to {{ end_date|date:"F j, Y" }}</h5>
            </div>
            
            <!-- Revenue Section -->
            <div class="mb-4">
                <h4 class="border-bottom pb-2">Revenue</h4>
                <div class="table-responsive">
                    <table class="table">
                        <tbody>
                            {% for revenue in revenues %}
                            {% if revenue.total %}
                            <tr>
                                <td>{{ revenue.code }} - {{ revenue.name }}</td>
                                <td class="text-end">{{ revenue.total|floatformat:2 }}</td>
                            </tr>
                            {% endif %}
                            {% endfor %}
                            <tr class="table-active">
                                <td><strong>Total Revenue</strong></td>
                                <td class="text-end"><strong>{{ total_revenue|floatformat:2 }}</strong></td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
            
            <!-- Expense Section -->
            <div class="mb-4">
                <h4 class="border-bottom pb-2">Expenses</h4>
                <div class="table-responsive">
                    <table class="table">
                        <tbody>
                            {% for expense in expenses %}
                            {% if expense.total %}
                            <tr>
                                <td>{{ expense.code }} - {{ expense.name }}</td>
                                <td class="text-end">{{ expense.total|floatformat:2 }}</td>
                            </tr>
                            {% endif %}
                            {% endfor %}
                            <tr class="table-active">
                                <td><strong>Total Expenses</strong></td>
                                <td class="text-end"><strong>{{ total_expenses|floatformat:2 }}</strong></td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
            
            <!-- Net Income -->
            <div class="text-end">
                <h3>
                    <span class="badge bg-{% if net_income >= 0 %}success{% else %}danger{% endif %}">
                        Net Income: {{ net_income|floatformat:2 }}
                    </span>
                </h3>
            </div>
        </div>
    </div>
</div>
{% endblock %}