<?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="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">Input Nilai</h5>
							</div>
							
							<div class="d-flex justify-content-between align-items-center mb-2">
								<a href="/dashboard/dosen-input-nilai" class="text-light text-decoration-none btn btn-danger" title="Back">
									<i class="bi bi-skip-backward-circle"></i> Back
								</a>
								<a href="#" class="btn btn-dark" style="font-size:11px; width: 60%;">
									<marquee>Pembagian Nilai Akhir diambil otomatis dari Absensi 20% Tugas 20% UTS 30% dan UAS 30%</marquee>
								</a>
							</div>
							</br>
							<p>Daftar mahasiswa untuk mata kuliah : <strong><?= $infoMK['kode_mk'] ?? '-' ?></strong> - Kelas : <strong><?= $infoMK['kode_kelas'] ?? '-' ?></strong></p>
                            <a href="/dashboard/dosen-download-nilai-akhir?kode_mk=<?= $infoMK['kode_mk'] ?>&kelas=<?= $infoMK['kode_kelas'] ?>" target="_blank" class="btn btn-success mb-2">
                                <i class="bi bi-download"></i> Download Nilai Excel
                            </a>
							<form method="POST" action="/dashboard/dosen-dosen-view-nilaikelas">
								<table class="table table-bordered" id="data-table">
									<thead>
										<tr style="font-size: 13px;">
											<th><center>No</center></th>
											<th><center>NIM</center></th>
											<th><center>Nama</center></th>
											<th><center>UTS</center></th>
											<th><center>UAS</center></th>
											<th><center>Tugas</center></th>
											<th><center>Presensi</center></th>
											<th><center>Nilai Akhir</center></th>
										</tr>
									</thead>
									<tbody>
									<?php if (!empty($dataMahasiswa)): ?>
										<?php $no = 1; foreach ($dataMahasiswa as $mhs): 
											$uts = $mhs['nilai_uts'] ?? 0;
											$uas = $mhs['nilai_uas'] ?? 0;
											$tugas = $mhs['nilai_tugas'] ?? 0;
											$absensi = $mhs['nilai_absensi'] ?? 0;
											$nilai_akhir = round(($uts * 0.30) + ($uas * 0.30) + ($tugas * 0.20) + ($absensi * 0.20));
										?>
											<tr style="font-size: 13px;">
												<td><?= $no ?></td>
												<td><?= htmlspecialchars($mhs['nim']) ?></td>
												<td><?= htmlspecialchars($mhs['nama']) ?></td>
												<td><input type="number" name="nilai_uts<?= $no ?>" value="<?= $uts ?>" readonly class="form-control" style="width:70px;"></td>
												<td><input type="number" name="nilai_uas<?= $no ?>" value="<?= $uas ?>" readonly class="form-control" style="width:70px;"></td>
												<td><input type="number" name="nilai_tugas<?= $no ?>" value="<?= $tugas ?>" class="form-control" style="width:70px;" oninput="handleChange(this, <?= $no ?>)" onfocus="clearZero(this)"></td>
												<td><input type="number" name="nilai_absensi<?= $no ?>" value="<?= round($absensi) ?>" readonly class="form-control" style="width:70px;"></td>
												<td><input type="number" name="nilai_akhir<?= $no ?>" value="<?= $nilai_akhir ?>" readonly class="form-control" style="width:70px;"></td>
												<input type="hidden" name="nim<?= $no ?>" value="<?= $mhs['nim'] ?>">
											</tr>
										<?php $no++; endforeach; ?>
										<input type="hidden" name="n" value="<?= $no - 1 ?>">
										<input type="hidden" name="kode_mk" value="<?= $infoMK['kode_mk'] ?>">
										<input type="hidden" name="sks" value="<?= $infoMK['sks'] ?>">
									<?php else: ?>
										<tr><td colspan="8" class="text-center">Belum ada data.</td></tr>
									<?php endif; ?>
									</tbody>
								</table>
								<?php if (!empty($dataMahasiswa)): ?>
									<input type="submit" value="Simpan" name="simpan" class="btn btn-primary"/>
								<?php endif; ?>
							</form>

							<script>
							function clearZero(input) {
								if (input.value == "0") input.value = "";
							}

							function handleChange(input, row) {
								let value = Math.min(100, Math.max(0, Math.round(parseFloat(input.value) || 0)));
								input.value = value;
								calculateFinal(row);
							}

							function calculateFinal(row) {
								const uts = parseFloat(document.querySelector(`[name="nilai_uts${row}"]`).value) || 0;
								const uas = parseFloat(document.querySelector(`[name="nilai_uas${row}"]`).value) || 0;
								const tugas = parseFloat(document.querySelector(`[name="nilai_tugas${row}"]`).value) || 0;
								const absensi = parseFloat(document.querySelector(`[name="nilai_absensi${row}"]`).value) || 0;
								const akhir = Math.round(uts * 0.3 + uas * 0.3 + tugas * 0.2 + absensi * 0.2);
								document.querySelector(`[name="nilai_akhir${row}"]`).value = akhir;
							}
							</script>

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