<?php 

if(isset($_REQUEST['id']) && $_REQUEST['id']!=''){
	$editId = trim($_REQUEST['id']);
	$editRec = mysqli_fetch_array(mysqli_query($con, "SELECT * FROM tbl_admin WHERE admin_id = '".$editId."'"));
}


if(isset($_POST['add_user'])) {
	
	if(isset($_POST['re_password']) && strlen($_POST['re_password'])>0) {
		$re_password = trim($_POST['re_password']);
		if(isset($_POST['password']) && strlen($_POST['password'])>0) {
			if($re_password == $_POST['password']){
				//ok
			} else {
				$error = 'Password mis-match error!';
			}
		}
	} else {
		$error = 'Please re-enter Password';
	}
	
	if(isset($_POST['password']) && strlen($_POST['password'])>0) {
		$password = trim($_POST['password']);
		if(strlen($password)<5){
			$error = 'Password should contain a minimum of 5 chars.';
		}
	} else {
		$error = 'Please enter Password';
	}
	
	if(isset($_POST['email']) && strlen($_POST['email'])>0) {
		$email = trim($_POST['email']);
	} else {
		$error = 'Please enter Email Address';
	}
	
	if(isset($_POST['user_name']) && strlen($_POST['user_name'])>0) {
		$userName = trim($_POST['user_name']);
		$chkUser = mysqli_num_rows(mysqli_query($con, "SELECT admin_id FROM tbl_admin WHERE ad_username = '".$userName."'"));
		if($chkUser>0) { $error = "Username Already Exists"; }
	} else {
		$error = 'Please enter User Name';
	}	
	
	if(isset($_POST['type']) && strlen($_POST['type'])>0) {
		$type = trim($_POST['type']);
	} else {
		$type = '';
	}

	if(empty($error)) {
		$password = md5($password);
		mysqli_query($con, "INSERT INTO tbl_admin (ad_username, ad_password, email, type, date, status) VALUES ('".$userName."', '".$password."', '".$email."', '".$type."', NOW(), 'Active')");
		?><script language="javascript">
		window.location='index.php?page=manage_admin';
		</script><?php
	}      
}

if(isset($_POST['update_user'])) {
	
	$changePass = FALSE;
	
	if(isset($_POST['change_pass']) && $_POST['change_pass']=='yes'){
	
		if(isset($_POST['re_password']) && strlen($_POST['re_password'])>0) {
			$re_password = trim($_POST['re_password']);
			if(isset($_POST['password']) && strlen($_POST['password'])>0) {
				if($re_password == $_POST['password']){
					//ok
					$changePass = TRUE;
				} else {
					$error = 'Password mis-match error!';
				}
			}
		} else {
			$error = 'Please re-enter Password';
		}
		
		if(isset($_POST['password']) && strlen($_POST['password'])>0) {
			$password = trim($_POST['password']);
			if(strlen($password)<5){
				$error = 'Password should contain a minimum of 5 chars.';
			}
		} else {
			$error = 'Please enter Password';
		}
	} 
	
	if(isset($_POST['email']) && strlen($_POST['email'])>0) {
		$email = trim($_POST['email']);
	} else {
		$error = 'Please enter Email Address';
	}
	
	if(isset($_POST['user_name']) && strlen($_POST['user_name'])>0) {
		$userName = trim($_POST['user_name']);
		$chkUser = mysqli_num_rows(mysqli_query($con, "SELECT admin_id FROM tbl_admin WHERE ad_username = '".$userName."' AND admin_id != '".$editId."'"));
		if($chkUser>0) { $error = "Username Already Exists"; }
	} else {
		$error = 'Please enter User Name';
	}	
	
	if(isset($_POST['type']) && strlen($_POST['type'])>0) {
		$type = trim($_POST['type']);
	} else {
		$type = '';
	}

	if(empty($error)) {
		if($changePass){
			$password = md5($password);
			$update = mysqli_query($con, "UPDATE tbl_admin SET ad_username = '".$userName."', ad_password = '".$password."', email = '".$email."', type = '".$type."' WHERE admin_id = '".$editId."'");
		} else {
			$update = mysqli_query($con, "UPDATE tbl_admin SET ad_username = '".$userName."', email = '".$email."', type = '".$type."' WHERE admin_id = '".$editId."'");
		}	
		if($update){
			$success = 'Admin details has been updated';
		?><script language="javascript">
		window.location='index.php?page=manage_admin';
		</script><?php }
	}      
}



?>