{% extends "base.html" %}
{% block title %}General Ledger{% endblock %}
{% block content %}
<div class="container">
    <h2 class="my-4">General Ledger</h2>

       <!-- Filter Form -->
       <form method="get" class="mb-4">
        <div class="row align-items-end">
            <div class="col-md-6">
                <label for="account_ids" class="form-label">Filter by Accounts:</label>
                <select name="account_ids" id="account_ids" class="form-select" multiple size="6">
                    {% for acc in all_accounts %}
                        <option value="{{ acc.id }}" {% if acc.id|stringformat:"s" in selected_account_ids %}selected{% endif %}>
                            {{ acc.code }} - {{ acc.name }}
                        </option>
                    {% endfor %}
                </select>
                <small class="text-muted">Hold Ctrl (Windows) or Cmd (Mac) to select multiple</small>
                <div class="mt-2">
                    <button type="button" class="btn btn-sm btn-outline-secondary me-1" onclick="selectAllAccounts()">Select All</button>
                    <button type="button" class="btn btn-sm btn-outline-warning" onclick="clearAllAccounts()">Clear All</button>
                </div>
            </div>
            <div class="col-md-2">
                <button type="submit" class="btn btn-primary">Filter</button>
            </div>
        </div>
    </form>

    {% for account, data in ledger_data.items %}
        <div class="card mb-4 shadow-sm">
            <div class="card-header bg-secondary text-white">
                <h4 class="mb-0">{{ account.name }} ({{ account.account_type }})</h4>
            </div>
            <div class="card-body">
                <table class="table table-bordered">
                    <thead class="table-dark">
                        <tr>
                            <th>Date</th>
                            <th>Description</th>
                            <th>Debit</th>
                            <th>Credit</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for transaction in data.transactions %}
                        <tr>
                            <td>{{ transaction.journal_entry.date }}</td>
                            <td>{{ transaction.journal_entry.description }}</td>
                            <td class="text-success">{{ transaction.debit }}</td>
                            <td class="text-danger">{{ transaction.credit }}</td>
                        </tr>
                        {% endfor %}
                        <tr style="font-weight:bold;">
                            <td colspan="2">Total</td>
                            <td>{{ data.total_debit }}</td>
                            <td>{{ data.total_credit }}</td>
                          </tr>
                    </tbody>
                </table>
            </div>
        </div>
    {% endfor %}
</div>

<script>
    function selectAllAccounts() {
        const select = document.getElementById("account_ids");
        for (let option of select.options) {
            option.selected = true;
        }
    }

    function clearAllAccounts() {
        const select = document.getElementById("account_ids");
        for (let option of select.options) {
            option.selected = false;
        }
    }
</script>

{% endblock %}
