<% var pageTitle = 'Financije'; var currentPage = 'finance'; %>
<%- include('partials/header') %>

<h1>Financije — <%= year %>.</h1>

<div class="btn-row">
  <a href="/finance?year=<%= year - 1 %>" class="btn btn-secondary">← <%= year - 1 %>.</a>
  <a href="/finance?year=<%= year + 1 %>" class="btn btn-secondary"><%= year + 1 %>. →</a>
</div>

<div class="grid-stats">
  <div class="stat"><div class="num"><%= fmt.money(totalRevenue) %></div><div class="label">Ukupan prihod</div></div>
  <div class="stat"><div class="num"><%= fmt.money(totalExpenses) %></div><div class="label">Ukupni troškovi</div></div>
  <div class="stat"><div class="num"><%= fmt.money(totalRevenue - totalExpenses) %></div><div class="label">Neto dobit</div></div>
</div>

<div class="panel">
  <h2 style="margin-top:0;">Po apartmanu</h2>
  <% if (rows.length === 0) { %>
    <p class="empty-state">Nema podataka za ovu godinu.</p>
  <% } else { %>
    <table>
      <thead><tr><th>Apartman</th><th>Broj rezervacija</th><th>Prihod</th><th>Troškovi</th><th>Neto</th></tr></thead>
      <tbody>
        <% rows.forEach(r => { %>
          <tr>
            <td><a href="/apartments/<%= r.id %>"><%= r.name %></a></td>
            <td><%= r.num_bookings %></td>
            <td><%= fmt.money(r.revenue) %></td>
            <td><%= fmt.money(r.expenses) %></td>
            <td><%= fmt.money(r.net) %></td>
          </tr>
        <% }) %>
      </tbody>
    </table>
  <% } %>
</div>

<div class="panel">
  <h2 style="margin-top:0;">Dodaj trošak</h2>
  <form method="POST" action="/finance/expenses/new">
    <div class="form-row">
      <div class="form-group">
        <label for="apartment_id">Apartman</label>
        <select id="apartment_id" name="apartment_id">
          <option value="">Opći trošak (bez apartmana)</option>
          <% apartments.forEach(a => { %>
            <option value="<%= a.id %>"><%= a.name %></option>
          <% }) %>
        </select>
      </div>
      <div class="form-group">
        <label for="category">Kategorija</label>
        <input type="text" id="category" name="category" placeholder="npr. čišćenje, održavanje, računi">
      </div>
      <div class="form-group">
        <label for="amount">Iznos (€)</label>
        <input type="number" id="amount" name="amount" step="0.01" min="0" required>
      </div>
      <div class="form-group">
        <label for="expense_date">Datum</label>
        <input type="date" id="expense_date" name="expense_date" required>
      </div>
    </div>
    <div class="form-group">
      <label for="notes">Napomena</label>
      <input type="text" id="notes" name="notes">
    </div>
    <button type="submit" class="btn">Dodaj trošak</button>
  </form>
</div>

<div class="panel">
  <h2 style="margin-top:0;">Nedavni troškovi</h2>
  <% if (recentExpenses.length === 0) { %>
    <p class="empty-state">Nema unesenih troškova.</p>
  <% } else { %>
    <table>
      <thead><tr><th>Datum</th><th>Apartman</th><th>Kategorija</th><th>Iznos</th><th></th></tr></thead>
      <tbody>
        <% recentExpenses.forEach(e => { %>
          <tr>
            <td><%= fmt.date(e.expense_date) %></td>
            <td><%= e.apartment_name || 'Opći' %></td>
            <td><%= e.category %></td>
            <td><%= fmt.money(e.amount) %></td>
            <td>
              <form class="inline" method="POST" action="/finance/expenses/<%= e.id %>/delete" onsubmit="return confirm('Obrisati trošak?');">
                <button type="submit" style="background:none;border:none;color:#a83c3c;cursor:pointer;font-family:inherit;">obriši</button>
              </form>
            </td>
          </tr>
        <% }) %>
      </tbody>
    </table>
  <% } %>
</div>

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