<?php 
  error_reporting(E_ALL);
  date_default_timezone_set('ASIA/Kuala_Lumpur'); 
	//sendMessage3(26236173,date('Y-m-d').' '.date('H:ia'));
$getHousekeepId = $_GET['hk'];
$getRoomId = getDetailByTable('housekeep',$getHousekeepId)['r_id'];//$_GET['room'];
$getDate = getDetailByTable('housekeep',$getHousekeepId)['hk_date']; //$_GET['date'];
$getReservationId = getDetailByTable('housekeep',$getHousekeepId)['rv_id']; //$_GET['date'];
$getRespondioId = getDetailByTable('reservation',$getReservationId)['rv_respondio_id']; //$_GET['date'];
$todayDate = date("Y-m-d"); //$_GET['date'];
?>
        <!-- page content -->
        <div class="right_col" role="main">
          <div class="">
            <div class="page-title">
              <div class="title_left">
                <h3><?php echo getDetailByTable('properties',getDetailByTable('rooms',$getRoomId)['p_id'])['pty_short']." ".getDetailByTable('rooms',$getRoomId)[1]; ?> - Housekeeping Schedule</h3>
              </div>

            </div>

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

            <div class="row">

              <?php if(isset($_GET['report'])){ ?>
              <div class="col-md-6 col-sm-6 col-xs-12">
                <div class="x_panel">
                  <div class="x_title">
                    <h2>LOST/DAMAGE REPORT</h2>
                    <div class="clearfix"></div>
                  </div>
                  <div class="x_content">
                    <?php
                      $resultReport=mysqli_query($conn, "SELECT * FROM room_report WHERE r_id=$getRoomId ORDER BY rr_id DESC");
                      while($rowReport = mysqli_fetch_array($resultReport)){
                    ?>
					          <a href="?pagecontent=housekeep_cleaning_update&hk=<?php echo $getHousekeepId; ?>&status=0"><button class="btn btn-default btn-block"><?php $rowReport['rr_title']; ?></button></a>
                    <?php } ?>
                  </div>
                  <div class="x_content">
                    <form method="post" id="demo-form2" data-parsley-validate class="form-horizontal form-label-left">

                      <div class="form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">Text</label>
                        <div class="col-md-9 col-sm-9 col-xs-12">
                          <input type="text" name="title" id="first-name" required="required" class="form-control col-md-7 col-xs-12">
                        </div>
                      </div>
                      <div class="ln_solid"></div>
                      <div class="form-group">
                        <div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
                          <input class="btn btn-primary btn-block" type="submit" name="btnReport" value="SUBMIT">
                        </div>
                      </div>

                    </form>
                    <?php
                      if(isset($_POST['btnReport'])){
                        $postDate = date('Y-m-d');
                        $postTitle = $_POST['title'];
                        $postDescription = urlencode($_POST['description']);
                        
                        $sessionAdminId = $_SESSION['admin'][0];
                        
                        mysqli_query($conn, "INSERT INTO room_report (r_id, rr_date, rr_title, rr_description, a_id)
                        VALUES ($getRoomId, '$postDate', '$postTitle', '$postDescription', $sessionAdminId)");
                      
                        header("Location: ?pagecontent=housekeep_cleaning_update&hk=$getHousekeepId");
                      }
                    ?>
                  </div>
                </div>
              </div>
              <?php } ?>

              <div class="col-md-6 col-sm-6 col-xs-12">
                <div class="x_panel">
                  <div class="x_title">
                    <h2>ENTRANCE</h2>
                    <div class="clearfix"></div>
                  </div>
                  <div class="x_content">
					  <a href="?pagecontent=housekeep_cleaning_update&hk=<?php echo $getHousekeepId; ?>&status=0"><button class="btn btn-danger btn-block">STAYING</button></a>
					  
					  <a href="?pagecontent=housekeep_cleaning_update&hk=<?php echo $getHousekeepId; ?>&status=1"><button class="btn btn-danger btn-block">DOOR LOCKED</button></a>
					  
					  <a href="?pagecontent=housekeep_cleaning_update&hk=<?php echo $getHousekeepId; ?>&status=2"><button class="btn btn-primary btn-block">CHECKED OUT</button></a>
					  
					  <a href="?pagecontent=housekeep_cleaning_update&hk=<?php echo $getHousekeepId; ?>&status=5"><button class="btn btn-danger btn-block">SO MESSY!!</button></a>
					  
					  <a href="?pagecontent=housekeep_cleaning_update&hk=<?php echo $getHousekeepId; ?>&status=6"><button class="btn btn-warning btn-block">EXTEND??</button></a>
					  
					  <a href="?pagecontent=housekeep_cleaning_update&hk=<?php echo $getHousekeepId; ?>&status=7"><button class="btn btn-success btn-block">CLEANING 🚹</button></a>
					  
					  <a href="?pagecontent=housekeep_cleaning_update&hk=<?php echo $getHousekeepId; ?>&status=8"><button class="btn btn-warning btn-block">PAUSE</button></a>
                  </div>
                </div>
              </div>

              <div class="col-md-6 col-sm-6 col-xs-12">
                <div class="x_panel">
                  <div class="x_title">
                    <h2>KEY STATUS</h2>
                    <div class="clearfix"></div>
                  </div>
                  <div class="x_content">
					  <a href="?pagecontent=housekeep_cleaning_update&hk=<?php echo $getHousekeepId; ?>&status=3"><button class="btn btn-success btn-block">KEY COLLECTED 🗝✅</button></a>
					  <a href="?pagecontent=housekeep_cleaning_update&hk=<?php echo $getHousekeepId; ?>&status=4"><button class="btn btn-danger btn-block">NO KEY FOUND 🗝🚨</button></a>
                  </div>
                </div>
              </div>

              <div class="col-md-6 col-sm-6 col-xs-12">
                <div class="x_panel">
                  <div class="x_title">
                    <h2>LOST/DAMAGE</h2>
                    <div class="clearfix"></div>
                  </div>
                  <div class="x_content">
					  <a href="?pagecontent=housekeep_cleaning_update&hk=<?php echo $getHousekeepId; ?>&report=y"><button class="btn btn-danger btn-block">🚨🚨 REPORT 🚨🚨</button></a>
                  </div>
                </div>
              </div>

              <div class="col-md-6 col-sm-6 col-xs-12">
                <div class="x_panel">
                  <div class="x_title">
                    <h2>SHORTAGE</h2>
                    <div class="clearfix"></div>
                  </div>
                  <div class="x_content">
					  <a href="?pagecontent=housekeep_cleaning_update&hk=<?php echo $getHousekeepId; ?>&shortage=y"><button class="btn btn-warning btn-block">⚠️⚠️ UPDATE ⚠️⚠️</button></a>
                  </div>
                </div>
              </div>

              <div class="col-md-6 col-sm-6 col-xs-12">
                <div class="x_panel">
                  <div class="x_title">
                    <h2>BEDSHEET STATUS</h2>
                    <div class="clearfix"></div>
                  </div>
                  <div class="x_content">
					  <a href="?pagecontent=housekeep_cleaning_update&hk=<?php echo $getHousekeepId; ?>&bedsheet=1"><button class="btn btn-success btn-block">CHANGED 🛏✨</button></a>
					  <a href="?pagecontent=housekeep_cleaning_update&hk=<?php echo $getHousekeepId; ?>&bedsheet=0"><button class="btn btn-danger btn-block">UNCHANGED 🛏💩</button></a>
                  </div>
                </div>
              </div>

              <div class="col-md-6 col-sm-6 col-xs-12">
                <div class="x_panel">
                  <div class="x_title">
                    <h2>COMMUNICATION</h2>
                    <div class="clearfix"></div>
                  </div>
                  <div class="x_content">
					  
					  <a href="?pagecontent=reservation_communication&rv=<?php echo $getReservationId; ?>"><button class="btn btn-success btn-block">COMMUNICATION</button></a>
					  
					  <a href="https://wa.me/<?php echo getDetailByTable('reservation',$getReservationId)['rv_contact']; ?>"><button class="btn btn-success btn-block">DIRECT WHATSAPP</button></a>
					  
					  <button class="btn btn-primary  btn-block" onclick="navigator.clipboard.writeText('https://web.whatsapp.com/send?phone=<?php echo getDetailByTable('reservation',$getReservationId)['rv_contact']; ?>&text&app_absent=0')">COPY WS LINK</button>
					  
					  <?php if($getRespondioId){ ?>
					  <a href="https://app.respond.io/space/80806/message/<?php echo getDetailByTable('reservation',$getReservationId)['rv_respondio_id']; ?>" target="_blank"><button class="btn btn-success btn-block">RESPOND.IO</button></a>
					  <?php } ?>
					  
                  </div>
                </div>
              </div>
				
              <div class="col-md-6 col-sm-6 col-xs-12">
                <div class="x_panel">
                  <div class="x_title">
                    <h2>PAYMENT COLLECTION</h2>
                    <div class="clearfix"></div>
                  </div>
                  <div class="x_content">
                    <form method="post" id="demo-form2" data-parsley-validate class="form-horizontal form-label-left">

                      <div class="form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">RM <span class="required">*</span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                          <input type="text" name="payment" id="first-name" required="required" class="form-control col-md-7 col-xs-12">
                        </div>
                      </div>
                      <div class="ln_solid"></div>
                      <div class="form-group">
                        <div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
                          <input class="btn btn-primary btn-block" type="submit" name="btnPayment" value="COLLECT">
                        </div>
                      </div>

                    </form>
					  <?php
					  	if(isset($_POST['btnPayment'])){
							$payment = $_POST['payment'];
							mysqli_query($conn,"UPDATE housekeep SET hk_payment=$payment WHERE hk_id=$housekeepId");
							
							header("Location: ?pagecontent=housekeep_cleaning_update&hk=$getHousekeepId&room=$getRoomId&date=$getDate");
						}
					  ?>
                  </div>
                </div>
              </div>

              <div class="col-md-6 col-sm-6 col-xs-12">
                <div class="x_panel">
                  <div class="x_title">
                    <h2>CLEANER</h2>
                    <div class="clearfix"></div>
                  </div>
                  <div class="x_content">
					  <?php
							$resultHousekeepStaff = mysqli_query($conn,"SELECT * FROM staffs WHERE s_status=1");
							while($rowHousekeepStaff = mysqli_fetch_assoc($resultHousekeepStaff)){
								
								if(isStaffSelected($getHousekeepId,$rowHousekeepStaff['s_id'])){
									$husekeepStaffId = isStaffSelected($getHousekeepId,$rowHousekeepStaff['s_id'])['hks_id'];
									?>
					  				<a a href="?pagecontent=housekeep_cleaning_update&hk=<?php echo $getHousekeepId; ?>&removeHks=<?php echo $husekeepStaffId; ?>"><button class="btn btn-primary btn-block"><?php echo $rowHousekeepStaff['s_nickname']; ?></button></a>
					  				<?php
								}else{
									?>
					  				<a a href="?pagecontent=housekeep_cleaning_update&hk=<?php echo $getHousekeepId; ?>&cleaner=<?php echo $rowHousekeepStaff['s_id']; ?>"><button class="btn btn-default btn-block"><?php echo $rowHousekeepStaff['s_nickname']; ?></button></a>
					  				<?php
								}
							} ?>
                  </div>
                </div>
              </div>

              <div class="col-md-6 col-sm-6 col-xs-12">
                <div class="x_panel">
                  <div class="x_title">
                    <h2>COMPLETION</h2>
                    <div class="clearfix"></div>
                  </div>
                  <div class="x_content">
					  <a href="?pagecontent=housekeep_cleaning_update&hk=<?php echo $getHousekeepId; ?>&status=9"><button class="btn btn-success btn-block">NO MESSY ❤️</button></a>
					  
					  <a href="?pagecontent=housekeep_cleaning_update&hk=<?php echo $getHousekeepId; ?>&status=10"><button class="btn btn-primary btn-block">EXTEND ❌</button></a>
					  
					  <a href="?pagecontent=housekeep_cleaning_update&hk=<?php echo $getHousekeepId; ?>&status=12"><button class="btn btn-danger btn-block">POSTPONE ❌</button></a>
                  </div>
                    <div class="clearfix"></div>
                  <div class="x_content">
					  
					  <a href="?pagecontent=housekeep_cleaning_update&hk=<?php echo $getHousekeepId; ?>&status=11"><button class="btn btn-primary btn-block">COMPLETE ✅</button></a>
                  </div>
                </div>
              </div>
				
            </div>
          </div>
        </div>
        <!-- /page content -->
<?php
	if(isset($_REQUEST['status'])){
		$housekeepId = $_GET['hk'];
		$status = $_GET['status'];
		
		$currentDateTime = date('Y-m-d H:i:s');
		if($status == 7){
			mysqli_query($conn,"UPDATE housekeep SET hk_start='$currentDateTime' WHERE hk_id=$housekeepId");
		}
		if($status == 11){
			mysqli_query($conn,"UPDATE housekeep SET hk_end='$currentDateTime' WHERE hk_id=$housekeepId");
		}
		
		
			$tomorrowDate = date('Y-m-d', strtotime($getDate." +1 day"));
		if($status == 12){
			$reservationId = getDetailByTable('housekeep',$housekeepId)['rv_id'];
			$roomId = getDetailByTable('housekeep',$housekeepId)['r_id'];
			$adminId = $_SESSION['admin'][0];
			
			mysqli_query($conn,"INSERT INTO housekeep (r_id,rv_id,hk_date,a_id) VALUES ($roomId,$reservationId,'$tomorrowDate',$adminId)");
		}
		
		
		if($status == 11 && getDetailByTable('housekeep',$housekeepId)['hk_start'] == NULL){
			mysqli_query($conn,"UPDATE housekeep SET hk_start='$currentDateTime' WHERE hk_id=$housekeepId");
		}
		
		mysqli_query($conn,"UPDATE housekeep SET hk_status=$status WHERE hk_id=$housekeepId");
		
		//header("Location: ?pagecontent=housekeep_cleaning_list&date=$getDate");
		if(isMobile()){
			header("Location: https://t.me/joinchat/P-rPsh1-1VUJBji3");}else{
			header("Location: ?pagecontent=housekeep_cleaning_list&date=$todayDate");
		}
		
		telegramHousekeepingUpdate($getDate);
	}

	if(isset($_REQUEST['bedsheet'])){
		
		$housekeepId = $_GET['hk'];
		$status = $_GET['bedsheet'];
		
		mysqli_query($conn,"UPDATE housekeep SET hk_bedsheet=$status WHERE hk_id=$housekeepId");
		
		if(isMobile()){
			header("Location: https://t.me/joinchat/P-rPsh1-1VUJBji3");}else{
			header("Location: ?pagecontent=housekeep_cleaning_list&date=$todayDate");
		}
		
		telegramHousekeepingUpdate($getDate);
	}

	if(isset($_REQUEST['cleaner'])){
		
		$staffId = $_GET['cleaner'];
		
		mysqli_query($conn,"DELETE FROM housekeep_staff WHERE hk_id=$getHousekeepId AND s_id=$staffId");
		mysqli_query($conn,"INSERT INTO housekeep_staff (hk_id,s_id) VALUES ($getHousekeepId,$staffId)");
		
		header("Location: ?pagecontent=housekeep_cleaning_update&hk=$getHousekeepId");
		
		telegramHousekeepingUpdate($getDate);
	}

	if(isset($_REQUEST['removeHks'])){
		$housekeepStaffId = $_GET['removeHks'];
		mysqli_query($conn,"DELETE FROM housekeep_staff WHERE hks_id=$housekeepStaffId");
		header("Location: ?pagecontent=housekeep_cleaning_update&hk=$getHousekeepId");
		telegramHousekeepingUpdate($getDate);
	}

function isStaffSelected($housekeepId,$staffId){ global $conn;
	$resultUserStaff = mysqli_query($conn,"SELECT * FROM housekeep_staff WHERE hk_id=$housekeepId AND s_id=$staffId");
	return mysqli_fetch_array($resultUserStaff);
}
?>