<?
	$this->title = 'Gift cards';
	$this->titleicon = 'gift';

	$hasPermissionToAllRestaurants = c::admin()->permission()->check( [ 'global', 'gift-card-all', 'gift-card-list-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="code" type="text" value="<?=strip_tags($_REQUEST['code'])?>" placeholder="Code">
								</li>
								<li>
									<label class="span3">Limit</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">
									<?
										$date = new DateTime('now');
										$start = $date->format('m/d/Y');
										$date->modify('14 days ago');
										$end = $date->format('m/d/Y');
									?>
									<label class="span3">Date range</label>
									<span class="span8 pull-right">
										<span class="dates">
											from &nbsp; <input name="date-range-end" class="date-picker" type="text" style="width: 90px;" value="<?=$end?>">
											&nbsp; to &nbsp; <input name="date-range-start" class="date-picker" type="text" style="width: 90px;" value="<?=$start?>">
										</span>
										<input type="checkbox" name="giftcard-range-all"><label>&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">
											<?php  
											$restaurants = Restaurant::q('select * from restaurant order by name');
											if( $hasPermissionToAllRestaurants ){
												echo '<option value="" selected>[ All ]</option>';
												foreach ( $restaurants as $restaurant ) {
													?><option value="<?=$restaurant->id_restaurant?>"><?=$restaurant->name?></option><?php 
												}
											} else {
												foreach ( $restaurants as $restaurant ) {
													if( c::admin()->permission()->check( [ "gift-card-list-restaurant-{$restaurant->id_restaurant}", "gift-card-restaurant-{$restaurant->id_restaurant}" ] ) ){
														?><option value="<?=$restaurant->id_restaurant?>"><?=$restaurant->name?></option><?php 
													}
												}
											}
											?>
										</select>
									</span>
									<div class="clearfix"></div>
								</li>
							</ul>	
						</div>
					</form>

				</div>
				
			</div>
			<div style="text-align: center;">


				<?php if (c::admin()->permission()->check(['global','gift-card-all', 'gift-card-anti-cheat'])) { ?>
				<a href="/giftcards/credits/" class="btn btn-red"><i class=" icon-warning-sign"></i>&nbsp;&nbsp; Anti Cheat </a> 
				&nbsp;&nbsp;&nbsp;
				<?php } ?>
				<button type="submit" class="btn btn-blue admin-giftcard-search"><i class="icon-search"></i>&nbsp;&nbsp;&nbsp; Search </button> 
				
				<?php if (c::admin()->permission()->check(['global','gift-card-all', 'gift-card-groups'])) { ?>
				&nbsp;&nbsp;&nbsp;
				<a href="/giftcards/groups/" class="btn btn-green admin-giftcard-group-new"><i class="icon-group"></i>&nbsp;&nbsp;&nbsp; Groups </a> 
				<?php } ?>

				<?php if (c::admin()->permission()->check(['global','gift-card-all', 'gift-card-create', 'gift-card-create-all'])) { ?>
				&nbsp;&nbsp;&nbsp;
				<div class="btn-group">
					<button class="btn btn-default" >
						 <a href="/giftcards/new">Create a new code</a>&nbsp;&nbsp;&nbsp;<!--<span class="caret"></span>-->
					</button>
                    <!--
					<ul class="dropdown-menu">
						<li><a href="/giftcards/new">Create a new code</a></li>
						<li class="divider"></li>
						<li><a href="/giftcards/email">Send by email</a></li>
						<li><a href="/giftcards/sms">Send by sms</a></li>
					</ul>-->
				</div>
				<?php } ?>
				&nbsp;&nbsp;&nbsp;
				
			</div>
			<br />
		</div>

		<div class="box">
			<div class="box-header"><span class="title">Results</span></div>
			<div class="box-content">
				<div class="giftcards-loader"><center><i class="icon-spinner icon-spin" style="font-size: 50px;"></i></center></div>
				<div class="giftcards-content"></div>
			</div>
		</div>
	</div>
</div>

<script>
	$(function() {		
		$(document).on('click', '.admin-giftcard-search', function() {
			App.giftcards.load();
		} );

		$(document).on('keyup', '[name="code"]', function( e ) {
			if (e.which == 13) {
				App.giftcards.load();
			}
		} );

		$(document).on('keyup', '[name="id_user"]', function( e ) {
			if (e.which == 13) {
				App.giftcards.load();
			}
		} );

		$(document).on('click', '.admin-giftcard-new', function() {
			location.href="/giftcards/new";
		} );

		$(document).on('click', '.admin-giftcard-sms', function() {
			location.href="/giftcards/sms";
		} );
		
		$(document).on('click', '.admin-giftcard-email', function() {
			location.href="/giftcards/email";
		} );
		
		$( '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 );
			}
		});

		$(document).on('change', 'input[name="giftcard-range-all"]', function( e ) {
			if ($(this).prop('checked')) {
				$('.date-picker').attr('disabled', 'disabled');
				$('.date-picker').val('');
				$('.dates').hide();
			} else {
				$('.date-picker').removeAttr('disabled');
				$('.dates').show();
			}
		});

		App.giftcards.load();

	});
</script>