<?php 
	$_GET['date'] ? $getDate = $_GET['date'] : header("Location: ?pagecontent=report_availability&date=".date("Y-m-d")); ?>

<div class="right_col" role="main">
	<div class="">
        <div class="page-title">
            <div class="title_left">
				<h3>Cleaning List</h3>
				
				<a href="?pagecontent=cleaninglist&date=<?php echo addDaysToDate($getDate,1,"-"); ?>">	<button type="button" class="btn btn-dark">Yesterday</button></a>
		  
				<a href="?pagecontent=cleaninglist"><button type="button" class="btn btn-dark">Today</button></a>

				<a href="?pagecontent=cleaninglist&date=<?php echo addDaysToDate($getDate,1,"+"); ?>"><button type="button" class="btn btn-dark">Next Day</button></a>
				
				<a href="?pagecontent=cleaninglist&refresh=y&date=<?php echo $getDate; ?>"><button type="button" class="btn btn-dark">Refresh Telegram</button></a>

				<form method="get">
						<input type="text" name="pagecontent" value="cleaninglist" hidden="">
						<input class="form-control col-md-2" name="date" type="date" value="<?php echo date("Y-m-d"); ?>">
						<input type="submit" class="btn btn-primary" value="Search">
				</form>
				
				
            </div>

            <div class="title_right">
				<form method="get">
					<input type="text" name="pagecontent" value="cleaninglist" hidden="">
					<div class="col-md-5 col-sm-5 col-xs-12 form-group pull-right top_search">
						<div class="input-group">
							<?php $today = date("Y-m-d");?>
							<input type="date" class="form-control" name="date" value="<?php echo $today;?>">
							<span class="input-group-btn">
								<button id="send" class="btn btn-dark" type="submit">Go!</button>
							</span>
						</div>
					</div>
				</form>
            </div>
        </div>
	</div>
		<div class="clearfix"></div>
		<div class="row">
			<div class="col-md-12 col-sm-12 col-xs-12">
				<div class="x_panel">
					<div class="x_content">
						<table id="datatable" class="table table-striped table-bordered">
							<thead>
								<tr>
									<th>No Room</th>
									<th>Platform</th>
									<th>CO Date/Time</th>
									<th>Guest</th>
									<th>Action</th>
									<th>Bedsheet</th>
									<th>Status</th>
									<th>Collected</th>
									<th>In Charge</th>
								</tr>
							</thead>
							<tbody>
								<?php
									if(isset($_GET['search'])){ $keyword = $_GET['search']; }
									
									if(isset($_GET['date'])){
										$getDate = $_GET['date'];
									}else{
										$getDate = date('Y-m-d');
										header('Location: index.php?pagecontent=cleaninglist&date='.$getDate);
									}
									
									$result = mysqli_query($conn, "SELECT * FROM housekeep AS HK LEFT JOIN rooms AS R ON HK.r_id=R.r_id WHERE HK.hk_date='$getDate' OR HK.hk_date<='$getDate' AND HK.hk_status<9 ORDER BY R.p_id ASC, R.r_no ASC");
								
									//$result = mysqli_query($conn, "SELECT * FROM housekeep AS HK LEFT JOIN rooms AS R ON HK.r_id=R.r_id WHERE HK.hk_date='$getDate' ORDER BY R.p_id ASC") or die (mysql_error());
								
									while($row = mysqli_fetch_array($result))
									{
								?>
								<tr>
									<td>
										<?php echo getDetailFromTable('properties',getDetailFromTable('rooms',$row['r_id'])['p_id'])['pty_code']." ".getDetailFromTable('rooms',$row['r_id'])['r_no'];?>
									</td>
									<td><?php echo platformTitle(getDetailFromTable('reservation',$row['rv_id'])['rv_platform']);?></td>
									<td><?php 
										echo getDetailFromTable('reservation',$row['rv_id'])['rv_co_date'].' / '.date_format(date_create(getDetailFromTable('reservation',$row['rv_id'])['rv_co_time']),"ha");
										?>
									</td>
									<td>
										<a href="?pagecontent=reservation_communication&rv=<?php echo $row['rv_id'];?>"><?php echo getDetailFromTable('reservation',$row['rv_id'])['rv_guest'];?></a>
										
									<?php if(getDetailFromTable('reservation',$row['rv_id'])['rv_respondio_id']){ ?>
										<a href="https://app.respond.io/space/80806/message/<?php echo getDetailFromTable('reservation',$row['rv_id'])['rv_respondio_id']; ?>" target="_blank"><button class="btn btn-primary btn-xs">Respondio</button></a>
									<?php } ?>
										
									<?php if(getDetailFromTable('reservation',$row['rv_id'])['rv_airbnb_id']){ ?>
										<a href="http://airbnb.com/hosting/inbox/folder/all/thread/<?php echo getDetailFromTable('reservation',$row['rv_id'])['rv_airbnb_id']; ?>" target="_blank"><button class="btn btn-danger btn-xs">Airbnb</button></a>
									<?php } ?>
										
									<?php if(getDetailFromTable('reservation',$row['rv_id'])['rv_contact']){ ?>
										<a href="https://web.whatsapp.com/send?phone=<?php echo getDetailFromTable('reservation',$row['rv_id'])['rv_contact']; ?>" target="_blank"><button class="btn btn-success btn-xs">Whatsapp</button></a>
									<?php } ?>
									
									</td>
									
									<td>
										<a href="https://admin.homestay.dzvisual.my/production/index.php?pagecontent=reservation_new&name=<?php echo getDetailFromTable('reservation',$row['rv_id'])['rv_guest'];?>&contact=<?php echo getDetailFromTable('reservation',$row['rv_id'])['rv_contact'];?>&email=&room=<?php echo getDetailFromTable('reservation',$row['rv_id'])['r_id'];?>&date=<?php echo getDetailFromTable('reservation',$row['rv_id'])['rv_co_date'];?>&price=<?php echo getDetailFromTable('reservation',$row['rv_id'])['rv_total'];?>" target="_blank"><button class="btn btn-warning btn-xs">Extend</button></a>	
									</td>
									
									<td><a href="?pagecontent=cleaninglist&date=<?php echo $getDate; ?>&hk=<?php echo $row['hk_id']; ?>&bedsheet=<?php echo $row['hk_bedsheet']; ?>"><?php if($row['hk_bedsheet']==1){echo "<span style='color:blue; font-weight:bold;'>CHANGED</span>";}else{echo "<span style='color:red; font-weight:bold;'>UNCHANGED</span>";};?></a></td>
									
									<td>
										<a href="?pagecontent=housekeep_cleaning_update&hk=<?php echo $row['hk_id']; ?>">
										<?php	switch($row['hk_status'])
											{
												case "0": echo '<span style="color:red;font-weight:bold;">STAYING</span>';
												break;
												case "1": echo '<span style="color:red;font-weight:bold;">DOOR LOCKED</span>';
												break;
												case "2": echo '<span style="color:blue;font-weight:bold;">CHECKED OUT</span>';
												break;
												case "3": echo '<span style="color:blue;font-weight:bold;">KEY COLLECTED</span>';
												break;
												case "4": echo '<span style="color:red;font-weight:bold;">NO KEY</span>';
												break;
												case "5": echo '<span style="color:red;font-weight:bold;">SO MESSY</span>';
												break;
												case "6": echo '<span style="color:red;font-weight:bold;">EXTEND?</span>';
												break;
												case "7": echo '<span style="color:blue;font-weight:bold;">CLEANING</span>';
												break;
												case "8": echo '<span style="color:red;font-weight:bold;">WILL CONTINUE</span>';
												break;
												case "9": echo '<span style="color:blue;font-weight:bold;">NO MESSY</span>';
												break;
												case "10": echo '<span style="color:black;font-weight:bold;">EXTENDING</span>';
												break;
												case "11": echo '<span style="color:black;font-weight:bold;">COMPLETED</span>';
												break;
												case "12": echo '<span style="color:black;font-weight:bold;">POSTPONED</span>';
												break;
												default: echo '<span style="color:black;font-weight:bold;">Check Out</span>';
											}
										;?>
											</a>
									</td>
									<td>RM <?php echo $row['hk_payment'] ? $row['hk_payment'] : "0.00";?></td>
									<td>
										<?php
											$rowHousekeepId = $row['hk_id'];
											$resultUserStaff = mysqli_query($conn,"SELECT * FROM housekeep_staff WHERE hk_id=$rowHousekeepId");
												while($rowUserStaff = mysqli_fetch_assoc($resultUserStaff)){
													echo getDetailFromTable('staffs',$rowUserStaff['s_id'])['s_nickname'];
													$housekeepStaffId = $rowUserStaff['hks_id'];
													
													echo " <a href='?pagecontent=cleaninglist&date=$getDate&remove=$housekeepStaffId'><button class='btn btn-xs btn-danger'>X</button></a>";
													echo "<br>";
												}
										?>
									<form method="post">
										<select name="staffId">
											<?php
												$resultUserStaff2 = mysqli_query($conn,"SELECT * FROM staffs WHERE s_status=1");
												while($rowUserStaff2 = mysqli_fetch_assoc($resultUserStaff2)){
													?><option value="<?php echo $rowUserStaff2['s_id']; ?>"><?php echo $rowUserStaff2['s_nickname']; ?></option><?php
												}
											?>
										</select>
										<input type="text" name="housekeepId" value="<?php echo $row['hk_id']; ?>" hidden>
										<input type="submit" name="btnStaff<?php echo $row['hk_id']; ?>" value="Add">
										</form>
										<?php
											if(isset($_POST['btnStaff'.$row['hk_id']])){
												$staffId = $_POST['staffId'];
												$housekeepId = $_POST['housekeepId'];
												$resultUserStaff = mysqli_query($conn,"INSERT INTO housekeep_staff (hk_id,s_id) VALUES ($housekeepId,$staffId)");
												header("Location: ?pagecontent=cleaninglist&date=$getDate");
											}
										?>
									</td>
								</tr>
								<?php
									}
								?>
						</table>
					</div>
				</div>
			</div>
        </div>
    </div>
</div>

<?php
	if(isset($_REQUEST['remove'])){
		
		$housekeepStaffId = $_GET['remove'];
		$date = $_GET['date'];
		
		mysqli_query($conn,"DELETE FROM housekeep_staff WHERE hks_id=$housekeepStaffId");
		header('Location: ?pagecontent=cleaninglist&date='.$date);  
	}

	if(isset($_REQUEST['bedsheet'])){
		
		$housekeepId = $_GET['hk'];
		$status = $_GET['bedsheet'];
		
		if($status == 0){$status = 1;}else{$status = 0;}
		
		$date = $_GET['date'];
		
		mysqli_query($conn,"UPDATE housekeep SET hk_bedsheet=$status WHERE hk_id=$housekeepId");
		header('Location: ?pagecontent=cleaninglist&date='.$date);
		
		telegramHousekeepingUpdate($getDate);
	}

	if(isset($_REQUEST['refresh'])){

		if(isMobile()){
			header("Location: https://t.me/joinchat/P-rPsh1-1VUJBji3");}else{
			header('Location: ?pagecontent=cleaninglist&date='.$getDate);}
		
		telegramHousekeepingUpdate($getDate);
	}

	if(isset($_POST['submit']))
	{
		$search_btn=$_POST['searchdate'];
		
		header('Location: index.php?pagecontent=cleaninglist&search='.$search_btn); 
	}
?>