public function updateReservation(Request $request)
 {
     $check = $this->checkAvailability($request);
     if (!$check) {
         return 0;
     }
     if (session_status() == PHP_SESSION_NONE) {
         session_start();
     }
     $usern = $_SESSION['username'];
     $un = DB::table('user_table')->where('username', "{$usern}")->first();
     $userid = $un->id;
     $res = DB::table('reservation_table')->where('user_id', $userid)->first();
     if (!$res) {
         return 5;
     }
     $form = new formaction();
     // make checkin and checkout dates
     $checkin = $form->make_date($request->dayin, $request->monthin);
     $checkout = $form->make_date($request->dayout, $request->monthout);
     $today = date("Y-m-d");
     $single = $request->single;
     $double = $request->double;
     $tripple = $request->tripple;
     $this->validate($request, ['single' => 'required | numeric', 'double' => 'required | numeric', 'tripple' => 'required | numeric']);
     if ($single == 0 && $double == 0 && $tripple == 0) {
         return 3;
     }
     // get user id
     $username = $_SESSION['username'];
     $u = DB::table('user_table')->where('username', "{$username}")->first();
     $id = $u->id;
     //update info in reservation table
     $r = DB::table('reservation_table')->where('user_id', $id)->first();
     if ($form->checkReservednow($r)) {
         return 4;
     }
     DB::table('reservation_table')->where('user_id', $id)->update(['checkin' => "{$checkin}", 'checkout' => "{$checkout}", 'reservation_date' => "{$today}", 'checked' => 0]);
     //get reservation_id
     if ($r) {
     } else {
         return 2;
     }
     $reserve_id = $r->id;
     // update the rooms in table_reserved_rooms
     //$rr = DB::table('table_reserved_rooms')->where('reservation_id',$reserve_id)->get();
     if ($single > 0) {
         $x1 = DB::table('table_reserved_rooms')->where('reservation_id', $reserve_id)->where('room_type', 1)->first();
         if ($x1) {
             DB::table('table_reserved_rooms')->where('reservation_id', $reserve_id)->where('room_type', 1)->update(['num_rooms' => $single]);
         } else {
             DB::table('table_reserved_rooms')->insert(['reservation_id' => $reserve_id, 'num_rooms' => $single, 'room_type' => 1]);
         }
     } else {
         if ($single == 0) {
             DB::table('table_reserved_rooms')->where('reservation_id', $reserve_id)->where('room_type', 1)->delete();
         }
     }
     if ($double > 0) {
         $x2 = DB::table('table_reserved_rooms')->where('reservation_id', $reserve_id)->where('room_type', 2)->first();
         if ($x2) {
             DB::table('table_reserved_rooms')->where('reservation_id', $reserve_id)->where('room_type', 2)->update(['num_rooms' => $double]);
         } else {
             DB::table('table_reserved_rooms')->insert(['reservation_id' => $reserve_id, 'num_rooms' => $double, 'room_type' => 2]);
         }
     } else {
         if ($double == 0) {
             DB::table('table_reserved_rooms')->where('reservation_id', $reserve_id)->where('room_type', 2)->delete();
         }
     }
     if ($tripple > 0) {
         $x3 = DB::table('table_reserved_rooms')->where('reservation_id', $reserve_id)->where('room_type', 3)->first();
         if ($x3) {
             DB::table('table_reserved_rooms')->where('reservation_id', $reserve_id)->where('room_type', 3)->update(['num_rooms' => $tripple]);
         } else {
             DB::table('table_reserved_rooms')->insert(['reservation_id' => $reserve_id, 'num_rooms' => $tripple, 'room_type' => 3]);
         }
     } else {
         if ($tripple > 0) {
             DB::table('table_reserved_rooms')->where('reservation_id', $reserve_id)->where('room_type', 3)->delete();
         }
     }
     $form->change_room_status();
     return 1;
 }
 public function checkAvailability(Request $request)
 {
     // same operation for each room type
     // get all empty rooms  -- 1
     // get all rooms that will checkout before my chech in date -- 2
     // add 1 and 2 -- 2.5
     // get all check in between the range of my in and out dates -- 3
     // get all who chech in and end after my check in date -- 4 not sure will not do
     // get all who will checkin between today and my checkin
     // 2.5 - (3 + 4)
     $single_room = -1;
     $double_room = -1;
     $triple_room = -1;
     $form = new formaction();
     $checkin = $form->make_date($request->dayin, $request->monthin);
     $checkout = $form->make_date($request->dayout, $request->monthout);
     $today = date("Y-m-d");
     $form->change_room_status();
     $diff_now = DB::select(DB::raw("SELECT DATEDIFF('{$checkin}','{$today}') AS day"));
     if ($diff_now[0]->day < 0) {
         return 0;
     }
     $diff_co = DB::select(DB::raw("SELECT DATEDIFF('{$checkout}','{$checkin}') AS day"));
     if ($diff_co[0]->day < 0) {
         return 0;
     }
     if ($request->single > 0) {
         // getting all empty rooms
         $sroom = DB::table('table_rooms')->where('room_type_id', 1)->where('room_state', 1)->get();
         // getting all who will checkout between today and my checkin
         $unsroom = DB::table('reservation_table')->whereBetween("checkout", array("{$today}", "{$checkin}"))->get();
         // add 1 and 2 which is empty rooms and rooms that will checkout before my checkin
         $totalEmpty = sizeof($sroom) + $form->get_room(1, $unsroom);
         // getting checkin between my checkin and checkout
         //$broom = DB::table('reservation_table')->where('checkin','>',"$checkin",'and','checkin','<=',"$checkout")->get();
         $broom = DB::select(DB::raw("select * from reservation_table where checkin > '{$checkin}' and checkin <= '{$checkout}' "));
         $btw_io = $form->get_room(1, $broom);
         //echo $btw_io;
         //getting all who will checkin between today and my checkin
         $btwroom = DB::table('reservation_table')->whereBetween("checkin", array("{$today}", "{$checkin}"))->get();
         $btwroom_s = $form->get_room(1, $btwroom);
         //echo $btwroom_s;
         // calculate total empty rooms
         //$room = $totalEmpty - ($btw_io + $btwroom_s);
         $single_room = $totalEmpty - ($btw_io + $btwroom_s);
         //echo $single_room;
         //echo $room;
     } else {
         $single_room = -2;
     }
     if ($request->double > 0) {
         // getting all empty rooms
         $sroom = DB::table('table_rooms')->where('room_type_id', 2)->where('room_state', 1)->get();
         // getting all who will checkout between today and my checkin
         $unsroom = DB::table('reservation_table')->whereBetween("checkout", array("{$today}", "{$checkin}"))->get();
         // add 1 and 2 which is empty rooms and rooms that will checkout before my checkin
         $totalEmpty = sizeof($sroom) + $form->get_room(2, $unsroom);
         // getting checkin between my checkin and checkout
         $broom = DB::select(DB::raw("select * from reservation_table where checkin > '{$checkin}' and checkin <= '{$checkout}' "));
         $btw_io = $form->get_room(2, $broom);
         // echo $btw_io;
         //getting all who will checkin between today and my checkin
         $btwroom = DB::table('reservation_table')->whereBetween("checkin", array("{$today}", "{$checkin}"))->get();
         $btwroom_s = $form->get_room(2, $btwroom);
         //echo $btwroom_s;
         // calculate total empty rooms
         //$room = $totalEmpty - ($btw_io + $btwroom_s);
         $double_room = $totalEmpty - ($btw_io + $btwroom_s);
         //echo $double_room;
         //echo $room;
     } else {
         $double_room = -2;
     }
     if ($request->tripple > 0) {
         // getting all empty rooms
         $sroom = DB::table('table_rooms')->where('room_type_id', 3)->where('room_state', 1)->get();
         // getting all who will checkout between today and my checkin
         $unsroom = DB::table('reservation_table')->whereBetween("checkout", array("{$today}", "{$checkin}"))->get();
         // add 1 and 2 which is empty rooms and rooms that will checkout before my checkin
         $totalEmpty = sizeof($sroom) + $form->get_room(3, $unsroom);
         // getting checkin between my checkin and checkout
         $broom = DB::select(DB::raw("select * from reservation_table where checkin > '{$checkin}' and checkin <= '{$checkout}' "));
         $btw_io = $form->get_room(3, $broom);
         // echo $btw_io;
         //getting all who will checkin between today and my checkin
         $btwroom = DB::table('reservation_table')->whereBetween("checkin", array("{$today}", "{$checkin}"))->get();
         $btwroom_s = $form->get_room(3, $btwroom);
         //echo $btwroom_s;
         // calculate total empty rooms
         //$room = $totalEmpty - ($btw_io + $btwroom_s);
         $triple_room = $totalEmpty - ($btw_io + $btwroom_s);
         //echo $room;
     } else {
         $triple_room = -2;
     }
     //var_dump();
     $room_av = array($single_room, $double_room, $triple_room);
     $req_av = array((int) $request->single, (int) $request->double, (int) $request->tripple);
     for ($i = 0; $i < 3; $i++) {
         if ($room_av[$i] != -2) {
             if (!($room_av[$i] >= $req_av[$i])) {
                 return 0;
             }
         }
     }
     return 1;
 }