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

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

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

$qual_labels = [
    '1jahr'         => '1 Jährige Examinierte Pflegekraft',
    '3jahr'         => '3 Jährige Examinierte Pflegekraft',
];

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

layout_start('OMSORG – Mitarbeiter werben', 'werben');
?>
<style>
.modal-backdrop{position:fixed;inset:0;background:rgba(0,0,0,.55);display:none;align-items:center;justify-content:center;z-index:1000}
.modal-box{background:var(--glass-bg,#1e2235);border:1px solid var(--border,rgba(255,255,255,.1));border-radius:14px;padding:28px 32px;max-width:480px;width:90%;position:relative}
.modal-box h3{margin:0 0 20px;font-family:'KindelSerif',Georgia,serif;font-size:1.3rem}
.modal-close{position:absolute;top:12px;right:16px;background:none;border:none;font-size:1.5rem;cursor:pointer;color:var(--muted,#888);line-height:1}
.modal-dl{display:grid;grid-template-columns:auto 1fr;gap:8px 16px;font-size:.95rem}
.modal-dl dt{color:var(--muted,#888);white-space:nowrap}
.modal-dl dd{margin:0;font-weight:500}
.btn-sm{display:inline-flex;align-items:center;gap:6px;padding:7px 13px;border-radius:9px;border:1px solid rgba(255,255,255,.2);background:rgba(255,255,255,.08);color:#fff;font-weight:600;font-size:.82rem;cursor:pointer;font:inherit;transition:background .15s}
.btn-sm:hover{background:rgba(255,255,255,.15)}
</style>

<h1 class="main-title">🤝 Mitarbeiter werben</h1>
<p class="main-sub">Empfehle einen Kandidaten für eine offene Stelle bei OMSORG.</p>

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

<div id="werb-modal" class="modal-backdrop" onclick="if(event.target===this)this.style.display='none'">
  <div class="modal-box glass">
    <button class="modal-close" onclick="document.getElementById('werb-modal').style.display='none'">&times;</button>
    <h3>Empfehlung Details</h3>
    <dl class="modal-dl">
      <dt>Name</dt>          <dd id="werb-name">–</dd>
      <dt>E-Mail</dt>        <dd id="werb-email">–</dd>
      <dt>Qualifikation</dt> <dd id="werb-qual">–</dd>
      <dt>Nachricht</dt>     <dd id="werb-nachricht">–</dd>
      <dt>Eingereicht</dt>   <dd id="werb-eingereicht">–</dd>
      <dt>Status</dt>        <dd id="werb-status">–</dd>
      <dt>Admin-Notiz</dt>   <dd id="werb-note">–</dd>
    </dl>
  </div>
</div>

<div class="page-split">

  <div class="page-split-list">
    <h2>Meine Empfehlungen</h2>
    <?php if (empty($requests)): ?>
      <p style="color:var(--muted)">Noch keine Empfehlungen eingereicht.</p>
    <?php else: ?>
      <table class="requests-table">
        <thead>
          <tr>
            <th>Name</th>
            <th>Status</th>
            <th></th>
          </tr>
        </thead>
        <tbody>
          <?php foreach ($requests as $r): ?>
          <tr>
            <td style="font-weight:800"><?= e($r['name']) ?></td>
            <td><span class="badge badge-<?= e($r['status']) ?>"><?= e($status_labels[$r['status']] ?? $r['status']) ?></span></td>
            <td>
              <button class="btn-sm js-werb-details"
                data-name="<?= e($r['name']) ?>"
                data-email="<?= e($r['email']) ?>"
                data-qual="<?= e($qual_labels[$r['qualifikation']] ?? $r['qualifikation']) ?>"
                data-nachricht="<?= e($r['nachricht'] !== '' ? $r['nachricht'] : '–') ?>"
                data-eingereicht="<?= e(date('d.m.Y', strtotime($r['created_at']))) ?>"
                data-status-label="<?= e($status_labels[$r['status']] ?? $r['status']) ?>"
                data-status-key="<?= e($r['status']) ?>"
                data-note="<?= e($r['admin_note'] !== '' ? $r['admin_note'] : '–') ?>"
              >Details</button>
            </td>
          </tr>
          <?php endforeach; ?>
        </tbody>
      </table>
    <?php endif; ?>
  </div>

  <div class="page-split-form">
    <div class="glass detail-card">
      <h2>Kandidaten empfehlen</h2>
      <form action="../actions/submit-werben.php" method="post">
        <?= csrf_field() ?>
        <label>
          <span>Name des Kandidaten <span style="color:#ff8080">*</span></span>
          <input type="text" name="name" required placeholder="Vorname Nachname">
        </label>
        <label>
          <span>E-Mail des Kandidaten <span style="color:#ff8080">*</span></span>
          <input type="email" name="email" required placeholder="kandidat@example.com">
        </label>
        <label>
          <span>Qualifikation <span style="color:#ff8080">*</span></span>
          <select name="qualifikation" required>
            <option value="">– Bitte wählen –</option>
            <option value="1jahr">1 Jährige Examinierte Pflegekraft</option>
            <option value="3jahr">3 Jährige Examinierte Pflegekraft</option>
          </select>
        </label>
        <label>
          Nachricht
          <textarea name="nachricht" rows="4" placeholder="Warum empfiehlst du diese Person?"></textarea>
        </label>
        <button type="submit" class="btn" style="margin-top:6px">Empfehlung einreichen</button>
      </form>
    </div>
  </div>

</div>
<script>
document.addEventListener('click', function(e) {
  var btn = e.target.closest('.js-werb-details');
  if (!btn) return;
  var d = btn.dataset;
  document.getElementById('werb-name').textContent       = d.name;
  document.getElementById('werb-email').textContent      = d.email;
  document.getElementById('werb-qual').textContent       = d.qual;
  document.getElementById('werb-nachricht').textContent  = d.nachricht;
  document.getElementById('werb-eingereicht').textContent= d.eingereicht;
  document.getElementById('werb-status').innerHTML =
    '<span class="badge badge-' + d.statusKey + '">' + d.statusLabel + '</span>';
  document.getElementById('werb-note').textContent       = d.note;
  document.getElementById('werb-modal').style.display = 'flex';
});
document.addEventListener('keydown', function(e) {
  if (e.key === 'Escape') document.getElementById('werb-modal').style.display = 'none';
});
</script>
<?php layout_end(); ?>
