<?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">Detail Soal</h5>
                    </div>

                    <div class="bg-white rounded shadow-sm p-4">
                      <!-- HTML untuk menampilkan mata kuliah -->
                      <div class="container">
						<?php if (!empty($mata_kuliah) && is_array($mata_kuliah)): ?>
						  <ul class="list-group mt-4">
							<?php foreach ($mata_kuliah as $mk): ?>
							  <?php if (isset($mk['kode_mk'], $mk['kode_kelas'], $mk['nama_mk'])): ?>
								<li class="list-group-item">
								  <a href="/dashboard/dosen-lihatsoal?kode_mk=<?= htmlspecialchars($mk['kode_mk']) ?>&kode_kelas=<?= htmlspecialchars($mk['kode_kelas']) ?>&niy=<?= htmlspecialchars($_SESSION['user']['niy']) ?>" class="btn btn-primary">
									<?= htmlspecialchars($mk['nama_mk']) ?> (<?= htmlspecialchars($mk['kode_mk']) ?> - <?= htmlspecialchars($mk['kode_kelas']) ?>)
								  </a>
								</li>
							  <?php endif; ?>
							<?php endforeach; ?>
						  </ul>
						<?php else: ?>
						  <p>Tidak ada mata kuliah yang ditemukan.</p>
						<?php endif; ?>
                      </div>

                      <!-- Tabel Soal (jika ada) -->
                      <?php if (!empty($soal)): ?>
                        <div id="soalTable" class="mt-5">
                          <h5>Daftar Soal - <?= htmlspecialchars($_GET['kode_mk']) ?> (<?= htmlspecialchars($_GET['kode_kelas']) ?>)</h5>
                          <a href="/dashboard/dosen-download_pdfsoal?kode_mk=<?= urlencode($_GET['kode_mk']) ?>&kode_kelas=<?= urlencode($_GET['kode_kelas']) ?>&niy=<?= urlencode($_GET['niy']) ?>" class="btn btn-success mb-3" target="_blank">Download PDF</a>
                          <button id="closeTable" class="btn btn-danger mb-3">Tutup Tabel</button>

                          <div class="table-responsive">
                            <table class="table table-bordered">
                              <thead class="table-dark">
                                <tr>
                                  <th>No</th>
                                  <th>Soal</th>
                                  <th>Tipe Soal</th>
                                  <th>Tanggal Upload</th>
                                  <th>Aksi</th>
                                </tr>
                              </thead>
                              <tbody>
                                <?php $no = 1; foreach ($soal as $s): ?>
                                  <tr>
                                    <td><?= $no++ ?></td>
                                    <td><?= htmlspecialchars($s['soal']) ?></td>
                                    <td><?= htmlspecialchars($s['tipe_soal']) ?></td>
                                    <td><?= htmlspecialchars($s['tanggal_upload']) ?></td>
                                    <td>
                                      <button type="button" class="btn btn-warning btn-sm edit-btn"
                                        data-id="<?= $s['id_soal'] ?>"
                                        data-soal="<?= htmlspecialchars($s['soal']) ?>">
                                        Edit
                                      </button>
                                      <form method="POST" action="/dashboard/dosen-lihatsoal/delete?kode_mk=<?= urlencode($_GET['kode_mk']) ?>&kode_kelas=<?= urlencode($_GET['kode_kelas']) ?>" style="display:inline;">
                                        <input type="hidden" name="action" value="delete">
                                        <input type="hidden" name="deleteId" value="<?= $s['id_soal'] ?>">
                                        <button type="submit" class="btn btn-danger btn-sm">Hapus</button>
                                      </form>
                                    </td>
                                  </tr>
                                <?php endforeach; ?>
                              </tbody>
                            </table>
                          </div>
                        </div>
                      <?php elseif (isset($_GET['kode_mk'])): ?>
                        <div class="alert alert-warning mt-4">Belum ada soal yang tersedia untuk mata kuliah ini.</div>
                      <?php endif; ?>

                      <!-- Modal Edit Soal -->
                      <div class="modal fade" id="editModal" tabindex="-1" aria-labelledby="editModalLabel" aria-hidden="true">
                        <div class="modal-dialog">
                          <form id="editForm" method="POST" action="/dashboard/dosen-lihatsoal/edit?kode_mk=<?= urlencode($_GET['kode_mk'] ?? '') ?>&kode_kelas=<?= urlencode($_GET['kode_kelas'] ?? '') ?>">
                            <input type="hidden" name="action" value="edit">
                            <input type="hidden" name="editId" id="editId">
                            <div class="modal-content">
                              <div class="modal-header">
                                <h5 class="modal-title" id="editModalLabel">Edit Soal</h5>
                                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                              </div>
                              <div class="modal-body">
                                <div class="mb-3">
                                  <label for="editSoal" class="form-label">Soal</label>
                                  <textarea name="editSoal" id="editSoal" class="form-control" rows="5" required></textarea>
                                </div>
                              </div>
                              <div class="modal-footer">
                                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                                <button type="submit" class="btn btn-primary">Simpan Perubahan</button>
                              </div>
                            </div>
                          </form>
                        </div>
                      </div>

                      <!-- Script edit modal -->
                      <script>
                        document.querySelectorAll('.edit-btn').forEach(button => {
                          button.addEventListener('click', function () {
                            const soal = this.getAttribute('data-soal');
                            const id = this.getAttribute('data-id');
                            document.getElementById('editSoal').value = soal;
                            document.getElementById('editId').value = id;
                            const modal = new bootstrap.Modal(document.getElementById('editModal'));
                            modal.show();
                          });
                        });

                        const closeBtn = document.getElementById('closeTable');
                        if (closeBtn) {
                          closeBtn.addEventListener('click', () => {
                            const table = document.getElementById('soalTable');
                            if (table) table.style.display = 'none';
                          });
                        }
                      </script>
                    </div>
                  </div>
                </div>

              </div>
            </div>

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