<?php
	include "db_config.php";
	include "auth.php";

	$id="";
	if(isset($_GET['id'])){
	$id = $_GET['id'];
	} // get id


	$question_number = $question_text = $choice = "";

	if(isset($_POST['question_number'])){
		$question_number = $_POST['question_number'];
	}
	if(isset($_POST['question_text'])){
		$question_text  =  $_POST['question_text'];
	}
	if(isset($_POST['correct_choice'])){
		$correct_choice = $_POST['correct_choice'];
	}
	
	// Choice Array
	$choice =  array();
	if(isset($_POST['choice1'])){
		$choice[1] = $_POST['choice1'];
	}
	if(isset($_POST['choice2'])){
		$choice[2] = $_POST['choice2'];
	}
	if(isset($_POST['choice3'])){
		$choice[3] = $_POST['choice3'];
	}
	if(isset($_POST['choice4'])){
		$choice[4] = $_POST['choice4'];
	}
	
	
	//First Query for Questions Table

	$query = "INSERT into questions(question_number,question_text)VALUES('$question_number','$question_text')";
	$result = mysqli_query($conn,$query);

	//Validate First Query
	if($result){
		foreach ($choice as $option => $value) {
			if($correct_choice==$option){
				$is_correct = 1;
			}else{
				$is_correct = 0;
			}

			//Second Query for Option Table
			$query = "INSERT into  options(id,question_number,is_correct,options)VALUES(NULL,'$question_number','$is_correct','$value')";

			$insert_row= mysqli_query($conn,$query) or die($query);

			//Validate Insertion of Choices
			if($insert_row){
				continue;
			}else{
				die("2nd Query for Choices could not be executed");
			}
		}
		$message = "Questions has been added Successfully.";
		$alerttype = "success";
	}

	$query = "SELECT * FROM questions";
	$questions = mysqli_query($conn,$query) or die($query);
	$total  =  mysqli_num_rows($questions);
	$next = $total + 1;


?>

<!DOCTYPE html>
<html lang="en">
    

<head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0">
        <title>JSMS - Add Exam Question</title>
		
		<!-- Favicon -->
        <link rel="shortcut icon" href="assets/img/favicon.png">
	
		<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,500;0,600;0,700;1,400&amp;display=swap">
		
		<!-- Bootstrap CSS -->
        <link rel="stylesheet" href="assets/plugins/bootstrap/css/bootstrap.min.css">
		
		<!-- Fontawesome CSS -->
		<link rel="stylesheet" href="assets/plugins/fontawesome/css/fontawesome.min.css">
		<link rel="stylesheet" href="assets/plugins/fontawesome/css/all.min.css">
		
		<!-- Main CSS -->
        <link rel="stylesheet" href="assets/css/style.css">
    </head>
    <body>
	
		<!-- Main Wrapper -->
        <div class="main-wrapper">
		
			<?php include "hrm.php"; ?>
			<?php include "navigation.php";?>
			
			
			
			<!-- Page Wrapper -->
            <div class="page-wrapper">
                <div class="content container-fluid">
				
					<!-- Page Header -->
					<!-- <div class="page-header">
						<div class="row">
							<div class="col-sm-12">
								<h3 class="page-title">Blank Page</h3>
								<ul class="breadcrumb">
									<li class="breadcrumb-item"><a href="index.html">Dashboard</a></li>
									<li class="breadcrumb-item active">Blank Page</li>
								</ul>
							</div>
						</div>
					</div> -->
					<!-- /Page Header -->

					<div class="row">
						<div class="col-sm-12">
							<form method="POST" action="add.php">
								<?php if(isset($message)){?><div style="margin-top: 10px;" class="alert alert-<?php echo $alerttype;?> alert-dismissible fade show" role="alert">
					             <?php echo $message; ?>
					             <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
					             </div><?php } ?>
								<h2 style="margin-top: 20px;">Add Questions</h2>
								<div class="row">
									<p><label>Question Number:</label>
									<input class="form-control"  type="number" name="question_number" value="<?php echo $next;?>">
								</p>

								</div>
								<div class="row">
									<div class="form-group">
										<label>Question Text</label>
										<input type="text" style="width:500%; margin-bottom: 10px;" name="question_text" class="form-control">
									
										<label>Choice 1:</label>
										<input type="text" style="width:400%; margin-bottom: 10px;" name="choice1" class="form-control" >
									
									
										<label>Choice 2:</label>
										<input type="text" style="width:400%;margin-bottom: 10px;" name="choice2" class="form-control" >
									
									
										<label>Choice 3:</label>
										<input type="text" style="width:400%;margin-bottom: 10px;" name="choice3" class="form-control" >
									
									
										<label>Choice 4:</label>
										<input type="text" style="width:400%;" name="choice4" class="form-control" >
										<p>
											<label>Correct Option Number:</label>
											<input type="number" name="correct_choice" class="form-control" >
										</p>
										<input type="submit" name="submit" value="submit" class="btn  btn-success">
									</div>
								</div>
							</form>
						</div>			
					</div>
					
					
		

						</div>			
					</div>
					
				</div>			
			</div>
			<!-- /Page Wrapper -->
		
        </div>
		<!-- /Main Wrapper -->
		
		<!-- jQuery -->
        <script src="assets/js/jquery-3.5.1.min.js"></script>
		
		<!-- Bootstrap Core JS -->
        <script src="assets/js/popper.min.js"></script>
        <script src="assets/plugins/bootstrap/js/bootstrap.min.js"></script>
		
		<!-- Slimscroll JS -->
        <script src="assets/plugins/slimscroll/jquery.slimscroll.min.js"></script>
		
		<!-- Custom JS -->
		<script  src="assets/js/script.js"></script>
		
    </body>


</html>