<?php
require_once __DIR__ . '/_nav.php';
$pageTitle  = 'Assignments';
$sidebarNav = teacherNav();
$u = function($p) { return \Core\View::url($p); };
?>
<div class="page-header"><div><h1>Assignments</h1></div><button onclick="openModal('addAssign')" class="btn btn-primary"><i class="bi bi-plus-lg"></i> New Assignment</button></div>
<div class="card">
  <div class="table-wrap">
    <table>
      <thead><tr><th>Title</th><th>Subject</th><th>Stream</th><th>Due Date</th><th>Marks</th></tr></thead>
      <tbody>
        <?php foreach($assignments as $a): ?>
        <tr>
          <td><strong><?= htmlspecialchars($a['title']) ?></strong></td>
          <td><?= htmlspecialchars($a['subject_name']) ?></td>
          <td><?= htmlspecialchars($a['stream_name']) ?></td>
          <td><?= $a['due_date'] ?> <?= $a['due_date'] < date('Y-m-d') ? '<span class="badge badge-danger">Overdue</span>' : '' ?></td>
          <td><?= $a['total_marks'] ?></td>
        </tr>
        <?php endforeach; ?>
        <?php if(empty($assignments)): ?><tr><td colspan="5" style="text-align:center;padding:40px;color:var(--gray)">No assignments yet</td></tr><?php endif; ?>
      </tbody>
    </table>
  </div>
</div>

<div class="modal" id="addAssign"><div class="modal-box">
  <div class="card-header"><h5>Upload Assignment</h5><button onclick="closeModal('addAssign')" style="background:none;border:none;cursor:pointer;font-size:20px">&times;</button></div>
  <div class="card-body">
    <form method="POST" action="<?= $u('teacher/assignments/store') ?>" enctype="multipart/form-data">
      <?= \Core\View::csrf() ?>
      <div class="form-group"><label>Title *</label><input type="text" name="title" required></div>
      <div class="form-group"><label>Description</label><textarea name="description" rows="3"></textarea></div>
      <div class="grid g2">
        <div class="form-group"><label>Subject *</label><select name="subject_id" required><option value="">Select</option><?php foreach($subjects as $s): ?><option value="<?= $s['id'] ?>"><?= htmlspecialchars($s['name']) ?></option><?php endforeach; ?></select></div>
        <div class="form-group"><label>Stream *</label><select name="stream_id" required><option value="">Select</option><?php foreach($streams as $s): ?><option value="<?= $s['stream_id'] ?>"><?= htmlspecialchars($s['class_name'].' '.$s['stream_name']) ?></option><?php endforeach; ?></select></div>
        <div class="form-group"><label>Due Date *</label><input type="date" name="due_date" required></div>
        <div class="form-group"><label>Total Marks</label><input type="number" name="total_marks" value="10" min="1"></div>
      </div>
      <div class="form-group"><label>File (PDF/DOC, max 5MB)</label><input type="file" name="file" accept=".pdf,.doc,.docx" style="padding:8px"></div>
      <button type="submit" class="btn btn-primary">Upload Assignment</button>
    </form>
  </div>
</div></div>
