<?php

/* 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);

$get_id = $request->getAttribute('id');
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$response=array();


$sql="SELECT * FROM seated_students WHERE id ='$get_id'";

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

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

      $user=json_decode($row['student_roll']);
      $user_date=$row['date'];
      $user_time=$row['time'];
      $room_id_get=json_decode($row['room_id']);
      $proctor_id_get=json_decode($row['proctors_list']);

    }

$proctors_id =$proctor_id_get[0];

$sql_select = "SELECT id FROM faculty WHERE  Staff_number = '$proctors_id'";
$sql_query = mysqli_query($con,$sql_select);

while($row_faculty = mysqli_fetch_assoc($sql_query))
{
    $proctor_data = $row_faculty['id'];
}


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

for($i=0;$i<count($user);$i++)
{

$sql_roll="SELECT set_student_exampre.*,set_papers.papers_shortcode FROM set_student_exampre INNER JOIN set_papers ON set_papers.papers_id = set_student_exampre.paper  WHERE set_student_exampre.roll_number = '$user[$i]'";

$result_roll=mysqli_query($con,$sql_roll);

    while ($row_roll=mysqli_fetch_assoc($result_roll)) {  

  $user_data[]=$row_roll;
  $roll_number[]=$row_roll['roll_number'];
     $paper[]=$row_roll['paper'];
     $paper_stcode[]=$row_roll['papers_shortcode'];
      $slots[]=$row_roll['slots'];
      $status[]='1';

    }

}
 $user_roll_number = json_encode($roll_number);
 $user_paper = json_encode($paper);
 $user_paper_shortcode = json_encode($paper_stcode);
$user_slots = json_encode($slots);
$user_status = json_encode($status);
$course_id = $user_data[0]['course_id'];
$season_id = $user_data[0]['season_id'];
$exams_id = $user_data[0]['exams_id'];
// //print_r($user_data);
$date = $user_date;
$time = $user_time;
$file = "papers.cvc";
$file_path = "https://aahaconsulting.com/Staging/Restax_api_data/hari/slimframework/v1/uploads/exams";
$proctor_id = $proctor_data;
$pwi_submission = "1";
// //echo json_encode($user_data);

$sql12="SELECT * FROM pwi_submit WHERE proctor_sheet_id='$get_id'";

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

if(mysqli_num_rows($res12) > 0)
{

$response=array('status' => "true",'Message' => "Data Already Exits");

//echo json_encode($response);

}
else{

 $sqlinsert="INSERT INTO  pwi_submit (course_id,season_id,exams_id,roll_number,paper,papers_shortcode,slot,status,date,time,file,file_path,proctor_id,proctor_sheet_id,process,room_id)VALUES ('$course_id','$season_id','$exams_id','$user_roll_number','$user_paper','$user_paper_shortcode','$user_slots','$user_status','$date','$time','$file','$file_path','$proctor_id','$get_id','$pwi_submission','$room_id_get')";

$res= $con->query($sqlinsert);
 
if($res==true)
{

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

}

//////////////get submit answer/////////////////

$sql_get_submit="SELECT * FROM pwi_submit WHERE proctor_sheet_id ='$get_id'";

$result_get_submit=mysqli_query($con,$sql_get_submit);

    while ($row_get_submit=mysqli_fetch_assoc($result_get_submit)) {  


      $roll_number_last=json_decode($row_get_submit['roll_number']);
      $paper_last=json_decode($row_get_submit['paper']);
        $papers_shortcode_last=json_decode($row_get_submit['papers_shortcode']);
        $slot_last=json_decode($row_get_submit['slot']);


     // print_r($list_present); 

    }

    for($i=0;$i<count($roll_number_last);$i++)
{

 $list_answer[$i][] = $roll_number_last[$i]; 
 $list_answer[$i][] = $paper_last[$i]; 
 $list_answer[$i][] = $papers_shortcode_last[$i]; 
 $list_answer[$i][] = $slot_last[$i]; 

 $keylist = array('roll_number','paper_id','papers_shortcode','slot');

$list_answer_data[] = array_combine($keylist, $list_answer[$i]);

}

echo json_encode($list_answer_data);

    // $user_get_submit[0]['roll_number']=json_decode($user_get_submit[0]['roll_number']);
    //  $user_get_submit[0]['status']=json_decode($user_get_submit[0]['status']);
    // $user_get_submit[0]['paper']=json_decode($user_get_submit[0]['paper']);
    // $user_get_submit[0]['papers_shortcode']=json_decode($user_get_submit[0]['papers_shortcode']);
    // $user_get_submit[0]['slot']=json_decode($user_get_submit[0]['slot']);




  //echo json_encode($user_get_submit, JSON_PRETTY_PRINT);

        

?>