<!-- <?php include("db.php"); ?> -->


<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script>
$(document) .ready(function(){
	$("#form1 #select-all").click(function(){
		$("#form1 input[type='checkbox']").prop('checked', this.checked);
	});
});

</script>

                <h3>Print course registration</h3>
				
                <hr/>
                <div class="container">
					<div class="row">
						<div class="col-md-12" style="margin-left:-10px">
				
							<?php
                            // session_start();

                            if(!isset($_SESSION['sname'])){
                                header("location:validation/index.php");
                                exit();
                            }

                            $sname = $_SESSION["sname"];
							

                            $query ="SELECT sname FROM portal WHERE sname = '$sname'";
                            $query_result = $db->query($query);

                            if($query_result->num_rows == 1){
                                $row = $query_result->fetch_assoc();
                               
                            }
                        ?>


							<!-- <form id="form1" method="POST"> -->
							<table class="table table-stripped" border="1" cellpadding="5" cellspacing="0" >
									<thead class="thead-dar" style="background-color:#218838;color:#ffffff;">
										<!-- <th><input type="checkbox" id="select-all"></th> -->
										<th>Course Title</th>
										<th>Course Code</th>
										<th>Course Unit</th>
										<th>Course Status</th>
									</thead>
									<?php
								// if(isset($_POST['leveld_id'], $_POST['department_id'])){

								// 	$leveld_id = $_POST['leveld_id'];
								// 	$department_id = $_POST['department_id'];

									$query = "SELECT * FROM carryover_courses WHERE sname = '$sname' ";

									$query_run = mysqli_query($db, $query);

									if(mysqli_num_rows($query_run) > 0){
										foreach($query_run as $row){
											$course_title = $row['course_title'];
											$course_code = $row['course_code'];
											$course_unit= $row['course_unit'];
											$course_Status = $row['course_status'];
											$id = $row['leveld_id'];
			
											echo "<tr>
												
													<td><input type='hidden' name='course_title[]' value='$course_title' /> $course_title</td>
													<td><input type='hidden' name='course_code[]' value='$course_code' /> $course_code</td>
													<td><input type='hidden' name='course_unit[]' value='$course_unit' /> $course_unit</td>
													<td><input type='hidden' name='course_status[]' value='$course_Status' /> $course_Status</td>
													<input type='hidden' name='sname[]' value='$sname' />
													
											</tr>";
										}
									}else{
										echo "No Data Found";
									}
								// }
							?>

							</table>
								<!-- <button type="submit" name="submitdata" class="btn-block btn-success" >Submit</button> -->
							<!-- </form> -->
						</div>
					</div>
				</div>
					

                <div class="row" style="margin-top:50px;">
						<div class="col-md-6">
                            <button type="submit" onclick="window.print()" class="btn btn-primary btn-block"><i class="fa fa-print"></i> Print</button>
                        </div>
                        <div class="col-md-6">
                            <a href="index.php?env=courses" style="color:#ffffff;text-decoration:none;"> 
                                <button type="button" class="btn btn-primary btn-block"><i class="fa fa-arrow-left"></i> Go back</button>
                            </a>
                        </div>
                       
                    </div>
<?php

if(isset($_POST['submitdata']))
{
	
	$checkitem = $_POST['check'] ?? [];

	foreach ($checkitem as $index => $isChecked)
	{
		if($isChecked > 0){
			$id = $_POST['check'][$index];
			$course_title = $_POST['course_title'][$index];
			$course_code = $_POST['course_code'][$index];
			$course_unit = $_POST['course_unit'][$index];
			$course_Status = $_POST['course_status'][$index];
			$sname = $_POST['sname'][$index];

			$query = "INSERT INTO carryover_courses (sname, leveld_id, course_title, course_code, course_unit, course_status) 
			VALUES('$sname', '$id','$course_title', '$course_code', '$course_unit', '$course_Status')";

			$query_result = mysqli_query($db, $query);

			
			
		}
	}
	if($query_result){
		echo "<Script>alert('Your Course registration is successful')</Script>";
	}else{
		echo "Error";
	}		
}	


?>