<?php 
$this->Html->css(array('ui/jquery-ui.min','timeentry/jquery.timeentry'), '', array('inline'=>false));
$this->Html->script(array('ui/jquery-ui-custom.min','inventory', 'timeentry/jquery.plugin', 'timeentry/jquery.timeentry'),  array('inline'=>false));
?>
<h2 class="title">
	<?php 
	if(!empty($userData)){
		echo __('%s\'s History Records for %s', $userData['User']['first_name'].' '.$userData['User']['last_name'], date('M d, Y',strtotime($date)));
	}else{
		echo __('History Records');
	} ?></h2>
<div class="search-frm">
	<?php echo $this->Form->create('UserTracking', array('id'=>'filter-form', 'novalidate' => true,   'type' => 'get', 'error'=>true, 'inputDefaults'=>array('div'=>false, 'label'=>false), 'errorMessage' => false)); ?>
		
		<div class="inline">
			<label><?php echo __('User:'); ?></label>
			<?php echo $this->Form->input(null, array('name'=>'usr', 'options' => $users, 'empty'=>__('Select User'), 'class'=>'field-inline',  'value' => isset($this->request->query['usr'])?$this->request->query['usr']:''));?>
		</div>
		
		<div class="inline">
			<label><?php echo __('Date of History:'); ?></label>
			<?php echo $this->Form->input(null, array('name'=>'dt', 'id'=>'hs-date', 'type'=>'text', 'class'=>'field-inline', 'value' => isset($this->request->query['dt'])?$this->request->query['dt']:''));?>
		</div>
		<div class="inline">
			<label><?php echo __('Start Time:') ?></label>
			<?php echo $this->Form->input(null, array('name'=>'stime', 'id'=>'stime', 'type'=>'text', 'class'=>'field-inline timeRange', 'style'=>'width:70px; margin-right:0px;', 'value' => isset($this->request->query['stime'])?$this->request->query['stime']:'06:00:00'));?>
		</div>
		<div class="inline">
			<label><?php echo __('End Time:') ?></label>
			<?php echo $this->Form->input(null, array('name'=>'etime', 'id'=>'etime', 'type'=>'text', 'class'=>'field-inline timeRange', 'style'=>'width:70px; margin-right:0px;', 'value' => isset($this->request->query['etime'])?$this->request->query['etime']:'23:59:59'));?>
		</div>
		
	<?php
		echo $this->Form->submit('Filter', array('class'=>'field-inline'));
		echo $this->Form->end(); ?> 
</div>


<?php 

		
		if (!empty($historyData)): ?>
			
			<table class="tbl-list" cellpadding="0" cellspacing="0" border="0">
				<tr>
					<th>#</th>
					<th><?php echo __('Start');?></th>
					<th><?php echo __('End');?></th>
					<th><?php echo __('Mileage(Km)');?></th>
				</tr>
				<?php  
				$i = 0;
				foreach($historyData as $ind => $history): ?>
				<tr>
					<td><?php echo ++$i; ?></td>
					<td>
						<div><?php echo date('h:ia', $history['start']['datetime']) ?></div>
						<div><?php echo $history['start']['lat'].', '.$history['start']['long']?></div>
						<div><?php echo getaddress($history['start']['lat'], $history['start']['long']) ?></div>
						
					</td>
					<td>
						<div><?php echo date('h:ia', $history['end']['datetime']) ?></div>
						<div><?php echo $history['end']['lat'].', '.$history['end']['long']?></div>
						<div><?php echo getaddress($history['end']['lat'], $history['end']['long']) ?></div>
					</td>
					<td><?php echo distance($history['start']['lat'], $history['start']['long'], $history['end']['lat'], $history['end']['long'], 'k')?></td>
					
					
					
				</tr>
				<?php endforeach; ?>
			</table>
			
			
		<?php else: ?>
			<p><?php echo __('Data not found') ?></p>
		<?php endif; ?>

</section>


<script>
jQuery(document).ready(function(){
	$( "#hs-date" ).datepicker({
		changeMonth: true,
		changeYear: true,
		dateFormat: 'yy-mm-dd'
    });
	$('.timeRange').timeEntry({beforeShow: customRange, spinnerImage: siteUrl+'img/spinnerDefault.png', show24Hours: true,showSeconds: true}); 
});
function customRange(input) { 
    return {minTime: (input.id === 'etime' ? 
        $('#stime').timeEntry('getTime') : null),  
        maxTime: (input.id === 'stime' ? 
        $('#etime').timeEntry('getTime') : null)}; 
}
</script>
