<?php
$errmsg='';
$sucmsg='';
$subjectcount='3';
if(isset($_POST['update'])){
    extract($_POST);
    $errmsg='';
    $sucmsg='';
    
	//print_r($_POST);
	
		//update record
	$qry1=mysqli_query($con, "UPDATE epci_olevel SET grade='$enggrade' WHERE subjecttag='ENG' AND applicant_id=$applicant_idpci");
	$qry2=mysqli_query($con, "UPDATE epci_olevel SET grade='$mathsgrade' WHERE subjecttag='MTH' AND applicant_id=$applicant_idpci");
	$qry3=mysqli_query($con, "UPDATE epci_olevel SET grade='$phygrade' WHERE subjecttag='PHY' AND applicant_id=$applicant_idpci");
	
	
	if($subjectcount=="4")
	{	
		//only one other subject
		$qry4=mysqli_query($con, "UPDATE epci_olevel SET grade='$subject1' WHERE subjecttag='subject1' AND applicant_id=$applicant_idpci");
	}elseif($subjectcount=="5")	{	
		//two other subject
		$qry4=mysqli_query($con, "UPDATE epci_olevel SET grade='$subject1' WHERE subjecttag='subject1' AND applicant_id=$applicant_idpci");
		$qry5=mysqli_query($con, "UPDATE epci_olevel SET grade='$subject2' WHERE subjecttag='subject2' AND applicant_id=$applicant_idpci");
	}
	
	
	if($qry3){
		$sucmsg='Successful!';
	}else{
		$errmsg='Not Successful!';
	}
	
    
}elseif(isset($_POST['addsubject'])){
    extract($_POST);
	
    $errmsg='';
    $sucmsg='';
    
		//insert new grade
	$qry2=mysqli_query($con, "insert into epci_olevel (applicant_id, subject, grade, subjecttag) values('$applicant_idpci', '$newsubject', '$newgrade', '$subjecttag')");
	if($qry2){
		$sucmsg='Successful!';
	}else{
		$errmsg='Not Successful!';
	}
}

	
	
//GET DETAILS FROM DB

	$q = "Select * from epci_olevel where applicant_id='$applicant_idpci'";
    $qry=mysqli_query($con, $q);
	if($qry){
		
		if(mysqli_num_rows($qry) >0){
			$row=mysqli_fetch_assoc($qry);
			$ss = mysqli_num_rows($qry);
			extract($row);
			
		}else{ $errmsg='No subject found!'; }
				
	}else{
		$errmsg='No subject found!';
	}

?>

<div class="graph-visual tables-main">
<h2 class="inner-tittle">O'LEVEL SUBJECTS RESULT (5 CREDITS FROM WAEC/NECO/NABTEB)</h2>
You may add two other subjects from the list of subjects below to make it five.
	<div class="graph-form">
    		<div class="form-body">
    			<form method="post" name="olevelform" action="index.php?page=olevel"> 
                <?php 
                if($errmsg!=''){
                    echo '<div class="alert alert-danger">'.$errmsg.'</div>';
                }elseif($sucmsg!=''){
                    echo '<div class="alert alert-success">'.$sucmsg.'</div>';
                }
                
                ?>
                    <div class="form-group"> 
                        <label>Registered Subjects</label> 
                        <table border='1' padding='20' width='50%'>
						  
						  <tr><td><b>Subject </td><td><b>Grade </td></tr>
						  
						  <?php
							//ENG
							$q1 = "Select * from epci_olevel where applicant_id='$applicant_idpci' and subject='ENGLISH'";
							$qry1=mysqli_query($con, $q1);
							
							while($rows = mysqli_fetch_array($qry1))
							{
								extract($rows);
								echo '<tr><td> ENGLISH </td><td><b>';
								
								echo '<select class="form-control" name="enggrade" required="" style="height:auto">';
								echo '<option>'.$grade.'</option>';
								echo '<option>A1</option><option>A2</option><option>A3</option><option>B2</option><option>B3</option><option>C4</option><option>C5</option><option>C6</option><option>D7</option><option>E8</option><option>F9</option>';
								echo '</select> </td></tr>';
							}
						  
							//MTH
							$q1 = "select * from epci_olevel where applicant_id='$applicant_idpci' and subject='MATHEMATICS'";
							$qry1=mysqli_query($con, $q1);
							
							while($rows = mysqli_fetch_array($qry1))
							{
								extract($rows);
								echo '<tr><td> MATHEMATICS </td><td><b>';
								
								echo '<select class="form-control" name="mathsgrade" required="" style="height:auto">';
								echo '<option>'.$grade.'</option>';
								echo '<option>A1</option><option>A2</option><option>A3</option><option>B2</option><option>B3</option><option>C4</option><option>C5</option><option>C6</option><option>D7</option><option>E8</option><option>F9</option>';
								echo '</select> </td></tr>';
							}
						  
							//PHY
							$q1 = "select * from epci_olevel where applicant_id='$applicant_idpci' and subject='PHYSICS'";
							$qry1=mysqli_query($con, $q1);
							
							while($rows = mysqli_fetch_array($qry1))
							{
								extract($rows);
								echo '<tr><td> PHYSICS </td><td><b>';
								
								echo '<select class="form-control" name="phygrade" required="" style="height:auto">';
								echo '<option>'.$grade.'</option>';
								echo '<option>A1</option><option>A2</option><option>A3</option><option>B2</option><option>B3</option><option>C4</option><option>C5</option><option>C6</option><option>D7</option><option>E8</option><option>F9</option>';
								echo '</select> </td></tr>';
							}
						  
							///////////////////
						  
							$q = "Select * from epci_olevel where applicant_id='$applicant_idpci' AND (subjecttag='subject1' OR subjecttag='subject2')";
							$qry=mysqli_query($con, $q);
							$othersubject = mysqli_num_rows($qry);
							
							//display other subject
							//echo "Other Subjects: ".$othersubject;
							if($othersubject==1)
							{
							
								while($rows = mysqli_fetch_array($qry))
								{
									extract($rows);
									echo '<tr><td> '.$subject.' </td><td><b>';
									
									//$sname = strtolower($subject);
									
									echo '<select class="form-control" name="subject1" required="" style="height:auto">';
									echo '<option>'.$grade.'</option>';
									echo '<option>A1</option><option>A2</option><option>B2</option><option>B3</option><option>C4</option><option>C5</option><option>C6</option><option>D7</option><option>E8</option><option>F9</option>';
									echo '</select> </td></tr>';
									
								}
								//subject count
								echo "<input type='hidden' name='subjectcount' value='4'>"; 
								
							}elseif($othersubject>1){
								$yy = 1;
								while($rows = mysqli_fetch_array($qry))
								{
									extract($rows);
									echo '<tr><td> '.$subject.' </td><td><b>';
									
									$sname = "subject".$yy;
									
									echo '<select class="form-control" name="'.$sname.'" required="" style="height:auto">';
									echo '<option>'.$grade.'</option>';
									echo '<option>A1</option><option>A2</option><option>B2</option><option>B3</option><option>C4</option><option>C5</option><option>C6</option><option>D7</option><option>E8</option><option>F9</option>';
									echo '</select> </td></tr>';
									$yy++;
								}
								//subject count
								echo "<input type='hidden' name='subjectcount' value='5'>"; 
							}else{ echo "<input type='hidden' name='subjectcount' value='3'>"; }
								
							
						  ?>
						  
						<tr><td> </td><td> <input name="update" type="submit" class="btn btn-default" value="Update"/> </td></tr>  
						</table>	
						
						</form> 
                    </div> 
					
					
				<?php 
                if($ss<5){
					if($ss==3){$subjecttag=1;}
					if($ss==4){$subjecttag=2;}
                    
                ?>
                <form method="post" name="addsubjectform" action="index.php?page=olevel"> 
                    <div class="form-group"> 
                        <label>Subject</label> 
                        <select class="form-control" name="newsubject" required=""  style="height:auto; width:40%"/>  
							<option>CHEMISTRY</option>
							<option>FURTHER MATHEMATICS</option>
							<option>ECONOMICS</option>
							<option>COMMERCE</option>
							<option>GEOGRAPHY</option>
							<option>TECHNICAL DRAWING</option>
							<option>BIOLOGY</option>
							<option>AGRIC SICENCE</option>
							<option>SOCIAL STUDIES</option>
						</select>
                    </div> 
                    
					<div class="form-group"> 
                        <label>Grade</label> 
                        <select class="form-control" name="newgrade" required=""  style="height:auto; width:40%"/>  
							<option>A1</option><option>A2</option><option>B2</option><option>B3</option><option>C4</option><option>C5</option><option>C6</option><option>D7</option><option>E8</option><option>F9</option>
						</select>
                    </div> 
                    <input type="hidden" name="subjecttag" value="subject<?php echo $subjecttag; ?>">
                    <input name="addsubject" type="submit" class="btn btn-default" value="Add Subject"/> 
                </form> 
				
				<?php }  ?>		
				
    		</div>
    
    </div>

</div>

<?php
if($ss>=5){
 echo "<div>";
 echo "  <a href='?page=passport'><button class='btn'>Back</button></a>"; 
 echo "  <a href='?page=education'><button class='btn'>Next</button></a>"; 
 echo "</div>";
}
?>