<?php

$site_id =$_POST['site_id'];
$student_id =$_POST['student_id'];
$cat_id = $_POST['cat_id'];
$room_id = $_POST['room_id'];
//$output="";

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


$result = mysqli_query($conn_1,"SELECT * FROM hostel_rooms WHERE block_id = '$cat_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['bedspace'];

      //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
                hostel_site='$site_id',
                hostel_categories='$cat_id',
                room_id='$room_id',
                status='0',
                chk_in_date_time=NOW() WHERE student_id='$user_id'
              ") or die (mysqli_error($conn_1));
              if ($update) {
                $output= '
                <div class="alert alert-success" role="alert">Student Allocation Edited Successful!</div>
                <br><br><br>';
              }else {
                $output= '
                <div class="alert alert-danger" role="alert">Error in updating record</div>
                <br><br><br>';
              }

            }
          }
        }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($conn_1));
          if ($delete) {
            $insert = mysqli_query($conn_1, "INSERT INTO hostel_allocation(campus_id,block_id,room_id,student_id,status,chk_in_date_time)
            VALUES('$site_id','$cat_id','$room_id','$student_id','0',NOW())") or die(mysql_error($conn_1));
            if($insert){

              mysqli_query($conn_1, "UPDATE students_nce SET hostel_allocation = '1', hostel_eligibility='1' WHERE id ='$student_id' ") or die(mysqli_error($conn_1));

              $output= '
              <div class="alert alert-success" role="alert">Student Allocation Edited 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;
}








   ?>
