<?php
 	session_start(); 

	if(!isset($_SESSION['tulsilogin']))
	{
		header('Location: index.php');
		return;
	}



$_SESSION['_token']=bin2hex(openssl_random_pseudo_bytes(16));
	$usermid = $_SESSION['umid'];
date_default_timezone_set("Asia/Kolkata");
	$mdate1= date('Y-m-d H:i:s');
	
	include("connect.php");
	include("connectp.php");

		
	$stiqmid=$_POST['stiqmid'];
	$clgamid=$_POST['clgamid'];
	$cdsdid=$_POST['cdsdid'];
	$attechment="";
	
	
	foreach ($cdsdid as $one_by_one_sid) 
					{
					$cdsdocument2 = "";
	if(empty($_FILES['cdsdocument2'.$one_by_one_sid]['name']) == false  )
	{
		$cdsdocument2 = date("YmdHis").$_FILES['cdsdocument2'.$one_by_one_sid]['name'];
		$path = "docs/";
		move_uploaded_file( $_FILES['cdsdocument2'.$one_by_one_sid]['tmp_name'], $path ."/". $cdsdocument2 );
	
	}
					
					 $sql1="Insert into res_collegeappmanstep_detail (clgamid,cdsdid,clgdaddedon,clgaddedby,cdsdocument2) values (?,?,?,?,?)";
					$stmt1= $db7->prepare($sql1);
				$stmt1->execute([$clgamid,$one_by_one_sid,$mdate1,$usermid,$cdsdocument2]);
				$evepay = "select * from res_collegeapp_master,res_collegeappman_master where res_collegeappman_master.clgappmid= res_collegeapp_master.clgappmid and  res_collegeappman_master.clgamid=$clgamid";
															$repay = mysqli_query($conn, $evepay);
															$i=1;
															while($rtpay = mysqli_fetch_assoc($repay))
															{
																$camdid = $rtpay['camdid'];
																$clgappmid = $rtpay['clgappmid'];
															}
				
				$eve13 = "select count(*) as a2 from res_collegeappmanstep_detail where clgamid in (select clgamid from res_collegeappman_master where camdid=$camdid and clgamid=$clgamid and clgappmid in (select clgappmid from res_collegeapp_master where clgappmid=$clgappmid)) ";
								$re13 = mysqli_query($conn, $eve13);
								$totalcsteps=0;
								while($rt13 = mysqli_fetch_assoc($re13))
								{
									  $totalcsteps=$rt13['a2'];
								}
								
								if($totalcsteps < $totalsteps)
								{
									
								}else{
									$eve12 = "select * from res_clgdefapp_master where clgamid=$clgamid";
				$re = mysqli_query($conn, $eve12);
	 while($rtpay = mysqli_fetch_assoc($re))
										
	{
		$cdamid=$rtpay['cdamid'];
															$imid=$rtpay['imid'];
															$coursefees=$rtpay['applicationfess'];
								
								$stiqmid=$rtpay['stiqmid'];
								
								$coursemid=$rtpay['coursemid'];
								$clgamid=$rtpay['clgamid'];
								$capstatus=$rtpay['cdapstatus'];
								
								$capstatus=$capstatus;			
								$collegemid=$rtpay['collegemid'];
								$cintekmonth=$rtpay['cdintakemonth'];
								$cintakeyear=$rtpay['cdintakeyear'];
								$countrymid=$rtpay['countrymid'];
								
								$coremarks=$rtpay['cdfremarks'];
								
								$doapplication=$rtpay['cdoapplication'];
	}
								 $eve1 = "INSERT INTO res_collegeapp_master (imid,stiqmid, coursemid,collegemid,countrymid,coremarks,coursefees,capstatus,doapplication,capaddedon,capaddedby,capupdatedon,capupdatedby,cintakemonth,cintakeyear,clgamid) VALUES('$imid','$stiqmid','$coursemid','$collegemid','$countrymid','$coremarks','$coursefees','$capstatus','$doapplication','$mdate1','$usermid','$mdate1','$usermid','$cintekmonth','$cintakeyear','$clgamid')";
								 if ($conn->query($eve1) === TRUE)
			{
				
				$_SESSION['msg'] = "Record has been added successfully";
				
				
				$eve3 = "select * from res_emailacc_master LIMIT 0,1";
				$re3 = mysqli_query($conn, $eve3);
				
				if(mysqli_num_rows($re3) > 0)
				{
					while($rt3 = mysqli_fetch_assoc($re3))
					{
						$providername=$rt3['providername'];
						$email=$rt3['email'];
						$epassword=$rt3['epassword'];
						$esmtp=$rt3['esmtp'];
						$eport=$rt3['eport'];
						$essltls=$rt3['essltls'];
						
					}
				}
				
				
				/* Email  */
				 $eveag = "select ipemail,ipname,ipsurname,ipcouncellor from res_inquiry_master where imid=$imid";
				$reag = mysqli_query($conn, $eveag);
				if(mysqli_num_rows($reag) > 0)
				{
					while($rtag = mysqli_fetch_assoc($reag))
					{
						$ipemail=$rtag['ipemail'];
						$ipname=$rtag['ipname'];
						$ipsurname=$rtag['ipsurname'];
						$counsellorid=$rtag['ipcouncellor'];
						
					}
				}
				else
				{
					$ipemail="";
					$ipname="";
					$ipsurname="";
					$counsellorid=0;
					
				}
				
				$eveag = "select ufullname from res_user_master where umid=$counsellorid";
				$reag = mysqli_query($conn, $eveag);
				if(mysqli_num_rows($reag) > 0)
				{
					while($rtag = mysqli_fetch_assoc($reag))
					{
						$ufullname=$rtag['ufullname'];
						
					}
				}
				else
				{
					$ufullname="";
					
				}
				
				$uu = "$email";
				$pp = "$epassword";

				ob_start();
				require_once('class.phpmailer.php');
				
			$eve33 = "select * from res_communication_master where csubject='Confirmation' and cdepartment='Application' and ctemplatetype='Email'  ";
				$re33 = mysqli_query($conn, $eve33);
				
				if(mysqli_num_rows($re33) > 0)
				{
					while($rt33 = mysqli_fetch_assoc($re33))
					{
						$cemailsubject = $rt33['cemailsubject'];
						$cmessage = $rt33['cmessage'];
					}
				}
				
				$eve2 = "select cmname from res_college_master where collegemid=$collegemid";
	$re2 = mysqli_query($conn, $eve2);
	while($rt2 = mysqli_fetch_assoc($re2))

	{
		$cmname = $rt2['cmname'];
		
	}
	
	$cintekmonth = date('F',strtotime($cintekmonth));
	$intake = $cintekmonth.','.$cintakeyear;
	
				
				$message="Dear $par1 <br><br>Your Mobile Number (Username) is: $par2 <br>Your OTP  is: $mobileotp <br><br>Regards,<br> $providername";
$cmessage=str_replace("#leadfname","$ipname",$cmessage);
$cmessage=str_replace("#leadlname","$ipsurname",$cmessage);
$cmessage=str_replace("#collegename","$cmname",$cmessage);
$cmessage=str_replace("#intake ","$intake",$cmessage);
$cemailsubject=str_replace("#intake ","$intake",$cemailsubject);
$cmessage=str_replace("#contactname ","$ufullname",$cmessage);


$cemailsubject = $cemailsubject."-".$mdate2;

				$mail = new PHPMailer();
				$mail->IsSMTP();
				$mail->CharSet="UTF-8";
				$mail->SMTPSecure = "$essltls";
				$mail->Host = "$esmtp";
				$mail->Port = "$eport";
				$mail->Username = $uu;
				$mail->Password = $pp;
				$mail->SMTPAuth = true;
				$mail->SMTPDebug = 1;
				$mail->From = $uu;
				$mail->FromName = "$providername";
			if($ipemail=='')
					{					
					$ipemail='at.zeelsheth@gmail.com';
					}
				if($ipemail!='')
					{
					$mail->AddAddress("$ipemail");
				
					}
					
				
					
				
				
				//$mail->addBCC("$alertemailid");
				$mail->IsHTML(true);
				$mail->Subject    = $cemailsubject;
				$mail->AltBody    = ".";
				$mail->Body    = $cmessage;


				if(!$mail->Send())
				{
					$err=$mail->ErrorInfo;
					echo $err;
					//return;
				}
				else
				{
					//header("location:contact-us.php");
				}
				
				
				
			}
			else
			{
				$_SESSION['msg1'] = "Error";
			}
			
			
	
		
								
	}
									
								
								}
				
					
					 
					$_SESSION['msg'] = "Record saved successfully";
	
			
				header("Location: applicationinquirydetail.php?id=$stiqmid");
			
?>