{% extends 'layouts/app.twig' %}

{% block content %}
  <div class="page-header page-header-split">
    <div>
      <h1>Tax Report</h1>
      <p class="page-subtitle">Taxable sales and tax collected from non-cancelled invoices.</p>
    </div>
    <div class="page-actions">
      <a class="btn btn-outline" href="{{ base_url }}/reports/tax/pdf?date_from={{ date_from }}&date_to={{ date_to }}">Export PDF</a>
      <a class="btn btn-outline" href="{{ base_url }}/reports/tax/csv?date_from={{ date_from }}&date_to={{ date_to }}">Export CSV</a>
      <a class="btn btn-outline" href="{{ base_url }}/reports/sales">Sales Report</a>
      <a class="btn btn-outline" href="{{ base_url }}/reports/outstanding">Outstanding Invoices</a>
    </div>
  </div>

  {% if error %}
    <div class="alert alert-danger">{{ error }}</div>
  {% endif %}

  <form class="report-filter" method="get" action="{{ base_url }}/reports/tax">
    <div class="field">
      <label>From</label>
      <input type="date" name="date_from" value="{{ date_from }}">
    </div>
    <div class="field">
      <label>To</label>
      <input type="date" name="date_to" value="{{ date_to }}">
    </div>
    <button class="btn btn-primary" type="submit">Apply Filter</button>
  </form>

  <div class="dashboard-grid">
    <section class="metric-card">
      <span>Taxable Invoices</span>
      <strong>{{ summary.invoice_count }}</strong>
    </section>
    <section class="metric-card">
      <span>Taxable Value</span>
      <strong class="metric-money">{{ currency }} {{ summary.taxable_value|number_format(2) }}</strong>
    </section>
    <section class="metric-card">
      <span>Tax Collected</span>
      <strong class="metric-money">{{ currency }} {{ summary.tax_total|number_format(2) }}</strong>
    </section>
  </div>

  <section class="list-panel report-section">
    <div class="panel-heading">
      <div>
        <h2>Tax Rate Breakdown</h2>
        <p>{{ date_from }} to {{ date_to }}</p>
      </div>
    </div>

    {% if breakdown is empty %}
      <div class="empty-state">
        <h2>No tax activity</h2>
        <p>No invoice line items were found in the selected period.</p>
      </div>
    {% else %}
      <div class="table-wrap dashboard-table">
        <table>
          <thead>
            <tr>
              <th>Tax Rate</th>
              <th>Invoices</th>
              <th>Taxable Value</th>
              <th>Tax Collected</th>
            </tr>
          </thead>
          <tbody>
            {% for row in breakdown %}
              <tr>
                <td><strong>{{ row.tax_rate|number_format(2) }}%</strong></td>
                <td>{{ row.invoice_count }}</td>
                <td>{{ currency }} {{ row.taxable_value|number_format(2) }}</td>
                <td class="line-total">{{ currency }} {{ row.tax_total|number_format(2) }}</td>
              </tr>
            {% endfor %}
          </tbody>
        </table>
      </div>
    {% endif %}
  </section>

  <section class="list-panel report-section">
    <div class="panel-heading">
      <div>
        <h2>Invoice Tax Audit</h2>
        <p>Invoice totals used in the report.</p>
      </div>
    </div>

    {% if invoices is empty %}
      <div class="empty-state">
        <h2>No invoices found</h2>
        <p>No non-cancelled invoices were issued in the selected period.</p>
      </div>
    {% else %}
      <div class="table-wrap dashboard-table">
        <table>
          <thead>
            <tr>
              <th>Invoice</th>
              <th>Customer</th>
              <th>Issue Date</th>
              <th>Status</th>
              <th>Subtotal</th>
              <th>Discount</th>
              <th>Tax</th>
              <th>Total</th>
              <th></th>
            </tr>
          </thead>
          <tbody>
            {% for invoice in invoices %}
              <tr>
                <td><div class="item-name">{{ invoice.document_number }}</div></td>
                <td>{{ invoice.company_name ?: invoice.customer_name ?: '-' }}</td>
                <td>{{ invoice.issue_date }}</td>
                <td><span class="status-badge status-{{ invoice.status }}">{{ invoice.status|replace({'_': ' '})|title }}</span></td>
                <td>{{ invoice.currency }} {{ invoice.subtotal|number_format(2) }}</td>
                <td>{{ invoice.currency }} {{ invoice.discount_total|number_format(2) }}</td>
                <td>{{ invoice.currency }} {{ invoice.tax_total|number_format(2) }}</td>
                <td class="line-total">{{ invoice.currency }} {{ invoice.grand_total|number_format(2) }}</td>
                <td><a class="btn btn-outline btn-small" href="{{ base_url }}/invoices/{{ invoice.id }}">View</a></td>
              </tr>
            {% endfor %}
          </tbody>
        </table>
      </div>
    {% endif %}
  </section>
{% endblock %}
