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

////////////////////---- 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 add_delete_control=1 AND session !='' order by id desc limit 1");
	$sem=mysqli_fetch_assoc($bc);
}
///////////////////--- END FETCH STUDENT DETAILS


if(isset($_POST['remove_course'])){
	$bcc=mysqli_query($con,"select * from `reg_course` where id='".$_POST['remove_course']."' AND sid='".$_SESSION['stureg_id']."' AND matric_no='".$me['matric_no']."'");
	$bbc=mysqli_fetch_assoc($bcc);
	$un=$bbc['unit'];
	
	$bo=mysqli_query($con,"select * from `semester` where programme_id='".$me['programme_id']."' AND add_delete_control=1 AND session !='' order by id desc");
	$sek=mysqli_fetch_assoc($bo);
	
	$mac=mysqli_query($con,"select SUM(unit) AS sum_tnu from reg_course where sid='".$_SESSION['stureg_id']."' AND  matric_no='".$me['matric_no']."' AND programme_id='".$me['programme_id']."' AND semester_num='".$sek['semester']."' AND dep_id='".$me['dept_id']."' AND session='".$sek['session']."' order by course_code asc");
	
	$saka=mysqli_fetch_assoc($mac);
	
	if((((int)$saka['sum_tnu'])-((int)$un)) < ((int)$sek['min_tnu'])){
		
		echo 'minimum reached!';
		
	}else{
		
	$www=mysqli_query($con,"delete from reg_course where id='".$_POST['remove_course']."' AND sid='".$_SESSION['stureg_id']."' AND matric_no='".$me['matric_no']."'");
	
	if($www){
	echo (((int)$_POST['s_tnu'])-((int)$un));
	}
	}
	
}



if(isset($_POST['signal'])){
	$mac=mysqli_query($con,"select * from course where id='".$_POST['course_id']."'");	
	$cos=mysqli_fetch_assoc($mac);
	
	if($_POST['signal']=='add'){
	echo (((int)$_POST['tnu'])+((int)$cos['unit']));
	}else if ($_POST['signal']=='delete'){
	echo (((int)$_POST['tnu'])-((int)$cos['unit']));
	}else{
	echo ((int)$_POST['tnu']);
	}
}


if(isset($_POST['execute_add_delete'])){
	
	$bb=mysqli_query($con,"select * from `semester` where programme_id='".$me['programme_id']."' AND add_delete_control=1 AND session !='' order by id desc limit 1");
	$res=mysqli_fetch_assoc($bb);
	
	if(count($_POST['cid'])>0){
		
	$gunit=0;
	for($j=0; $j<count($_POST['cid']); $j++){
	if($_POST['cid'][$j]!=''){
	$pel=mysqli_query($con,"select * from course where id='".$_POST['cid'][$j]."'");
	$ces=mysqli_fetch_assoc($pel);
	$gunit=$gunit+$ces['unit'];
	}
	}
	
	$mac=mysqli_query($con,"select SUM(unit) AS sum_tnu from reg_course where sid='".$_SESSION['stureg_id']."' AND  matric_no='".$me['matric_no']."' AND programme_id='".$me['programme_id']."' AND semester_num='".$res['semester']."' AND dep_id='".$me['dept_id']."' AND session='".$res['session']."' order by course_code asc");
	$mam=mysqli_fetch_assoc($mac);
	
	if(isset($_POST['extra_year']) && $_POST['extra_year']!=''){
		
		
	
	if((((int)$gunit)+((int)$mam['sum_tnu'])) > $res['max_tnu']){
		echo '<div class="alert alert-dismissible alert-danger">
				<strong>Oh No!</strong> Total number of units cannot be more than '.$res['max_tnu'].'.
              </div>';
	}else{
	
		$cn=0;
	for($i=0; $i<count($_POST['cid']); $i++){
	
	if($_POST['cid'][$i]!=''){
	
	$pal=mysqli_query($con,"select * from course where id='".$_POST['cid'][$i]."'");
	$cos=mysqli_fetch_assoc($pal);
	
	$mec=mysqli_query($con,"select id from reg_course where sid='".$me['id']."' AND  matric_no='".$me['matric_no']."' AND programme_id='".$me['programme_id']."' AND course_code='".$cos['course_code']."' AND session='".$sem['session']."'");
	
	
	if(mysqli_num_rows($mec)==0){
	
	$sql=mysqli_query($con,"insert into reg_course(sid,matric_no,level,dep_id,course_id,course_code,description,unit,status,programme_id,programme,programme_description,semester_num,semester_description,session,date) values('".$me['id']."','".$me['matric_no']."','".$_POST['s_level']."','".$me['dept_id']."','".$cos['id']."','".$cos['course_code']."','".$cos['description']."','".$cos['unit']."','".$cos['status']."','".$pro['id']."','".$pro['name']."','".$pro['description']."','".$sem['semester']."','".$sem['description']."','".$sem['session']."','$date')");
	
	}else{
		$sql='';
	}
	
	if($sql){
		$cn=$cn+1;
	}else{
		$cn=$cn+0;
	}
	}
	}
	
	if($cn >= count($_POST['cid'])){
		echo '<script>
		alert("Operation Successful!")
		location.href="add_delete.php"
		</script>';
	}else{
		echo '<script>
		alert("Some courses cannot be added!")
		location.href="add_delete.php"
		</script>';
	}
	}
		
		
	}else{
		
		
		
			if((((int)$gunit)+((int)$mam['sum_tnu'])) < $res['min_tnu']){
		echo '<div class="alert alert-dismissible alert-danger">
				<strong>Oh No!</strong> Total number of units cannot be lesser than '.$res['min_tnu'].'.
              </div>';
	}else if((((int)$gunit)+((int)$mam['sum_tnu'])) > $res['max_tnu']){
		echo '<div class="alert alert-dismissible alert-danger">
				<strong>Oh No!</strong> Total number of units cannot be more than '.$res['max_tnu'].'.
              </div>';
	}else{
	
		
		$cn=0;
	for($i=0; $i<count($_POST['cid']); $i++){
	
	if($_POST['cid'][$i]!=''){
	
	$pal=mysqli_query($con,"select * from course where id='".$_POST['cid'][$i]."'");
	$cos=mysqli_fetch_assoc($pal);
	
	
	$mec=mysqli_query($con,"select id from reg_course where sid='".$me['id']."' AND  matric_no='".$me['matric_no']."' AND programme_id='".$me['programme_id']."' AND course_code='".$cos['course_code']."' AND session='".$sem['session']."'");
	
	if(mysqli_num_rows($mec)==0){
	
	$sql=mysqli_query($con,"insert into reg_course(sid,matric_no,level,dep_id,course_id,course_code,description,unit,status,programme_id,programme,programme_description,semester_num,semester_description,session,date) values('".$me['id']."','".$me['matric_no']."','".$_POST['s_level']."','".$me['dept_id']."','".$cos['id']."','".$cos['course_code']."','".$cos['description']."','".$cos['unit']."','".$cos['status']."','".$pro['id']."','".$pro['name']."','".$pro['description']."','".$sem['semester']."','".$sem['description']."','".$sem['session']."','$date')");
	
	}else{
	    $sql='';
	}
	
	if($sql){
		$cn=$cn+1;
	}else{
		$cn=$cn+0;
	}
	
	}
	}
	
	if($cn >= count($_POST['cid'])){
		echo '<script>
		alert("Operation Successful!")
		location.href="add_delete.php"
		</script>';
	}else{
		echo '<script>
		alert("Some courses cannot be added!")
		location.href="add_delete.php"
		</script>';
	}
	}
	
	
	
	}
	
	}else{
	echo '<div class="alert alert-dismissible alert-danger">
				<strong>Oh No!</strong> No course selected.
              </div>';	
	}

}
?>


 