<?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">
						
							<!-- wates -->
							<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">Manajemen Data PMB</h5>
									</div>
									
									<!-- app/views/pegawai/manage_pmb.php -->
									<div class="container">
                                    <div class="d-flex gap-3">
                                      <a href="/dashboard/pegawai-data_pmb" class="text-primary text-decoration-none">
                                        <i class="bi bi-file-earmark-bar-graph"></i> Data Mahasiswa Baru
                                      </a>
                                    
                                      <a href="/dashboard/pegawai-ubah_gel_mhs" class="text-primary text-decoration-none">
                                        <i class="bi bi-file-earmark-bar-graph"></i> Ubah Gel Mhs
                                      </a>
                                    </div>
                                    </p>
										<div class="row">
											<!-- Aktivasi PMB -->
											<div class="col-md-6">
												<div class="border rounded p-4 mb-3">
													<h5 class="fw-bold mb-3">Aktivasi PMB</h5>
													<form method="POST" action="/manage_pmb/updatePegAksesPmb">
														<div class="mb-2">
															<select name="status_pmb" class="form-select">
																<option value="Aktif" <?= ($status_akses == 'Aktif') ? 'selected' : '' ?>>Aktif</option>
																<option value="Tidak Aktif" <?= ($status_akses == 'Tidak Aktif') ? 'selected' : '' ?>>Tidak Aktif</option>
															</select>
														</div>
														<button type="submit" class="btn btn-primary">Simpan</button>
													</form>
												</div>
											</div>

											<!-- Upload Soal Tes PMB -->
										<?php if (isset($_SESSION['flash_message'])): ?>
										<script>
										  Swal.fire({
											icon: 'info',
											title: 'Info',
											text: <?= json_encode($_SESSION['flash_message']) ?>,
											timer: 3000,
											timerProgressBar: true,
											showConfirmButton: false
										  });
										</script>
										<?php unset($_SESSION['flash_message']); endif; ?>
											<div class="col-md-6">
												<div class="border rounded p-4 mb-3">
													<h5 class="fw-bold mb-3">Upload Soal Tes PMB</h5>
													<form method="POST" enctype="multipart/form-data" action="/dashboard/pegawai-manage_pmb/uploadSoal">
														<div class="mb-2">
															<input type="file" name="excelFile" class="form-control" accept=".xls,.xlsx" required>
														</div>
														<button type="submit" name="upload_soal" class="btn btn-success">Upload</button>
														<a href="/download_format?file=Format_Soal_Tes_PMB.xlsx" class="text-danger text-decoration-none">
															<i class="bi bi-file-earmark-bar-graph"></i> Download Format</a>
													</form>
												</div>
											</div>
										</div>

										<div class="row">
											<!-- Aktivasi Soal PMB -->
											<div class="col-md-6">
												<div class="border rounded p-4 mb-3">
													<h5 class="fw-bold mb-3">Aktivasi Soal PMB</h5>
													<form method="POST" action="/manage_pmb/updatePegStatusSoal">
														<div class="mb-2">
															<select name="status_pmb" class="form-select" required>
																<option value="Aktif" <?= ($status_soal == 'Aktif') ? 'selected' : '' ?>>Aktif</option>
																<option value="Tidak Aktif" <?= ($status_soal == 'Tidak Aktif') ? 'selected' : '' ?>>Tidak Aktif</option>
															</select>
														</div>
														<button type="submit" class="btn btn-primary">Simpan</button>
													</form>
												</div>
											</div>

											<!-- Download Hasil Tes PMB -->
											<div class="col-md-6">
												<div class="border rounded p-4 mb-3">
													<h5 class="fw-bold mb-3">Download Hasil Tes PMB</h5>
													<form method="POST" action="/manage_pmb/downloadPegHasil" target="_blank">
														<div class="mb-2">
															<select name="tanggal" class="form-control" required>
																<option value="">Pilih Tanggal</option>
																<?php foreach ($tanggal_options as $row) : ?>
																	<option value="<?= $row['tanggal'] ?>"><?= date('d-m-Y', strtotime($row['tanggal'])) ?></option>
																<?php endforeach; ?>
															</select>
														</div>
														<button type="submit" class="btn btn-success">Download</button>
													</form>
												</div>
											</div>
										</div>

										<div class="border rounded p-4 mb-3">
											<h5 class="fw-bold mb-3">Tambah Gelombang PMB</h5>
											<form method="POST" action="/manage_pmb/tambahPegGelombang">
												<div class="mb-2">
													<label class="fw-bold">Nama Gelombang:</label>
													<input type="text" name="gelombang" class="form-control" required>
												</div>
												<div class="mb-2">
													<label class="fw-bold">Tanggal:</label>
													<input type="date" name="tanggal" class="form-control" required>
												</div>
												<div class="mb-2">
													<label class="fw-bold">Tahun Penerimaan:</label>
													<input type="text" class="form-control" value="<?= $tahun_sekarang ?>" readonly>
												</div>
												<button type="submit" name="tambah_gelombang" class="btn btn-primary">Tambah</button>
											</form>
										</div>

										<div class="border rounded p-4 mb-3">
											<h5 class="fw-bold mb-3">Daftar Gelombang PMB</h5>
											<table class="table table-bordered table-striped">
												<thead>
													<tr>
														<th>No</th>
														<th>Gelombang</th>
														<th>Tanggal</th>
														<th>Status</th>
														<th>Aksi</th>
													</tr>
												</thead>
												<tbody>
													<?php if (empty($gelombang_list)) : ?>
														<tr>
															<td colspan="5" class="text-center">Tidak ada data gelombang</td>
														</tr>
													<?php else : ?>
														<?php $no = 1; foreach ($gelombang_list as $gel) : ?>
															<tr>
																<td><?= $no++ ?></td>
																<td><?= htmlspecialchars($gel['gelombang']) ?></td>
																<td><?= date('d-m-Y', strtotime($gel['tanggal'])) ?></td>
																<td><?= $gel['status'] ?></td>
																<td>
																	<form method="POST" action="/manage_pmb/hapusPegGelombang" style="display:inline-block;" onsubmit="return confirm('Yakin hapus gelombang ini?');">
																		<input type="hidden" name="id_gel_pmb" value="<?= $gel['id_gel_pmb'] ?>">
																		<button type="submit" name="hapus_gelombang" class="btn btn-danger btn-sm">Hapus</button>
																	</form>
																	<button class="btn btn-warning btn-sm" onclick="editGelombang('<?= $gel['id_gel_pmb'] ?>', '<?= htmlspecialchars($gel['gelombang']) ?>', '<?= $gel['tanggal'] ?>')">Edit</button>
																</td>
															</tr>
														<?php endforeach; ?>
													<?php endif; ?>
												</tbody>
											</table>
										</div>

										<!-- Modal Edit Gelombang -->
										<div class="modal fade" id="editGelombangModal" tabindex="-1" aria-labelledby="editGelombangLabel" aria-hidden="true">
											<div class="modal-dialog">
												<form method="POST" action="/manage_pmb/editPegGelombang">
													<div class="modal-content">
														<div class="modal-header">
															<h5 class="modal-title" id="editGelombangLabel">Edit Gelombang PMB</h5>
															<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
														</div>
														<div class="modal-body">
															<input type="hidden" name="id_gel_pmb" id="edit_id_gel_pmb">
															<div class="mb-2">
																<label class="fw-bold">Nama Gelombang:</label>
																<input type="text" name="gelombang" id="edit_gelombang" class="form-control" required>
															</div>
															<div class="mb-2">
																<label class="fw-bold">Tanggal:</label>
																<input type="date" name="tanggal" id="edit_tanggal" class="form-control" required>
															</div>
														</div>
														<div class="modal-footer">
															<button type="submit" name="edit_gelombang" class="btn btn-primary">Simpan Perubahan</button>
															<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
														</div>
													</div>
												</form>
											</div>
										</div>
									</div>

									<script>
									function editGelombang(id, gelombang, tanggal) {
										document.getElementById('edit_id_gel_pmb').value = id;
										document.getElementById('edit_gelombang').value = gelombang;
										document.getElementById('edit_tanggal').value = tanggal;
										var modal = new bootstrap.Modal(document.getElementById('editGelombangModal'));
										modal.show();
									}
									</script>

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