<?php
require_once '../includes/config.php';
requireAdmin();
$success = ''; $error = '';

if ($_SERVER['REQUEST_METHOD']==='POST') {
    $action = $_POST['action']??'';
    if ($action==='add') {
        $name = sanitize($_POST['exam_name']??'');
        $date = $_POST['exam_date']??'';
        $desc = sanitize($_POST['description']??'');
        if ($name && $date) {
            $pdo->prepare("INSERT INTO al_exam_dates (exam_name,exam_date,description) VALUES (?,?,?)")->execute([$name,$date,$desc]);
            $success = 'Exam date added!';
        } else $error = 'Name and date required';
    }
    if ($action==='toggle') {
        $pdo->prepare("UPDATE al_exam_dates SET is_active=NOT is_active WHERE id=?")->execute([(int)$_POST['id']]);
        $success = 'Updated!';
    }
    if ($action==='delete') {
        $pdo->prepare("DELETE FROM al_exam_dates WHERE id=?")->execute([(int)$_POST['id']]);
        $success = 'Deleted!';
    }
}

$exams = $pdo->query("SELECT * FROM al_exam_dates ORDER BY exam_date DESC")->fetchAll();

require_once 'includes/header.php';
renderAdminHeader('A/L Exam Dates', 'exams');
?>
<?php if ($error): ?><div class="alert alert-error">⚠️ <?= $error ?></div><?php endif; ?>
<?php if ($success): ?><div class="alert alert-success">✅ <?= $success ?></div><?php endif; ?>

<div class="grid-2" style="align-items:start">
<div class="card">
  <div class="card-header"><div class="card-title">➕ Add Exam Date</div></div>
  <form method="POST">
    <input type="hidden" name="action" value="add">
    <div class="form-group"><label class="form-label">Exam Name</label><input type="text" name="exam_name" class="form-control" placeholder="e.g. A/L Examination 2025" required></div>
    <div class="form-group"><label class="form-label">Exam Date</label><input type="date" name="exam_date" class="form-control" required></div>
    <div class="form-group"><label class="form-label">Description</label><textarea name="description" class="form-control" rows="2" placeholder="Optional details..."></textarea></div>
    <button type="submit" class="btn btn-primary btn-full">Add Exam Date</button>
  </form>
</div>

<div class="card">
  <div class="card-header"><div class="card-title">📅 Exam Dates (<?= count($exams) ?>)</div></div>
  <?php if ($exams): ?>
  <div style="display:flex;flex-direction:column;gap:12px">
  <?php foreach ($exams as $e):
    $daysLeft = ceil((strtotime($e['exam_date'])-time())/86400);
  ?>
  <div style="background:var(--bg-secondary);border-radius:12px;padding:16px;border:1px solid var(--border);<?= $e['is_active']?'border-left:3px solid var(--accent)':'' ?>">
    <div style="display:flex;justify-content:space-between;align-items:flex-start;gap:12px">
      <div>
        <div style="font-weight:600;color:var(--text-primary);font-size:15px"><?= htmlspecialchars($e['exam_name']) ?></div>
        <div style="font-size:13px;color:var(--text-muted);margin-top:4px">📅 <?= date('F j, Y',strtotime($e['exam_date'])) ?></div>
        <?php if ($e['description']): ?><div style="font-size:12px;color:var(--text-muted);margin-top:4px"><?= htmlspecialchars($e['description']) ?></div><?php endif; ?>
        <div style="margin-top:8px">
          <?php if ($daysLeft > 0): ?>
          <span class="badge <?= $daysLeft<=30?'badge-red':($daysLeft<=90?'badge-yellow':'badge-cyan') ?>"><?= $daysLeft ?> days left</span>
          <?php else: ?><span class="badge badge-green">Exam passed</span><?php endif; ?>
          <?php if ($e['is_active']): ?><span class="badge badge-green" style="margin-left:6px">Active</span><?php else: ?><span class="badge" style="margin-left:6px;background:var(--bg-card);color:var(--text-muted)">Inactive</span><?php endif; ?>
        </div>
      </div>
      <div style="display:flex;gap:6px;flex-shrink:0">
        <form method="POST" style="display:inline"><input type="hidden" name="action" value="toggle"><input type="hidden" name="id" value="<?= $e['id'] ?>"><button type="submit" class="btn btn-secondary btn-sm"><?= $e['is_active']?'Deactivate':'Activate' ?></button></form>
        <form method="POST" style="display:inline" onsubmit="return confirm('Delete this exam?')"><input type="hidden" name="action" value="delete"><input type="hidden" name="id" value="<?= $e['id'] ?>"><button type="submit" class="btn btn-danger btn-sm btn-icon">🗑️</button></form>
      </div>
    </div>
  </div>
  <?php endforeach; ?>
  </div>
  <?php else: ?><div class="empty-state"><div class="empty-icon">📅</div><p>No exam dates added yet</p></div><?php endif; ?>
</div>
</div>
<?php require_once 'includes/footer.php'; renderAdminFooter(); ?>
