<?
	$this->title = 'Drivers';
	$this->titleicon = 'road';
	$this->titleLink = '/drivers';
	$this->title2 = 'Delivered orders';
	$this->title2icon = 'truck';
?>
<!-- content -->
<div class="container-fluid padded">
	<div class="row-fluid">
		<div class="box">
			<div class="box-content">
				<div class="row-fluid">
					<form class="fill-up">
						<div class="span6 separate-sections">
							<ul class="padded separate-sections">
								<li class="input">
									<label class="span3">Date range</label>
									<span class="span8 pull-right">
										<span class="dates" style="display:none;">
											from &nbsp; <input name="date-range-end" class="date-picker" disabled="disabled" type="text" style="width: 90px;" value="">
											&nbsp; to &nbsp; <input name="date-range-start" class="date-picker" disabled="disabled" type="text" style="width: 90px;" value="">
										</span>
										<label><input type="checkbox" checked="checked" name="date-range-all">&nbsp;All Dates</label>
									</span>
									<div class="clearfix"></div>
			
								</li>
								<li>
									<label class="span3">Restaurant</label>
									<span class="span8 pull-right">
										<select name="restaurant" class="chzn-select">
											<option value="" selected>[ All ]</option>
											<?php  
											foreach ( $this->restaurants as $restaurant ) {
												?><option value="<?=$restaurant->id_restaurant?>"><?=$restaurant->name?></option><?php 
											}
											?>
										</select>
									</span>
									<div class="clearfix"></div>
								</li>
								<li>
									<label class="span3">Driver</label>
									<span class="span8 pull-right">
										<select name="driver" class="chzn-select">
											<option value="" selected>[ All ]</option>
											<?php
											foreach ( $this->drivers as $driver ) { 
												$selected = ( $driver->id_admin == $this->id_admin ) ? 'selected="selected"' : '';
												?>
												<option <?php echo $selected; ?> value="<?=$driver->id_admin?>"><?=$driver->name?></option>
											<? } ?>
										</select>
									</span>
									<div class="clearfix"></div>
								</li>
							</ul>	
						</div>
						<div class="span6 separate-sections">
							<ul class="padded separate-sections">
								<li>
									<label class="span3"># Orders displayed</label>
									<span class="span8 pull-right">
										<input type="number" name="limit" id="limit" value="25" min="0">
									</span>
								</li>
							</ul>	
						</div>

					</form>

				</div>

			</div>
			<center>
				<button type="submit" class="btn btn-blue admin-order-search"> Search </button>	
				&nbsp;&nbsp;
			</center>
			<br />
		</div>

		<div class="box">
			<div class="box-header"><span class="title">Results</span>
				
				<ul class="box-toolbar">
					<li class="toolbar-link">
						<a href="javascript:;" title="Show date and time at my Timezone - <?php echo $this->admin_tz; ?>" <?php if( c::view()->tz_default == 'restaurant-tz' ){ echo 'style="display:none;"'; } ?> class="change-to-restaurant-timezone admin-tz"><i class="icon-time"></i> My Timezone</a>
						<a href="javascript:;" title="Show date and time at restaurants Timezone" <?php if( c::view()->tz_default == 'admin-tz' ){ echo 'style="display:none;"'; } ?> class="change-to-my-timezone restaurant-tz"><i class="icon-time"></i> Restaurant Timezone</a>
					</li>
					<li class="toolbar-link">
						<a href="javascript:;" class="admin-order-export" title="Export"><i class="icon-download-alt"></i></a>
					</li>
				</ul>
			
			</div>
			<div class="box-content">
				<div class="orders-loader"><center><i class="icon-spinner icon-spin" style="font-size: 50px;"></i></center></div>
				<div class="orders-content"></div>
			</div>
		</div>
	</div>
</div>
<div class="modal hide fade" id="modal-hours" style="display: none; left: 50%">
	<div class="modal-header">
		<button class="close" data-dismiss="modal" type="button">×</button>
		<h6 id="modal-hours-title">Assign driver</h6>
	</div>
	<div class="modal-body" id="modal-hours-body"></div>
	<div class="modal-footer">
		<button class="btn btn-default" data-dismiss="modal">Close</button>
	</div>
</div>
<script>
var deliveries = {};

deliveries.action = function( id_order ){
	var url = '/drivers/deliveries/action/' + id_order;
	$.get( url, function( data ) {
		$( '#modal-hours' ).modal();
		$( '#modal-hours-body' ).html( data );
	} );
};
deliveries.load = function(){
	$( '.orders-loader' ).show();
	$( '.orders-content' ).html( '' );
	$.ajax({
		url: '/drivers/deliveries/content',
		data: deliveries.params(),
		complete: function(content) {
			$('.orders-content').html( content.responseText );
			$('.orders-loader').hide();
		}
	} );
};
deliveries.export = function() {
	var params = deliveries.params();
	params.export = 'csv';
	location.href = '/drivers/deliveries/content?' + jQuery.param( params );
};

deliveries.params = function(){
	var data = {};
	if( $('input[name="date-range-end"]').val() && $('input[name="date-range-start"]').val() ){
		data[ 'dates'] = $('input[name="date-range-end"]').val() + ',' + $('input[name="date-range-start"]').val();		
	}
	if( $('select[name="restaurant"]').val() ){
		data[ 'id_restaurant' ] = $('select[name="restaurant"]').val();
	}
	if( $('select[name="driver"]').val() ){
		data[ 'id_admin' ] = $('select[name="driver"]').val();
	}
	data[ 'limit' ] = $('input[name="limit"]').val();
	return data;
};

$(function() {

	$(document).on('click', '.admin-order-search', function() {
		deliveries.load();
	});

	$(document).on('click', '.admin-order-export', function() {
		deliveries.export();
	});

	$(document).on('click', '.change-to-restaurant-timezone', function() {
		deliveries.toggleTz( 'restaurant-tz' );
	});

	$(document).on('click', '.change-to-my-timezone', function() {
		deliveries.toggleTz( 'admin-tz' );
	});

	deliveries.load();

} );

deliveries.toggleTz = function( tz ){
	$( '.admin-tz' ).hide();
	$( '.restaurant-tz' ).hide();
	$( '.' + tz ).show();
	admin_config.save( '<?php echo Controller_drivers::ADMIN_DRIVER_LIST_TZ; ?>', tz );
};

deliveries.assign = function(){
	var id_admin = $( '#form-id_admin' ).val();
	var id_order = $( '#form-id_order' ).val();
	if( $.trim( id_admin ) == '' ){
		alert( 'Please select a driver' );
		$( '#form-id_admin' ).focus();
		return;
	}

	var actions = [];
	$( '[name="form-action"]' ).each( function(){
		var checkbox = $( this );
		if( checkbox.is( ':checked' ) ){
			actions.push( checkbox.val() );	
		}
	} );

	$.ajax( {
		url: '/api/drivers/assign/order/' + id_order,
		method: 'POST',
		data: { 'id_admin' : id_admin, 'actions' : actions },
		dataType: 'json',
	} ).done( function( data ) {
		if( data.success ){
			$( '#modal-hours' ).modal( 'hide' );
			deliveries.load();
		} else {
			alert( 'Oops! ' + data.error );
		}
	} );

};

</script>