<?php

/*Student API*/


/*Fetching API*/


/* Attempt to connect to MySQL database */
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'Restax_final');

define('DB_PASSWORD', 'Restax_final123');

define('DB_NAME', 'Restax_api_data'); 

$con = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);

$course_id_get = $request->getAttribute('course_id');
$season_id_get = $request->getAttribute('season_id');
$exams_id_get = $request->getAttribute('exams_id');
$date_get = $request->getAttribute('date');
$time_get = $request->getAttribute('time');

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$response=array();
 $loop_array=array(); 
 $countdata = 1;
 

////get schedule ecam paperid//////

$sql="SELECT schedule_exam.paper_id,schedule_exam.slots,set_papers.papers_id,set_papers.papers_shortcode FROM schedule_exam INNER JOIN set_papers ON set_papers.papers_id = schedule_exam.paper_id WHERE schedule_exam.course_id = '$course_id_get' AND schedule_exam.season_id = '$season_id_get' AND schedule_exam.exams_id = '$exams_id_get' AND schedule_exam.date = '$date_get' AND schedule_exam.time = '$time_get'";

$result=mysqli_query($con,$sql);

    while ($row=mysqli_fetch_assoc($result)) {  

$papers_shortcode[] = $row['papers_shortcode'];
 $user[] = $row['paper_id'];
 $slots[] = $row['slots'];

 


}


 $user_count = count($user);
//List all student naumber///

for($i=0;$i<$user_count;$i++){
$sql_firstvalue="SELECT roll_number FROM set_student_exampre WHERE course_id = '$course_id_get' AND season_id = '$season_id_get' AND exams_id = '$exams_id_get' AND paper = '$user[$i]'";

$result_firstvalue=mysqli_query($con,$sql_firstvalue);

    while ($row_firstvalue=mysqli_fetch_assoc($result_firstvalue)) {  


$user_firstvalue[$i][] = $row_firstvalue['roll_number'];

 $user_firstvalue[$i];

   // echo json_encode($user_firstvalue);
   
   
   

}

 //print_r($user_firstvalue[$i]);


if($countdata < count($user_firstvalue[$i]))
{
    
    $countdata = count($user_firstvalue[$i]);
}
else{
    $countdata = $countdata;
}

}

$value_data = $countdata;

// if(count($user_firstvalue[0]) < count($user_firstvalue[1]))
// {

//  $value_data = count($user_firstvalue[0]);

// }

// elseif(count($user_firstvalue[0]) > count($user_firstvalue[1]))
// {

//  $value_data = count($user_firstvalue[1]);

// }

// else{
//   $value_data = count($user_firstvalue[0]);

// }


///Assign all student number in one array/////



for($j=0;$j<$value_data;$j++)
{
for($i=0;$i<count($user_firstvalue);$i++)
{
 array_push($loop_array, $user_firstvalue[$i][$j]);

}
}


//print_r($loop_array);


/////get room number block id number/////

$sql_seat="SELECT * FROM seatingarrangements WHERE  course_id = '$course_id_get' AND season_id = '$season_id_get' AND exams_id = '$exams_id_get' AND date = '$date_get' AND time = '$time_get'";

$result_seat=mysqli_query($con,$sql_seat);

    while ($row_seat[]=mysqli_fetch_assoc($result_seat)) {  


$user_seat = $row_seat;


}

///put student thier rexpective rooms../////



//echo print_r($user_seat[0]['time']);

// echo json_encode($loop_array);


// echo json_encode($user_seat);

//////////////////////////////////////////////




for($seat_count=0;$seat_count<count($user_seat);$seat_count++)
{
 $get_student[$seat_count] = array();
 // echo json_encode($loop_array);
 $seatscountdata = $user_seat[$seat_count]['seats'];

	for($student_seat = 0;$student_seat<$seatscountdata;$student_seat++)
	{


 
		array_push($get_student[$seat_count], $loop_array[$student_seat]);


	}

$datarow[$seat_count] = $get_student[$seat_count];

$loop_array = array_slice($loop_array, $seatscountdata);



$course_id = $user_seat[$seat_count]['course_id'];
$season_id = $user_seat[$seat_count]['season_id'];
$exams_id = $user_seat[$seat_count]['exams_id'];
$date = $user_seat[$seat_count]['date'];
$time = $user_seat[$seat_count]['time'];
$blockid = $user_seat[$seat_count]['blockid'];
$floors = $user_seat[$seat_count]['floors'];
$room_id = $user_seat[$seat_count]['room_id'];
$seats = $user_seat[$seat_count]['seats'];
$proctors = $user_seat[$seat_count]['proctors'];
$student_roll = json_encode($datarow[$seat_count]);
$papers_list = json_encode($user);
$pap_short_list = json_encode($papers_shortcode);
$slots_final = $slots[$seat_count];
$punch_in = mt_rand(1000,9999);
$recovery_code = mt_rand(1000,9999);
$proctors_list = 1;

$sql12="SELECT * FROM seated_students WHERE course_id='$course_id' AND season_id = '$season_id' AND exams_id = '$exams_id' AND date = '$date' AND time = '$time' AND blockid = '$blockid' AND floors = '$floors' AND room_id = '$room_id'";

$query_excuet = $con->query($sql12);

if(mysqli_num_rows($query_excuet) > 0)
{
    $response=array('status' => "true",'Message' => "Data Already Exits");
    
    echo "Data Already Exits";
}

else{
$sqlinsert="INSERT INTO seated_students (course_id,season_id,exams_id,date,time,blockid,floors,room_id,seats,proctors,student_roll,papers_id,papers_shortcode,slots,recover_code,punch_in,proctors_list)
VALUES ('$course_id','$season_id','$exams_id','$date','$time','$blockid','$floors','$room_id','$seats','$proctors','$student_roll','$papers_list','$pap_short_list','$slots_final','$recovery_code','$punch_in','$proctors_list')";

$res= $con->query($sqlinsert);

if($res==true)
{

	$response=array('status' => "true",'Message' => "Data inserted successfully",'seatingarrangements' => $message);
//echo json_encode($response);
        
}
// echo "dgds";
}

}


//echo print_r($loop_array);

//$loop_array = array_slice($loop_array, 3);

//echo print_r($loop_array);



echo print_r($datarow[0]);
echo print_r($datarow[1]);
//echo print_r($get_student);

?>