<?php
	//Connects to your Database
	include "../db_connection/db_connection.php";
	include "../config/config.php";		
	                    
	$page_id	=	base64_decode(base64_decode(base64_decode($_GET['page_id']))); //THIS IS PAGE ID
	
	if($page_id==1) //this is for employee details add page	
	{ 
		$login_for		=	$_POST['login_for']; 
		
		if($login_for=='Employee')
		{	
			if(isset($_POST['emp_name']) && isset($_POST['designation']) && isset($_POST['email_id']) && isset($_POST['user_name']))
			{
				function genPwd($length) 
				{ 
					$password = ''; 
					$possible = '23456789BCDFGHJKMNPQRSTVWXYZ'; 
					$i = 0; 
					while ($i < $length) 
					{   
						$password .= substr($possible, mt_rand(0, strlen($possible)-1), 1); 
						$i++; 
					}   
					return $password; 
				}
				
				$empid=$_POST['emp_name'];
				$desig=$_POST['designation'];
				$u_name=$_POST['user_name'];
				$pass = genPwd(8);
				$emailid=$_POST['email_id'];
				
				$SLCT_log = "SELECT * FROM login_details WHERE emp_det_id='$empid'";
				//echo $SLCT_log;
				$QURY_log = mysqli_query($connect,$SLCT_log);
				if(mysqli_num_rows($QURY_log)>0)
				{
					echo "<script>alert('This User Alredy Exist');</script>";  
				}
				else
				{
					$SLCT_emp = "SELECT * FROM emp_det WHERE emp_det_id='$empid'";
					$QURY_emp = mysqli_query($connect,$SLCT_emp);
					$FTCH_emp = mysqli_fetch_object($QURY_emp);	
					$empname=$FTCH_emp->name;
					if($FTCH_emp->master_dept_id==9)
					{
						$emp_des = 'admin';
					}
					else
					{
						$emp_des = 'employee';
					}
						
					$query_search = "INSERT INTO admin(`user_name`, `designation`,`pass_word`,`email_id`)VALUES('$u_name','$emp_des','$pass','$emailid')";
					//echo $query_search;
					$query_exec = mysqli_query($connect,$query_search) or die(mysql_error());
						
					$SLCT1="SELECT MAX(admin_id) AS ad_id FROM admin";
					$QRY1=mysqli_query($connect,$SLCT1);
					$FTCH1=mysqli_fetch_object($QRY1);
					$ad_id=$FTCH1->ad_id;
						
					$query_search1 = "INSERT INTO login_details(`admin_id`, `emp_det_id`, `post_dt`)VALUES('$ad_id','$empid','$cur_time')";
					//echo $query_search1;
					$query_exec1 = mysqli_query($connect,$query_search1) or die(mysql_error());
										
					//$email_ida="sales@thahaayaa.com";
					//$subject ="Reg:Login Details"; 
												
					//include "../mail/login_det_mail.php";
					//echo $mail_body;		
					 //if(mail($cli_emailid, $subject,$mail_body, "From:" . $email_ida ));
					 
					 /*$tto=$cli_emailid;
				  	 $subject="Reg:Login Details";
				  
					 
					 //$subject = "MNC4U Registration Confirmation";	
					$headers = "MIME-Version: 1.0" . "\r\n";
					$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
				
					// More headers
					$headers .= 'From: <sales@thahaayaa.com>' . "\r\n";
				  //$message = "Thanks for your purchase, your products will send you sortly\n" ;
				  mail("$tto",$subject,$mail_body,$headers);*/
				}
			}
		}
		else if($login_for=='Client')
		{
			if(isset($_POST['cli_name']) && isset($_POST['cli_email_id']) && isset($_POST['cli_user_name']))
			{
				function genPwd($length) 
				{ 
					$password = ''; 
					$possible = '23456789BCDFGHJKMNPQRSTVWXYZ'; 
					$i = 0; 
					while ($i < $length) 
					{   
						$password .= substr($possible, mt_rand(0, strlen($possible)-1), 1); 
						$i++; 
					}   
					return $password; 
				}
				$cli_id=$_POST['cli_name'];
				//echo $cli_id;
				$u_name=$_POST['cli_user_name'];
				//$cli_desig=$_POST['cli_designation'];				
				$pass = genPwd(8);
				$cli_emailid=$_POST['cli_email_id'];
				
				$SLCT_log = "SELECT * FROM login_details WHERE party_det_id='$cli_id'";
				//echo $SLCT_log;
				$QURY_log = mysqli_query($connect,$SLCT_log);
				if(mysqli_num_rows($QURY_log)>0)
				{
					echo "<script>alert('This User Alredy Exist');</script>";  
				}
				else
				{
					$SLCT_cli = "SELECT * FROM party_det WHERE party_det_id='$cli_id'";
					$QURY_cli= mysqli_query($connect,$SLCT_cli);
					$FTCH_cli = mysqli_fetch_object($QURY_cli);	
					$cliname=$FTCH_cli->cmp_name;
					$mobile_no=$FTCH_cli->con_no_1;
					
					$SLCT_ACC_PARTY = "SELECT * FROM ac_party_det WHERE party_det_id='$cli_id'";			
					$QURY_ACC_PARTY = mysqli_query($connect,$SLCT_ACC_PARTY);		
					if(mysqli_num_rows($QURY_ACC_PARTY)>0)
					{
					}
					else
					{
						$INSRT_ac_party_det		= "INSERT INTO ac_party_det(`party_det_id`, `post_dt`) VALUES('$cli_id','$cur_time')";		
						$QURY_ac_party_det		= mysqli_query($connect,$INSRT_ac_party_det);
					}
						
					$query_search = "INSERT INTO admin(`user_name`, `designation`,`pass_word`,`email_id`)VALUES('$u_name','client','$pass','$cli_emailid')";
					//echo $query_search;
					$query_exec = mysqli_query($connect,$query_search) or die(mysql_error());
						
					$SLCT1="SELECT MAX(admin_id) AS ad_id FROM admin";
					$QRY1=mysqli_query($connect,$SLCT1);
					$FTCH1=mysqli_fetch_object($QRY1);
					$ad_id=$FTCH1->ad_id;
						
					$query_search1 = "INSERT INTO login_details(`admin_id`, `party_det_id`, `post_dt`)VALUES('$ad_id','$cli_id','$cur_time')";
					//echo $query_search1;
					$query_exec1 = mysqli_query($connect,$query_search1) or die(mysql_error());
					
					//$email_ida="sales@thahaayaa.com";
					//$subject ="Reg:Login Details"; 
					
					if($mobile_no!='')
					{
						$to = $mobile_no;
						//$sms = "Warm greating from thahaayaa CreaDons! We are happy to announce this we launch our product on online to reach you easily. For Login Details Check your Mail";
						//$sms_url = sprintf("http://alerts.kapsystem.com/api/web2sms.php?workingkey=%s&to=%s&sender=%s&message=%s", $workingkey, $to , $sender, urlencode($sms) );
						$sms = "Warm greetings from thahaayaa Crea-Dons! We are happy to announce this we launch our product on online to reach you easily. Your login details are as follow. Username - ".$u_name.", Password - ".$pass;
						$sms_url   	= 	sprintf($sms_domain, $workingkey, $to, $senderid, urlencode($sms));
						//echo $sms_url;
						openurl($sms_url);
						
					}
					
					include "../mail/login_det_mail.php";
					//echo $mail_body;		
					 //if(mail($cli_emailid, $subject,$mail_body, "From:" . $email_ida ));
					 
					$tto=$cli_emailid;
					$subject="Reg:Login Details";					
					$headers = "MIME-Version: 1.0" . "\r\n";
					$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
					
					// More headers
					$headers .= 'From: Thahaayaa Sales Team <sales@thahaayaa.com>' . "\r\n";							
					$headers .= 'Bcc:'.$cc_id. "\r\n";	
					if(mail("$tto",$subject,$mail_body,$headers)){
						//echo "aaa";
					}
					else{
						//echo "bbb";
					}
					//echo $mail_body;
					

				}
			}
		}
		
?>
		<script type="text/javascript">
            //alert("Data inserted successfully");
            //history.back();
            window.location='../login_det_list.php';
        </script>
		
	
<?php
	}
	else if($page_id==2) //this is for Client details act/inact page	
	{
		$login_id	= base64_decode(base64_decode(base64_decode($_GET['login_id'])));
		
		if($_GET['statu_s']=="active")
		{
			$UPDATE = "UPDATE login_details SET statu_s = 'inactive',update_dt = '$cur_time' WHERE login_details_id = '$login_id'";
			$QRY    = mysqli_query($connect,$UPDATE);
			
			$SLCT_stu = "SELECT * FROM login_details WHERE login_details_id = '$login_id'";
			$QURY_stu = mysqli_query($connect,$SLCT_stu);
			$FTCH_stu = mysqli_fetch_object($QURY_stu);	
			
			$UPDATE1 = "UPDATE admin SET statu_s = 'inactive' WHERE admin_id = '$FTCH_stu->admin_id'";
			$QRY1   = mysqli_query($connect,$UPDATE1); 
		}
		else if($_GET['statu_s']=="inactive")
		{
			$UPDATE = "UPDATE login_details SET statu_s = 'active',update_dt = '$cur_time' WHERE login_details_id = '$login_id'";
			$QRY    = mysqli_query($connect,$UPDATE); 
			
			$SLCT_stu = "SELECT * FROM login_details WHERE login_details_id = '$login_id'";
			$QURY_stu = mysqli_query($connect,$SLCT_stu);
			
			$FTCH_stu = mysqli_fetch_object($QURY_stu);	
			$UPDATE1 = "UPDATE admin SET statu_s = 'active' WHERE admin_id = '$FTCH_stu->admin_id'";
			$QRY1   = mysqli_query($connect,$UPDATE1); 
		}
		if(isset($_GET['log_det']))
		{
			header("location:../login_det_list.php?log_det=".$_GET['log_det']);
		}
		else
		{
			header("location:../login_det_list.php");
		}
 	}
	else if($page_id==3) //this is for Client details act/inact page	
	{
			$log_for		=	$_POST['log_for']; 
				
			$lp = 1;
			
			function genPwd($length) 
			{ 
				$password = ''; 
				$possible = '23456789BCDFGHJKMNPQRSTVWXYZ'; 
				$i = 0; 
				while ($i < $length) 
				{   
					$password .= substr($possible, mt_rand(0, strlen($possible)-1), 1); 
					$i++; 
				}   
				return $password; 
			}	
			
			foreach($_POST['category'] as $cat_id)
			{				
				
				$pass = genPwd(8);
				
				if($log_for=='Employee')
				{
					$SLCT = "SELECT * FROM login_details WHERE emp_det_id = '$cat_id'";
					$QURY = mysqli_query($connect,$SLCT);
					$FTCH = mysqli_fetch_object($QURY);						
				}
				else if($log_for=='Client')
				{
					$SLCT = "SELECT * FROM login_details WHERE party_det_id='$cat_id'";
					$QURY = mysqli_query($connect,$SLCT);
					$FTCH = mysqli_fetch_object($QURY);
				}
				
				$UPDATE1 = "UPDATE admin SET pass_word='$pass' WHERE admin_id = '$FTCH->admin_id' AND statu_s = 'active'";
				$QRY1   = mysqli_query($connect,$UPDATE1);
				$lp ++;
			}
					
			
?>
		<script type="text/javascript">
            //alert("Data inserted successfully");
            //history.back();
            window.location='../login_det_list.php';
        </script>
<?php
	}
	else if($page_id==4)
	{
		$login_id = base64_decode(base64_decode(base64_decode($_GET['login_id'])));
		
		$SLCT = "SELECT * FROM login_details,admin,party_det WHERE admin.admin_id = login_details.admin_id AND login_details.login_details_id='$login_id' AND party_det.party_det_id = login_details.party_det_id";	
		//echo $SLCT;	
		$QURY = mysqli_query($connect,$SLCT);
		$FTCH = mysqli_fetch_object($QURY);
		
		$login_for = "Client";
		
		$u_name = $FTCH->user_name;
		$pass	= $FTCH->pass_word;	
		$cliname = $FTCH->cmp_name;
				
		include "../mail/login_det_rest_mail.php";
		//echo $mail_body;		
		 //if(mail($cli_emailid, $subject,$mail_body, "From:" . $email_ida ));
		 if($FTCH->email_id_1!='')
		{
			$pa_mail_id = $FTCH->email_id_1;
		}
		else if($FTCH->email_id_2!='')
		{
			$pa_mail_id = $FTCH->email_id_2;
		}
		else if($FTCH->email_id_3!='')
		{
			$pa_mail_id = $FTCH->email_id_3;
		}
//		echo 
		$tto=$pa_mail_id;   //die();
		$subject="Reg:Login Details";
		
		$headers = "MIME-Version: 1.0" . "\r\n";
		$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
		
		// More headers
		$headers .= 'From: Thahaayaa Sales Team <sales@thahaayaa.com>' . "\r\n";				
		$headers .= 'Bcc:'.$cc_id. "\r\n";	
		mail("$tto",$subject,$mail_body,$headers);		
?>
		<script type="text/javascript">
            //alert("Data inserted successfully");
            //history.back();
            window.location='../login_det_list.php';
        </script>
<?php 
	} 
	else if($page_id==5)
	{
		$u_name 	= $_POST['cli_user_name'];
		$pass		= $_POST['cli_pass_word'];
		$mail_id	= $_POST['mail_id'];
				
		include "../mail/login_det_guest_mail.php";
		
		$tto=$mail_id;
		$subject="Reg:Login Details";
		
		$headers = "MIME-Version: 1.0" . "\r\n";
		$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
		
		// More headers
		$headers .= 'From: Thahaayaa Sales Team <sales@thahaayaa.com>' . "\r\n";				
		$headers .= 'Bcc:'.$cc_id. "\r\n";	
		mail("$tto",$subject,$mail_body,$headers);		
		
		//echo $tto."<br />".$subject."<br />".$mail_body."<br />".$headers;
?>
		<script type="text/javascript">
            //alert("Data inserted successfully");
            //history.back();
           window.location='../login_det_list.php';
        </script>
<?php } ?>


