<?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);

$date = $request->getAttribute('date');
$course_id = $request->getAttribute('course_id');
$session_id = $request->getAttribute('session_id');
$exams_id = $request->getAttribute('exams_id');

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

$response=array();
$g = 0;



//$sql="SELECT * FROM seated_students WHERE date ='2019-12-12'";
$sql="SELECT seated_students.*,set_seats.rows,set_seats.columns,set_seats.seat_rows FROM seated_students INNER JOIN set_seats ON set_seats.set_rooms_room_id = seated_students.room_id AND set_seats.set_blocks_block_id = seated_students.blockid AND set_seats.floors = seated_students.floors WHERE seated_students.date ='$date' AND seated_students.course_id ='$course_id' AND seated_students.season_id ='$session_id' AND seated_students.exams_id ='$exams_id'";

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

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

      $user=$row;

    }

    // echo json_encode($user);
   
 // $datatable = json_decode($user[0]['student_roll']);
//  echo $datatable[0];
  // echo count($datatable);
   
 //  print_r($user[0]['student_roll']);
   

// for($i=0;$i<count($user);$i++){
// $user[$i]['student_roll'] = json_decode($user[$i]['student_roll']);
// $user[$i]['papers_id'] = json_decode($user[$i]['papers_id']);

// }


for($i=0;$i<count($user);$i++)
{
  $first[$i][] = $user[$i]['id'];
     $first[$i][] = $user[$i]['room_id'];
     $first[$i][] = $user[$i]['rows'];
     $first[$i][] = $user[$i]['columns'];
      $first[$i][] = $user[$i]['proctors'];
    // $first[$i][] = $user[$i]['student_roll'];
        $second[$i] = json_decode($user[$i]['student_roll']);
        $get_row_list[$i] = json_decode($user[$i]['seat_rows']);

     


    
      $g=0;
      $rotate =0;
     for($row_array=0;$row_array<$user[$i]['rows'];$row_array++)
     {
        // echo $row_array;
        
        $row_data_add = $row_array + 1;
         
         for($column_array=0;$column_array<$user[$i]['columns'];$column_array++)
         {
             $column_data_add = $column_array +1;
             
             $check_rowlist = "C".$row_data_add."R".$column_data_add;
           
             if(in_array($check_rowlist,$get_row_list[$i]))
             {
                 
                        if(is_null($second[$i][$rotate]))
                {
                    $array_data_column[$column_array] = "Empty Seat";
            $array_data_row[$row_array] = $array_data_column;
            $array_data_last[$i] = $array_data_row;
                }
                else
                {
                      $array_data_column[$column_array] =   $second[$i][$rotate];
                      $array_data_row[$row_array] = $array_data_column;
                      
                      $array_data_last[$i] = $array_data_row;
                }
                
                    $rotate++;
               
             }
             else
             {
                $array_data_column[$column_array] =  "No Seat";
                      $array_data_row[$row_array] = $array_data_column;
                      
                      $array_data_last[$i] = $array_data_row;
             }
             
             

             
             
            //     if(is_null($second[$i][$g]))
            //     {
            //         $array_data_column[$column_array] = "null";
            // $array_data_row[$row_array] = $array_data_column;
            // $array_data_last[$i] = $array_data_row;
            //     }
            //     else
            //     {
            //           $array_data_column[$column_array] =   $second[$i][$g];
            //           $array_data_row[$row_array] = $array_data_column;
                      
            //           $array_data_last[$i] = $array_data_row;
            //     }
           
            //  echo $g;
            // print_r($get_row_list[$i][$g]);

            $secon_save[$row_array] = $second[$i][$column_array];
            
            $g++;
             
                
                // echo $rotate;
            
           
         }
        //  echo "<br>";
      
     }
    
//echo json_encode($array_data_last[0]);
$first[$i][] = $array_data_last[$i];
    
    $keys = array('id','room_id','rows','columns','proctors','student_roll');
    
    $finaldata[] = array_combine($keys,$first[$i]);
    
}

//print_r($secon_save[0]);

echo json_encode($finaldata, JSON_PRETTY_PRINT);


//print_r($array_data_last);

//echo json_encode($array_data_last[0]);

?>