<?= $this->extend('layout/management_layout') ?>
<?= $this->section('content') ?>
<div class="container-fluid mt-3">
  <h2 class="mb-3">Late Slip Logs</h2>

  <?php if (session()->getFlashdata('success')): ?>
    <div class="alert alert-success"><?= esc(session()->getFlashdata('success')) ?></div>
  <?php endif; ?>
  <?php if (session()->getFlashdata('error')): ?>
    <div class="alert alert-danger"><?= esc(session()->getFlashdata('error')) ?></div>
  <?php endif; ?>

  <form class="row g-2 align-items-end mb-3" method="get" action="<?= site_url('administrator/late_slips') ?>">
    <div class="col-auto">
      <label for="school_year" class="form-label">School Year</label>
      <input type="text" name="school_year" id="school_year" class="form-control" value="<?= esc($schoolYear ?? ($_GET['school_year'] ?? '')) ?>" placeholder="e.g. 2025-2026">
    </div>
    <div class="col-auto">
      <label for="semester" class="form-label">Semester</label>
      <?php $semVal = (string)($semester ?? ($_GET['semester'] ?? '')); ?>
      <select name="semester" id="semester" class="form-select">
        <option value="">—</option>
        <option value="Fall"   <?= (strcasecmp($semVal, 'Fall')   === 0 ? 'selected' : '') ?>>Fall</option>
        <option value="Spring" <?= (strcasecmp($semVal, 'Spring') === 0 ? 'selected' : '') ?>>Spring</option>
      </select>
    </div>
    <div class="col-auto">
      <label for="q" class="form-label">Student</label>
      <input type="text" name="q" id="q" class="form-control" value="<?= esc($filters['q'] ?? '') ?>" placeholder="search name">
    </div>
    <div class="col-auto">
      <label for="date_from" class="form-label">From</label>
      <input type="date" name="date_from" id="date_from" class="form-control" value="<?= esc($filters['date_from'] ?? '') ?>">
    </div>
    <div class="col-auto">
      <label for="date_to" class="form-label">To</label>
      <input type="date" name="date_to" id="date_to" class="form-control" value="<?= esc($filters['date_to'] ?? '') ?>">
    </div>
    <div class="col-auto">
      <button type="submit" class="btn btn-primary">Filter</button>
      <a class="btn btn-secondary" href="<?= site_url('administrator/late_slips') ?>">Reset</a>
    </div>
  </form>

  <div class="table-responsive">
    <table class="table table-striped" id="lateSlipLogsTable">
      <thead>
        <tr>
          <th>ID</th>
          <th>Printed At</th>
          <th>Student</th>
          <th>Grade</th>
          <th>Date</th>
          <th>Time In</th>
          <th>Reason</th>
          <th>School Year</th>
          <th>Admin</th>
          <th>Action</th>
        </tr>
      </thead>
      <tbody>
        <?php foreach (($logs ?? []) as $r): ?>
          <tr>
            <td><?= (int)($r['id'] ?? 0) ?></td>
            <td><?= esc(!empty($r['printed_at']) ? local_datetime($r['printed_at'], 'm-d-Y H:i') : '') ?></td>
            <td><?= esc($r['student_name'] ?? '') ?></td>
            <td><?= esc($r['grade'] ?? '') ?></td>
            <td><?= esc(!empty($r['slip_date']) ? local_date($r['slip_date'], 'm-d-Y') : '') ?></td>
            <td><?= esc($r['time_in'] ?? '') ?></td>
            <td><?= esc($r['reason'] ?? '') ?></td>
            <td><?= esc($r['school_year'] ?? '') ?></td>
            <td><?= esc($r['admin_name'] ?? '') ?></td>
            <td>
              <form action="<?= site_url('administrator/late_slips/reprint/' . (int)($r['id'] ?? 0)) ?>" method="post" class="d-inline">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-sm btn-primary" title="Reprint">
                  <i class="bi bi-printer"></i> Reprint
                </button>
              </form>
            </td>
          </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>
</div>

<?= $this->endSection() ?>

<?= $this->section('scripts') ?>
<script>
  $(function(){
    $('#lateSlipLogsTable').DataTable({
      pageLength: 25,
      order: [[0, 'desc']]
    });
  });
  </script>
<?= $this->endSection() ?>
