<?php
$collapse_add_area = "collapse";
$add_btn_hide = "";
$cancel_btn_hide = "";
$search_list = "block";
$listing = "block";

$where 	= " WHERE 1=1 ";

$query_string = "";

$button_value 	= "Add";
$table_name		= "timesheet";
$page_action 	= "add";
$page_name		= "Timesheet Management";	
################################################## START EDIT ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit'){
	$collapse_add_area = "";
	
	$page_action 	= "update";
	$button_value 	= "Update";
	$page_name		= "Edit Post Out";	
		
    $id = $_REQUEST['id'];
    $sql= "SELECT * FROM $table_name WHERE id = $id";
    $result_edit = mysqli_query($conn,$sql);
    $row_edit = mysqli_fetch_array($result_edit);

    $id= $row_edit['id'];
    $job_id= $row_edit['job_id'];
    //$admin_id= explode(",",$row_edit['admin_id']);
	$admin_id= $row_edit['admin_id'];
	$note_text= $row_edit['note_text'];
	$note_date= $row_edit['note_date'];
	$scorecard_notes_option= $row_edit['scorecard_notes_option'];
}
################################################# END EDIT ################################################# 

###########################################  PAGING WITH PER PAGE  #####################################
$query = "SELECT * FROM timesheet $where ORDER BY created_on DESC ";
$sql=mysqli_query($conn,$query);
$total_no_of_timesheets=mysqli_num_rows($sql);
?>
<section class="content-header">
  <h1>
	<? echo $page_name; ?>
  </h1>		
</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_ajax_for_admin.inc.php');?>
					</div>
					<div class="table-responsive" id="listing">
						<input type="text" name="search" id="search" class="pull-right">
						<table id="post_out_table" class="table table-bordered table-striped">
							<thead>
								<tr>
									<th>Client</th>
									<th>Description</th>
									<!--<th>Project</th>
									<th>Post Type</th>-->									
									<th>T. Option</th>
									<th>Consultant</th>
									<th width="10%">Date</th>
									<th>Time</th>
									<th>From Time</th>
									<th>To Time</th>
									<th>Units</th>
									<!--<th>Recipient</th>-->
									<th>Edit</th>
									<th>Delete</th>
								</tr>
							</thead>
						</table>
					</div>
				</div>
			</div>
		</div>
	</div>
</section>
	
<script type="text/javascript">
$(document).ready(function(){
	$('#add_post_out').click(function(){
		$('#post_out_area').slideToggle();
		var frm = document.getElementsByName('add_form')[0];
		frm.reset();
		var blank = "";	
		$("#job_id").select2("val", blank);
		//$("#admin_id").select2("val", blank);
		$("#page_action").val("add");
		$("#submit_form").html("Add");
	});
	
	$('#post_out_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_admin",
        "columns": [
            { "data": "client" },
            { "data": "timesheet_text" },
            { "data": "timesheet_option" },
            { "data": "admin_id" },
            { "data": "timesheet_date" },
            { "data": "timesheet_time" },
            { "data": "from_time" },
            { "data": "to_time" },
            { "data": "units" },
			{
				sortable: false,
                 "render": function ( data, type, full, meta ){
                     var buttonID = full.id;
                     return '<span style="cursor:pointer;" class="post_out_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_notes'+buttonID+'"></i>';
                 }
            },
			{
				sortable: false,
                 "render": function ( data, type, full, meta ){
                     var buttonID = full.id;
                     return '<span style="cursor:pointer;" class="post_out_remove" alt="'+buttonID+'"><i class="fa fa-trash-o" style="font-size:20px;"></i></span>';
                 }
            }
        ],
		"drawCallback": function( settings ) {
			//alert( 'DataTables has redrawn the table' );
			//// Filtering 
			if($('#search').val() !=""){
				var value = $('#search').val().toLowerCase();
				//alert($("#telephone_message_table tr").text().toLowerCase().indexOf(value));
				//$("#telephone_message_table tr").filter(function() {
					setTimeout(function(){
						$("#post_out_table tbody tr").filter(function(){
							$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1);
						});
					},1000);
				//});
			}
		}
    });
	
	var post_out_t = $('#post_out_table').DataTable();
	$('#post_out_table tbody').on('click', '.post_out_edit',function(){
		$("html,body").animate({
			scrollTop: $("#post_out_area").offset().top - 110},
		'slow');
		
		var post_out_id = $(this).attr("alt");
		$("#edit_notes"+post_out_id).show();
		var form_data   = {'post_out_id':post_out_id};
		$.ajax({
			async: true,
			type: "POST",
			url: "<?php echo $siteURL?>/timesheet_management/index?page=ajax_timesheet_for_admin&action=edit_post_out",
			data: form_data,
			success: function(result){
				var resultObj = jQuery.parseJSON(result); 
				console.log(resultObj);
				if(resultObj.status == "success"){
					$('#post_out_area').slideDown();
					$.each(resultObj.data_array,function(data_array_key,data_array_val){
						if(data_array_key == "job_id"){
							$("#"+data_array_key).select2("val",data_array_val);
						}else if(data_array_key == "from_time"){
							$("#"+data_array_key).select2("val",data_array_val);
						}else if(data_array_key == "to_time"){
							$("#"+data_array_key).select2("val",data_array_val);
						}else if(data_array_key == "timesheet_time"){
							$("#"+data_array_key).select2("val",data_array_val);
						}else if(data_array_key == "admin_id"){
							$("#"+data_array_key).val(data_array_val);
							$("#hidden_admin_id").val(data_array_val);
						}else{
							$("#"+data_array_key).val(data_array_val);
						}
					});
					$('.hide_show').hide();
					//$('.cancel_btn').hide();
					$("#edit_notes"+post_out_id).hide();
					$("#page_action").val("update");
					$("#submit_form").html("Update");
				}
			}
		});	
	});
	
	$('#post_out_table tbody').on('click', '.post_out_remove',function(){
		var post_out_edit_id = $("#id").val();
		var post_out_id = $(this).attr("alt");
		if(post_out_edit_id == post_out_id){
			alert("This Record open in edit mode so you can not delete it.");
			return false;
		}
		if(confirm("Do you want to delete.")){
			var form_data   = {'post_out_id':post_out_id};
			$.ajax({
				async: true,
				type: "POST",
				url: "<?php echo $siteURL?>/timesheet_management/index?page=ajax_timesheet_for_admin&action=delete_post_out",
				data: form_data,
				success: function(result){
					var resultObj = jQuery.parseJSON(result); 
					if(resultObj.status == "success"){
						post_out_t.draw( false );
					}
				}
			});	
		}
	});
	
	$(document).on('submit','form#add_form',function(e){
		$(this).find('#post_out_notes_done').hide();
		$(this).find('#page_load_div_for_post_out').show();
		$(this).find("#add_master_data").attr("disabled", true);
		
		e.preventDefault(); //prevent Default functionality
		var page_action = $("#page_action").val();
		var flag = 0;
		if(page_action == "add"){
			flag = 1;
		}
		
		var form_data = $('form').serialize();     
		$.ajax({
			async: true,
			type: "POST",
			url: "<?php echo $siteURL?>/timesheet_management/index?page=ajax_timesheet_for_admin&action=add_post_out",
			data: form_data,
			success: function(result){ 
				if(flag == 0){
					$("#page_action").val("add");
					$("#submit_form").html("Add");
					//$('#post_out_area').slideToggle();
				}
				$("#id").val("");
				$("#job_id").select2("val","");
				//$("#to_employee").select2("val","");
				var frm = document.getElementsByName('add_form')[0];
				frm.reset();  // Reset all form data	
					
				$('#post_out_notes_done').show();
				$('#page_load_div_for_post_out').hide();
				$("#add_master_data").attr("disabled", false);
				
				var resultObj = jQuery.parseJSON(result); 
				if(resultObj.status == "success"){
					post_out_t.draw( false );
				}
				setTimeout(function() {
					$('#post_out_notes_done').hide();
				},1000);
				setTimeout(function() {
					$('#add_post_out').trigger("click");
					$('.hide_show').show();
				},1000);
			}
		});
	});
	
	$("#search").on("keyup", function() {
		var value = $(this).val().toLowerCase();
		$("#post_out_table tbody tr").filter(function() {
		  $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
		});
	});
});	
</script>