<?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, tpc(month, year, examno, center) logbook3(month, year, examno), olevel(english, maths, physics,.. )
*/


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 prob_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";</script>';
	   exit();
	}
	
	//get unique Id
	$uniqueId = 1;
	$lastuniqueId = 0;
	
	$q_uid = "select * from prob_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 prob_application values('', '$memberNumber', '$pin', '$memberSurname', '$memberOthernames', '$memberTitle', '$memberSex', '$memberState', '$memberPhone', '$memberPhone2', '$memberEmail', '$memberAddress', '$memberGrade', '$memberDOB', '$memberRegDate', '$date', '$regSet', '$examCentre', 'Pending', '$uniqueId', '', '', '')"; 
	$r = mysqli_query($con, $q);
	if(!$r){echo "<br><br>Error: Not Successful"; }
		
	//get app id
	$q = "select * from prob_application where password='$pin'"; 
	$r = mysqli_query($con, $q);
	while($row2 = mysqli_fetch_array($r))
	{
		$applicant_id = $row2['id'];
	}
	
	
	//create sponsor
	$q = "insert into prob_sponsor(applicant_id) values('$applicant_id')"; 
	$r = mysqli_query($con, $q);
	
	//create olevel
	//$q = "insert into prob_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 prob_employer(applicant_id) values('$applicant_id')"; 
	$r = mysqli_query($con, $q);
	
		
	//create logbook
	//$q = "insert into prob_logbook(applicant_id) values('$applicant_id')"; 
	//$r = mysqli_query($con, $q);
	
	//create gde
	$q = "insert into prob_gde(applicant_id) values('$applicant_id')"; 
	$r = mysqli_query($con, $q);
	
	//create gde
	$q = "insert into prob_exemption(applicant_id) values('$applicant_id')"; 
	$r = mysqli_query($con, $q);
	
	
	
	
	//send email
			$subject = "Probation 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="https://register.niqs.org.ng/niqs_logo.png"> </td></tr>   <tr><td>
			
			<table width="95%" align="center">
			<tr><td>
			<p>
			Dear '.$memberSurname.' '.$memberOthernames.', <br/><br/>
			
			This is to acknowledge the receipt of your payment for Probation Examination 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/probation/login.php">https://register.niqs.org.ng/probation/login.php</a>';
			
			$content1 = $content1.'<table><tr><td bgcolor=""><b> &nbsp;  &nbsp; PIN: </td> <td>'.$pin.'</td> </tr>';
			$content1 = $content1.'<tr><td bgcolor=""><b> &nbsp;  &nbsp; Set:  </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;
		
		$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))
		{
		  $subject = $memberNumber.": ".$subject;
		  $headers .= 'CC: '.$memberEmail. "\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>';
		  //header("location: index.php");
		  echo '<script>window.location = "https://register.niqs.org.ng/probation/login.php";</script>';
		  exit();
		  
		 // echo "Done";
		} else{ echo $content1; }
		
} else {
	echo '<script>alert("Invalid token, you will be redirected to the application page"); header("Location:startapplication.php");</script>';
}

?>