<?php
  $currentDate = date("Y-m-d");

  if(empty($_GET['date'])){header("Location: ?pagecontent=housekeep_room_list&date=$currentDate");}
?>
        <!-- page content -->
        <div class="right_col" role="main">
          <div class="">
            <div class="page-title">
              <div class="title_left">
                <h3>Housekeeping Schedule</h3>
              </div>

              <div class="title_right">
                <div class="col-md-5 col-sm-5 col-xs-12 form-group pull-right top_search">
                  <div class="input-group">
                    <input type="text" class="form-control" placeholder="Search for...">
                    <span class="input-group-btn">
                      <button class="btn btn-default" type="button">Go!</button>
                    </span>
                  </div>
                </div>
              </div>
            </div>

            <div class="clearfix"></div>

            <div class="row">
              <div class="col-md-6 col-sm-6 col-xs-12">
                <div class="x_panel">
                  <div class="x_title">
                    <h2>SOLSTICE SERVICE APARTMENT</h2>
                    <div class="clearfix"></div>
                  </div>
                  <div class="x_content">
                    <table class="table table-hover">
                      <thead>
                        <tr>
                          <th>#</th>
                          <th>Property</th>
                          <th>Room No</th>
                          <th>Action</th>
                        </tr>
                      </thead>
                      <tbody>
						  <?php
                $no = 0;
						  	$result = mysqli_query($conn, "SELECT * FROM rooms WHERE r_status=1 AND r_profit_type=2");

						  	while($row = mysqli_fetch_assoc($result)){
                                // if(filterRoomAdmin($row['r_id'],$_SESSION['admin']['a_id'])==true){continue;};
                                ?>

                        <tr>
                          <th scope="row"><?php echo ++$no; ?></th>
                          <td><?php echo roomNo($row['r_id'])['pty_title']; ?></td>
                          <td><?php echo roomNo($row['r_id'])['r_no']; ?></td>
                          <td>

							  <?php

                $getDate = $currentDate;

								$room_id = $row['r_id'];
							  
								$resultIsRequested = mysqli_query($conn, "SELECT * FROM housekeep WHERE hk_date='$getDate' AND r_id=$room_id");

								$rowNumIsRequested = mysqli_num_rows($resultIsRequested);

								if($rowNumIsRequested >= 1){
																	  ?>
                  <?php
                    // Try to get the rv_id for the housekeeping reservation for this room and date
                    $rv_id = '';
                    $rvQuery = mysqli_query($conn, "SELECT rv_id FROM housekeep WHERE r_id=$room_id AND hk_date='$getDate'");
                    if($rvRow = mysqli_fetch_assoc($rvQuery)) {
                      $rv_id = $rvRow['rv_id'];
                    }
                  ?>
                  <?php if ($rv_id) { ?>
                    <a href="?pagecontent=housekeep_room_list&housekeep_unrequest&rv_id=<?php echo $rv_id; ?>"><button class="btn btn-success btn-xs">REQUESTED</button></a>
                  <?php } ?>

							  <?php }else{ ?>

							  <a href="?pagecontent=housekeep_room_list&housekeep_request=<?php echo $row['r_id']; ?>&get_date=<?php echo $getDate; ?>"><button class="btn btn-default btn-xs">REQUEST</button></a></td>

							<?php } ?>
                        </tr>
						  <?php } 

                if(isset($_REQUEST['housekeep_request'])){
                  $room_id = $_REQUEST['housekeep_request'];
                  $admin_id = isset($_SESSION['admin']['a_id']) ? $_SESSION['admin']['a_id'] : 0;

                  //mysqli_query($conn, "INSERT INTO housekeep (r_id, hk_date, a_id) VALUES ($room_id, '$getDate', $admin_id)");

                  $checkOutDate = date("Y-m-d");
                  $checkInDate = addDaysToDate($checkOutDate,1,"-");

                  if(mysqli_query($conn, "INSERT INTO reservation (r_id, rv_guest, rv_ci_date, rv_co_date, rv_night, rv_platform, rv_notes, rv_total,a_id,rv_status) VALUES ($room_id,'Guest','$checkInDate', '$checkOutDate',1,1,'Housekeeping',40,$admin_id,2)")){
                    $getLastRVRecordId = getLastRecord('reservation','rv_id')['rv_id'];
                    //sendMessage2(26236173,$getLastRVRecordId);
                    header("Location: ?pagecontent=reservation_status&id=$getLastRVRecordId&status=2");
                  }
                }

                // Logic for unrequest housekeeping
                if(isset($_REQUEST['housekeep_unrequest']) && isset($_REQUEST['rv_id'])){
                  // Only process unrequest if rv_id is provided
                  $rv_id = $_REQUEST['rv_id'];
                  // Delete housekeeping request and reservation using rv_id only
                  mysqli_query($conn, "DELETE FROM housekeep WHERE rv_id=$rv_id");
                  mysqli_query($conn, "DELETE FROM reservation WHERE rv_id=$rv_id");
                  // Redirect to the main list with the current date
                  header("Location: ?pagecontent=housekeep_room_list&date=$currentDate");
                  exit;
                }

						  ?>
                      </tbody>
                    </table>

                  </div>
                </div>
              </div>
            </div>
          </div>

        </div>

        <!-- /page content -->

