<?php
$collapse_add_area = "collapse";
$where 	= " WHERE 1=1 ";
$query_string = "";
$button_value_timesheet 	= "Add";
$table_name		= "timesheet";
$next_page_condition 	= "";
$paging ="";
$page_action_timesheet 	= "add";
if($_REQUEST['id']){
	$job_id = $_REQUEST['id'];
	$where 	.= " AND job_id = $job_id ";
	$next_page_condition.="&job_id=".$job_id;
}

if(isset($_REQUEST['source'])){
	$source = $_REQUEST['source'];
	$next_page_condition.="&source=$source";
}


###########################################  PAGING WITH PER PAGE  #####################################
$file_name		= "index"; 
$paging_table_name	= $table_name;
$paging_query		= $where;
$join_query_for_paging = "SELECT * FROM $table_name  $where ";
$page_header = "Timesheet Management";
include("paging/paging_query.inc.php");
############################################ END PAGING WITH PER PAGE  #######################################
$query = "SELECT * FROM $table_name $where ORDER BY timesheet_date DESC LIMIT $eu, $limit ";
$sql=mysqli_query($conn,$query);
$result=mysqli_num_rows($sql);
?>

<section class="content-header">
  <h1>
	<? echo str_replace(array("_"),array(" "),$page_header); ?>
  </h1>	
  <div style="float:right;margin-right:5px;border:0px solid;width:150px;height:24px;margin-top:8px;">
		<b>Total Time</b>
		<b><? echo $gsi->getTotalTimeByJobId($_REQUEST['id'])?></b>
	</div>	
</section>

	<section class="content">
		<div class="row">
			<div class="col-xs-12">
				<div class="box">

				<div class="box-body">
				<div class="row" style="padding:0px 10px;">
					<?php include_once('add_timesheet_for_clients.inc.php');?>
				</div>
				<div class="table-responsive">
					<table id="timesheet_for_clients_table" class="table table-bordered table-striped">
						<thead>
							<tr>
								<th>Client</th>
								<th style="width:25%;">Description</th>
								<th>T. Option</th>
								<th>Consultant</th>
								<th>Date</th>
								<th>Time</th>
								<th>From Time</th>
								<th>To Time</th>
								<th>Edit</th>						
								<th>Delete</th>
							</tr>
						</thead>		
					</table>
				 </div>
				</div>
				</div>
			</div>
		</div>
	</section>
<script type="text/javascript">
$(document).ready(function(){
	$('#add_timesheet_for_clients').click(function(){
		$('#timesheet_for_clients_area').slideToggle();
		var frm = document.getElementsByName('add_timesheet_for_clients_form')[0];
		frm.reset();
		$("#page_action_timesheet").val("add");
		$("#submit_form_timesheet").html("Add");
	});
	
	$('#timesheet_for_clients_table').DataTable({
        "processing": true,
        "serverSide": true,
		"paging": false,
		"lengthChange": false,
		"searching": false,
		"ordering": false,
		"info": false,
		"autoWidth": false,
        "ajax": "<?php echo $siteURL?>/timesheet_management/index?page=ajax_timesheet_for_clients&id=<?= $_REQUEST['id'];?>",
        "columns": [
            { "data": "client" },
			{ "data": "description" },
			{ "data": "timesheet_option" },
			{ "data": "consultant" },
            { "data": "timesheet_date" },
            { "data": "timesheet_time" },
            { "data": "from_time" },
            { "data": "to_time" },
			{
				sortable: false,
                 "render": function ( data, type, full, meta ){
                     var buttonID = full.id;
                     return '<span style="cursor:pointer;" class="timesheet_for_clients_edit" alt="'+buttonID+'"><i class="fa fa-edit" style="font-size:20px;"></i></span><i class="fa fa-refresh fa-spin" style="display:none;" id="edit_timesheet'+buttonID+'"></i>';
                 }
            },
			{
				sortable: false,
                 "render": function ( data, type, full, meta ){
                     var buttonID = full.id;
                     return '<span style="cursor:pointer;" class="timesheet_for_clients_remove" alt="'+buttonID+'"><i class="fa fa-trash-o" style="font-size:20px;"></i></span><i class="fa fa-refresh fa-spin" style="display:none;" id="delete_timesheet_'+buttonID+'"></i>';
                 }
            }
        ]
	});
	
	var timesheet_for_clients_t = $('#timesheet_for_clients_table').DataTable();
	$('#timesheet_for_clients_table tbody').on('click', '.timesheet_for_clients_edit',function(){
		/*$("html,body").animate({
			scrollTop: $("#timesheet_for_clients_area").offset().top - 110},
		'slow');*/
		
		var timesheet_for_clients_id = $(this).attr("alt");
		$("#edit_timesheet"+timesheet_for_clients_id).show();
		var form_data   = {'timesheet_for_clients_id':timesheet_for_clients_id};
		$.ajax({
			async: true,
			type: "POST",
			url: "<?php echo $siteURL?>/timesheet_management/index?page=ajax_timesheet_for_clients&action=edit_timesheet_for_clients",
			data: form_data,
			success: function(result){
				var resultObj = jQuery.parseJSON(result); 
				console.log(resultObj);
				if(resultObj.status == "1"){
					var time=resultObj.timesheet_time;
					var to_time=resultObj.to_time;
					var from_time=resultObj.from_time;
					//var admin_id=resultObj.admin_id; 

					$('#timesheet_for_clients_area').slideDown();
					$("#timesheet_text").val(resultObj.timesheet_text);
					$("#t_admin_id").val(resultObj.admin_id);
					//$("#admin_id").select2("val", admin_id);
					$("#timesheet_date").val(resultObj.timesheet_date);
					$("#timesheet_time").select2("val", time);
					$("#from_time").select2("val", from_time);
					$("#to_time").select2("val", to_time);
					//$("#timesheet_option").val("val", timesheet_option);
					
					$("#edit_timesheet"+timesheet_for_clients_id).hide();
					$("#page_action_timesheet").val("update");
					$("#timesheet_id").val(timesheet_for_clients_id);
					$("#submit_form_timesheet").html("Update");
				}
			}
		});
	});
	
	$('#timesheet_for_clients_table tbody').on('click', '.timesheet_for_clients_remove',function(){
		var timesheet_for_clients_edit_id = $("#id").val();
		var timesheet_for_clients_id = $(this).attr("alt");
		$("#delete_timesheet_"+timesheet_for_clients_id).show();
		if(timesheet_for_clients_edit_id == timesheet_for_clients_id){
			alert("This Id open in edit mode so you can not delete it.");
			return false;
		}
		if(confirm("Do you want to delete.")){
			var form_data   = {'timesheet_for_clients_id':timesheet_for_clients_id};
			$.ajax({
				async: true,
				type: "POST",
				url: "<?php echo $siteURL?>/timesheet_management/index?page=ajax_timesheet_for_clients&action=delete_timesheet_for_clients",
				data: form_data,
				success: function(result){
					var resultObj = jQuery.parseJSON(result); 
					if(resultObj.status == "success"){
						$('#delete_timesheet_'+timesheet_for_clients_id).hide();
						timesheet_for_clients_t.draw( false );
					}
				}
			});	
		}
	});
	
	$(document).on('submit','form#add_timesheet_for_clients_form',function(e){
		$(this).find('#timesheet_for_clients_done').hide();
		$(this).find('#page_load_div_for_timesheet_for_clients').show();
		$(this).find("#add_master_data").attr("disabled", true);
		
		e.preventDefault(); //prevent Default functionality
		var page_action_timesheet = $("#page_action_timesheet").val();
		var flag = 0;
		if(page_action_timesheet == "add"){
			flag = 1;
		}
		
		var form_data = $('form').serialize();     
		$.ajax({
			async: true,
			type: "POST",
			url: site_url+"/modules/timesheet_management/index.php?page=ajax_timesheet_for_clients&action=add_timesheet_for_clients",
			data: form_data,
			success: function(result){ 
				if(flag == 0){
					$("#page_action_timesheet").val("add");
					$("#submit_form_timesheet").html("Add");
					//$('#timesheet_for_clients_area').slideToggle();
				}
				$("#id").val("");
				//$("#job_id").val("");
				var frm = document.getElementsByName('add_timesheet_for_clients_form')[0];
				for (var i=0; i < frm.length; i++){
					if (frm[i].getAttribute('id') == 'timesheet_for_clients_id'){
						$("#"+frm[i].getAttribute('id')).select2("val","");
					}else if(frm[i].getAttribute('class') == 'checkbox_div flat-red section_checkbox'){
						$(".section_checkbox").iCheck('uncheck');
					}
				}
				frm.reset();  // Reset all form data	
					
				$('#timesheet_for_clients_done').show();
				$('#page_load_div_for_timesheet_for_clients').hide();
				$("#add_master_data").attr("disabled", false);
				
				var resultObj = jQuery.parseJSON(result); 
				if(resultObj.status == "success"){
					timesheet_for_clients_t.draw( false );
				}
				setTimeout(function() {
					$('#timesheet_for_clients_done').hide();
				},1000);
				setTimeout(function() {
					$('#add_timesheet_for_clients').trigger("click");
				},1000);
			}
		});
	}); 
	
	/* $("#search_for_timesheet_for_clients").on("keyup", function() {
		var value = $(this).val().toLowerCase();
		$("#timesheet_for_clients_table tbody tr").filter(function() {
		  $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
		});
	}); */
});	
</script>		