<%- include('../layout') %>
<title>Wochenbericht</title>

<h1>Meine Zeiterfassung</h1>

<div class="mb-3">
  <a href="/time/new" class="btn btn-primary">Neue Zeit erfassen</a>
  <a href="/time/my/week" class="btn btn-secondary">Wochenübersicht</a>
  <a href="/time/my/month" class="btn btn-secondary">Monatsübersicht</a>
</div>

<div class="mb-3">
  <form action="/time/my/week/table" method="get" target="_blank">

    <label for="week">Kalenderwoche auswählen:</label>

    <select name="week" id="week" required class="form-control">
      <%
        const today = new Date();
        const dayNum = today.getDay() === 0 ? 7 : today.getDay();
        today.setDate(today.getDate() + 4 - dayNum);
        const yearStart = new Date(today.getFullYear(), 0, 1);
        const week = Math.ceil((((today - yearStart) / 86400000) + 1) / 7);

        const currentYear = today.getFullYear();
        const currentWeek = String(week).padStart(2, '0');
        const currentIso = `${currentYear}-W${currentWeek}`;
      %>

      <% for (let w = 1; w <= 52; w++) { 
          const wStr = String(w).padStart(2, '0');
          const iso = `${currentYear}-W${wStr}`;
      %>
        <option value="<%= iso %>" <%= iso === currentIso ? 'selected' : '' %>>
          <%= iso %>
        </option>
      <% } %>
    </select>

    <button type="submit" class="btn btn-primary mt-2">
      PDF erstellen
    </button>
  </form>
</div>

<table class="table table-striped">
  <thead>
    <tr>
      <th>Datum</th>
      <th>Stunden</th>
      <th>Kommission</th>
      <th>Beschreibung</th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <% entries.forEach(e => { %>
      <tr>
        <td><%= e.date %></td>
        <td><%= e.hours ? e.hours.toFixed(2) : '0.00' %></td>
        <td><%= e.commission_number || '-' %></td>
        <td><%= e.description %></td>
        <td>
          <form action="/time/<%= e.id %>/delete" method="post">
            <button class="btn btn-sm btn-danger">Löschen</button>
          </form>
        </td>
      </tr>
    <% }) %>
  </tbody>
</table>

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