<?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="row px-0 m-0">
          <?php include_once __DIR__ . '/../layouts/menu_body.php'; ?>

          <div class="col-12 col-md-9 px-0">
            <div class="p-4">
              <h5 class="fw-bold">Koreksi Jawaban</h5>

				<?php if ($success_message): ?>
				  <div class="alert alert-success alert-dismissible fade show" role="alert" id="flash-success">
					<?= htmlspecialchars($success_message) ?>
				  </div>
				<?php endif; ?>

				<?php if ($error_message): ?>
				  <div class="alert alert-danger alert-dismissible fade show" role="alert" id="flash-error">
					<?= htmlspecialchars($error_message) ?>
				  </div>
				<?php endif; ?>

				<!-- Script auto-hide after 3 seconds -->
				<script>
				  setTimeout(() => {
					const success = document.getElementById('flash-success');
					const error = document.getElementById('flash-error');
					if (success) success.style.display = 'none';
					if (error) error.style.display = 'none';
				  }, 3000); // 3 seconds = 3000 milliseconds
				</script>


              <!-- Tabel Mahasiswa, sembunyikan jika ada yang dikoreksi -->
              <div id="mahasiswa-table" style="display: <?= ($nimYangDikoreksi) ? 'none' : 'block' ?>;">
                <?php if ($kodeMkDipilih && $kodeKelasDipilih): ?>
                  <div class="mt-4">
				    <a href="/dashboard/dosen-download_jawaban?kode_mk=<?= urlencode($kodeMkDipilih) ?>&kode_kelas=<?= urlencode($kodeKelasDipilih) ?>" class="btn btn-primary mt-3" target="_BLANK">
						<i class="bi bi-file-earmark-pdf"></i> Download
					</a>
					<a href="/dashboard/dosen-koreksi-soal" class="btn btn-danger mt-3" >
						<i class="bi bi-arrow-counterclockwise"></i> Tutup Tabel
					</a>
                    </br>
                    </br>
                    
                    <div class="table-responsive">
                    <table class="table table-bordered">
                      <thead>
                        <tr>
                          <th>No</th>
                          <th>NIM</th>
                          <th>Nama</th>
                          <th>Status</th>
                          <th>Ujian</th>
                          <th>Nilai</th>
                          <th>Aksi</th>
                        </tr>
                      </thead>
                      <tbody>
                        <?php if (!empty($mahasiswa)): ?>
                          <?php $no = 1; ?>
                          <?php foreach ($mahasiswa as $mhs): ?>
                            <tr>
                              <td><?= $no++ ?></td>
                              <td><?= htmlspecialchars($mhs['nim']) ?></td>
                              <td><?= htmlspecialchars($mhs['nama']) ?></td>
                              <td class="fs-6"><?= htmlspecialchars($mhs['status_jawaban']) ?></td>
                              <td><?= htmlspecialchars($mhs['jenis_soal'] ?? '-') ?></td>
                              <td><?= htmlspecialchars($mhs['nilai']) ?></td>
                              <td>
                                <a href="/dashboard/dosen-koreksi-jawaban?kode_mk=<?= urlencode($kodeMkDipilih) ?>&kode_kelas=<?= urlencode($kodeKelasDipilih) ?>&nim=<?= urlencode($mhs['nim']) ?>" 
                                   class="btn btn-warning btn-sm">Koreksi</a>
                              </td>
                            </tr>
                          <?php endforeach; ?>
                        <?php else: ?>
                          <tr><td colspan="6" class="text-center">Belum ada mahasiswa ditemukan.</td></tr>
                        <?php endif; ?>
                      </tbody>
                    </table>
                    </div>
                  </div>
                <?php endif; ?>
              </div>

              <!-- Form Koreksi -->
              <?php if ($nimYangDikoreksi && $kodeMkDipilih): ?>
                <div class="mt-4 border p-3 bg-light rounded" id="form-koreksi">
                  <h6>
                    Koreksi Jawaban Mahasiswa: 
                    <span class="text-primary"><?= htmlspecialchars($nimYangDikoreksi) ?></span> - 
                    <span class="text-secondary"><?= htmlspecialchars($namaMahasiswa ?? 'Nama tidak ditemukan') ?></span>
                  </h6>
                  <form method="post" action="/dashboard/dosen-koreksi-jawaban?kode_mk=<?= urlencode($kodeMkDipilih) ?>&kode_kelas=<?= urlencode($kodeKelasDipilih) ?>&nim=<?= urlencode($nimYangDikoreksi) ?>">
                    <table class="table table-striped">
                      <thead>
                        <tr>
                          <th>Soal</th>
                          <th>Jawaban</th>
                        </tr>
                      </thead>
                      <tbody>
                        <?php if ($jawaban): ?>
                          <?php foreach ($jawaban as $item): ?>
                            <tr>
                              <td><?= nl2br(htmlspecialchars($item['soal'])) ?></td>
                              <td><?= nl2br(htmlspecialchars($item['jawaban'])) ?></td>
                            </tr>
                          <?php endforeach; ?>
                        <?php else: ?>
                          <tr><td colspan="2" class="text-center">Jawaban tidak ditemukan.</td></tr>
                        <?php endif; ?>
                      </tbody>
                    </table>

                    <div class="mb-3">
                      <label for="nilai_total" class="form-label">Nilai Total</label>
                      <input type="number" min="0" max="100" step="0.01" class="form-control" id="nilai_total" name="nilai_total" value="<?= htmlspecialchars($nilaiMahasiswa ?? '') ?>" required>
                    </div>

                    <input type="hidden" name="kode_mk" value="<?= htmlspecialchars($kodeMkDipilih) ?>">
                    <input type="hidden" name="kode_kelas" value="<?= htmlspecialchars($kodeKelasDipilih) ?>">

                    <?php if (!empty($item['tipe_soal'])): ?>
                        <button type="submit" class="btn btn-success">
                            Simpan Nilai <?= nl2br(htmlspecialchars($item['tipe_soal'])) ?>
                        </button>
                    <?php endif; ?>
                    <a href="/dashboard/dosen-koreksi-jawaban?kode_mk=<?= urlencode($kodeMkDipilih) ?>&kode_kelas=<?= urlencode($kodeKelasDipilih) ?>" class="btn btn-secondary">Kembali</a>
                  </form>
                </div>
              <?php endif; ?>

            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

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