<?php 
if(isset($_POST['add_user'])) {
	$user_name = trim($_POST['user_name']);
	$user_password = trim($_POST['user_password']);
	if(strlen($user_name) == 0) {$error[] = 'Please Enter User Name';}
	if(strlen($user_password) == 0) {$error[] = 'Please Enter Password';}
	if(strlen($user_password) < 5) {$error[] = 'Password Should have minimum 5 chrs';}
	
	$chkUser = mysqli_num_rows(mysqli_query($con, "SELECT id FROM tbl_admin WHERE ad_username = '$user_name'"));
	if($chkUser>0) { $error[] = "Username Already Exists"; }
	if(empty($error)) {
		mysqli_query($con, "INSERT INTO tbl_admin (ad_username,ad_password,access)VALUES('$user_name',MD5('$user_password'),'')");
		?><script language="javascript">
window.location='index.php?page=adminManagement';
</script><?php
	}      
}
if(isset($_POST['delete_user'])) {
	$admin_id = trim($_POST['admin_id'][0]);
	mysqli_query($con, "DELETE FROM tbl_admin WHERE admin_id = $admin_id");
}

if(isset($_POST['access_control'])) {
	$admin_id = trim($_POST['admin_id'][0]);
	header('Location:index.php?page=access_control&userId='.$admin_id);
	exit();
}

if(isset($_POST['submit_pass'])) {
	$admin_id = trim($_POST['admin_id'][0]);
	$changePassword = trim($_POST['password'.$admin_id]);
	if(strlen($changePassword) == 0) {$error[] = 'Please Enter Password';}
	if(strlen($changePassword) < 5) {$error[] = 'Password Should have minimum 5 chrs';}
	if(empty($error)) {
		mysqli_query($con, "UPDATE tbl_admin SET ad_password = MD5('$changePassword') WHERE admin_id = $admin_id");
	}
}
$getAdmin = mysqli_query($con, "SELECT * FROM tbl_admin where admin_id >1 ORDER BY admin_id DESC");
?>