<?php
session_start();
$where 					= " WHERE 1=1 ";
$next_page_condition 	= "";
$paging ="";
if($_REQUEST['start']){
	$paging .="&start=".$_REQUEST['start'];
}

###########################################  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		= "working_hours";
$paging_table_name	= $table_name;
$paging_query		= $where;
$join_query_for_paging = "SELECT * FROM $table_name  $where ";
$page_header = "Shift Management";
include("paging/paging_query.inc.php");
############################################ END PAGING WITH PER PAGE  #######################################
$query = "SELECT * FROM $table_name $where ORDER BY id ASC LIMIT $eu, $limit";
$sql=mysqli_query($conn,$query);
$result=mysqli_num_rows($sql);

$total_no_of_currency = mysqli_num_rows(mysqli_query($conn,$join_query_for_paging));
#################################   DELETE    #########################################
// when we click on delete this area of php code will execute
if(isset($_REQUEST['action']) && $_REQUEST['action']=='delete')
{
	$sql=mysqli_query($conn,"DELETE FROM $table_name WHERE id = '".$_REQUEST['id']."' ");
	$_SESSION['info_message'] = "Shift deleted successfully";
	header("location: $file_name?page=view".$next_page_condition.$paging);
	exit;
}
#################################  END DELETE    #########################################
?>
<style>
  #includeContent {
    display: none;
    max-height: 0;
    overflow: hidden;
    transition: all 0.5s ease;
    padding: 0 1rem; 
    background-color: #f9f9f9;
    border: 1px solid #ddd;
    border-radius: 5px;

  }

  #includeContent.active {
    display: block;
    max-height: 100%; 
    padding: 1rem;
  }

  #toggleButton {
    transition: background-color 0.3s, transform 0.3s;
  }

  #toggleButton:hover {
    background-color: #cb64d9;
  }
</style>
<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">
			<!----------------------- Message ------------------------------->
			<? if(isset($_SESSION['info_message'])){ ?>
			  <div class="alert alert-info alert-primary">
				<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">
				<button type="button" class="btn btn-primary mb-5" id="toggleButton">
              Add Shift
            </button>
            <div id="includeContent">
              <?php include_once('add_working_hrs.inc.php'); ?>
            </div>
				
					<form action="index?page=add" method="post" name="form1" id="form1">
					<? if($result>0){?>
					 <table id="example1" class="table table-bordered table-striped">
						<thead>
						  <tr>
							<th>Shift Id</th>
							<th class="">Shift Name</th>
							<th class="">Day</th>
							<th>Start Time</th>
							<th>End Time</th>
							<th>Hours</th>
							<th>Pay Rate</th>
							<th>Edit</th>
							<th>Delete</th>
						  </tr>
						</thead>
						<tbody>
							<? $query = "SELECT * FROM $table_name $where ORDER BY id ASC LIMIT $eu, $limit";
								$sql=mysqli_query($conn,$query);
								$result=mysqli_num_rows($sql);

								$total_no_of_currency = mysqli_num_rows(mysqli_query($conn,$join_query_for_paging));
								while($row =mysqli_fetch_array($sql)){ 
							?>
						  <tr>
							<td class=""><?php echo $row['shift_id'];?></td>
							<td class=""><?php echo $row['shift_name'];?></td>
							<td class=""><?php echo $gsi->getWeekById($row['day_shifts']);?></td>
							<td><?php echo date("H:i",strtotime($row["start_time"]));?></td>
							<td><?php echo date("H:i",strtotime($row["end_time"]));?></td>
							<td><?php   
								$start_time = strtotime($row["start_time"]);
								$end_time = strtotime($row["end_time"]);
								$hourss = (abs($end_time - $start_time));
								$hours = floor($hourss / 3600); 
                                $minutes = floor(($hourss % 3600)/60);
								echo $total_hrs = $hours . ':' . $minutes ;
								?>
						    </td>
							<td><?php echo $row['pay_rate'];?></td>
							<td>
								<a href="" data-toggle="modal" data-target="#myModal" class="edit_shift"
									alt="<?=$row['id'];?>" data-shift_id="<?=$row['id'];?>"
									data-shift_name="<?=ucwords($row['day_shifts']);?>"><i
										class="fa fa-fw fa-edit text-primary"></i></a>
							</td>
							<td><a href="index?page=view&action=delete&id=<?=$row['id'].$next_page_condition.$paging; ?>"onclick=" javascript:return confirm('Are you sure you want to delete?');"><i class="fa fa-fw fa-trash text-danger"></i> </a></td>
						  </tr>
							<? } ?>
						</tbody>
					 </table>
					 <? }else{ ?>
						No Record found...
					 <? } ?>
					 <div class="box-footer clearfix">
						<!-- Paging starts here  -->
						<? 
							$next_page_condition .="&page=view";
							include("paging/paging_row.inc.php") 
						?>
						<!-- Paging ends starts here  -->
					</div>
				</div>
				<div class="card-footer hide">
                   <button type="submit" name="add" class="btn btn-primary">Add Currency</button>
                </div>
				</form>
			</div>
		</div>
	</div>
	</div>
</section>
<script>
document.getElementById('toggleButton').addEventListener('click', function () {
    const includeContent = document.getElementById('includeContent');
    includeContent.classList.toggle('active');
});
$(".edit_shift").click(function(){
		//$("#myModalLabel").text("Notes");
		var station_name = $(this).attr("data-station_name");	
		$("#myModalLabel").html("Edit Shift");
		var hide_top_left = "&hide_top=1&hide_left=1";
		var shift_id = $(this).attr("data-shift_id");			
	    $('.modal-dialog').css({"max-width":"60%"});
        $('.modal-content').css({"height": "60vh","overflow": "hidden"});		
		$(".modal_iframe").attr("src","<?=$config->frontEndURL?>/admin/working_hrs_management/index?page=add&action=edit&from=view&id="+shift_id+hide_top_left);
	});
	

	$(document).ready(function(){
         $("#myModal .close_button2").click(function(){
				location.reload();
		  });
	});	  
</script>