<!doctype html>
<html lang="id">

<head>
	<meta charset="utf-8">
	<title>Manajemen Petugas</title>
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>

<body class="bg-light">

	<div class="container py-4">
		<?php if ($this->session->flashdata('err')): ?>
			<div class="alert alert-danger">
				<ul class="mb-0"><?= $this->session->flashdata('err'); ?></ul>
			</div>
		<?php endif; ?>
		<?php if ($this->session->flashdata('ok')): ?>
			<div class="alert alert-success mb-3"><?= $this->session->flashdata('ok'); ?></div>
		<?php endif; ?>

		<div class="card shadow rounded-4">
			<div class="card-header d-flex justify-content-between align-items-center bg-primary text-white">
				<h5 class="mb-0">Manajemen Petugas</h5>
				<button class="btn btn-light btn-sm" data-bs-toggle="modal" data-bs-target="#modalTambah">+ Tambah Petugas</button>
			</div>
			<div class="card-body">
				<div class="table-responsive">
					<table class="table table-bordered table-striped align-middle">
						<thead class="table-primary">
							<tr>
								<th>#</th>
								<th>NIP</th>
								<th>Nama</th>
								<th>Jabatan</th>
								<th>Username</th>
								<th>Hak Akses</th>
								<th>Email</th>
								<th>Telp</th>
								<th>Unit</th>
								<th>Poli</th>
								<th>Status</th>
								<th width="210">Aksi</th>
							</tr>
						</thead>
						<tbody>
							<?php foreach ($petugas as $i => $p): ?>
								<tr>
									<td><?= $i + 1 ?></td>
									<td><?= htmlentities($p->nip) ?></td>
									<td><?= htmlentities($p->nama_lengkap) ?></td>
									<td><?= htmlentities($p->nama_jabatan) ?></td>
									<td><?= htmlentities($p->username) ?></td>
									<td><?= htmlentities($p->nama_hak_akses) ?></td>
									<td><?= htmlentities($p->email) ?></td>
									<td><?= htmlentities($p->no_telp) ?></td>
									<td><?= htmlentities($p->id_unit) ?></td>
									<td><?= htmlentities($p->id_poli) ?></td>
									<td>
										<?php if ($p->status_aktif): ?>
											<span class="badge bg-success">Aktif</span>
										<?php else: ?>
											<span class="badge bg-secondary">Nonaktif</span>
										<?php endif; ?>
									</td>
									<td>
										<button class="btn btn-warning btn-sm" data-bs-toggle="modal" data-bs-target="#edit<?= $p->petugas_id ?>">Edit</button>
										<a class="btn btn-sm btn-outline-secondary" href="<?= site_url('petugas/toggle/' . $p->petugas_id) ?>">
											<?= $p->status_aktif ? 'Nonaktifkan' : 'Aktifkan' ?>
										</a>
										<a class="btn btn-sm btn-danger" onclick="return confirm('Hapus data ini?')" href="<?= site_url('petugas/delete/' . $p->petugas_id) ?>">Hapus</a>
									</td>
								</tr>

								<!-- Modal Edit -->
								<div class="modal fade" id="edit<?= $p->petugas_id ?>" tabindex="-1">
									<div class="modal-dialog modal-lg modal-dialog-scrollable">
										<div class="modal-content">
											<form method="post" action="<?= site_url('petugas/update/' . $p->petugas_id) ?>">
												<div class="modal-header">
													<h5 class="modal-title">Edit Petugas</h5>
													<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
												</div>
												<div class="modal-body">
													<div class="row g-2">
														<div class="col-md-3"><label class="form-label">NIP</label><input name="nip" class="form-control" value="<?= htmlentities($p->nip) ?>" required></div>
														<div class="col-md-5"><label class="form-label">Nama Lengkap</label><input name="nama_lengkap" class="form-control" value="<?= htmlentities($p->nama_lengkap) ?>" required></div>
														<div class="col-md-4"><label class="form-label">Jabatan</label>
															<select name="id_jabatan" class="form-select" required>
																<?php foreach ($jabatan as $j): ?>
																	<option value="<?= $j->kode_jabatan ?>" <?= $p->id_jabatan == $j->kode_jabatan ? 'selected' : ''; ?>><?= $j->nama_jabatan ?></option>
																<?php endforeach; ?>
															</select>
														</div>

														<div class="col-md-4"><label class="form-label">Tempat Lahir</label><input name="tempat_lahir" class="form-control" value="<?= htmlentities($p->tempat_lahir) ?>" required></div>
														<div class="col-md-4"><label class="form-label">Tanggal Lahir</label><input type="date" name="tanggal_lahir" class="form-control" value="<?= htmlentities($p->tanggal_lahir) ?>" required></div>
														<div class="col-md-4"><label class="form-label">Jenis Kelamin</label>
															<select name="jenis_kelamin" class="form-select" required>
																<option value="L" <?= $p->jenis_kelamin == 'L' ? 'selected' : ''; ?>>Laki-laki</option>
																<option value="P" <?= $p->jenis_kelamin == 'P' ? 'selected' : ''; ?>>Perempuan</option>
															</select>
														</div>

														<div class="col-md-8"><label class="form-label">Alamat</label><input name="alamat" class="form-control" value="<?= htmlentities($p->alamat) ?>" required></div>
														<div class="col-md-4"><label class="form-label">No. Telp</label><input name="no_telp" class="form-control" value="<?= htmlentities($p->no_telp) ?>" required></div>

														<div class="col-md-6"><label class="form-label">Email</label><input type="email" name="email" class="form-control" value="<?= htmlentities($p->email) ?>" required></div>
														<div class="col-md-3"><label class="form-label">Unit</label><input name="id_unit" class="form-control" value="<?= htmlentities($p->id_unit) ?>" placeholder="(boleh kosong)"></div>
														<div class="col-md-3"><label class="form-label">Poli</label><input name="id_poli" class="form-control" value="<?= htmlentities($p->id_poli) ?>" placeholder="(boleh kosong)"></div>


														<hr class="mt-3">

														<!-- Kelola Akun -->
														<div class="col-12"><strong>Pengaturan Akun Login</strong></div>
														<input type="hidden" name="current_user_id" value="<?= htmlentities($p->user_id) ?>">
														<div class="col-md-4">
															<label class="form-label">Mode</label>
															<select name="mode_user" class="form-select" required>
																<option value="keep" selected>Biarkan seperti sekarang</option>
																<option value="bind">Ganti ke user existing</option>
																<option value="create">Buat user baru</option>
															</select>
														</div>

														<div class="col-md-4">
															<label class="form-label">User Existing</label>
															<select name="id_user" class="form-select">
																<option value="">-- pilih --</option>
																<?php foreach ($users as $u): ?>
																	<option value="<?= $u->user_id ?>"><?= $u->username ?></option>
																<?php endforeach; ?>
															</select>
														</div>

														<div class="col-md-4">
															<label class="form-label">Hak Akses (untuk buat user baru)</label>
															<select name="id_hak_akses" class="form-select">
																<option value="">-- pilih --</option>
																<?php foreach ($roles as $r): ?>
																	<option value="<?= $r->hak_akses_id ?>"><?= $r->nama_hak_akses ?></option>
																<?php endforeach; ?>
															</select>
														</div>

														<div class="col-md-4">
															<label class="form-label">Username (baru)</label>
															<input name="username" class="form-control" placeholder="username baru">
														</div>
														<div class="col-md-4">
															<label class="form-label">Password (baru)</label>
															<input type="password" name="password" class="form-control" placeholder="min 4 karakter">
														</div>
													</div>
												</div>
												<div class="modal-footer">
													<button class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
													<button class="btn btn-primary" type="submit">Simpan Perubahan</button>
												</div>
											</form>
										</div>
									</div>
								</div>
							<?php endforeach; ?>
						</tbody>
					</table>
				</div>
			</div>
		</div>
	</div>

	<!-- MODAL TAMBAH -->
	<div class="modal fade" id="modalTambah" tabindex="-1">
		<div class="modal-dialog modal-lg modal-dialog-scrollable">
			<div class="modal-content">
				<form method="post" action="<?= site_url('petugas/store') ?>">
					<div class="modal-header bg-primary text-white">
						<h5 class="modal-title">Tambah Petugas</h5>
						<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
					</div>
					<div class="modal-body">
						<div class="row g-2">
							<div class="col-md-3"><label class="form-label">NIP</label><input name="nip" class="form-control" required></div>
							<div class="col-md-5"><label class="form-label">Nama Lengkap</label><input name="nama_lengkap" class="form-control" required></div>
							<div class="col-md-4"><label class="form-label">Jabatan</label>
								<select name="id_jabatan" class="form-select" required>
									<option value="">-- pilih --</option>
									<?php foreach ($jabatan as $j): ?>
										<option value="<?= $j->kode_jabatan ?>"><?= $j->nama_jabatan ?></option>
									<?php endforeach; ?>
								</select>
							</div>

							<div class="col-md-4"><label class="form-label">Tempat Lahir</label><input name="tempat_lahir" class="form-control" required></div>
							<div class="col-md-4"><label class="form-label">Tanggal Lahir</label><input type="date" name="tanggal_lahir" class="form-control" required></div>
							<div class="col-md-4"><label class="form-label">Jenis Kelamin</label>
								<select name="jenis_kelamin" class="form-select" required>
									<option value="L">Laki-laki</option>
									<option value="P">Perempuan</option>
								</select>
							</div>

							<div class="col-md-8"><label class="form-label">Alamat</label><input name="alamat" class="form-control" required></div>
							<div class="col-md-4"><label class="form-label">No. Telp</label><input name="no_telp" class="form-control" required></div>

							<div class="col-md-6"><label class="form-label">Email</label><input type="email" name="email" class="form-control" required></div>
							<div class="col-md-3"><label class="form-label">Unit</label><input name="id_unit" class="form-control" value="<?= htmlentities($p->id_unit) ?>" placeholder="(boleh kosong)"></div>
							<div class="col-md-3"><label class="form-label">Poli</label><input name="id_poli" class="form-control" value="<?= htmlentities($p->id_poli) ?>" placeholder="(boleh kosong)"></div>


							<hr class="mt-3">

							<div class="col-md-4">
								<label class="form-label">Mode Akun</label>
								<select name="mode_user" class="form-select" required>
									<option value="new">Buat user baru</option>
									<option value="existing">Pakai user existing</option>
									<option value="none">Tanpa user login</option>
								</select>
							</div>

							<div class="col-md-4">
								<label class="form-label">User Existing</label>
								<select name="id_user" class="form-select">
									<option value="">-- pilih --</option>
									<?php foreach ($users as $u): ?>
										<option value="<?= $u->user_id ?>"><?= $u->username ?></option>
									<?php endforeach; ?>
								</select>
							</div>

							<div class="col-md-4">
								<label class="form-label">Hak Akses (untuk user baru)</label>
								<select name="id_hak_akses" class="form-select">
									<option value="">-- pilih --</option>
									<?php foreach ($roles as $r): ?>
										<option value="<?= $r->hak_akses_id ?>"><?= $r->nama_hak_akses ?></option>
									<?php endforeach; ?>
								</select>
							</div>

							<div class="col-md-4">
								<label class="form-label">Username (baru)</label>
								<input name="username" class="form-control" placeholder="wajib jika mode user = new">
							</div>
							<div class="col-md-4">
								<label class="form-label">Password (baru)</label>
								<input type="password" name="password" class="form-control" placeholder="disimpan MD5">
							</div>
						</div>
					</div>
					<div class="modal-footer">
						<button class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
						<button class="btn btn-primary" type="submit">Simpan</button>
					</div>
				</form>
			</div>
		</div>
	</div>

	<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>

</html>
