<?php
	session_start();
	include"../../lib/conn.php";
	include"../../lib/all_function.php";

	if(!isset($_SESSION['login_user'])){
		header('location: ../../login.php'); // Mengarahkan ke Home Page
	}


	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
		die();
	}

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

	$linkaksi = 'med.php?mod=group&idmx=69';
	if ($mod == "group" AND $act == "edit") {
				
				$id= anti_inject($_POST['id']);
				$nama= anti_inject($_POST['nama']);
				$keterangan= anti_inject($_POST['keterangan']);
				$status= anti_inject($_POST['status']);
					$cek_group = mysqli_query($conn,"SELECT * FROM `tb_group` WHERE
												 `kode_group` =  '$id'") or die(mysqli_error());

					if (mysqli_num_rows($cek_group) > 0) {

						mysqli_query($conn,"UPDATE `tb_group` SET `kode_group`='$id',`nama`='$nama', `keterangan`='$keterangan',`status`='$status' WHERE `kode_group` =  '$id'") or die(mysqli_error());

					}else{
						mysqli_query($conn,"INSERT INTO `tb_group`(`kode_group`, `nama`, `keterangan`, `status`) VALUES ('$id','$nama','$keterangan','$status')") or die(mysqli_error());
					}
				
				$id_modul= $_POST['id_modul'];
				$tot = count($id_modul);
				//echo $tot;
				for ($i=0; $i < $tot; $i++) { 
					$cek_modul = mysqli_query($conn,"SELECT * FROM `tb_group_modul` WHERE
												 `kode_group` =  '$id' AND `id_modul` = '$id_modul[$i]'") or die(mysqli_error());

					if (mysqli_num_rows($cek_modul) > 0) {

						mysqli_query($conn,"UPDATE `tb_group_modul` SET `kode_group`='$id',`id_modul`='$id_modul[$i]',`status`='Aktif' WHERE `kode_group` = '$id' AND `id_modul` = '$id_modul[$i]'") or die(mysqli_error());	

					}else{
						mysqli_query($conn,"INSERT INTO `tb_group_modul`(`kode_group`, `id_modul`, `status`) VALUES ('$id','$id_modul[$i]','Aktif')") or die(mysqli_error());
					}

									
				}
				$id_menu= $_POST['id_menux'];
					$tmenu = count($id_menu);
					//echo count($_POST['viewx']);echo "<br>";
					//echo $tmenu;
					for ($x=0; $x < $tmenu; $x++) { 
						$idmnx = $id_menu[$x];
						$tambah= $_POST['tambahx'][$x];
						$ubah= $_POST['ubahx'][$x];
						$view= $_POST['viewx'][$x];
						$delete= $_POST['hapusx'][$x];
						//echo $view; echo "<br>";
						//echo "idx = "; echo $idmnx; echo "<br>";
						//echo "id = "; echo $id; echo "<br>";
						$cek_menu = mysqli_query($conn,"SELECT * FROM `tb_group_menu` WHERE `id_menu` = '$idmnx' AND `kode_group` = '$id'") or die(mysqli_error());
						//echo mysqli_num_rows($cek_menu); echo "<br>";
						if (mysqli_num_rows($cek_menu) > 0) {
							if ($tambah == "Aktif" OR $ubah == "Aktif" OR $view == "Aktif" OR $delete == "Aktif") {
							mysqli_query($conn,"UPDATE `tb_group_menu` SET `kode_group`='$id',`id_menu`='$idmnx',`insert`='$tambah',`update`='$ubah',`delete`='$delete',`view`='$view' WHERE `id_menu` = '$idmnx' AND `kode_group` = '$id'") or die(mysqli_error());
							}else{

								mysqli_query($conn,"DELETE FROM tb_group_menu WHERE kode_group = '$id'");
							}
						}else{
							if ($tambah == "Aktif" OR $ubah == "Aktif" OR $view == "Aktif" OR $delete == "Aktif") {

								mysqli_query($conn,"INSERT INTO `tb_group_menu`(`kode_group`, `id_menu`, `insert`, `update`, `delete`, `view`) VALUES ('$id','$idmnx','$tambah','$ubah','$delete','$view')") or die(mysqli_error());
							}
							
						}
						
					}				
			log_update($id,"tb_group","Mengubah Data Group",$_SESSION['login_user'] );
			flash('example_message', 'Berhasil menambah data group.' );

			header("location: ../../".$linkaksi);
		
	}
	elseif($mod == "group" AND $act == "simpan")
	{
				$id= anti_inject($_POST['id']);
				$nama= anti_inject($_POST['nama']);
				$keterangan= anti_inject($_POST['keterangan']);
				$status= anti_inject($_POST['status']);

				mysqli_query($conn,"INSERT INTO `tb_group`(`kode_group`, `nama`, `keterangan`, `status`) VALUES ('$id','$nama','$keterangan','$status')") or die(mysqli_error());

				$id_modul= $_POST['id_modul'];
				$tot = count($id_modul);
				for ($i=0; $i < $tot; $i++) { 
					mysqli_query($conn,"INSERT INTO `tb_group_modul`(`kode_group`, `id_modul`, `status`) VALUES ('$id','$id_modul[$i]','Aktif')") or die(mysqli_error());
					
				}
				$id_menu= $_POST['id_menu'];
					$tmenu = count($id_menu);
				for ($x=0; $x < $tmenu; $x++) { 
					$idmnx = $id_menu[$x];
					$tambah= $_POST['tambah'][$x];
					$ubah= $_POST['ubah'][$x];
					$view= $_POST['view'][$x];
					$delete= $_POST['hapus'][$x];
					if ($tambah == "Aktif" OR $ubah == "Aktif" OR $view == "Aktif" OR $delete == "Aktif") {
					mysqli_query($conn,"INSERT INTO `tb_group_menu`(`kode_group`, `id_menu`, `insert`, `update`, `delete`, `view`) VALUES ('$id','$idmnx','$tambah','$ubah','$view','$delete')") or die(mysqli_error());
					}
				}
								
			log_insert($id,"tb_group","Menambahakan Data Group",$_SESSION['login_user'] );
			flash('example_message', 'Berhasil menambah data group.' );

			header("location: ../../".$linkaksi);
			
		
	}
	elseif ($mod == "group" AND $act == "hapus") 
	{
		log_delete($_GET['id'],"tb_group","Menghapus Data Group",$_SESSION['login_user'] );
		mysqli_query($conn,"DELETE FROM tb_group WHERE kode_group = '$_GET[id]'");
		mysqli_query($conn,"DELETE FROM tb_group_menu WHERE kode_group = '$_GET[id]'");
		mysqli_query($conn,"DELETE FROM `tb_group_modul` WHERE kode_group = '$_GET[id]'");
		flash('example_message', 'Berhasil menghapus data group.' );
		header('Location: ' . $_SERVER["HTTP_REFERER"] );	
	}elseif ($mod == "group" AND $act == "upload") {
				$imgName = $_FILES['foto']['name'];
				$imgTmp = $_FILES['foto']['tmp_name'];
				$imgSize = $_FILES['foto']['size'];
				$imgExt = strtolower(pathinfo($imgName, PATHINFO_EXTENSION));

				$allowExt  = array('jpeg', 'jpg', 'png', 'gif');

				$userPic = time().'_'.rand(1000,9999).'.'.$imgExt;

				if(in_array($imgExt, $allowExt)){

					if($imgSize < 5000000){
						move_uploaded_file($imgTmp ,$upload_bukti.$userPic);
					}else{
						$errorMsg = 'Image too large';
						flash('example_message', $errorMsg );

						header('Location: ' . $_SERVER["HTTP_REFERER"] );
					}
				}
		log_insert($_POST['id'],"tb_pembayaran_buka_cabang","Upload Bukti Buka Cabang",$_SESSION['login_user'] );
		mysqli_query($conn,"INSERT INTO `tb_pembayaran_buka_cabang`(`id_group`, `file`, `keterangan`, `status`) VALUES ('$_POST[id]','$userPic','Belum Aktif Tunggu Approval Kami','Pending')") or die(mysqli_error());
			flash('example_message', 'Berhasil Upload Bukti Pembayaran .' );
			header('Location: ' . $_SERVER["HTTP_REFERER"] );
	}
?>