<?php session_start();
	error_reporting(E_ERROR | E_WARNING | E_PARSE);
	include_once("../../functions/init.php"); 

	$tmp = $thisUrl."adm1n/functions/access.php";
	include ($tmp);

	$tmp = $thisUrl."adm1n/functions/koneksi.php";
	include_once ($tmp);

	$tmp = $thisUrl."functions/indonesian_date.php";
	include_once ($tmp);

	echo '
	<script type="text/javascript">
		function goBack() {
			window.location.href = "'.$thisPage.'adm1n/'.$curPage1.'"; 
		}
		function submitFormUser () {
			document.forms["formUser"].submit();
		}
	</script>';
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<?php
		$tmp = $thisUrl."adm1n/functions/head_section.php";
		include ($tmp);
		?>
    </head>	
	<body>
    	<div class="se-pre-con"></div>
		<center>
			<table width="1024">
				<tr valign="top">
					<td width="207" class="bg">
						<?php 
						$tmp = $thisUrl."adm1n/functions/kiri.php";
						include ($tmp);
						?>
					</td>
					<td width="10"></td>
					<td width="807">
						<?php 
						include ($thisUrl."adm1n/functions/logo.php");
						include ($thisUrl."adm1n/functions/getUrl.php");
						include ($thisUrl."adm1n/functions/php_functions.php");
						include ($thisUrl."adm1n/functions/request_data.php");
						
						echo '<div>'.$txt_legend.'</div>';
						
						$pass	= (isset($_REQUEST["pass"])   ? $_REQUEST["pass"]   : null);
						$edit	= (isset($_REQUEST["edit"])   ? $_REQUEST["edit"]   : null);
						$delete	= (isset($_REQUEST["delete"]) ? $_REQUEST["delete"] : null);
						$users  = (isset($_POST["users"]) 	  ? $_POST["users"] 	: null);
						
						if ($pass || $edit || $delete) {
							if ($pass) 			$admin_id = $pass;
							else if ($edit) 	$admin_id = $edit;
							else if ($delete)  	$admin_id = $delete;
							else $admin_id = 0;
							
							$sql =
							" SELECT * FROM user ".
							" WHERE admin_id = ".$admin_id;
							if ($hasil = mysql_query($sql)) {
								while ($data = mysql_fetch_array($hasil)) {
									$admin_id 	= (isset($data["admin_id"]) ? $data["admin_id"] : null);
									$username 	= (isset($data["username"]) ? $data["username"] : null);
									$password 	= (isset($data["password"]) ? $data["password"] : null);
									$user_roles = (isset($data["user_roles"]) ? $data["user_roles"] : null);
								}
							}
						}
						
						if ($pass) {
							if ($users == "users") {
								//$old_password = (isset($_POST["old_password"]) ? $_POST["old_password"] : null);
								$new_password = (isset($_POST["new_password"]) ? $_POST["new_password"] : null);
								$sql =
								" UPDATE user ".
								" SET password = '".$new_password."' ".
								" WHERE admin_id = ".$pass;
								if (!mysql_query($sql)) {
									echo '<p>ERROR: Failed to Execute: '.$sql.'</p>';
								} else {
									echo '
									<p>
										<div style="text-align:center; padding:15px; background-color:#d7fae9">
											<strong><u>Congratulations !!!</u></strong> <br>
											Password for <strong>user '.$username.'</strong> has been changed.
										</div>
									</p>';
									include ("listed_users.php");
								}								
							} else {
								$idEvent = "changePasswordForm";
								include ($thisUrl."adm1n/functions/form_kelengkapan_data.php");
							}
						} else if ($edit) {
							if ($users == "users") {
								$username	= (isset($_POST["username"])   ? $_POST["username"]   : $username);
								$user_roles	= (isset($_POST["user_roles"]) ? $_POST["user_roles"] : $user_roles);	
								$menu		= (isset($_POST["menu"]) 	   ? $_POST["menu"] 	  : null);
								//var_dump($menu);
								
								$sql = 
								" UPDATE user ".
								" SET username = '".$username."' ";
								
								if ($user_roles)
								$sql .=
								" , user_roles = ".$user_roles;
								
								if ($admin_id)
								$sql .=
								" WHERE admin_id = ".$admin_id; 
								
								//echo "<br>sql: ".$sql;
								if (!mysql_query($sql)) {
									exit('ERROR: '.$sql);
								} else {
									$sql7 =
									" DELETE FROM user_akses ".
									" WHERE user = ".$admin_id;	
									if (!mysql_query($sql7)) {
										exit('ERROR: '.$sql7);
									} else {
										sort($menu); $cekArr = null;
										foreach ($menu as $arr) {
											if ($cekArr != $arr) {
												$sql8 =
												" SELECT menusIDParent FROM user_menu ".
												" WHERE menu_id = ".$arr.
												" AND menusActive <> 0 ";
												//echo "<br>sql8: ".$sql8;
												if ($hasil8 = mysql_query($sql8)) {
													while ($data8 = mysql_fetch_array($hasil8)) {
														if ($data8["menusIDParent"] != 0 && $cekArr != $data8["menusIDParent"]) {
															$menu[] = $data8["menusIDParent"];
															$cekArr = $data8["menusIDParent"];
															//echo "<br>cekAr: ".$cekArr." - ";
															//echo "menusIDParent: ".$data8["menusIDParent"];
														}
													}
												}
												
											}
										}
										$menu[] = 1; $menu[] = 4;
										sort($menu);
										foreach ($menu as $arr) {
											$sql8 =
											" INSERT INTO user_akses ".
											" (akses_id, user, menu) ".
											" VALUES (null, ".$admin_id.", ".$arr.") ";	
											if (!mysql_query($sql8)) {
												exit('ERROR: '.$sql8);
											}
										}
										$pesan = '
										<p>
											<div style="text-align:center; padding:15px; background-color:#d7fae9">
												<strong><u>Congratulations !!!</u></strong> <br>
												Data for <strong>user '.$username.'</strong> has been changed.
											</div>
										</p>';
										echo '
										<form name="formUserSubmit" id="formUserSubmit" action="'.$thisPage.'adm1n/'.$curPage1.'" method="post">
											<input type="hidden" name="pesan" value="'.$pesan.'" />
										</form>
										<script type="text/javascript">
											document.forms["formUserSubmit"].submit();
										</script>';
									}
								}
							} else {
								include ($thisUrl."adm1n/".$curPage1."form_user.php");
							}
						} else if ($add) {
							if ($users == "users") {
								
							} else {
								include ($thisUrl."adm1n/".$curPage1."form_user.php");
							}
						} else if ($delete) {
							if ($users == "users") {
							
							} else {
								$idEvent = "deleteUserForm";
								include ($thisUrl."adm1n/functions/form_kelengkapan_data.php");
							}
						} else {
							$pesan = (isset($_POST["pesan"]) ? $_POST["pesan"] : null);
							echo $pesan;
							include ("listed_users.php");
						}
						?>
					</td>
				</tr>
			</table>
		</center>
	</body>
</html>	