<% layout('layouts/main') %>
<% const adminBase = typeof adminBaseUrl !== 'undefined' ? adminBaseUrl : '/admin'; %>
<div class="app-shell">
  <%- partial('partials/admin-nav') %>
  <section class="content">
    <div class="page-head">
      <div><p class="eyebrow">Double entrée</p><h1>Journal comptable</h1><p class="muted">Écritures générées par les factures, paiements CAMT et achats.</p></div>
      <a class="button secondary" href="<%= adminBase %>/comptabilite/journal.csv">Exporter CSV</a>
    </div>

    <div class="mobile-list">
      <% entries.forEach((entry) => { const lines = linesByEntry[entry.id] || []; %>
        <article class="list-card journal-entry-card">
          <div class="list-card-head">
            <div><strong><%= entry.label %></strong><span><%= ui.date(entry.entry_date) %> · <%= entry.journal_code %> · <%= entry.reference || 'Sans référence' %></span></div>
            <span class="badge"><%= ui.label(entry.status) %></span>
          </div>
          <div class="table-wrap compact-table">
            <table>
              <thead><tr><th>Compte</th><th>Description</th><th>Débit</th><th>Crédit</th></tr></thead>
              <tbody>
                <% lines.forEach((line) => { %>
                  <tr><td><strong><%= line.code %></strong><br><span class="muted"><%= line.account_name %></span></td><td><%= line.description %></td><td><%= ui.money(line.debit) %></td><td><%= ui.money(line.credit) %></td></tr>
                <% }) %>
              </tbody>
            </table>
          </div>
          <p>Total débit <strong><%= ui.money(entry.total_debit) %></strong> · total crédit <strong><%= ui.money(entry.total_credit) %></strong></p>
        </article>
      <% }) %>
      <% if (!entries.length) { %><p class="empty-state"><strong>Aucune écriture</strong><span>Envoyez une facture, importez un CAMT ou saisissez une dépense.</span></p><% } %>
    </div>
  </section>
</div>
