<?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">Halaman KRS & KHS</h5>
                    </div>
                    <div class="row row-cols-1 row-cols-md-2 g-4">
                      <div class="col">
                        <div class="border rounded d-flex gap-3 p-4">
                          <i class="bi bi-printer m-0 rounded-pill rounded-icon"></i>
                          <div class="text-start">
                            <h6 class="mb-1 fw-bold">KRS</h6>
                            <div class="text-muted small text-opacity-50 mb-3">Cetak KRS</div>
                            <div id="v-pills-tab" role="tablist" aria-orientation="vertical">
                              <a href="/dashboard/mahasiswa-krs" class="fw-bold text-danger text-decoration-none text-uppercase">Open</a>
                            </div>
                          </div>
                          <i class="bi bi-check-circle-fill ms-auto"></i>
                        </div>
                      </div>
                      <div class="col">
                        <div class="border rounded d-flex gap-3 p-4">
                          <i class="bi bi-printer-fill m-0 rounded-pill rounded-icon"></i>
                          <div class="text-start">
                            <h6 class="mb-1 fw-bold">KHS</h6>
                            <div class="text-muted small text-opacity-50 mb-3">Cetak KHS</div>
                            <a href="/dashboard/mahasiswa-khs" class="fw-bold text-danger text-decoration-none text-uppercase">Open</a>
                          </div>
                          <i class="bi bi-check-circle-fill ms-auto"></i>
                        </div>
                      </div>
                    </div>
                    <div class="col">
                      </br>
                    </div>
					
					  <div class="shadow-sm bg-white rounded overflow-hidden border">
						<div class="p-4">

							<?php
							// Proteksi CSRF sederhana (bisa kamu sesuaikan dengan implementasi framework)
							if (!isset($_SESSION['csrf_token'])) {
								$_SESSION['csrf_token'] = bin2hex(random_bytes(32));
							}
							?>

							<form method="post" action="/dashboard/mahasiswa-krs/submit" id="krsForm">
							  <input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">

							  <?php
								$total_sks = 0;
								$kode_mk_array = [];

								foreach ($matakuliah_list as $mk) {
									if (!in_array($mk['kode_mk'], $krs_mahasiswa)) {
										$kode_mk_array[] = $mk['kode_mk'];
										$total_sks += $mk['sks'];
									}
								}
							  ?>

							  <!-- Hidden inputs untuk semua kode MK dan total SKS -->
							  <input type="hidden" name="kode_mk_list" value="<?= implode(',', $kode_mk_array) ?>">
							  <input type="hidden" name="total_sks" value="<?= $total_sks ?>">

							  <button type="submit" name="submit_all" class="btn btn-warning mb-3">Kontrak Semua Mata Kuliah</button>

							  <table class="table table-bordered">
								<thead>
								  <tr>
									<th>Kode MK</th>
									<th>Nama Mata Kuliah</th>
									<th>SKS</th>
									<th>Status</th>
								  </tr>
								</thead>
								<tbody>
								  <?php foreach ($matakuliah_list as $mk): ?>
									<?php $sudah_kontrak = in_array($mk['kode_mk'], $krs_mahasiswa); ?>
									<tr>
									  <td><?= htmlspecialchars($mk['kode_mk']) ?></td>
									  <td><?= htmlspecialchars($mk['nama_mk']) ?></td>
									  <td><?= $mk['sks'] ?></td>
									  <td><center>
										<?php if ($sudah_kontrak): ?>
										  <span class="text-info"> <i class="bi bi-check-circle-fill"></i></span>
										<?php else: ?>
										  <span class="text-danger"> <i class="bi bi-x-circle"></i></span>
										<?php endif; ?>
									  </center></td>
									</tr>
								  <?php endforeach; ?>
								</tbody>
							  </table>

							  <div class="mt-3">
								<strong>Total SKS yang akan dikontrak: </strong> <?= $total_sks ?>
							  </div>
							</form>


						  <hr>

						  <form action="/dashboard/pdfkrs" method="get" target="_blank" style="display: inline;">
							<button type="submit" class="btn btn-info">
							  <i class="bi bi-file-earmark-pdf"></i> Cetak KRS
							</button>
						  </form>

						</div>
					  </div>
				 
				  </div>
				</div>
				
				<?php if (!empty($_SESSION['info'])): ?>
				<script>
					Swal.fire({
						icon: '<?= $_SESSION['info_type'] ?? 'info'; ?>',
						title: 'Informasi',
						text: '<?= $_SESSION['info']; ?>',
						timer: 3000,
						showConfirmButton: false
					});
				</script>
				<?php unset($_SESSION['info'], $_SESSION['info_type']); ?>
				<?php endif; ?>


				<script>
				  // Hitung total SKS saat checkbox diubah
				  document.getElementById('krsForm').addEventListener('change', function(e) {
					if (e.target.name === 'kode_mk[]') {
					  let totalSks = 0;
					  const checkboxes = document.querySelectorAll('input[name="kode_mk[]"]:checked');
					  checkboxes.forEach(cb => {
						const row = cb.closest('tr');
						const sks = parseInt(row.children[2].textContent);
						totalSks += sks;
					  });
					  document.getElementById('totalSks').textContent = totalSks;
					}
				  });
				</script>

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