<?
	$this->title = 'Orders';
	$this->subtitle = 'people order shit! then eat it!';
	$this->titleicon = 'truck';

	$hasPermissionToAllRestaurants = c::admin()->permission()->check( [ 'global', 'orders-all' ] );

?>


<!-- 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">
									<input name="order-search" type="text" placeholder="Search" value="<?=strip_tags($_REQUEST['s'])?>">
								</li>
								<li>
									<label class="span3">Environment</label>
									<span class="span8 pull-right">
										<? $envs = ['live','beta','dev','local']; ?>
										<select name="env" class="uniform">
											<? foreach ($envs as $env) : ?>
												<option value="<?=$env?>"<?=c::env() == $env ? ' selected' : ''?>><?=$env?></option>
											<? endforeach ; ?>
										</select>
									</span>
									<div class="clearfix"></div>
								</li>
								<li>
									<label class="span3"># Orders displayed</label>
									<span class="span8 pull-right">
										<? $envs = ['live','beta','dev','local']; ?>
										<input type="number" name="limit" value="25" min="0">
									</span>
								</li>
							</ul>
						</div>

						<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">Processor</label>
									<span class="span8 pull-right">
										<select name="processor" class="uniform">
											<option value="">[ All ]</option>
											<option value="stripe" selected>stripe</option>
											<option value="balanced">balanced</option>
										</select>
									</span>
									<div class="clearfix"></div>
								</li>
								<li>
									<label class="span3">Restaurant</label>
									<span class="span8 pull-right">
										<select name="restaurant" class="chzn-select">
										echo '<option value="" selected>[ All ]</option>';
											<?php
											$restaurants = Restaurant::q('select * from restaurant order by name');
											if( $hasPermissionToAllRestaurants ){
												foreach ( $restaurants as $restaurant ) {
													?><option value="<?=$restaurant->id_restaurant?>"><?=$restaurant->name?> - <?=$restaurant->communityNames()?></option><?php
												}
											} else {
												foreach ( $restaurants as $restaurant ) {
													if( c::admin()->permission()->check( [ "orders-list-restaurant-{$restaurant->id_restaurant}" ] ) ){
														?><option value="<?=$restaurant->id_restaurant?>"><?=$restaurant->name?> - <?=$restaurant->communityNames()?></option><?php
													}
												}
											}
											?>
										</select>
									</span>
									<div class="clearfix"></div>
								</li>
								<li>
									<label class="span3">Community</label>
									<span class="span8 pull-right">
										<select name="community" class="chzn-select">
											<option value="" selected>[ All ]</option>
											<?php if( $hasPermissionToAllRestaurants ) { ?>
											<? foreach (Restaurant::getCommunities() as $community) :  ?>
												<option value="<?=$community?>"><?=$community?></option>
											<? endforeach ; ?>
											<?php } ?>
										</select>
									</span>
									<div class="clearfix"></div>
								</li>
							</ul>
						</div>
					</form>

				</div>

			</div>
			<center>
				<button type="submit" class="btn btn-blue admin-order-search"> Search </button>
				&nbsp;&nbsp;
				<?php if( c::admin()->permission()->check(['global','orders-all','orders-new-users']) ) { ?>
				<a class="btn btn-green" href="/orders/lastusers/">New users</a>
				<?php } ?>
			</center>
			<br />
		</div>

		<div class="box">
			<div class="box-header"><span class="title">Results</span>
				<?php if( c::admin()->permission()->check(['global','orders-all','orders-export']) ) { ?>
				<ul class="box-toolbar">
					<li class="toolbar-link">
						<a href="javascript:;" class="admin-order-export" title="Export"><i class="icon-download-alt"></i></a>
					</li>
				</ul>
			<?php } ?>
			</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>



<script>
	$(function() {

		App.orders.createEvents();

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

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

		$(document).on('keyup', 'input[name="order-search"]', function(e) {
			if (e.which == 13) {
				App.orders.load();
			}
		});
		$('.chzn').select2();

		App.orders.load();

		$( 'input[name="date-range-end"]' ).datepicker({
			defaultDate: "+1w",
			changeMonth: true,
			numberOfMonths: 3,
			onClose: function( selectedDate ) {
				$( 'input[name="date-range-start"]' ).datepicker( "option", "minDate", selectedDate );
			}
		});

		$( 'input[name="date-range-start"]' ).datepicker({
			defaultDate: "+1w",
			changeMonth: true,
			numberOfMonths: 3,
			onClose: function( selectedDate ) {
				$( 'input[name="date-range-end"]').datepicker( "option", "maxDate", selectedDate );
			}
		});

	});
</script>
