<?php
error_reporting(0);
session_start();

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

$site_id =$_POST['campus_id'];
$block_id =$_POST['block_id'];
$room_id =$_POST['room_id'];
$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'];

      //fetch from allocation and count to see if the room is full or not
      $count = mysqli_query($conn_1, "SELECT *FROM hostel_allocation WHERE room_id='$room_id' ") or die(mysqli_error($conn_1));
      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);
              $user_id = $vv['student_id'];

              $update = mysqli_query($conn_1, "UPDATE hostel_allocation SET
                campus_id='$site_id',
                block_id='$block_id',
                room_id='$room_id',
                status='1',
                chk_in_date_time=NOW() WHERE student_id='$user_id'
              ") or die (mysqli_error($conn_1));
              if ($update) {

                echo '1';
              }else {
                echo $error='<div class="alert alert-success alert-dismissible" role="alert">
                  <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                  <strong>Alert!</strong> Error in updating record, Thank you!.
                 </div>';

              }

            }
          }
        }else{
          //if the room is full, i will delete the person from hostel allocation and reinsert the person
          $delete = mysqli_query($conn_1, " DELETE FROM hostel_allocation WHERE student_id='$student_id'") or die(mysqli_error($con));
          if ($delete) {
            $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($con));
            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_udus 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' ");
              }

              echo '1';
            }

          }

        }
        break;
      }

    }

  }else {
    echo $error='<div class="alert alert-success alert-dismissible" role="alert">
      <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
      <strong>Alert!</strong> There is no more available space in the room selected!, Thank you!.
     </div>';

  }

}

?>
