<?php
require_once __DIR__ . '/../lib/auth.php';
require_once __DIR__ . '/../lib/layout.php';
require_login();

$user_id = current_user()['id'];

$stmt = db()->prepare('SELECT * FROM requests_stundennachweis WHERE user_id = ? ORDER BY created_at DESC');
$stmt->execute([$user_id]);
$requests = $stmt->fetchAll();

$status_labels = ['pending' => 'Ausstehend', 'accepted' => 'Akzeptiert', 'rejected' => 'Abgelehnt'];

layout_start('OMSORG – Stundennachweis', 'stundennachweis');
?>
<h1 class="main-title">🕐 Stundennachweis</h1>
<p class="main-sub">Monatlichen Stundennachweis einreichen.</p>

<?php if (isset($_GET['success'])): ?>
  <div class="success" style="margin-top:16px">Nachweis erfolgreich eingereicht.</div>
<?php elseif (isset($_GET['error'])): ?>
  <div class="error" style="margin-top:16px"><?= e(urldecode($_GET['error'])) ?></div>
<?php endif; ?>

<div class="glass detail-card" style="margin-top:24px;max-width:480px">
  <h2>Nachweis einreichen</h2>
  <form action="../actions/submit-stundennachweis.php" method="post" enctype="multipart/form-data">
    <?= csrf_field() ?>
    <div class="monat-grid">
      <label>
        <span>Monat <span style="color:#ff8080">*</span></span>
        <select name="monat_m" required>
          <option value="">– Monat –</option>
          <?php foreach (['01'=>'Januar','02'=>'Februar','03'=>'März','04'=>'April','05'=>'Mai','06'=>'Juni','07'=>'Juli','08'=>'August','09'=>'September','10'=>'Oktober','11'=>'November','12'=>'Dezember'] as $v => $l): ?>
          <option value="<?= $v ?>"><?= $l ?></option>
          <?php endforeach; ?>
        </select>
      </label>
      <label>
        <span>Jahr <span style="color:#ff8080">*</span></span>
        <input type="number" name="monat_j" min="2020" max="2099"
               value="<?= date('Y') ?>" required style="width:100%">
      </label>
    </div>
    <label>
      <span>Nachweis <span style="color:#ff8080">*</span></span>
      <input type="file" name="datei" accept=".pdf,.jpg,.jpeg,.png" required>
      <span class="hint">PDF, JPG, PNG &mdash; max. 12 MB</span>
    </label>
    <button type="submit" class="btn" style="margin-top:6px">Einreichen</button>
  </form>
</div>

<?php if (!empty($requests)): ?>
<div style="margin-top:40px">
  <h2>Meine Nachweise</h2>
  <table class="requests-table">
    <thead>
      <tr>
        <th>Monat</th>
        <th>Datei</th>
        <th>Eingereicht</th>
        <th>Status</th>
        <th>Notiz</th>
      </tr>
    </thead>
    <tbody>
      <?php foreach ($requests as $r): ?>
      <tr>
        <td style="font-weight:800"><?= e(date('F Y', strtotime($r['monat'] . '-01'))) ?></td>
        <td>
          <a href="upload-serve.php?file=<?= e(rawurlencode($r['filename'])) ?>&name=<?= e(rawurlencode($r['original_name'])) ?>"
             style="color:var(--cyan2)"><?= e($r['original_name']) ?></a>
        </td>
        <td style="color:var(--muted);font-size:.9rem"><?= e(date('d.m.Y', strtotime($r['created_at']))) ?></td>
        <td><span class="badge badge-<?= e($r['status']) ?>"><?= e($status_labels[$r['status']] ?? $r['status']) ?></span></td>
        <td style="color:var(--muted);font-size:.9rem"><?= $r['admin_note'] !== '' ? e($r['admin_note']) : '–' ?></td>
      </tr>
      <?php endforeach; ?>
    </tbody>
  </table>
</div>
<?php endif; ?>

<?php layout_end(); ?>
