<?php
//send with PHP SMTP
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

// Load Composer's autoloader			
require ('./PHPMailer/vendor/autoload.php');

require("database.php");

/*
1. receive response from payment gateway
2. get member details
3. 	3.1	create profile
	3.2	create details in sponsor, employer, 
*/

if(isset($_GET['memberNumber']))
{
	//get data
	extract($_GET);
	//print_r($_POST);
	
	//get details
	$q = "Select * from members where memberNo='$memberNumber' limit 1";
    	$qry=mysqli_query($con, $q);
    	$row=mysqli_fetch_assoc($qry);
   	extract($row);
	

	///////
	//print_r($row);
	$pin = time();
	$date = date("d-m-Y");
	
	$pin = $pin + $memberID;
	//echo $pin;
	
	//check existing record
	$qcheck = "select * from epci_application where memberNo='$memberNumber' AND regSet='$regSet'"; 
	$rcheck = mysqli_query($con, $qcheck);
	if($rcheck){ $mycount = mysqli_num_rows($rcheck); }else{ $mycount =0; }
	
	if($mycount>0)
	{
	   //application already exist
	   echo '<script>alert("You already have a profile\nCheck your mail box for your PIN to proceed with your application ");window.location = "login.php";6</script>';
	   exit();
	}
	
	
	//get unique Id
	$uniqueId = 1;
	$lastuniqueId = 0;
	
	$q_uid = "select * from epci_application where regSet='$regSet' order by uniqueId ASC";
	$r_uid = mysqli_query($con, $q_uid);
	if($r_uid)
	{
	  $countR = mysqli_num_rows($r_uid);
	  if($countR>0)
	  {
	    while($rows_uid = mysqli_fetch_array($r_uid))
	    {
	       $lastuniqueId = $rows_uid["uniqueId"];
	    }
	    
	    $uniqueId = $uniqueId + $lastuniqueId;
	  } //--
	
	}
	//get unique Id--
	
	//create profile
	$memberAddress = str_replace("'", "\'", $memberAddress);
	$memberSurname = str_replace("'", "\'", $memberSurname);
	$memberOthernames = str_replace("'", "\'", $memberOthernames);
	
	$q = "insert into epci_application values('', '$memberNumber', '$pin', '$memberSurname', '$memberOthernames', '$memberTitle', '$memberSex', '$memberState', '$memberPhone', '$memberPhone2', '$memberEmail', '$memberAddress', '$memberGrade', '$memberDOB', '$memberRegDate', '$date', '$regSet', '', 'Pending', '$uniqueId', '', '')"; 
	$r = mysqli_query($con, $q);
	if(!$r){echo "<br><br>Error: Not Successful"; }
		
	//get app id
	$q = "select * from epci_application where password='$pin'"; 
	$r = mysqli_query($con, $q);
	while($row2 = mysqli_fetch_array($r))
	{
		$applicant_id = $row2['id'];
	}
	
	
	//create sponsor
	$q = "insert into epci_sponsor(applicant_id) values('$applicant_id')"; 
	$r = mysqli_query($con, $q);
	
	//create olevel
	$q = "insert into epci_olevel(applicant_id, subject, grade, subjecttag) values ('$applicant_id', 'ENGLISH', '-', 'ENG'), ('$applicant_id', 'MATHEMATICS', '-', 'MTH'), ('$applicant_id', 'PHYSICS', '-', 'PHY');"; 
	$r = mysqli_query($con, $q);
	
	//create employer
	$q = "insert into epci_employer(applicant_id) values('$applicant_id')"; 
	$r = mysqli_query($con, $q);

	
	
	//send email
			$subject = "Experienced Route Assessment Payment Acknowledgement - PIN: #".$pin;
			$content1 = '<table align="center" style="width: 100%; border: 1pt outset #d9950d;" border="1" cellspacing="0" cellpadding="0"><tr><td> <img src="http://www.niqs.org.ng/logo_niqs.png"> </td></tr>   <tr><td>
			
			<table width="95%" align="center">
			<tr><td>
			<p>
			Dear '.$memberSurname.' '.$memberOthernames.', <br/><br/>
			
			<p align="justify">
			This is to acknowledge the receipt of your payment for Experienced Route Assessment of the Nigerian Institute of Quantity Surveyors. <br><br>Kindly use your PIN below to login and continue with your application here: <a href="https://register.niqs.org.ng/exproute/login.php">https://register.niqs.org.ng/exproute/login.php</a> </p>';
			
			$content1 = $content1.'<table><tr><td bgcolor=""><b> &nbsp;  &nbsp; PIN: </td> <td>'.$pin.'</td> </tr>';
			$content1 = $content1.'<tr><td bgcolor=""><b> &nbsp;  &nbsp; Diet:  </td> <td>'.$regSet.'</td> </tr>';
			$content1 = $content1.'<tr><td bgcolor=""><b> &nbsp;  &nbsp; Amount:  </td> <td> <del>N</del>'.number_format($amount, 2).'</td> </tr>';
			$content1 = $content1.'<tr><td bgcolor=""><b> &nbsp;  &nbsp;  </td> <td> </td> </tr> </table>';
			
			$content1 .= '
			
	
			
			Thank you. 
			<br>
			<br>
			Secretary General

			<br><br><b>For more enquiries, please contact</b> 
	<br>Tel:  <a href="tel:+2348167593807">08167593807</a>
	<br>E-mail: <a href="mailto:info@niqs.org.ng" style="color: #336699;">info@niqs.org.ng</a>
				
		
		</td>
		</tr>
		</table>
				
	</td>  
	</tr>
	
	<tr style="height: 17px;">
<td style="width: 100%; padding: 0.75pt; height: 17px;">
<p style="text-align: center;" align="center"><a href="http://www.niqs.org.ng">www.niqs.org.ng</a>&nbsp;|&nbsp;<a href="http://twitter.com/niqslive">Twitter</a>&nbsp;|&nbsp;<a href="http://facebook.com/TheNigerianInstituteOfQuantitySurveyors">Facebook</a></p>
</td>
</tr>

	</table>   ';
	
		//$to = "salisu@niqs.org.ng";
		$to = $memberEmail;

		// Instantiation and passing `true` enables exceptions
		/*$mail = new PHPMailer(true);

		try {
			//Server settings
			$mail->isSMTP();                                            // Send using SMTP
			$mail->Host       = 'c16252.sgvps.net';                    // Set the SMTP server to send through
			$mail->SMTPAuth   = true;                                   // Enable SMTP authentication
			$mail->Username   = 'exams@niqs.org.ng';                     // SMTP username
			$mail->Password   = '1x44I1fl3]1:';                               // SMTP password
			$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
			$mail->Port       = 587;                                    // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

			//Recipients
			$mail->setFrom('exams@niqs.org.ng', 'NIQS');
			$mail->addAddress($to);     // Add a recipient
			$mail->addAddress('exams@niqs.org.ng');               // Name is optional
			//$mail->addReplyTo('sgtest@niqs.org.ng', 'Information');

			// Content
			$mail->isHTML(true);                                  // Set email format to HTML
			$mail->Subject = $subject;
			$mail->Body    = $content1;
			//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

			$mail->send();
			//prompt member
		  echo '<script>alert("Congratulations! \nCheck your mail for your PIN to continue with your application");</script>';

		  echo '<script>window.location = "http://niqs.org.ng/onlineapplication/exproute/login.php";</script>';
		  exit();
		
		/*
		} catch (Exception $e) {
			echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
		}
		*/
		
		$cc = "exams@niqs.org.ng";
		$headers = "MIME-Version: 1.0" . "\r\n"; 
		$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n"; 
		
		// More headers 
		$headers .= 'From: exams@niqs.org.ng' . "\r\n"; 
		
		
		if(mail($to,$subject,$content1,$headers))
		{
		  //copy exam officer
		  $subject = $memberNumber.": ".$subject;
		  $headers .= 'CC: '.$cc. "\r\n"; 
		
		  mail("exams@niqs.org.ng",$subject,$content1,$headers);
		  
		  //prompt member
		  echo '<script>alert("Congratulations! \nCheck your mail for your PIN to continue with your application");</script>';

		  echo '<script>window.location = "https://register.niqs.org.ng/exproute/login.php";</script>';
		  exit();
		} else{ echo $content1; }
		

} else {
	echo '<script>alert("Invalid token, you will be redirected to the application page"); header("Location:startapplication.php");</script>';
}

?>