<%- include('../partials/header') %>

<section class="sheet-toolbar">
  <div class="sheet-toolbar-main">
    <p class="eyebrow">Bilan prévisionnel</p>
    <h2 class="sheet-title"><%= programme.nom_commercial %></h2>
    <p class="muted sheet-meta">
      <code><%= programme.code_interne %></code>
      · <%= [programme.numero, programme.rue].filter(Boolean).join(' ') %>,
      <%= programme.code_postal %> <%= programme.ville %>
      <% if (programme.surface_plancher != null) { %>
        · SDP <%= formatNumber(programme.surface_plancher, { digits: 2 }) %> m²
      <% } %>
      <% if (margePct != null) { %>
        · Marge <span data-marge-pct><%= formatNumber(margePct, { digits: 2 }) %></span> %
      <% } %>
    </p>
  </div>
  <div class="actions">
    <button type="button" class="btn btn-ghost" id="budget-toggle-all" aria-expanded="false">Tout déplier</button>
    <a href="/programmes/<%= programme.id %>" class="btn btn-ghost">Synthèse</a>
    <a href="/programmes/<%= programme.id %>/modifier" class="btn btn-ghost">Fiche</a>
    <a href="/programmes/<%= programme.id %>/budget/pdf" class="btn btn-ghost" target="_blank" rel="noopener">PDF</a>
    <a href="/programmes" class="btn btn-ghost">Programmes</a>
  </div>
</section>

<% if (saved) { %>
  <div class="notice">Budget enregistré.</div>
<% } %>

<script type="application/json" id="budget-config"><%- JSON.stringify({
  sousTotaux: typeof sousTotaux !== 'undefined' ? sousTotaux : {},
  totalParts: ['foncier', 'taxes', 'amenagement_vrd', 'travaux_construction', 'honoraires_techniques', 'autres_frais'],
  prixVenteHt: typeof prixVenteHt !== 'undefined' ? Number(prixVenteHt) || 0 : 0,
  lotsUrl: '/programmes/' + programme.id + '/budget/lots',
}) %></script>

<form method="POST" action="/programmes/<%= programme.id %>/budget" class="budget-form" id="budget-form">
  <div class="sheet">
    <table class="sheet-table">
      <thead>
        <tr>
          <th class="col-poste">Postes</th>
          <th class="col-num">Budget prévi HT</th>
          <th class="col-num">Budget prévi TTC</th>
        </tr>
      </thead>
      <tbody>
        <% groups.forEach(function(group) { %>
          <%
            const sousTotal = group.lignes.find(function(l) { return l.type_ligne === 'sous_total'; });
            const hasCollapse = Boolean(sousTotal);
            const saisieLignes = group.lignes.filter(function(l) { return l.type_ligne === 'saisie'; });
            const hideSection = hasCollapse || (saisieLignes.length === 1 && saisieLignes[0].poste.toLowerCase() === group.label.toLowerCase());
          %>
          <% if (!hideSection) { %>
            <tr class="sheet-section">
              <td colspan="3"><%= group.label %></td>
            </tr>
          <% } %>
          <% group.lignes.forEach(function(ligne) { %>
            <%
              const isEmphasis = ligne.code === 'travaux_construction' || ligne.emphasis;
              const beforeSousTotal = hasCollapse
                && group.lignes.indexOf(ligne) < group.lignes.indexOf(sousTotal);
              let rowClass = 'sheet-row sheet-row--' + ligne.type_ligne;
              if (isEmphasis) rowClass += ' sheet-row--emphasis';
              if (beforeSousTotal) rowClass += ' sheet-detail is-collapsed';
              if (ligne.type_ligne === 'sous_total') rowClass += ' sheet-toggle';
              if (ligne.code === 'prix_vente') rowClass += ' sheet-row--lots-link';
            %>
            <tr
              class="<%= rowClass %>"
              data-code="<%= ligne.code %>"
              data-type="<%= ligne.type_ligne %>"
              data-tva-auto="<%= ligne.tva_auto ? '1' : '0' %>"
              <% if (beforeSousTotal) { %>data-collapse-for="<%= sousTotal.code %>"<% } %>
              <% if (ligne.type_ligne === 'sous_total') { %>data-toggle-code="<%= ligne.code %>" title="Double-clic pour afficher le détail"<% } %>
              <% if (ligne.code === 'prix_vente') { %>data-open-lots="1" title="Ouvrir le détail des lots"<% } %>
            >
              <td class="col-poste">
                <% if (ligne.type_ligne === 'sous_total') { %>
                  <span class="sheet-toggle-label">
                    <span class="sheet-toggle-mark" aria-hidden="true"></span>
                    <span><%= ligne.poste %></span>
                  </span>
                <% } else if (ligne.code === 'prix_vente') { %>
                  <span class="sheet-toggle-label">
                    <span class="sheet-toggle-mark" aria-hidden="true"></span>
                    <span><%= ligne.poste %></span>
                    <span class="muted tiny">lots</span>
                  </span>
                <% } else { %>
                  <%= ligne.poste %>
                <% } %>
              </td>
              <% if (ligne.type_ligne === 'saisie') { %>
                <td class="col-num">
                  <input type="hidden" name="code" value="<%= ligne.code %>">
                  <input
                    type="text"
                    name="montant_ht"
                    value="<%= formatMoney(ligne.montant_ht, '0') %>"
                    class="sheet-input js-thousand js-budget-input"
                    data-field="ht"
                    data-digits="0"
                    data-round="100"
                    inputmode="decimal"
                    autocomplete="off"
                  >
                </td>
                <td class="col-num">
                  <% if (ligne.tva_auto) { %>
                    <input type="hidden" name="montant_ttc" value="<%= Number(ligne.montant_ttc) %>" data-field="ttc-hidden">
                    <span class="sheet-computed" data-field="ttc"><%= formatMoney(ligne.montant_ttc) %></span>
                  <% } else { %>
                    <input
                      type="text"
                      name="montant_ttc"
                      value="<%= formatMoney(ligne.montant_ttc, '0') %>"
                      class="sheet-input js-thousand js-budget-input"
                      data-field="ttc"
                      data-digits="0"
                      data-round="100"
                      inputmode="decimal"
                      autocomplete="off"
                    >
                  <% } %>
                </td>
              <% } else { %>
                <td class="col-num sheet-num" data-field="ht"><%= formatMoney(ligne.montant_ht) %></td>
                <td class="col-num sheet-num" data-field="ttc"><%= formatMoney(ligne.montant_ttc) %></td>
              <% } %>
            </tr>
          <% }) %>
        <% }) %>
      </tbody>
    </table>
  </div>

  <div class="form-actions sticky-actions">
    <button type="submit" class="btn btn-primary">Enregistrer</button>
    <a href="/programmes" class="btn btn-ghost">Annuler</a>
  </div>
</form>

<%- include('../partials/footer') %>
