<!---------- Post Out ---------------->
<section class="content">
      <div class="container-fluid">
	  <div class="row">
	  <div class="col-12">
<div id="timesheet_div" class="card">
	<div class="card-header">
		<h3 class="card-title">Timesheet</h3>

		<div class="card-tools">
			<div class="input-group input-group-sm" style="width: 150px;">
				<a href="" data-toggle="modal" data-backdrop="static"  data-target="#myModalAjaxforTimesheet" alt="<?=$_REQUEST['id'];?>" class="btn btn-primary add_new_timesheet" style="float:right;margin-right:5px;margin-bottom:5px;">
		Add Timesheet</a>
		<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>
			</div>
		</div>
	</div>
	
	

	
			<div class="card-body table-responsive" style="height: auto;">
				<div class="box box-primary" id="overflow_div">
					<!-- Start  records header -->
					<table id="timesheet_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>
	<!---------- Post Out ---------------->
</div>
</div>
</div>
</div>
</section>

<!-- Modal -->
<div class="modal fade" id="myModalAjaxforTimesheet" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <div class="modal-header">
        <div class="col-lg-12">
		<div class="row">
			<div class="col-lg-8">
				<h4 class="modal-title" id="myModalLabelAjaxTimesheet">Modal title</h4>
			</div>
			<div class="col-lg-4">
				<button type="button" class="close close_button2" data-dismiss="modal" aria-hidden="true">&times;</button>
			</div>
		</div>
		</div>
      </div>
      <div class="modal-body modal_iframe2">
			<section class="content">
				<div class="row">
					<div class="col-lg-12">
					 <div class="alert alert-info alert-dismissable timesheet_message" style="display:none;">
						<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
					  </div>
						<div class="box box-primary">
							<div class="box-body">
							<form action="" method="post" name="add_form_timesheet" id="add_form_timesheet">
								<div class="col-lg-12">
									<label for="Timesheet Text" class="control-label">Description</label>
									<textarea class="form-control" rows="4" id="timesheet_text" name="timesheet_text" required><?php echo stripslashes($timesheet_text);?></textarea>
									
									<label for="Timesheet Option" class="control-label">Timesheet Option</label>
									<? echo Keycode::getKeycodeListValue("timesheet_option", $timesheet_option, $width = "100%", "Timesheet Option", "id", "code", "timesheet_option","","form-control",false,"required"); ?>
								</div>
								<div class="col-lg-12">
									<label for="From Time" class="control-label">From Time</label>
									<div class="bootstrap-timepicker">
										<? utility::getTimeList("from_time", $from_time, "From Time", "100%");?>
									</div>
									
									<label for="To Time" class="control-label">To Time</label>
									<div class="bootstrap-timepicker">
										<? utility::getTimeList("to_time", $to_time, "To Time", "100%");?>
									</div>
								</div>
								<div class="col-lg-12">
									<label for="Timesheet Date" class="control-label">Date</label>
									<input type="text" class="form-control datepicker" id="timesheet_date" name="timesheet_date" value="<?php (isset($timesheet_date) && $timesheet_date !="") ? utility::getFormatedDate($timesheet_date) : utility::getFormatedDate(date($config->dateFormat));?>" required>
									
									<label for="Timesheet Time" class="control-label">Time</label>
									<div class="bootstrap-timepicker">
										<? utility::getTimeList("timesheet_time", $timesheet_time, "Time", "100%");?>
									</div>
								</div>
								<div class="col-lg-12">
									<label for="Consultant" class="control-label">Consultant</label>
										<?
											$t_admin_ids = "";
											Admin::getAdminList("t_admin_id",$t_admin_id,"100%",false,"form-control","required",$t_admin_ids); 
										?>
										<!--<div id="admin_id_div">
										<? 
											/* $admin_ids = "";
											$sql_for_admin = "SELECT * FROM job_admin_mapping WHERE job_id = $job_id";
											$rs_for_admin = mysqli_query($conn,$sql_for_admin);
											if(mysqli_num_rows($rs_for_admin) > 0){
												$c=0;
												while($row_for_admin = mysqli_fetch_assoc($rs_for_admin)){
													if($c==0){
														$default_admin = $row_for_admin["admin_id"];								
													}
													$c=$c+1;
													$admin_ids .= $row_for_admin["admin_id"].",";
												}
												$admin_ids = substr($admin_ids,0,-1);
											}
											if($c==1 && $_REQUEST['action'] != 'get_edit_data_timesheet'){
												$admin_id = $default_admin;
											}
											Admin::getAdminList("admin_id",$admin_id,"100%",false,"form-control","required",$admin_ids);  */
										?>
									   </div>-->
								</div>								
								<div style="clear:both;margin-top:13px;"></div>
								<div class="col-lg-12" style="margin-top:13px;">
									<input type="hidden" id="timesheet_id" name="id" value="">
									<input type="hidden" id="job_id" name="job_id" value="<?=$job_id;?>">
									<input type="hidden" id="page_action_timesheet" name="page_action_timesheet" value="add">
									<button type="submit" name="submit_form_timesheet" id="submit_form_timesheet" class="btn btn-primary">Add</button>
									<i class="fas fa-sync-alt fa-spin" style="display:none;" id="page_load_div_for_timesheet"></i>
									<i class="fa fa-check" id="timesheet_done" style="color:#bad23a;font-size: 20px;display:none;"></i>
								</div>
							</form>
							</div>
						</div>
					</div>
				</div>
			</section>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default close_button2" data-dismiss="modal">Close</button>
      </div>
    </div>
    <!-- /.modal-content -->
  </div>
  <!-- /.modal-dialog -->
</div>
<!-- /.modal -->
<script type="text/javascript">
var flag_for_table = 0;
$(function () {
	$("#timesheet_option").change(function(){
		var timesheet_option_text = $("#timesheet_option option:selected").text();
		var timesheet_text = $("#timesheet_text").val();
		$("#timesheet_text").val(timesheet_option_text+"\n"+timesheet_text);
	});
	$("#from_time,#to_time").change(function(){
		var from_time = $("#from_time").val();
		var to_time = $("#to_time").val();		
		if(!((from_time == "00:00" || from_time == "00:00:00") && (to_time == "00:00" || to_time == "00:00:00"))){
			var form_data   = {'from_time':from_time,'to_time':to_time};       
			$.ajax({
				async: false,
				type: "POST",
				url: site_url+"/modules/timesheet_management/index.php?page=ajax_actions&action=for_ajax_calculate_time",
				data: form_data,
				success: function(result){  
					var resultObj = jQuery.parseJSON(result); 
					//$('#timesheet_time').val(resultObj.calculate_time);
					$('#timesheet_time').select2("val",resultObj.calculate_time);
				}
			});
		}
	});
	/*********************************** Post Out By Ajax and JSON *************************/
	var job_id = "<?=$_REQUEST['id'];?>";
	
	$('#timesheet_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&job_id="+job_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;" data-toggle="modal" data-backdrop="static"  data-target="#myModalAjaxforTimesheet" class="timesheet_edit" alt="'+buttonID+'"><i class="fa fa-edit" style="font-size:20px;"></i></span>';
                 }
            },
			{
				sortable: false,
                 "render": function ( data, type, full, meta ){
                     var buttonID = full.id;
                     return '<span style="cursor:pointer;" class="timesheet_remove" alt="'+buttonID+'"><i class="fa fa-trash" style="font-size:20px;"></i></span><i class="fa fa-refresh fa-spin" style="display:none;" id="delete_loading_timesheet_'+buttonID+'"></i>';
                 }
            }
        ]
    });
	$("#timesheet_div").on("click",".add_new_timesheet",function(){
		flag_for_table = 0;
		$("#add_form_timesheet")[0].reset();
		$("#myModalLabelAjaxTimesheet").text("Timesheet");
		$("#page_action_timesheet").val("add");
		$("#submit_form_timesheet").html("Add");
		var job_id = $(this).attr("alt");	
		$("#myModalAjaxforTimesheet").children().find("#job_id").val(job_id);
	});	
	$("#timesheet_table").on("click",".timesheet_edit",function(){	
		$("#myModalLabelAjaxTimesheet").text("Edit Timesheet");
		var id = $(this).attr("alt");		
		var form_data   = {'id':id};
		$.ajax({
			async: true,
			type: "POST",
			url: "<?php echo $siteURL?>/timesheet_management/index?page=ajax_timesheet&action=get_edit_data_timesheet",
			data: form_data,
			success: function(result){
				var resultObj = jQuery.parseJSON(result); 
				if(resultObj.status == "success"){
					$.each(resultObj.data_array,function(data_array_key,data_array_val){
						if(data_array_key == "job_id"){
							$("#myModalAjaxforTimesheet").children().find("#job_id").val(data_array_val);
						}else if(data_array_key == "from_time"){
							$("#myModalAjaxforTimesheet").children().find("#"+data_array_key).select2("val",data_array_val);
						}else if(data_array_key == "to_time"){
							$("#myModalAjaxforTimesheet").children().find("#"+data_array_key).select2("val",data_array_val);
						}else if(data_array_key == "timesheet_time"){
							$("#myModalAjaxforTimesheet").children().find("#"+data_array_key).select2("val",data_array_val);
						}
						$("#"+data_array_key).val(data_array_val);
					});
					$("#timesheet_id").val(id);
					$("#page_action_timesheet").val("update");
					$("#submit_form_timesheet").html("Update");
				}
			}
		});	
	});
	var post_out_t = $('#timesheet_table').DataTable();
	$('#timesheet_table tbody').on('click', '.timesheet_remove',function(){
		if(confirm("Do you want to delete.")){
			var timesheet_id = $(this).attr("alt");
			$("#delete_loading_timesheet_"+timesheet_id).show();
			var form_data   = {'timesheet_id':timesheet_id};
			$.ajax({
				async: true,
				type: "POST",
				url: "<?php echo $siteURL?>/timesheet_management/index?page=ajax_timesheet&action=delete_timesheet",
				data: form_data,
				success: function(result){
					var resultObj = jQuery.parseJSON(result); 
					if(resultObj.status == "success"){
						$("#delete_loading_timesheet_"+timesheet_id).hide();
						flag_for_table = 1;
						post_out_t.draw( false );
					}
				}
			});	
		}
	});
	
	$("#myModalAjaxforTimesheet").on("click",".close_button2",function(){
		//alert(flag_for_table);
		if(flag_for_table == 1){
			flag_for_table = 0;
			post_out_t.draw( true );
		}
	});
	/******************************* End Post Out By Ajax and JSON *************************/
});

$(document).on('submit','form#add_form_timesheet',function(e){
	var timesheet_done = $(this).find('#timesheet_done');
	timesheet_done.hide();
	var page_load_div_for_timesheet = $(this).find('#page_load_div_for_timesheet');
	page_load_div_for_timesheet.show();
	var submit_form_timesheet = $(this).find("#submit_form_timesheet");
	submit_form_timesheet.attr("disabled", true);
	var add_form_timesheet = $(this);
	
	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 = $('#add_form_timesheet').serialize();     
	$.ajax({
		async: true,
		type: "POST",
		url: "<?php echo $siteURL?>/timesheet_management/index?page=ajax_timesheet&action=add_timesheet",
		data: form_data,
		success: function(result){
			flag_for_table = 1;
			//alert(flag_for_table);
			if(flag == 1){
				add_form_timesheet[0].reset();  // Reset all form data
				var timesheet_message = "Timesheet added successfully";
			}else if(flag == 0){
				var timesheet_message = "Timesheet updated successfully";
			}
			timesheet_done.show();
			page_load_div_for_timesheet.hide();
			submit_form_timesheet.attr("disabled", false);
			$(".timesheet_message").show();
			$(".timesheet_message").html(timesheet_message);
			setTimeout(function(){
				timesheet_done.hide();
				$(".timesheet_message").hide();
			},3000);
		}
	});
});
</script>