<?php

/* Attempt to connect to MySQL database */
/* Attempt to connect to MySQL database */

$con = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);


$course_id=$request->getParsedBody()['course_id'];
 $season_id = $request->getParsedBody()['season_id'];
  $exams_id = $request->getParsedBody()['exams_id'];
  $paper_id = $request->getParsedBody()['paper_id'];
  $slots=$request->getParsedBody()['slots'];
  $date = $request->getParsedBody()['date'];
  $time=$request->getParsedBody()['time'];
  $duration = $request->getParsedBody()['duration'];
 $response=array();
 $data=array();
 
//  $sql_query ="SELECT * FROM Exams WHERE course_id = '$course_id' AND season_id = '$season_id' AND exam_start_date_time =< '$date' AND exam_end_date_time >= '$date'";
 $sql_query ="SELECT * FROM Exams WHERE course_id = '$course_id' AND season_id = '$season_id' AND exam_start_date_time <= '$date' AND exam_end_date_time >= '$date'";
 $queryconn = mysqli_query($con,$sql_query);
 if(mysqli_num_rows($queryconn)>0){
     
     $sql_check = "SELECT * FROM schedule_exam WHERE course_id = '$course_id' AND season_id = '$season_id' AND exams_id = '$exams_id' AND paper_id='$paper_id' AND slots ='$slots' AND date='$date' AND time='$time'";
                $sql_query_conn = mysqli_query($con,$sql_check);
                
                if(mysqli_num_rows($sql_query_conn)>0){
                   $response=array('status' => "true",'Message' => "Schedule Exam Already inserted",'Exam' => $message);
echo json_encode($response);
                    
                }
                else{
                $sql="INSERT INTO schedule_exam (course_id,season_id,exams_id,paper_id,slots,date,time,duration) VALUES ('$course_id','$season_id','$exams_id','$paper_id','$slots','$date','$time','$duration')";
$res=mysqli_query($con,$sql);
if ($res=true) {
     $data=
        array(   
	"course_id"=>$course_id,
 "season_id" => $season_id,
  "exams_id" => $exams_id,
  "paper_id" => $paper_id,
  "slots"=>$slots,
  "date" => $date,
  "time"=>$time,
  "duration" =>$duration,
         );
    
    $message=$data;

$response=array('status' => "true",'Message' => "Schedule Exam Succesfully inserted",'Exam' => $message);
echo json_encode($response);
        }
        else{
          $response=array('status' => "false",'Message' => "Schedule Exam Failed");
         echo json_encode($response);
        }
 }
        
 }
 
 else{
       $response=array('status' => "false",'Message' => "Date miss match");
       echo json_encode($response);
 }
   
?>