<?php
	include "../db_connection/db_connection.php";
	include "../config/config.php";


	##########################################--------> CREATING SESSION <--------###########################################
	
	if(isset($_GET['admin_login_id']))
	{
		$admin_login_id = base64_decode(base64_decode(base64_decode(base64_decode($_GET['admin_login_id']))));
		########--------> CREATING SESSION OF USERNAME
		$ip_ad=$_SERVER['REMOTE_ADDR'];
		//$ip_ad=$_SERVER["HTTP_X_REAL_IP"];
	
		$SLCT_admin = "SELECT * FROM admin WHERE admin_id='$admin_login_id' AND statu_s ='active'";
		$QURY_admin = mysqli_query($connect,$SLCT_admin);
		
		$UPDATE_c = "UPDATE admin SET user_sts='1' WHERE admin_id='$admin_login_id'";
		$QURY_c = mysqli_query($connect,$UPDATE_c);
				
					
		if(mysqli_num_rows($QURY_admin)>0)
		{
			$FTCH_admin = mysqli_fetch_object($QURY_admin);
	
			$_SESSION['admin_login_id'] = $FTCH_admin->admin_id;
			$_SESSION['admin_username'] = $FTCH_admin->user_name;
			$_SESSION['admin_designation'] = $FTCH_admin->designation;
			
									
			//echo $SLCT_admin;
			if($FTCH_admin->designation=='client')
			{					
				$INSRT_g = "INSERT INTO login_log(`admin_id`,`designation`, `ip_address`, `cur_sts`, `post_dt`) VALUES ('$admin_login_id','$FTCH_admin->designation','$ip_ad','1','$cur_time')";
				$QURY_g	= mysqli_query($connect,$INSRT_g);
				
				$_SESSION['lastid'] = 0;
				echo "<script>window.location='../client/design_sel.php';</script>";
							
			
			}
			else if($FTCH_admin->designation=='guest_user')
			{
			
				$DELT = "DELETE FROM des_sel_temp WHERE party_det_id='0'";
				//echo $DELT;
				$QRY    = mysqli_query($connect,$DELT); 
				
				$DELT1 = "DELETE FROM des_sel_temp_des WHERE party_det_id = '0' AND designation = 'guest_user'";
				//echo $DELT1;
				$QRY1    = mysqli_query($connect,$DELT1);					 
				
				function genPwd($length) 
				{ 
					$password = ''; 
					$possible = '23456789BCDFGHJKMNPQRSTVWXYZ'; 
					$i = 0; 
					while ($i < $length) 
					{   
						$password .= substr($possible, mt_rand(0, strlen($possible)-1), 1); 
						$i++; 
					}   
					return $password; 
				}
				$pass = genPwd(8);
				$user = genPwd(8);
				$en_date = date("d-m-Y",strtotime($cur_date));
				$UPDATE1 = "UPDATE admin SET user_name = '$user', pass_word='$pass' WHERE designation = 'guest_user' AND statu_s = 'active' AND admin_id = '$FTCH_admin->admin_id'";
				//echo $UPDATE1;
				//$QRY1   = mysqli_query($connect,$UPDATE1);
				$INSRT_g = "INSERT INTO  guest_user (`admin_id`,`ip_address`,`entry_date`,`post_dt`) VALUES ('$FTCH_admin->admin_id','$ip_ad','$en_date','$cur_time')";
				$QURY_g	= mysqli_query($connect,$INSRT_g);
				
				$slct_gust  = "SELECT MAX(guest_id) AS max_id FROM guest_user";
				$QURY_gust	= mysqli_query($connect,$slct_gust);
				$ftch_gust  = mysqli_fetch_object($QURY_gust);
				
				$INSRT_g1 = "INSERT INTO guest_login_log(`guest_user_id`,`admin_id`,`designation`, `ip_address`, `cur_sts`, `post_dt`) VALUES('$ftch_gust->max_id','$FTCH_admin->admin_id','$FTCH_admin->designation','$ip_ad','1','$cur_time')";
				$QURY_g1  = mysqli_query($connect,$INSRT_g1);
				
				$_SESSION['guest_login_id'] = $ftch_gust->max_id;
				$_SESSION['lastid'] = 0;
				echo "<script>window.location='../client/guest_user_add.php';</script>";
			}
			else if($FTCH_admin->designation=='employee')
			{		
				$_SESSION['start'] = time(); // Taking now logged in time.
				// Ending a session in 30 minutes from the starting time.
				$_SESSION['expire'] = $_SESSION['start'] + (8*60*60);	
				$SLCT_UPD = "UPDATE admin SET `login_sts`=1 WHERE admin_id = '$admin_login_id'";
				$QURY_UPD	= mysqli_query($connect,$SLCT_UPD);
						
				$INSRT_g = "INSERT INTO login_log(`admin_id`,`designation`, `ip_address`, `cur_sts`, `post_dt`) VALUES ('$admin_login_id','$FTCH_admin->designation','$ip_ad','1','$cur_time')";
				$QURY_g	= mysqli_query($connect,$INSRT_g);
				
				$_SESSION['lastid'] = 0;
				echo "<script>window.location='../employee/dashboard.php';</script>";
			}			
			else
			{
				$INSRT_g = "INSERT INTO login_log(`admin_id`,`designation`, `ip_address`, `cur_sts`, `post_dt`) VALUES ('$admin_login_id','$FTCH_admin->designation','$ip_ad','1','$cur_time')";
				$QURY_g	= mysqli_query($connect,$INSRT_g);

				// Get emp id 
				$login_sql = mysqli_query($connect,"SELECT * FROM `login_details` WHERE `admin_id`='".$adm_login_id."'");
				$login_rows= mysqli_num_rows($login_sql);
				$emp_det_id='';
				if($login_rows>0){
					$login_det = mysqli_fetch_array($login_sql,MYSQLI_ASSOC);
					$emp_det_id = $login_det['emp_det_id'];
				}
				if($emp_det_id!=''){
				$emp_det = mysqli_fetch_array(mysqli_query($connect,"SELECT * FROM `emp_det` WHERE `emp_det_id`='".$emp_det_id."'"),MYSQLI_ASSOC);

				$dept_id_new = $emp_det['master_dept_id'];
				}
				$re_page='';
				$red_sql = mysqli_fetch_array(mysqli_query($connect,"SELECT * FROM `menu_rights` WHERE dept_id='".$dept_id_new."' LIMIT 0,1"),MYSQLI_ASSOC);

				$re_page = $red_sql['redirects_to'];

				if($re_page!=''){
					$page_sql = mysqli_fetch_array(mysqli_query($connect,"SELECT * FROM `menu_pages` WHERE menu_page_id='".$re_page."'"),MYSQLI_ASSOC);
					$page_url = $page_sql['page_file'];
				}
				
				if($page_url==''){
					$page_url ='home.php';
				}

			 echo "<script>window.location='../".$page_url."';</script>";
			}
			/*echo "<script>window.location='../index.php?admin_panel=payout_update';</script>";*/
		}
		else
		{
			echo "<script>window.location='../index.php';</script>";
		}
	}


	############################################--------> DESTROY SESSION <--------############################################
	
	if(isset($_GET['daid']))
	{
		
	########--------> DESTROY LOGIN ID, USERNAME SESSION'S
//		echo $_GET[daid];
//		echo $_GET[dunm];

		$adm_id = $_GET['daid'];
		$ad_des = $_SESSION['admin_designation'];
		$INSRT_g = "UPDATE login_log SET `cur_sts`='0', update_dt='$cur_time' WHERE admin_id='$adm_id' AND designation='$ad_des' AND update_dt='0000-00-00 00:00:00'";
		$QURY_g	= mysqli_query($connect,$INSRT_g);

		unset($_SESSION['admin_login_id']);
		unset($_SESSION['admin_username']);
		unset($_SESSION['admin_designation']);		
		session_destroy();			
		echo "<script>window.location='../index.php';</script>";
	}	
?>