<?php include_once __DIR__ . '/../layouts/head.php'; ?>

<div class="container py-4" style="max-width: 1350px; width: 100%;">
  <div class="row">
    <div class="col-lg-12">
      <div class="shadow-sm bg-white rounded overflow-hidden border">
        <div class="p-0">
          <div class="row px-0 m-0">

            <?php include_once __DIR__ . '/../layouts/menu_body.php'; ?>

            <div class="col-12 col-md-9 px-0">
              <div class="tab-content" id="v-pills-tabContent">

                <div class="tab-pane fade show active" id="v-pills-home" role="tabpanel" aria-labelledby="v-pills-home-tab">
                  <div class="p-4">
                    <div class="mb-4">
                      <h5 class="mb-0 fw-bold">Presensi & Jadwal Perkuliahan</h5>
                    </div>

                    <!-- Navigasi Tab -->
                    <div class="btn-group gap-3 osahan-btn-group d-flex my-3" role="group" aria-label="Basic radio toggle button group">
                      <input type="radio" class="btn-check" name="btnmenu" id="btnmenu1" <?= $page == 'jadwal' ? 'checked' : ''; ?> onclick="location.href='/dashboard/mahasiswa-jadwal?page=jadwal'">
                      <label class="btn btn-outline-light d-flex gap-4 rounded p-4 col-6" for="btnmenu1">
                        <i class="bi bi-calendar-check h5 mb-0"></i>
                        <span class="text-start">
                          <b class="mb-1 h6 d-block fw-bold">Jadwal</b>
                          <small class="text-muted text-opacity-50 mb-4">Lihat jadwal perkuliahan</small>
                        </span>
                        <i class="bi bi-check-circle-fill ms-auto"></i>
                      </label>

                      <input type="radio" class="btn-check" name="btnmenu" id="btnmenu2" <?= $page == 'presensi' ? 'checked' : ''; ?> onclick="location.href='/dashboard/mahasiswa-jadwal?page=presensi'">
                      <label class="btn btn-outline-light d-flex gap-4 rounded p-4 col-6" for="btnmenu2">
                        <i class="bi bi-check-square h5 mb-0"></i>
                        <span class="text-start">
                          <b class="mb-1 h6 d-block fw-bold">Presensi</b>
                          <small class="text-muted text-opacity-50 mb-4">Cek status kehadiran</small>
                        </span>
                        <i class="bi bi-check-circle-fill ms-auto"></i>
                      </label>
                    </div>

                    <?php if (!empty($error)) : ?>
                      <div class="alert alert-danger"><?= $error; ?></div>
                    <?php endif; ?>

                    <?php if ($page == 'jadwal') : ?>
                      <br>
                      <center><h5>Tabel Jadwal</h5></center>
                      <br>

                      <table class="table table-bordered">
                        <thead>
                          <tr>
                            <th>No</th>
                            <th>Mata Kuliah</th>
                            <th>Dosen</th>
                            <th>Hari</th>
                            <th>Jam</th>
                            <th>Ruangan</th>
                          </tr>
                        </thead>
                        <tbody>
                          <?php if (empty($jadwal)) : ?>
                            <tr><td colspan="6" class="text-center text-danger">Jadwal belum tersedia</td></tr>
                          <?php else : ?>
                            <?php $no = 1; foreach ($jadwal as $j) : ?>
                              <tr>
                                <td><?= $no++ ?></td>
                                <td><?= htmlspecialchars($j['nama_mk']) ?></td>
                                <td><?= htmlspecialchars($j['nama_dosen']) ?></td>
                                <td><?= htmlspecialchars($j['hari']) ?></td>
                                <td><?= $j['jam_mulai'] . ' - ' . $j['jam_selesai'] ?></td>
                                <td><?= htmlspecialchars($j['ruangan']) ?></td>
                              </tr>
                            <?php endforeach; ?>
                          <?php endif; ?>
                        </tbody>
                      </table>

                    <?php elseif ($page == 'presensi') : ?>
                      <br>
                      <center><h5>Presensi Mahasiswa</h5></center>
                      <br>

                      <!-- Form Pilih Pertemuan -->
                      <form method="GET">
                        <input type="hidden" name="page" value="presensi">
                        <?php $pertemuan_ke = $_GET['pertemuan_ke'] ?? ''; ?>
                        <select name="pertemuan_ke" onchange="this.form.submit()" class="form-select w-25 mb-3">
                          <option value="">Pilih Pertemuan</option>
                          <?php for ($i = 1; $i <= 16; $i++): ?>
                            <option value="<?= $i ?>" <?= ($pertemuan_ke == $i) ? 'selected' : '' ?>>Pertemuan <?= $i ?></option>
                          <?php endfor; ?>
                        </select>
                      </form>

                      <!-- Tabel Presensi -->
                      <table class="table table-bordered">
                        <thead>
                          <tr>
                            <th>No</th>
                            <th>Kode MK</th>
                            <th>Nama MK</th>
                            <th>Pertemuan Ke</th>
                            <th>Status</th>
                          </tr>
                        </thead>
                        <tbody>
                          <?php if (empty($presensi)) : ?>
                            <tr><td colspan="5" class="text-center text-danger">Presensi belum tersedia</td></tr>
                          <?php else : ?>
                            <?php $no = 1; foreach ($presensi as $p) : 
                              $warna = match ($p['status_absensi']) {
                                'Hadir' => 'green',
                                'Izin' => 'yellow',
                                'Sakit' => 'purple',
                                'Alfa' => 'red',
                                default => 'white'
                              };
                            ?>
                              <tr>
                                <td><?= $no++ ?></td>
                                <td><?= htmlspecialchars($p['kode_mk']) ?></td>
                                <td><?= htmlspecialchars($p['nama_mk']) ?></td>
                                <td><?= $p['pertemuan_ke'] ?></td>
                                <td style="background-color:<?= $warna ?>"><?= htmlspecialchars($p['status_absensi']) ?></td>
                              </tr>
                            <?php endforeach; ?>
                          <?php endif; ?>
                        </tbody>
                      </table>
                    <?php endif; ?>

                    <!-- Informasi Prodi -->
                    <br>
                    <div class="border rounded d-flex gap-3 p-4">
                      <div class="text-start">
                        <div class="text-muted small text-opacity-50 mb-3">
                          Kode Prodi :</br>
                          <ul class="list-unstyled">
                            <li><i class="bi bi-terminal"></i>&nbsp; 86207 (Pendidikan Guru Sekolah Dasar)</li>
                            <li><i class="bi bi-terminal"></i>&nbsp; 86206 (Pendidikan Guru Pendidikan Anak Usia Dini)</li>
                          </ul>
                        </div>
                      </div>
                    </div>

                  </div> <!-- end p-4 -->
                </div> <!-- end tab-pane -->

              </div> <!-- end tab-content -->
            </div> <!-- end col-md-9 -->

          </div> <!-- end row -->
        </div> <!-- end p-0 -->
      </div> <!-- end shadow-sm -->
    </div> <!-- end col -->
  </div> <!-- end row -->
</div> <!-- end container -->

<?php include_once __DIR__ . '/../layouts/footer.php'; ?>
