<!-- <?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>Register Carry Over Courses</h3>
				
                <hr/>
                <div class="container">
					<div class="row">
						<div class="col-md-12" style="margin-left:-10px">
				
								<p><b>SELECT DEPARTMENT AND LEVEL THEN CLICK ON NEXT</b></p>

							<!-- <form  method="POST"> -->
							<form action="index.php?env=carryover-courses" method="POST" >
					   
					   <div class="form-group">
						   <!-- <h5 style="text-align:center;margin-bottom:40px;">Please Fill in Capital Letters</h5> -->

							<div class=" row form-group">
								<div class="col-md-6">
								<label for="name">department:</label>
									   <select name="department_id" value="<?php if(isset($_POST['department_id'])){echo $_POST['department_id'];} ?>" id="" class="form-control" required>
									   <option value="">Select department</option>
											<?php
												$sql = mysqli_query($db, "SELECT * from department");
												while($row = mysqli_fetch_array($sql))
												{
													?>
													<option value="<?php echo htmlentities($row['department_id']); ?>"><?php echo htmlentities($row['department_name']); ?></option>

												<?php
													}
												?>
									   </select><br/>
									   
								</div>
								<div class="col-md-6">
								<label for="name">Level:</label>
									   <select name="leveld_id" value="<?php if(isset($_POST['leveld_id'])){echo $_POST['leveld_id'];} ?>" id="" class="form-control" required>
									   <option value="">Select Level</option>
											<?php
												$sql = mysqli_query($db, "SELECT * from leveld");
												while($row = mysqli_fetch_array($sql))
												{
													?>
													<option value="<?php echo htmlentities($row['leveld_id']); ?>"><?php echo htmlentities($row['leveld_name']); ?></option>

												<?php
													}
												?>
									   </select><br/>
								</div>
								<button type="click" name="" class="btn-block btn-success" >Next</button>
							</div>
  
							
						</div>
						
					<!-- </form> -->


							</form>


							<?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" class="form-class" method="POST">
							<table class="table table-stripped" border="1" cellpadding="5" cellspacing="0" >
									<thead class="thead-dar" style="background-color:#218838;color:#ffffff;font-size:13px;">
										<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 courses WHERE leveld_id = '$leveld_id' AND department_id = '$department_id' ";

									$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='checkbox' name='checked[]' value='$id'/></td>
													<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="submitcover" class="btn-block btn-success" >Submit</button>
							</form>
						</div>
					</div>
				</div>

                
					

                    <div class="row" style="margin-top:50px;">
                        <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 class="col-md-6">
                        
                        </div>
                    </div>
<?php

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

	// $department_id = mysqli_real_escape_string($db, $_POST['department_id']);
	// $leveld_id = mysqli_real_escape_string($db, $_POST['leveld_id']);
	
	$checkitem = $_POST['checked'] ?? [];

	foreach ($checkitem as $index => $isChecked)
	{
		if($isChecked > 0){
			$id = $_POST['checked'][$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";
	}		
}	