<?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 einsatzbewertungen WHERE user_id = ? ORDER BY created_at DESC');
$stmt->execute([$user_id]);
$bewertungen = $stmt->fetchAll();

$extra_head = <<<'CSS'
<style>
.star-rating{display:flex;flex-direction:row-reverse;gap:4px;justify-content:flex-end}
.star-rating input{display:none}
.star-rating label{
  font-size:2rem;cursor:pointer;color:rgba(170,240,250,.25);
  transition:color .1s;line-height:1;
}
.star-rating input:checked ~ label,
.star-rating label:hover,
.star-rating label:hover ~ label{color:#f5c518}
.star-display{color:#f5c518;letter-spacing:2px;font-size:1.1rem}
.star-display .empty{color:rgba(170,240,250,.2)}
.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>
CSS;

layout_start('OMSORG – Einsatzbewertung', 'einsatzbewertung', $extra_head);
?>
<h1 class="main-title">⭐ Einsatzbewertung</h1>
<p class="main-sub">Bewerte deinen Einsatzort. Dein Feedback hilft uns, die Einsatzplanung zu verbessern.</p>

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

<div id="bew-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('bew-modal').style.display='none'">&times;</button>
    <h3>Bewertung Details</h3>
    <dl class="modal-dl">
      <dt>Einsatzort</dt> <dd id="bew-ort">–</dd>
      <dt>Von</dt>        <dd id="bew-von">–</dd>
      <dt>Bis</dt>        <dd id="bew-bis">–</dd>
      <dt>Bewertung</dt>  <dd id="bew-sterne">–</dd>
      <dt>Wieder?</dt>    <dd id="bew-wieder">–</dd>
      <dt>Feedback</dt>   <dd id="bew-feedback">–</dd>
      <dt>Datum</dt>      <dd id="bew-datum">–</dd>
    </dl>
  </div>
</div>

<div class="page-split">

  <div class="page-split-list">
    <h2>Meine Bewertungen</h2>
    <?php if (empty($bewertungen)): ?>
      <p style="color:var(--muted)">Noch keine Bewertungen abgegeben.</p>
    <?php else: ?>
      <table class="requests-table">
        <thead>
          <tr>
            <th>Einsatzort</th>
            <th>Bewertung</th>
            <th></th>
          </tr>
        </thead>
        <tbody>
          <?php foreach ($bewertungen as $b): ?>
          <tr>
            <td style="font-weight:800"><?= e($b['einsatzort']) ?></td>
            <td>
              <span class="star-display">
                <?php for ($i = 1; $i <= 5; $i++): ?>
                  <span<?= $i > $b['bewertung'] ? ' class="empty"' : '' ?>>★</span>
                <?php endfor; ?>
              </span>
            </td>
            <td>
              <button class="btn-sm js-bew-details"
                data-ort="<?= e($b['einsatzort']) ?>"
                data-von="<?= e(date('d.m.Y', strtotime($b['von']))) ?>"
                data-bis="<?= e(date('d.m.Y', strtotime($b['bis']))) ?>"
                data-bewertung="<?= (int)$b['bewertung'] ?>"
                data-wieder="<?= (int)$b['wieder'] ?>"
                data-feedback="<?= e($b['feedback'] !== '' ? $b['feedback'] : '–') ?>"
                data-datum="<?= e(date('d.m.Y', strtotime($b['created_at']))) ?>"
              >Details</button>
            </td>
          </tr>
          <?php endforeach; ?>
        </tbody>
      </table>
    <?php endif; ?>
  </div>

  <div class="page-split-form">
    <div class="glass detail-card">
      <h2>Neue Bewertung</h2>
      <form action="../actions/submit-einsatzbewertung.php" method="post">
        <?= csrf_field() ?>
        <label>
          <span>Einsatzort / Einrichtung <span style="color:#ff8080">*</span></span>
          <input type="text" name="einsatzort" required placeholder="z. B. Pflegeheim Sonnenhof">
        </label>
        <label>
          <span>Von <span style="color:#ff8080">*</span></span>
          <input type="date" name="von" required>
        </label>
        <label>
          <span>Bis <span style="color:#ff8080">*</span></span>
          <input type="date" name="bis" required>
        </label>
        <div style="margin-bottom:14px">
          <span style="display:block;margin-bottom:8px;font-weight:700">
            Bewertung <span style="color:#ff8080">*</span>
          </span>
          <div class="star-rating">
            <input type="radio" id="star5" name="bewertung" value="5" required>
            <label for="star5" title="5 Sterne">★</label>
            <input type="radio" id="star4" name="bewertung" value="4">
            <label for="star4" title="4 Sterne">★</label>
            <input type="radio" id="star3" name="bewertung" value="3">
            <label for="star3" title="3 Sterne">★</label>
            <input type="radio" id="star2" name="bewertung" value="2">
            <label for="star2" title="2 Sterne">★</label>
            <input type="radio" id="star1" name="bewertung" value="1">
            <label for="star1" title="1 Stern">★</label>
          </div>
        </div>
        <div style="margin-bottom:14px">
          <label style="display:flex;align-items:center;gap:10px;flex-direction:row;font-weight:700;cursor:pointer">
            <input type="checkbox" name="wieder" value="1" style="width:18px;height:18px;cursor:pointer">
            Würdest du wieder dort arbeiten?
          </label>
        </div>
        <label>
          Feedback
          <textarea name="feedback" rows="4" placeholder="Optionale Anmerkungen zu diesem Einsatz"></textarea>
        </label>
        <button type="submit" class="btn" style="margin-top:6px">Bewertung abgeben</button>
      </form>
    </div>
  </div>

</div>
<script>
document.addEventListener('click', function(e) {
  var btn = e.target.closest('.js-bew-details');
  if (!btn) return;
  var d = btn.dataset;
  document.getElementById('bew-ort').textContent     = d.ort;
  document.getElementById('bew-von').textContent     = d.von;
  document.getElementById('bew-bis').textContent     = d.bis;
  var n = parseInt(d.bewertung, 10);
  var stars = '';
  for (var i = 1; i <= 5; i++) stars += '<span' + (i > n ? ' class="empty"' : '') + '>★</span>';
  document.getElementById('bew-sterne').innerHTML = '<span class="star-display">' + stars + '</span>';
  document.getElementById('bew-wieder').innerHTML =
    parseInt(d.wieder) ? '<span style="color:#6eff9a;font-weight:800">Ja</span>' : '<span style="color:var(--muted)">Nein</span>';
  document.getElementById('bew-feedback').textContent = d.feedback;
  document.getElementById('bew-datum').textContent    = d.datum;
  document.getElementById('bew-modal').style.display = 'flex';
});
document.addEventListener('keydown', function(e) {
  if (e.key === 'Escape') document.getElementById('bew-modal').style.display = 'none';
});
</script>
<?php layout_end(); ?>
