<?php
	if(!isset($_SESSION['login_user'])){
		header('location: ../../login.php');
	}

	if(isset($_SESSION['group']) AND $_SESSION['group'] <> 'TRUE')
	{
		?>
		  <div class="alert alert-danger alert-dismissible" id="succsess-alert">
	        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
	        <h4><i class="icon fa fa-ban"></i> Alert!</h4>
	        Dilarang mengakses file ini.
	      </div>
		<?php
	}

	$linkaksi = 'med.php?mod=group';

	if(isset($_GET['act']))
	{
		$act = $_GET['act'];
		$linkaksi .= '&act='.$act;
	}
	else
	{
		$act = '';
	}

	$aksi = 'mod/sistem/act_group.php';

	$c1 = "SELECT max(kode_group)as kode FROM tb_group";
	$d1 = mysqli_query($conn,$c1);
	$e1 = mysqli_fetch_array($d1);
	$kode=substr($e1['kode'],3,4);
	$tambah=$kode+1;
	if($tambah<10){
		$idi="GP000".$tambah;
	}elseif ($tambah<100) {
		$idi="GP00".$tambah;
	}elseif ($tambah<1000) {
		$idi="GP0".$tambah;
	} else{
		$idi="GP".$tambah;
	}

	switch ($act) {
		case 'form':
			if(!empty($_GET['id']))
			{
				$act = "$aksi?mod=group&act=edit";
				$query = mysqli_query($conn,"SELECT * FROM tb_group WHERE kode_group = '$_GET[id]'");
				$temukan = mysqli_num_rows($query);
				if($temukan > 0)
				{
					$c = mysqli_fetch_assoc($query);
				}
				else
				{
					header("location:med.php?mod=group");
				}
			}
			else
			{
				$act = "$aksi?mod=group&act=simpan";
			}
			?>
			<div class="page-breadcrumb d-none d-sm-flex align-items-center mb-3">
				<div class="breadcrumb-title pe-3">Sistem</div>
				<div class="ps-3">
					<nav aria-label="breadcrumb">
						<ol class="breadcrumb mb-0 p-0">
							<li class="breadcrumb-item"><a href="javascript:;"><i class="bx bx-home-alt"></i></a></li>
							<li class="breadcrumb-item active" aria-current="page">Form Group</li>
						</ol>
					</nav>
				</div>
			</div>
			<?php
			flash('example_message');
			?>
			<div class="row">
				<div class="col-lg-12">
					<div class="card">
						<div class="card-header bg-primary text-center">
							<h4 class="m-b-0 text-white">Form Group</h4>
						</div>
						<form class="form-horizontal" role="form" method='POST' action='<?php echo $act ?>' enctype="multipart/form-data">
							<div class="card-body">
								<div class="row mb-3">
									<label class="col-lg-2 col-form-label">Kode Group</label>
									<div class="col-lg-10">
										<input type="text" name="id" value="<?php echo isset($c['kode_group']) ? $c['kode_group'] : $idi ;?>" class="form-control" readonly required>
									</div>
								</div>
								<div class="row mb-3">
									<label class="col-lg-2 col-form-label">Nama</label>
									<div class="col-lg-10">
										<input type="text" name="nama" value="<?php echo isset($c['nama']) ? $c['nama'] : '';?>" class="form-control" required <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>>
									</div>
								</div>
								<div class="row mb-3">
									<label class="col-lg-2 col-form-label">Keterangan</label>
									<div class="col-lg-10">
										<textarea class="form-control" name="keterangan" rows="3" <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>><?php echo isset($c['keterangan']) ? $c['keterangan'] : '';?></textarea>
									</div>
								</div>
								<div class="row mb-3">
									<label class="col-lg-2 col-form-label">Status</label>
									<div class="col-lg-10">
										<select class="form-control" name="status" style="width: 100%" <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>>
											<option value="<?php echo isset($c['status']) ? $c['status'] : 'Pilih Status' ;?>"><?php echo isset($c['status']) ? $c['status'] : 'Pilih Status' ;?></option>
											<option value="Aktif">Aktif</option>
											<option value="Non Aktif">Non Aktif</option>
										</select>
									</div>
								</div>
							</div>
							<div class="card">
								<div class="card-header bg-info text-center">
									<h4 class="m-b-0 text-white">Pengaturan Menu & Modul</h4>
								</div>
								<div class="card-body">
									<?php
									if(isset($_GET['view']))
									{
										?>		
										<div id="accordion">						
											<?php
											$qmodul = "SELECT a.*, b.* 
														FROM tb_modul AS a 
														JOIN `tb_group_modul` as b ON a.`id` = b.`id_modul`
														WHERE b.status = 'Aktif' AND `kode_group` = '$_GET[id]'";
											$sqlmodul = mysqli_query($conn,$qmodul);	
											$i=1;
											while ($modul = mysqli_fetch_assoc($sqlmodul)) {
												$coll = ($modul['id_modul']=="1") ? "show" : "";
												?>
												<div class="card card-info mb-2">
													<div class="card-header">
														<h4 class="card-title w-100">
															<a class="d-block w-100" data-bs-toggle="collapse" data-bs-target="#collapse<?php echo $modul['id']?>">
																<?php echo $modul['nama']?>
															</a>
														</h4>
													</div>
													<div id="collapse<?php echo $modul['id']?>" class="collapse <?php echo $coll ?>" data-parent="#accordion">
														<div class="card-body">	                        
															<div class="table-responsive">
																<table class="table table-bordered table-sm">
																	<thead>
																		<tr>
																			<th class="text-center" width="5%"><input type="checkbox" name="id_modul[]" value="<?php echo $modul['id']?>" onchange="checkAll(this,<?php echo $modul['id']?>)" <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>> </th>
																			<th class="text-center">Sub Menu</th>
																			<th class="text-center" width="12%">Tambah</th>
																			<th class="text-center" width="12%">Ubah</th>
																			<th class="text-center" width="12%">View</th>
																			<th class="text-center" width="12%">Hapus</th>
																		</tr>
																	</thead>
																	<tbody>
																		<?php
																		$qmenu = "SELECT a.*, b.* 
																					FROM tb_menu as a 
																					JOIN `tb_group_menu` as b ON a.id = b.`id_menu`
																					WHERE id_modul = '$modul[id_modul]' AND `kode_group` = '$_GET[id]' ORDER BY a.`urutan` ASC";
																		$sqlmenu = mysqli_query($conn,$qmenu);	
																		while ($menu = mysqli_fetch_assoc($sqlmenu)) {
																		?>
																		<tr>
																			<td class="text-center">
																				<input name="id_menu[]" class="<?php echo $modul['id']?>" value="<?php echo $menu['id']?>" type="checkbox" <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>>
																			</td>
																			<td><?php echo $menu['menu'] ?></td>
																			<td>
																				<select class="form-control form-control-sm" name="tambah[]" <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>>
																					<option value="<?php echo isset($menu['insert']) ? $menu['insert'] : 'Pilih Status' ;?>"><?php echo isset($menu['insert']) ? $menu['insert'] : 'Pilih Status' ;?></option>
																					<option value="Aktif">Aktif</option>
																					<option value="Non Aktif">Non Aktif</option>
																				</select>
																			</td>
																			<td>
																				<select class="form-control form-control-sm" name="ubah[]" <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>>
																					<option value="<?php echo isset($menu['update']) ? $menu['update'] : 'Pilih Status' ;?>"><?php echo isset($menu['update']) ? $menu['update'] : 'Pilih Status' ;?></option>
																					<option value="Aktif">Aktif</option>
																					<option value="Non Aktif">Non Aktif</option>
																				</select>
																			</td>
																			<td>
																				<select class="form-control form-control-sm" name="view[]" <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>>
																					<option value="<?php echo isset($menu['view']) ? $menu['view'] : 'Pilih Status' ;?>"><?php echo isset($menu['view']) ? $menu['view'] : 'Pilih Status' ;?></option>
																					<option value="Aktif">Aktif</option>
																					<option value="Non Aktif">Non Aktif</option>
																				</select>
																			</td>
																			<td>
																				<select class="form-control form-control-sm" name="hapus[]" <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>>
																					<option value="<?php echo isset($menu['delete']) ? $menu['delete'] : 'Pilih Status' ;?>"><?php echo isset($menu['delete']) ? $menu['delete'] : 'Pilih Status' ;?></option>
																					<option value="Aktif">Aktif</option>
																					<option value="Non Aktif">Non Aktif</option>
																				</select>
																			</td>
																		</tr>
																		<?php
																		}
																		?>
																	</tbody>
																</table>
															</div>
														</div>
													</div>
												</div>                 
												<?php
												$i++;
											}
											?>
										</div>			
										<?php
									}elseif(!empty($_GET['id'])){
										?>
										<div id="accordion">						
											<?php
											$qmodul = "SELECT * FROM tb_modul WHERE status = 'Aktif'";
											$sqlmodul = mysqli_query($conn,$qmodul);	
											$i=1;
											while ($modul = mysqli_fetch_assoc($sqlmodul)) {
												$qp = "SELECT * FROM `tb_group_modul` WHERE id_modul = '$modul[id]' AND `kode_group` = '$_GET[id]'";
												$sqlp = mysqli_query($conn,$qp);	
												$jp = mysqli_num_rows($sqlp);
												$check1 = ($jp>0) ? "checked" : "";
												$coll = ($modul['id']=="1") ? "show" : "";
												?>
												<div class="card card-info mb-2">
													<div class="card-header">
														<h4 class="card-title w-100">
															<a class="d-block w-100" data-bs-toggle="collapse" data-bs-target="#collapse<?php echo $modul['id']?>">
																<?php echo $modul['nama']?>
															</a>
														</h4>
													</div>
													<div id="collapse<?php echo $modul['id']?>" class="collapse <?php echo $coll ?>" data-parent="#accordion">
														<div class="card-body">	                        
															<div class="table-responsive">
																<table class="table table-bordered table-sm">
																	<thead>
																		<tr>
																			<th class="text-center" width="5%"><input type="checkbox" name="id_modul[]" value="<?php echo $modul['id']?>" onchange="checkAll(this,<?php echo $modul['id']?>)" <?php echo isset($_GET['view']) ? 'disabled' : '' ;?> <?php echo $check1 ?>> </th>
																			<th class="text-center">Sub Menu</th>
																			<th class="text-center" width="12%">Tambah</th>
																			<th class="text-center" width="12%">Ubah</th>
																			<th class="text-center" width="12%">View</th>
																			<th class="text-center" width="12%">Hapus</th>
																		</tr>
																	</thead>
																	<tbody>
																		<?php
																		$qmenu = "SELECT * FROM tb_menu WHERE id_modul = '$modul[id]' AND aktif = 'Aktif'";
																		$sqlmenu = mysqli_query($conn,$qmenu);	
																		while ($menu = mysqli_fetch_assoc($sqlmenu)) {
																			$qm = "SELECT * FROM tb_group_menu WHERE id_menu = '$menu[id]' AND `kode_group` = '$_GET[id]'";
																			$sqlm = mysqli_query($conn,$qm);	
																			$mx = mysqli_fetch_assoc($sqlm);
																			$jx = mysqli_num_rows($sqlm);
																			if ($jx>0) {
																				$tambah=$mx['insert'];
																				$view=$mx['view'];
																				$delete=$mx['delete'];
																				$ubah=$mx['update'];
																				$check = (($tambah=="Aktif" OR $view=="Aktif" OR $delete=="Aktif" OR $ubah=="Aktif")) ? "checked" : "";
																			} else {
																				$tambah="";
																				$view="";
																				$delete="";
																				$ubah="";
																				$check="";
																			}
																		?>
																		<tr>
																			<td class="text-center">
																				<input class="<?php echo $modul['id']?>" value="<?php echo $menu['id']?>" type="checkbox" <?php echo isset($_GET['view']) ? 'disabled' : '' ;?> <?php echo $check ?>>
																			</td>
																			<input type="hidden" name="id_menux[]" value="<?php echo $menu['id']?>">
																			<td><?php echo $menu['menu'] ?></td>
																			<td>
																				<select class="form-control form-control-sm" name="tambahx[]" <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>>
																					<option value="<?php echo isset($tambah) ? $tambah : 'Pilih Status' ;?>"><?php echo isset($tambah) ? $tambah : 'Pilih Status' ;?></option>
																					<option value="Aktif">Aktif</option>
																					<option value="Non Aktif">Non Aktif</option>
																				</select>
																			</td>
																			<td>
																				<select class="form-control form-control-sm" name="ubahx[]" <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>>
																					<option value="<?php echo isset($ubah) ? $ubah : 'Pilih Status' ;?>"><?php echo isset($ubah) ? $ubah : 'Pilih Status' ;?></option>
																					<option value="Aktif">Aktif</option>
																					<option value="Non Aktif">Non Aktif</option>
																				</select>
																			</td>
																			<td>
																				<select class="form-control form-control-sm" name="viewx[]" <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>>
																					<option value="<?php echo isset($view) ? $view: 'Pilih Status' ;?>"><?php echo isset($view) ? $view: 'Pilih Status' ;?></option>
																					<option value="Aktif">Aktif</option>
																					<option value="Non Aktif">Non Aktif</option>
																				</select>
																			</td>
																			<td>
																				<select class="form-control form-control-sm" name="hapusx[]" <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>>
																					<option value="<?php echo isset($delete) ? $delete : 'Pilih Status' ;?>"><?php echo isset($delete) ? $delete : 'Pilih Status' ;?></option>
																					<option value="Aktif">Aktif</option>
																					<option value="Non Aktif">Non Aktif</option>
																				</select>
																			</td>
																		</tr>
																		<?php
																		}
																		?>
																	</tbody>
																</table>
															</div>
														</div>
													</div>
												</div>                 
												<?php
												$i++;
											}
											?>
										</div>
										<?php
									}else{
										?>
										<div id="accordion">						
											<?php
											$qmodul = "SELECT * FROM tb_modul WHERE status = 'Aktif'";
											$sqlmodul = mysqli_query($conn,$qmodul);	
											$i=1;
											while ($modul = mysqli_fetch_assoc($sqlmodul)) {
												$coll = ($modul['id']=="1") ? "show" : "";
												?>
												<div class="card card-info mb-2">
													<div class="card-header">
														<h4 class="card-title w-100">
															<a class="d-block w-100" data-bs-toggle="collapse" data-bs-target="#collapse<?php echo $modul['id']?>">
																<?php echo $modul['nama']?>
															</a>
														</h4>
													</div>
													<div id="collapse<?php echo $modul['id']?>" class="collapse <?php echo $coll ?>" data-parent="#accordion">
														<div class="card-body">
															<div class="table-responsive">
																<table class="table table-bordered table-sm">
																	<thead>
																		<tr>
																			<th class="text-center" width="5%"><input type="checkbox" id="<?php echo $modul['id']?>" value="<?php echo $modul['id']?>" name="id_modul[]" onchange="checkAll(this,<?php echo $modul['id']?>)" <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>> </th>
																			<th class="text-center">Sub Menu</th>
																			<th class="text-center" width="12%">Tambah</th>
																			<th class="text-center" width="12%">Ubah</th>
																			<th class="text-center" width="12%">View</th>
																			<th class="text-center" width="12%">Hapus</th>
																		</tr>
																	</thead>
																	<tbody>
																		<?php
																		$qmenu = "SELECT * FROM tb_menu WHERE id_modul = '$modul[id]' AND aktif = 'Aktif'";
																		$sqlmenu = mysqli_query($conn,$qmenu);	
																		while ($menu = mysqli_fetch_assoc($sqlmenu)) {
																		?>
																		<tr>
																			<td class="text-center">
																				<input name="id_menu[]" class="<?php echo $modul['id']?>" value="<?php echo $menu['id']?>" type="checkbox" <?php echo isset($_GET['view']) ? 'disabled' : '' ;?> onchange="check(this,<?php echo $modul['id']?>)">
																			</td>
																			<td><?php echo $menu['menu'] ?></td>
																			<td>
																				<select class="form-control form-control-sm" name="tambah[]" <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>>
																					<option value="<?php echo isset($menu['aktif']) ? $menu['aktif'] : 'Pilih Status' ;?>"><?php echo isset($menu['aktif']) ? $menu['aktif'] : 'Pilih Status' ;?></option>
																					<option value="Aktif">Aktif</option>
																					<option value="Non Aktif">Non Aktif</option>
																				</select>
																			</td>
																			<td>
																				<select class="form-control form-control-sm" name="ubah[]" <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>>
																					<option value="<?php echo isset($menu['aktif']) ? $menu['aktif'] : 'Pilih Status' ;?>"><?php echo isset($menu['aktif']) ? $menu['aktif'] : 'Pilih Status' ;?></option>
																					<option value="Aktif">Aktif</option>
																					<option value="Non Aktif">Non Aktif</option>
																				</select>
																			</td>
																			<td>
																				<select class="form-control form-control-sm" name="view[]" <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>>
																					<option value="<?php echo isset($menu['aktif']) ? $menu['aktif'] : 'Pilih Status' ;?>"><?php echo isset($menu['aktif']) ? $menu['aktif'] : 'Pilih Status' ;?></option>
																					<option value="Aktif">Aktif</option>
																					<option value="Non Aktif">Non Aktif</option>
																				</select>
																			</td>
																			<td>
																				<select class="form-control form-control-sm" name="hapus[]" <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>>
																					<option value="<?php echo isset($menu['aktif']) ? $menu['aktif'] : 'Pilih Status' ;?>"><?php echo isset($menu['aktif']) ? $menu['aktif'] : 'Pilih Status' ;?></option>
																					<option value="Aktif">Aktif</option>
																					<option value="Non Aktif">Non Aktif</option>
																				</select>
																			</td>
																		</tr>
																		<?php
																		}
																		?>
																	</tbody>
																</table>
															</div>
														</div>
													</div>
												</div>                 
													
												<?php
													$i++;
												}
												?>
											</div>
										<?php	
										}
										?>
										<script type="text/javascript">
											function checkAll(box,group) 
											{
												let checkboxes = document.getElementsByClassName(group);
												if (box.checked) {
													for (let i = 0; i < checkboxes.length; i++) {
														if (checkboxes[i].type == 'checkbox') {
															checkboxes[i].checked = true;
														}
													}
												} else {
													for (let i = 0; i < checkboxes.length; i++) {
														if (checkboxes[i].type == 'checkbox') {
															checkboxes[i].checked = false;
														}
													}
												}
											}
											function check(box,group) 
											{				                                      	
												let checkboxes = document.getElementById(group);
												if (box.checked) {
													checkboxes.checked = true;
												}else{
													checkboxes.checked = false;
												}
											}
										</script>
									</div>
								</div>
							</div>
							<hr>
							<div class="d-flex align-items-center justify-content-between">
								<button type="submit" name="submit" value="simpan" class="btn btn-success"><i class='fa fa-save'></i> Simpan</button>
								<button type="button" name="submit" onclick="history.back()" class="btn btn-dark"><i class='fa fa-rotate-left'></i> Kembali</button>
							</div>
						</form>
					</div>
				</div>
			</div>
			<?php
		break;

		default :
		$qmn = "SELECT * FROM tb_group_menu WHERE `kode_group` = '$level_check' AND id_menu='$_GET[idmx]'";
		$sqqmn = mysqli_query($conn,$qmn);	
		$mn = mysqli_fetch_assoc($sqqmn);
		?>
		<div class="page-breadcrumb d-none d-sm-flex align-items-center mb-3">
			<div class="breadcrumb-title pe-3">Sistem</div>
			<div class="ps-3">
				<nav aria-label="breadcrumb">
					<ol class="breadcrumb mb-0 p-0">
						<li class="breadcrumb-item"><a href="javascript:;"><i class="bx bx-home-alt"></i></a></li>
						<li class="breadcrumb-item active" aria-current="page">Data Group</li>
					</ol>
				</nav>
			</div>
			<div class="ms-auto">
				<?php
					if ($mn['insert']=="Aktif") {
					?>	
						<a href="med.php?mod=group&act=form&idmx=<?php echo $mn['id_menu'] ?>">
							<button class="btn btn-primary px-4"><i class="bi bi-plus-lg me-2"></i>Add Group</button>
						</a>
				<?php }?>
			</div>
		</div>
		<?php
		flash('example_message');
		?>
		<div class="card mt-4">
			<div class="card-body">
				<div class="table-responsive">
					<table id="example" class="table table-striped table-hover align-middle">
						<thead>
							<tr>
								<th class="text-center">No</th>
								<th class="text-center">Kode Group</th>
								<th class="text-center">Nama</th>
								<th class="text-center">Keterangan</th>
								<th class="text-center">Status</th>
								<?php if($mn['update']=="Aktif" OR $mn['view']=="Aktif" OR $mn['delete']=="Aktif"){?>
									<th class="text-center">Aksi</th>
								<?php }?>
							</tr>
						</thead>
						<tbody>
							<?php
								$query = "SELECT * FROM tb_group";
								$sql_kul = mysqli_query($conn,$query);	
								$i=1;
								while ($m = mysqli_fetch_assoc($sql_kul)) {
							?>
							<tr>
								<td align="center"><?php echo $i ?></td>
								<td align="center"><?php echo $m['kode_group'] ?></td>
								<td><?php echo $m['nama'] ?></td>
								<td><?php echo $m['keterangan'] ?></td>
								<td align="center">
									<?php 
										if ($m['status']=='Aktif') {
										?>
											<span class="lable-table bg-success-subtle text-success rounded border border-success-subtle font-text2 fw-bold">Aktif<i class="bi bi-check2 ms-2"></i></span>
										<?php
										}else{
										?>
											<span class="lable-table bg-danger-subtle text-danger rounded border border-danger-subtle font-text2 fw-bold">Non Aktif<i class="bi bi-x-lg ms-2"></i></span>
										<?php
										}
									?>
								</td>
								<?php if($mn['update']=="Aktif" OR $mn['view']=="Aktif" OR $mn['delete']=="Aktif"){?>
								<td align="center">
									<div class="btn-group" role="group" aria-label="Aksi">
										<?php if ($mn['view'] == "Aktif") { ?>					
											<a href="<?php echo $linkaksi ?>&act=form&view=1&id=<?php echo $m['kode_group'] ?>" class="btn btn-outline-info btn-sm" title="View Data">
												<i class="material-icons-outlined">visibility</i>
											</a>
										<?php } ?>
										<?php if ($mn['update'] == "Aktif") { ?>					
											<a href="<?php echo $linkaksi ?>&act=form&id=<?php echo $m['kode_group'] ?>" class="btn btn-outline-success btn-sm" title="Edit Data">
												<i class="material-icons-outlined">edit</i>
											</a>
										<?php } ?>
										<?php if ($mn['delete'] == "Aktif") { ?>		
											<button type="button" class="btn btn-outline-danger btn-sm btn-hapus" data-url="<?php echo $aksi; ?>?mod=group&act=hapus&id=<?php echo $m['kode_group'] ?>" title="Hapus Data">
												<i class="material-icons-outlined">close</i>
											</button>
										<?php } ?>
									</div>
								</td>
								<?php }?>
							</tr>
							<?php
								$i++;
							}
							?>
						</tbody>
					</table>
				</div>
			</div>
		</div>
		<?php
		break;
	}
?>
