<% layout('layouts/main') %>
<% const adminBase = typeof adminBaseUrl !== 'undefined' ? adminBaseUrl : '/admin'; %>
<div class="app-shell">
  <%- partial('partials/admin-nav') %>
  <section class="content accounting-page">
    <div class="page-head">
      <div>
        <p class="eyebrow">Comptabilité suisse</p>
        <h1>Comptabilité</h1>
        <p class="muted">Suivi bancaire CAMT, rapprochement des factures, plan comptable, achats, TVA et journal en double entrée.</p>
      </div>
      <a class="button secondary" href="<%= adminBase %>/comptabilite/journal.csv">Exporter le journal</a>
    </div>

    <section class="stat-grid accounting-stats">
      <article class="stat"><strong><%= ui.money(accounting.openInvoices.amount || 0) %></strong><span>Créances ouvertes</span></article>
      <article class="stat"><strong><%= ui.money(accounting.bank.balance || 0) %></strong><span>Solde bancaire importé</span></article>
      <article class="stat"><strong><%= accounting.unmatched.total || 0 %></strong><span>Transactions à traiter</span></article>
      <article class="stat"><strong><%= ui.money(accounting.expenses.amount || 0) %></strong><span>Dépenses ouvertes</span></article>
    </section>

    <section class="premium-grid two">
      <form method="post" action="<%= adminBase %>/comptabilite/import-camt" enctype="multipart/form-data" class="panel form-grid">
        <h2>Importer CAMT</h2>
        <p class="muted">Importez les notifications de crédit CAMT.054 ou extraits de compte CAMT.053 fournis par la banque. Les factures QR sont rapprochées automatiquement.</p>
        <input type="hidden" name="_csrf" value="<%= csrfToken %>">
        <label>Compte bancaire
          <select name="bank_account_id">
            <option value="">Détection depuis le fichier</option>
            <% bankAccounts.forEach((account) => { %>
              <option value="<%= account.id %>"><%= account.label %> · <%= account.iban %></option>
            <% }) %>
          </select>
        </label>
        <label>Fichier CAMT XML<input type="file" name="camt_file" accept=".xml,.camt,text/xml,application/xml" required></label>
        <button>Importer et rapprocher</button>
      </form>

      <article class="panel">
        <h2>Modules comptables</h2>
        <div class="accounting-link-grid">
          <a href="<%= adminBase %>/comptabilite/transactions"><strong>Banque</strong><span>Transactions, matching, crédits et débits.</span></a>
          <a href="<%= adminBase %>/comptabilite/journal"><strong>Journal</strong><span>Écritures double entrée et export fiduciaire.</span></a>
          <a href="<%= adminBase %>/comptabilite/comptes"><strong>Plan comptable</strong><span>Comptes actifs, ventes, TVA, charges.</span></a>
          <a href="<%= adminBase %>/comptabilite/tva"><strong>TVA</strong><span>TVA collectée, préalable et solde.</span></a>
          <a href="<%= adminBase %>/comptabilite/bilan"><strong>Bilan</strong><span>Actifs, passifs, fonds propres et contrôle.</span></a>
          <a href="<%= adminBase %>/comptabilite/resultat"><strong>Résultat</strong><span>Produits, charges et marge de la période.</span></a>
          <a href="<%= adminBase %>/comptabilite/debiteurs"><strong>Débiteurs</strong><span>Créances ouvertes et ancienneté de retard.</span></a>
          <a href="<%= adminBase %>/comptabilite/periodes"><strong>Périodes</strong><span>Clôture et verrouillage des exercices.</span></a>
        </div>
      </article>
    </section>

    <section class="premium-grid two">
      <article class="panel">
        <div class="compact-heading"><h2>Transactions récentes</h2><a class="text-link" href="<%= adminBase %>/comptabilite/transactions">Tout voir</a></div>
        <div class="mobile-list compact-list">
          <% accounting.recentTransactions.forEach((tx) => { %>
            <article class="list-card">
              <div class="list-card-head">
                <div><strong><%= tx.counterparty_name || tx.remittance_info || tx.transaction_reference || 'Transaction bancaire' %></strong><span><%= ui.date(tx.booking_date || tx.value_date) %> · <%= tx.direction === 'credit' ? 'Crédit' : 'Débit' %></span></div>
                <span class="badge"><%= ui.label(tx.status) %></span>
              </div>
              <p><%= ui.money(tx.amount) %> · <%= tx.payment_reference || tx.remittance_info || 'Sans référence' %></p>
            </article>
          <% }) %>
          <% if (!accounting.recentTransactions.length) { %><p class="empty-state"><strong>Aucune transaction</strong><span>Importez un fichier CAMT pour alimenter la banque.</span></p><% } %>
        </div>
      </article>

      <article class="panel">
        <div class="compact-heading"><h2>Dernières écritures</h2><a class="text-link" href="<%= adminBase %>/comptabilite/journal">Journal</a></div>
        <div class="mobile-list compact-list">
          <% accounting.entries.forEach((entry) => { %>
            <article class="list-card">
              <div class="list-card-head"><div><strong><%= entry.label %></strong><span><%= ui.date(entry.entry_date) %> · <%= entry.journal_code %></span></div><span class="badge"><%= ui.label(entry.status) %></span></div>
              <p>Débit <%= ui.money(entry.total_debit) %> · Crédit <%= ui.money(entry.total_credit) %></p>
            </article>
          <% }) %>
          <% if (!accounting.entries.length) { %><p class="empty-state"><strong>Aucune écriture</strong><span>Les factures envoyées, paiements et achats créeront les écritures.</span></p><% } %>
        </div>
      </article>
    </section>
  </section>
</div>
