<?php

include('../../db_connect/db.php');


$site_id =$_POST['site'];
$block_id =$_POST['block'];
$room_id =$_POST['room'];
$student_id =$_POST['student_id'];
$type_id =$_POST['type_id'];



$result = mysqli_query($conn_1,"SELECT * FROM hostel_rooms WHERE block_id = '$block_id' AND campus_id='$site_id' AND id='$room_id' ") or die(mysqli_error($conn_1));
if($result){
  $num_rooms=mysqli_num_rows($result);
  if($num_rooms > 0){

    while($okk = mysqli_fetch_assoc($result)){
      $total_bedspace=$okk['bed_space'];

      $count = mysqli_query($conn_1, "SELECT *FROM hostel_allocation WHERE room_id='$room_id' ");
      if($count){
         $sql_row_room=mysqli_num_rows($count);

        if( $sql_row_room < $total_bedspace){
          $room_id== $room_id;



          $chk = mysqli_query($conn_1, "SELECT *FROM hostel_allocation WHERE student_id='$student_id'") or die(mysqli_error($conn_1));
          if($chk){

            $sql_row=mysqli_num_rows($chk);
            if($sql_row > 0){
              $vv=mysqli_fetch_assoc($chk);



              $output= '
              <div class="alert alert-warning" role="alert">Student have been allocated already, Thank you!</div>
              <br><br><br>';
            }else{
              //insert into hostel allocation table

                  $insert = mysqli_query($conn_1, "INSERT INTO hostel_allocation(school_id,type_id,campus_id,block_id,room_id,student_id,session_id, status,chk_in_date_time)
                  VALUES('1','$type_id','$site_id','$block_id','$room_id','$student_id','6','1',NOW())") or die(mysql_error($conn_1));
                  if($insert){
                    if($type_id==1)
                    {
                      mysqli_query($conn_1, "UPDATE students_prence SET hostel_allocation = '1' WHERE id ='$student_id' ");
                    }else if($type_id == 2)
                    {
                      mysqli_query($conn_1, "UPDATE students_nce SET hostel_allocation = '1' WHERE id ='$student_id' ");

                    }else if($type_id == 3)
                    {
                        mysqli_query($conn_2, "UPDATE students_nce SET hostel_allocation = '1' WHERE id ='$student_id' ");
                    }else if($type_id == 4)
                    {
                        mysqli_query($conn_2, "UPDATE students_nce SET hostel_allocation = '1' WHERE id ='$student_id' ");
                    }else if($type_id == 5)
                    {
                        mysqli_query($conn_1, "UPDATE summer_students SET hostel_allocation = '1' WHERE id ='$student_id' ");
                    }else if($type_id == 6)
                    {
                        mysqli_query($conn_1, "UPDATE students_lvsp SET hostel_allocation = '1' WHERE id ='$student_id' ");
                    }

                    $output= '
                    <div class="alert alert-success" role="alert">Student allocated successful!</div>
                    <br><br><br>';
                  }



              }
            }

            break;

          }else{

            $output= '
            <div class="alert alert-danger" role="alert">There is no more available space in the room selected!</div>
            <br><br><br>';

          }



        }

}
}
echo $output;
}

?>
