<?php
//include_once("config.php");
if (isset($_POST['type'])) {
	for ($i = 0; $i < count($_POST['type']); $i++) {	
		$user_id_form = mysql_real_escape_string(stripslashes($_POST['id'][$i])); 
		$emp_code_form = mysql_real_escape_string(stripslashes($_POST['emp_code_form'][$i])); 
		$username_form = mysql_real_escape_string(stripslashes($_POST['username_form'][$i])); 
		$first_name_form = mysql_real_escape_string(stripslashes(trim($_POST['first_name_form'][$i])));
		$last_name_form = mysql_real_escape_string(stripslashes(trim($_POST['last_name_form'][$i])));
		$orgunitid = mysql_real_escape_string(stripslashes(trim($_POST['orgunitid'][$i])));
		//$position_id = mysql_real_escape_string(stripslashes(trim($_POST['position_id'][$i])));
		$email_address = mysql_real_escape_string(stripslashes(trim($_POST['email_address'][$i])));
		$role_id_form = mysql_real_escape_string(stripslashes(trim($_POST['role_id_form'][$i])));
		$sys_type_ = mysql_real_escape_string(stripslashes(trim($_POST['sys_type_form'][$i])));
		$user_id = $_SESSION['sys_id'];
		/*--------------------------------------------------------------*/
		
		if ($_POST['username_form'][$i] != "" && $_POST['first_name_form'][$i] != "" && $_POST['role_id_form'][$i] != "" ) {	
			switch ($_POST['type'][$i]) {	
				case "added" :	
					if (mysql_num_rows(mysql_query("SELECT * FROM sys_user WHERE username = '$username_form'")) == 0) {
						sql_execute("INSERT INTO sys_user(emp_code,username,password,first_name,last_name,orgunitid, email_address, createby, role_id,sys_type)
									VALUES('$emp_code_form', '$username_form', md5('12345@ict'), '$first_name_form', '$last_name_form',
									'$orgunitid','$email_address', '$user_id', '$role_id_form','$sys_type_') ");
					}
				break;					
				case "edited":		
					sql_execute("UPDATE sys_user SET emp_code='$emp_code_form', username = '$username_form', first_name =
								'$first_name_form',last_name = '$last_name_form', orgunitid = '$orgunitid', email_address='$email_address',createby='$user_id', role_id = '$role_id_form', sys_type='$sys_type_'  
								WHERE sysuserid = '$user_id_form'");
				break;
			}
		}	
	}
}

$result = LoadAllUser($whereclause, $limitclause);


if(isset($_GET["del_id"]) &&(($_SESSION['role_id']) == 1)){

	$id = mysql_real_escape_string(stripslashes($_GET['del_id']));

	$sql = "DELETE FROM sys_user WHERE sysuserid ='$id'";
	$result = mysql_query($sql);	
	if($result){
		header("Location: ?d=webmaster/manage/user");
	}else{
		echo "<h2>ERROR Delete</h2>";
	}
}