<?php
	$getStaffId = $_GET['staff'];
	$getMonthYear = $_GET['month'];
?>
<!-- page content -->
<div class="right_col" role="main">
  <div class="">
	<div class="page-title">
	  <div class="title_left">
		<h3><?php if($getStaffId){echo "Report for ".getDetailByTable('staffs',$getStaffId)['s_nickname'];} ?></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>Staff List</h2>
			  
			<ul class="nav navbar-right panel_toolbox">
			  <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
			  </li>
			  <li class="dropdown">
				<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-wrench"></i></a>
				<ul class="dropdown-menu" role="menu">
				  <li><a href="#">Settings 1</a>
				  </li>
				  <li><a href="#">Settings 2</a>
				  </li>
				</ul>
			  </li>
			  <li><a class="close-link"><i class="fa fa-close"></i></a>
			  </li>
			</ul>
			<div class="clearfix"></div>
		  </div>
		  <div class="x_content">
			<table class="table table-hover">
			  <thead>
				<tr>
				  <th>#</th>
				  <th>Staff</th>
				  <th>Total</th>
				  <th>Worked</th>
				  <th>Avg Daily</th>
				  <th>This Month</th>
				  <th>Past Month</th>
				</tr>
			  </thead>
			  <tbody>
				  <?php
					$result=mysqli_query($conn, "SELECT * FROM staffs") or die (mysql_error());
					while($row = mysqli_fetch_array($result))
					{
						$rowStaffId = $row['s_id'];
				  ?>
				<tr>
				  <th><?php echo ++$no; ?></th>
				  <td><a href="?pagecontent=housekeeping_report&staff=<?php echo getDetailByTable('staffs',$rowStaffId)['s_id']; ?>"><?php echo getDetailByTable('staffs',$rowStaffId)['s_nickname']; ?></a></td>
				  <td><?php echo totalJob($rowStaffId,NULL)['total']; ?></td>
				  <td><?php echo totalWorkedDays($rowStaffId); ?> days</td>
				  <td><?php echo round(totalJob($rowStaffId,NULL)['total']/totalWorkedDays($rowStaffId),2); ?></td>
				  <td><?php echo totalJob($rowStaffId,date("Y-m"))['total']; ?></td>
				  <td><?php echo totalJob($rowStaffId,date("Y-m", strtotime(date("Y-m")." -1 month")))['total']; ?></td>
				</tr>
				  <?php } ?>
			  </tbody>
			</table>

		  </div>
		</div>
	  </div>
		
		<?php if($getStaffId){ ?>
	  <div class="col-md-6 col-sm-6 col-xs-12">
		<div class="x_panel">
		  <div class="x_title">
			<h2>Total Monthly Job Report </h2>
			  
			<ul class="nav navbar-right panel_toolbox">
			  <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
			  </li>
			  <li class="dropdown">
				<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-wrench"></i></a>
				<ul class="dropdown-menu" role="menu">
				  <li><a href="#">Settings 1</a>
				  </li>
				  <li><a href="#">Settings 2</a>
				  </li>
				</ul>
			  </li>
			  <li><a class="close-link"><i class="fa fa-close"></i></a>
			  </li>
			</ul>
			<div class="clearfix"></div>
		  </div>
		  <div class="x_content">
			<table class="table table-hover">
			  <thead>
				<tr>
				  <th>#</th>
				  <th>Month</th>
				  <th>Total</th>
				  <th>Avg Daily</th>
				  <th>Est. Paid</th>
				</tr>
			  </thead>
			  <tbody>
				  <?php $no = 0;
					$sql = "SELECT DATE_FORMAT(hk_date, '%Y-%m') as Months from housekeep GROUP BY Months ORDER BY Months DESC";
				  	$result = mysqli_query($conn,$sql);
				  	while($row = mysqli_fetch_assoc($result)){
						
				  ?>
				<tr>
				  <th><?php echo ++$no; ?></th>
				  <td><a href="?pagecontent=housekeeping_report&staff=<?php echo $getStaffId; ?>&month=<?php echo $row['Months']; ?>"><?php echo date("F Y",strtotime($row['Months'])); ?></a></td>
				  <td><?php echo $totalJob = totalJob($getStaffId,$row['Months'])['total']; ?></td>
				  <td><?php 
						$now = time(); // or your date as well
						$your_date = strtotime(date("Y-m-01"));
						$datediff = $now - $your_date;
						$daysUntilToday = round($datediff / (60 * 60 * 24));
						
						if($row['Months'] == date("Y-m")){
							echo round($totalJob/$daysUntilToday,2);
						}else{
							echo round($totalJob/date("t",strtotime($row['Months'])),2);
						}
					  ?>
					</td>
				  <td>RM <?php echo $totalJob * 10; ?></td>
				</tr>
				  <?php } ?>
			  </tbody>
			</table>

		  </div>
		</div>
	  </div>
		<?php } ?>
		
		<?php if($getMonthYear){ ?>
	
	  <div class="clearfix"></div>
	  <div class="col-md-6 col-sm-6 col-xs-12">
		<div class="x_panel">
		  <div class="x_title">
			<h2>Cleaning Report</h2>
			  
			<ul class="nav navbar-right panel_toolbox">
			  <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
			  </li>
			  <li class="dropdown">
				<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-wrench"></i></a>
				<ul class="dropdown-menu" role="menu">
				  <li><a href="#">Settings 1</a>
				  </li>
				  <li><a href="#">Settings 2</a>
				  </li>
				</ul>
			  </li>
			  <li><a class="close-link"><i class="fa fa-close"></i></a>
			  </li>
			</ul>
			<div class="clearfix"></div>
		  </div>
		  <div class="x_content">
			<table class="table table-hover">
			  <thead>
				<tr>
				  <th>#</th>
				  <th>Date</th>
				  <th>Prty/Room</th>
				  <th>Peroid</th>
				  <th>Occ Type</th>
				  <th>Room</th>
				</tr>
			  </thead>
			  <tbody>
				  <?php $no = 0; $totalCleaningFee = 0; $getMonth = date("m",strtotime($getMonthYear)); $getYear = date("Y",strtotime($getMonthYear));
					$sql = "SELECT * FROM housekeep AS HK LEFT JOIN housekeep_staff AS HS ON HK.hk_id=HS.hk_id WHERE HS.s_id=$getStaffId AND MONTH(HK.hk_date)=$getMonth AND YEAR(HK.hk_date)=$getYear ORDER BY HK.hk_date ASC";
				  	$result = mysqli_query($conn,$sql);
				  	while($row = mysqli_fetch_assoc($result)){
						
						$dateDiff = intval((strtotime($row['hk_end'])-strtotime($row['hk_start']))/60);
						$hours = intval($dateDiff/60);
						$minutes = $dateDiff%60;
						
				  ?>
				<tr>
				  <th><?php echo ++$no; ?></th>
				  <td><a href="?pagecontent=reservation_update&id=<?php echo $row['rv_id']; ?>"><?php echo 
					  $row['hk_date']; ?></a></td>
				  <td><?php echo getDetailByTable('properties',getDetailByTable('rooms',$row['r_id'])['p_id'])['pty_code']." ".getDetailByTable('rooms',$row['r_id'])['r_no']; ?></td>
				  <td><?php echo $hours.":".$minutes; ?></td>
				  <td><?php echo occuptionType(getDetailByTable('reservation',$row['rv_id'])['rv_occ_id'])['title']; ?></td>
				  <td><?php 
						$cleaningFee = getDetailByTable('reservation',$row['rv_id'])['rv_cleaning_fee']; 

						if($cleaningFee){
							echo "RM ".$cleaningFee;
						}else{
							echo "-";
						}

						$totalCleaningFee = $totalCleaningFee + $cleaningFee; 
					?>
					</td>
				</tr>
				  <?php } ?>
			  </tbody>
			  <tfoot>
			  	<th>#</th>
				  <th></th>
				  <th></th>
				  <th></th>
				  <th></th>
				  <th>RM <?php echo $totalCleaningFee; ?></th>
			  </tfoot>
			</table>

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

	  <div class="col-md-6 col-sm-6 col-xs-12">
		<div class="x_panel">
		  <div class="x_title">
			<h2>By Room</h2>
			  
			<ul class="nav navbar-right panel_toolbox">
			  <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
			  </li>
			  <li class="dropdown">
				<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-wrench"></i></a>
				<ul class="dropdown-menu" role="menu">
				  <li><a href="#">Settings 1</a>
				  </li>
				  <li><a href="#">Settings 2</a>
				  </li>
				</ul>
			  </li>
			  <li><a class="close-link"><i class="fa fa-close"></i></a>
			  </li>
			</ul>
			<div class="clearfix"></div>
		  </div>
		  <div class="x_content">
			<table class="table table-hover">
			  <thead>
				<tr>
				  <th>#</th>
				  <th>Prty/Room</th>
				  <th>Peroid</th>
				  <th>Occ Type</th>
				  <th>Room</th>
				</tr>
			  </thead>
			  <tbody>
				  <?php $no = 0; $totalCleaningFee = 0; $getMonth = date("m",strtotime($getMonthYear)); $getYear = date("Y",strtotime($getMonthYear));
					$sql = "SELECT *, COUNT() AS total_cleaned FROM housekeep AS HK LEFT JOIN housekeep_staff AS HS ON HK.hk_id=HS.hk_id WHERE HS.s_id=$getStaffId AND MONTH(HK.hk_date)=$getMonth AND YEAR(HK.hk_date)=$getYear GROUP BY HK.r_id";
				  	$result = mysqli_query($conn,$sql);
				  	while($row = mysqli_fetch_assoc($result)){
						
						$dateDiff = intval((strtotime($row['hk_end'])-strtotime($row['hk_start']))/60);
						$hours = intval($dateDiff/60);
						$minutes = $dateDiff%60;
						
				  ?>
				<tr>
				  <th><?php echo ++$no; ?></th>
				  <td><?php echo getDetailByTable('properties',getDetailByTable('rooms',$row['r_id'])['p_id'])['pty_code']." ".getDetailByTable('rooms',$row['r_id'])['r_no']; ?></td>
				  <td><?php echo $hours.":".$minutes; ?></td>
				  <td><?php echo occuptionType(getDetailByTable('reservation',$row['rv_id'])['rv_occ_id'])['title']; ?></td>
				  <td><?php 
						$cleaningFee = getDetailByTable('reservation',$row['rv_id'])['rv_cleaning_fee']; 

						if($cleaningFee){
							echo "RM ".$cleaningFee;
						}else{
							echo "-";
						}

						$totalCleaningFee = $totalCleaningFee + $cleaningFee; 
					?>
					</td>
				</tr>
				  <?php } ?>
			  </tbody>
			  <tfoot>
			  	<th>#</th>
				  <th></th>
				  <th></th>
				  <th></th>
				  <th></th>
				  <th>RM <?php echo $totalCleaningFee; ?></th>
			  </tfoot>
			</table>

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


		<?php } ?>
		<?php
			function totalJob($staffId,$date){
				global $conn; $getMonth = date("m",strtotime($date)); $getYear = date("Y",strtotime($date));
				
				if(is_null($date)){
					$result = mysqli_query($conn, "SELECT COUNT(hks_id) AS total FROM housekeep_staff WHERE s_id=$staffId");
					return mysqli_fetch_assoc($result);
				}else{
					$result = mysqli_query($conn, "SELECT *, COUNT(HS.hks_id) AS total FROM housekeep AS HK LEFT JOIN housekeep_staff AS HS on HK.hk_id=HS.hk_id WHERE HS.s_id=$staffId AND YEAR(HK.hk_date)=$getYear AND MONTH(HK.hk_date)=$getMonth");
					return mysqli_fetch_assoc($result);
				}
			}
		
			function totalWorkedDays($staffId){
				global $conn;
				
				$result = mysqli_query($conn, "SELECT * FROM housekeep AS HK LEFT JOIN housekeep_staff AS HS on HK.hk_id=HS.hk_id WHERE HS.s_id=$staffId GROUP BY HK.hk_date");
				
				return mysqli_num_rows($result);
			}
		
		?>

	  <div class="clearfix"></div>
	</div>
  </div>
</div>
<!-- /page content -->
