<?php
include("dbset.php");

$con = mysqli_connect("localhost",$dbu,$dbp);
if (!$con)
  {
  die('Could not connect to database: ' . mysqli_error());
  }
   
mysqli_select_db($con, $dbs);

date_default_timezone_set('Africa/Gaborone');
$entry_date = date("Y-m-d H:i:s", time()); 

$userid = $_GET['del_userid'];
$cx =$_GET['cx'];

$table_name = "system_users";
$dep_query = "SELECT access_id, username,  tlh_gab, tlh_ftw, rad_gab, rad_nam, vertex, pecus, stay FROM $table_name WHERE userid = '".$userid."' ";
$dep_result = $con->query($dep_query);
$row = mysqli_fetch_assoc($dep_result);
$del_user_name = $row["username"];
$access_id = $row["access_id"];
$tlh_gab = $row["tlh_gab"];
$tlh_ftw = $row["tlh_ftw"];
$rad_gab = $row["rad_gab"];
$rad_nam = $row["rad_nam"];
$vertex = $row["vertex"];
$pecus = $row["pecus"];
$stay = $row["stay"];

$table_name = "system_users";
$access_query = "SELECT username FROM $table_name WHERE access_id = '".$cx."' ";
$access_result = $con->query($access_query);
$row = mysqli_fetch_assoc($access_result);
$login_name = $row["username"];


if ($stay==0) {

$table_name = "system_users";
$del_user_query  = "DELETE FROM $table_name WHERE userid = '".$userid."' ";
$del_user_result = $con->query($del_user_query);

if ($tlh_gab==1) {
	   	$table_name = "tlh_users";
		$tlh_gab_del_user_query  = "DELETE FROM $table_name WHERE userid = '".$userid."' ";
		$tlh_gab_del_user_result = $con->query($tlh_gab_del_user_query);
		
		$table_name = "tlh_permissions";
		$tlh_del_user_permissions_query  = "DELETE FROM $table_name WHERE access_id = '".$access_id."' ";
		$tlh_del_user_permissions_result = $con->query($tlh_del_user_permissions_query);
}

if ($tlh_ftw==1) {
	   	$table_name = "ftw_users";
		$tlh_ftw_del_user_query  = "DELETE FROM $table_name WHERE userid = '".$userid."' ";
		$tlh_ftw_del_user_result = $con->query($tlh_ftw_del_user_query);
		
		$table_name = "ftw_permissions";
		$tlh_del_user_permissions_query  = "DELETE FROM $table_name WHERE access_id = '".$access_id."' ";
		$tlh_del_user_permissions_result = $con->query($tlh_del_user_permissions_query);
}

if ($rad_gab==1) {
	   	$table_name = "rds_users";
		$rad_gab_del_user_query  = "DELETE FROM $table_name WHERE userid = '".$userid."' ";
		$rad_gab_del_user_result = $con->query($rad_gab_del_user_query);
		
		$table_name = "rds_permissions";
		$rad_del_user_permissions_query  = "DELETE FROM $table_name WHERE access_id = '".$access_id."' ";
		$rad_del_user_permissions_result = $con->query($rad_del_user_permissions_query);		
}

if ($rad_nam==1) {
	   	$table_name = "nmb_users";
		$rad_nam_del_user_query  = "DELETE FROM $table_name WHERE userid = '".$userid."' ";
		$rad_nam_del_user_result = $con->query($rad_nam_del_user_query);
		
		$table_name = "nmb_permissions";
		$nam_del_user_permissions_query  = "DELETE FROM $table_name WHERE access_id = '".$access_id."' ";
		$nam_del_user_permissions_result = $con->query($nam_del_user_permissions_query);
}

if ($vertex==1) {
	   	$table_name = "ver_users";
		$vertex_del_user_query  = "DELETE FROM $table_name WHERE userid = '".$userid."' ";
		$vertex_del_user_result = $con->query($vertex_del_user_query);
		
		$table_name = "ver_permissions";
		$vertex_del_user_permissions_query  = "DELETE FROM $table_name WHERE access_id = '".$access_id."' ";
		$vertex_del_user_permissions_result = $con->query($vertex_del_user_permissions_query);
}

if ($pecus==1) {
	   	$table_name = "pec_users";
		$pecus_del_user_query  = "DELETE FROM $table_name WHERE userid = '".$userid."' ";
		$pecus_del_user_result = $con->query($pecus_del_user_query);
		
		$table_name = "pec_permissions";
		$pecus_del_user_permissions_query  = "DELETE FROM $table_name WHERE access_id = '".$access_id."' ";
		$pecus_del_user_permissions_result = $con->query($pecus_del_user_permissions_query);
}



$table_name = "system_admin_tracking";
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$login_name."', '15', '".$del_user_name."','','', '' )";
$tracking_result = $con->query($tracking_query);

}


?>
<script language="javascript" type="text/javascript">
      window.location="admin_settings.php?s=1&cx=<?php echo $cx ?>";
</script>

															  