<?php
 	session_start(); 

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

	include("connect.php");
date_default_timezone_set("Asia/Kolkata");
$mdate1= date('Y-m-d H:i:s');
$mdate= date('d-m-Y');
$mdate2= date('d-m-Y H:i');
	$usermid = $_SESSION['umid'];
	
	//$cmname=$_POST['cmname'];
	if(isset($_POST['imid1']))
	{
	$imid1=$_POST['imid1'];
	}
	
	$stiqmid=$_POST['stiqmid'];
	$eve2 = "select imid from res_studentinquiry_detail where stiqmid=$stiqmid";
	$re2 = mysqli_query($conn, $eve2);
	while($rt2 = mysqli_fetch_assoc($re2))

	{
		$imid1 = $rt2['imid'];
	}
	$coursemid=$_POST['coursemid'];
	$eve2 = "select collegemid,countrymid from res_course_master where coursemid=$coursemid";
	$re2 = mysqli_query($conn, $eve2);
	while($rt2 = mysqli_fetch_assoc($re2))

	{
		$collegemid = $rt2['collegemid'];
		$countrymid = $rt2['countrymid'];
	}
	
	$coremarks=$_POST['coremarks'];
	$coursefees=$_POST['coursefees'];
	$doapplication=date("Y-m-d", strtotime($_POST['doapplication']));
	$capstatus=$_POST['capstatus'];
	
	$cintakeyear='';
	$cintekmonth='';
	
			$cintakeyear=$_POST['cintakeyear'];
			
			$cintekmonth=$_POST['cintakemonth'];
			
			
		 $eve1 = "INSERT INTO res_collegeapp_master (imid,stiqmid, coursemid,collegemid,countrymid,coremarks,coursefees,capstatus,doapplication,capaddedon,capaddedby,capupdatedon,capupdatedby,cintakemonth,cintakeyear) VALUES('$imid1','$stiqmid','$coursemid','$collegemid','$countrymid','$coremarks','$coursefees','$capstatus','$doapplication','$mdate1','$usermid','$mdate1','$usermid','$cintekmonth','$cintakeyear')";
	
			
		if ($conn->query($eve1) === TRUE)
			{
				$eve11 = "update res_inquiry_master set imstatus='College Application' where imid=$imid1";
				$re11 = mysqli_query($conn, $eve11);
				$_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=$imid1";
				$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";
			}
			
			if($_SESSION['frompage']=='aplicationInquirydetailpage')
			{
				header("Location: applicationinquirydetail.php?id=$stiqmid");
			}else{
	header("Location: inquirydetail.php?id=$imid1&tab=student_application");
			}
			
	
?>