{% extends "base.html" %}

{% block title %}Meine Rechnungen{% endblock %}

{% block content %}
<section class="panel" style="display:flex; align-items:center; justify-content:space-between; gap:1rem;">
    <div>
        <h1>Rechnungseinsicht</h1>
        <p style="margin:0">Alle Rechnungen im Überblick. Der Admin prüft neue Rechnungen zuerst; der Status zeigt dir, ob die Rechnung noch geprüft wird oder bereits bearbeitet wurde.</p>
    </div>
    <div class="no-print">
        <button class="print-btn" type="button" onclick="printDocument()">Seite drucken</button>
    </div>
</section>

<section class="table-wrap">
    <table>
        <thead>
            <tr>
                <th>Rechnungsnummer</th>
                <th>Zeitraum</th>
                <th>Betrag</th>
                <th>Fälligkeit</th>
                <th>Status</th>
                <th>PDF</th>
            </tr>
        </thead>
        <tbody>
            {% for invoice in invoices %}
            <tr>
                <td>{{ invoice.invoice_number }}</td>
                <td>{{ invoice.period }}</td>
                <td>{{ '%.2f'|format(invoice.amount_eur) }} EUR</td>
                <td>{{ invoice.due_date }}</td>
                <td>{{ invoice.status }}</td>
                <td>
                    {% if invoice.pdf_path %}
                        <a href="{{ url_for('static', filename=invoice.pdf_path) }}" target="_blank" rel="noopener">PDF ansehen</a>
                        <button class="print-btn" type="button" onclick="printPdf('{{ url_for('static', filename=invoice.pdf_path) }}')">PDF drucken</button>
                    {% else %}
                        -
                    {% endif %}
                </td>
            </tr>
            {% else %}
            <tr>
                <td colspan="6">Noch keine Rechnungen vorhanden.</td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
</section>

<section class="panel">
    <h2>Instanz-Anfragen</h2>
    <p>Hier siehst du den Status deiner angeforderten Instanzen/Buchungen, die vom Admin geprüft werden.</p>
</section>

<section class="table-wrap">
    <table>
        <thead>
            <tr>
                <th>Anfrage</th>
                <th>Paket</th>
                <th>Subdomain</th>
                <th>Betrag</th>
                <th>Status</th>
            </tr>
        </thead>
        <tbody>
            {% for prov in instance_requests %}
            <tr>
                <td>{{ prov.booking_number or prov.booking_number }}</td>
                <td>{{ prov.package_label or prov.package }}</td>
                <td>{{ prov.subdomain }}</td>
                <td>{{ '%.2f'|format(prov.amount_eur or 0) }} EUR</td>
                <td>
                    {{ prov.provision_status or 'not_started' }}
                    <form method="post" action="{{ url_for('instance_request_delete') }}" style="display:inline-block; margin-left:0.6rem;">
                        <input type="hidden" name="prov_id" value="{{ prov.id }}">
                        <button type="submit" class="danger">Löschen</button>
                    </form>
                </td>
            </tr>
            {% else %}
            <tr>
                <td colspan="5">Keine Provisionierungsanfragen vorhanden.</td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
</section>

<style>
.panel { margin-bottom: 1rem; }
.table-wrap { background: #fff; border: 1px solid #d8e1e8; border-radius: 14px; padding: 1rem; overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 0.7rem; border-bottom: 1px solid #e8eef2; }
a { color: #0a4c74; font-weight: 700; }
</style>
{% endblock %}
