<?php
session_start();
$where 					= " WHERE 1=1 ";
$next_page_condition 	= "";
$paging ="";
if($_REQUEST['start']){
	$paging .="&start=".$_REQUEST['start'];
}
################################# START ORDERING COUNTY ################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'set_order'){	
	foreach($_REQUEST['oder'] as $key =>$val){
		mysqli_query($conn,"UPDATE schedular SET ord='$val' WHERE id='$key' ");		
	}
	
	
	$query_string = (isset($_REQUEST['query_string']) && !empty($_REQUEST['query_string'])) ? $_REQUEST['query_string'] : "";
	$query_string = str_ireplace("action=set_order", "", $query_string);
	$query_string = "&".$query_string;
	
	$_SESSION['info_message'] = "Order Updated sucessfully";
	header("Location: index?page=view$query_string");
	exit();
}
################################# END ORDERING COUNTY ################################
###########################################  STRAT FILTER CONITIONS    #####################################
$county 	= isset($_REQUEST['county']) ? $_REQUEST['county'] : "";
if($county) {
    $where	.= " AND county LIKE '%$county%' ";
    $next_page_condition.="&county=$county";
}

###########################################  	END  FILTER CONITIONS 	#####################################
###########################################  PAGING WITH PER PAGE  #####################################
$file_name		= "index"; // this is file name which is used during paging  , included at the bottom of the page
$table_name		= " job_schedular";
$paging_table_name	= $table_name;
$paging_query		= $where;
$join_query_for_paging = "SELECT * FROM $table_name  $where ";
$page_header = "Schedular Management";
include("paging/paging_query.inc.php");
############################################ END PAGING WITH PER PAGE  #######################################
$start_date = date('Y-m-d', strtotime('monday this week'));
$end_date = date('Y-m-d', strtotime('sunday this week'));
echo$sql = "SELECT * FROM job_schedular WHERE date BETWEEN '$start_date' AND '$end_date' ORDER BY date";
$sql=mysqli_query($conn,$sql);
$result=mysqli_num_rows($sql);

echo$sql22 = "SELECT DATE_FORMAT(date, '%W') AS day_name, DATE(date) AS date, SUM(hours) AS total_hours 
FROM job_schedular 
WHERE date BETWEEN '$start_date' AND '$end_date' 
GROUP BY DATE(date)";
$sql22=mysqli_query($conn,$sql22);
$result22=mysqli_num_rows($sql22);

?>
<div class="content-header">
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-sm-12">
                <h1 class="m-0"><?=$page_header;?></h1>
            </div>
        </div>
        <!-- /.row -->
    </div>
    <!-- /.container-fluid -->
</div>
<section class="content">
	<div class="container-fluid">
		<div class="row">
			<div class="col-lg-12">
				<div class="card">
				<!----------------------- Message ------------------------------->
				<? if(isset($_SESSION['info_message'])){ ?>
				  <div class="alert alert-info alert-dismissable">
					<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
					<? echo $_SESSION['info_message']; unset($_SESSION['info_message']);?>
				  </div>
				  <? } ?>
				<!----------------------- End Message ------------------------------->
					<div class="card-body">
					<!-- search form -->
				 <?php include_once('county_search_options.inc.php');?>
					<!-- /.search form -->
					<!--Total No. of  <?=$total_no_of_counties;?>-->
						
						
						    <? include ('add_job_schedular_with_ajax.inc.php'); ?>
						  <div class="table-responsive ModelfixTableHead" style="height: 300px;">
                        <div class="" id="overflow_div"style="overflow-x: hidden;">
                            <!-- Start  records header -->
							<?if ($result22 > 0) {
							// Output table header
							echo '<table id="" class="table table-bordered table-striped">';
							echo '<thead style="position: sticky">';
							echo '<tr>';
							echo '<th style="width: 200px;">Day</th>';
							echo '<th style="width: 200px;">Date</th>';
							echo '<th style="width: 200px;">Total Hours</th>';
							echo '</tr>';
							echo '</thead>';
							echo '<tbody>';

							// Output table data
							while($row22 =mysqli_fetch_array($sql22)){ 
								echo '<tr>';
								echo '<td>' . $row22["day_name"] . '</td>';
								echo '<td>' . $row22["date"] . '</td>';
								echo '<td>' . $row22["total_hours"] . '</td>';
								echo '</tr>';
								 $total_hours += $row22["total_hours"];
							}
							echo '<tr>';
							echo '<td colspan="2" style="text-align: right;"><strong>Total Hours:</strong></td>';
							echo '<td><strong>' . $total_hours . '</strong></td>';
							echo '</tr>';
							echo '</tbody>';
							echo '</table>';
						} else {
							echo "No records found";
						}

							
                            if ($result> 0) {
							$current_day = null;
							echo '<table id="" class="table table-bordered table-striped">';
							echo '<thead style="position: sticky">';
							echo '<tr>';
							echo '<th style="width: 525px;">Job</th>';
						   
							echo '</tr>';
							echo '</thead>';
							echo '<tbody>';

							
						   while($row =mysqli_fetch_array($sql)){ 
								
								if ($row['date'] != $current_day) {
									
									echo '<tr>';
									echo '<td colspan="6" class="bg-success text-bold">' . date('l', strtotime($row['date'])) . '</td>';
									echo '</tr>';
								   
									$current_day = $row['date'];
								}
							   
								echo '<tr>';
								echo '<td>' . $row["job_name"] . '</td>';
								
								echo '</tr>';
							}

							echo '</tbody>';
							echo '</table>';
						} else {
							echo "No records found";
						}

$conn->close();
?>
                        </div>
                    </div>
						</div>
				</div>
			</div>
		</div>
	</div>
</section>

<script type="text/javascript">
$(function () {
	$("#set_order").click(function(){
		
		
			$('#form1').attr("action","index?page=view&action=set_order<?=$next_page_condition.$paging;?>");
			$('#form1').submit();
		
	});
});
</script>