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

{% block content %}
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4 no-print">
        <h1 class="h3 mb-0 text-gray-800">Customer Statement</h1>
        {% if selected_customer %}
        <button onclick="window.print()" class="btn btn-secondary">
            <i class="bi bi-printer"></i> Print Statement
        </button>
        {% endif %}
    </div>

    <style>
        @media print {
            body * {
                visibility: hidden;
            }
            .printable-area, .printable-area * {
                visibility: visible;
            }
            .printable-area {
                position: absolute;
                left: 0;
                top: 0;
                width: 100%;
            }
            .no-print {
                display: none !important;
            }
            /* Reset card styles for print */
            .card {
                border: none !important;
                box-shadow: none !important;
            }
            .card-body {
                padding: 0 !important;
            }
        }
    </style>

    <div class="card shadow mb-4 no-print">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-primary">Filter</h6>
        </div>
        <div class="card-body">
            <form method="get" class="row g-3">
                <div class="col-md-4">
                    <label class="form-label">Customer</label>
                    <select name="customer" class="form-select" onchange="this.form.submit()">
                        <option value="">Select Customer...</option>
                        {% for customer in customers %}
                        <option value="{{ customer.id }}" {% if selected_customer.id == customer.id %}selected{% endif %}>
                            {{ customer.name }}
                        </option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-3">
                    <label class="form-label">Start Date</label>
                    <input type="date" name="start_date" class="form-control" value="{{ start_date }}">
                </div>
                <div class="col-md-3">
                    <label class="form-label">End Date</label>
                    <input type="date" name="end_date" class="form-control" value="{{ end_date }}">
                </div>
                <div class="col-md-2 d-flex align-items-end">
                    <button type="submit" class="btn btn-primary w-100">Filter</button>
                </div>
            </form>
        </div>
    </div>

    {% if selected_customer %}
    <div class="card shadow printable-area">
        <div class="card-body">
            <div class="mb-4">
                <h4 class="text-center mb-3">Customer Statement</h4>
                <div class="row">
                    <div class="col-6">
                        <h5 class="fw-bold">{{ selected_customer.name }}</h5>
                        {% if selected_customer.phone %}<p class="mb-0">Phone: {{ selected_customer.phone }}</p>{% endif %}
                    </div>
                    <div class="col-6 text-end">
                        <p class="mb-0">Date Range:</p>
                        <p class="fw-bold">
                            {% if start_date %}{{ start_date }}{% else %}Start{% endif %} 
                            to 
                            {% if end_date %}{{ end_date }}{% else %}Present{% endif %}
                        </p>
                    </div>
                </div>
            </div>

            <div class="table-responsive">
                <table class="table table-bordered">
                    <thead>
                        <tr class="table-light">
                            <th>Date</th>
                            <th>Invoice #</th>
                            <th class="text-end">Total</th>
                            <th class="text-end">Paid</th>
                            <th class="text-end">Balance</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for sale in sales %}
                        <tr>
                            <td>{{ sale.created_at|date:"Y-m-d" }}</td>
                            <td>{{ sale.invoice_number|default:sale.id }}</td>
                            <td class="text-end">{{ sale.total_amount }}</td>
                            <td class="text-end">{{ sale.paid_amount }}</td>
                            <td class="text-end">{{ sale.balance_due }}</td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="5" class="text-center">No transactions found.</td>
                        </tr>
                        {% endfor %}
                    </tbody>
                    <tfoot>
                        <tr class="fw-bold table-light">
                            <td colspan="2" class="text-end">Totals:</td>
                            <td class="text-end">{{ total_amount|floatformat:2 }}</td>
                            <td class="text-end">{{ total_paid|floatformat:2 }}</td>
                            <td class="text-end">{{ total_balance|floatformat:2 }}</td>
                        </tr>
                    </tfoot>
                </table>
            </div>
        </div>
    </div>
    {% endif %}
</div>
{% endblock %}