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

	$this->title2 = 'Create';
	$this->title2icon = 'file';

	$hasPermissionToAllRestaurants = true; //c::admin()->permission()->check( [ 'global', 'gift-card-all', 'gift-card-create-all' ] );

?>
<!-- Preview stuff -->
<link href='http://fonts.googleapis.com/css?family=Atomic+Age' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="/assets/css/giftcard/default.css">
<div class="container-fluid padded">
	<div class="row-fluid">
		<div class="span6">
			<div class="box">
				<div class="box-header">
					<span class="title">Essential Information</span>
				</div>
				<div class="box-content ">
					<ul class="box-list">
						<li>
							<span><strong>Value</strong></span>
							<span class="pull-right">
								<input type="number" name="value" maxlength="5" id="value" value="" />
							</span>
						</li>
						<li>
							<span><strong>Paid by</strong></span>
							<span class="pull-right">
								<select name="paid_by" id="paid_by" class="uniform">
									<option value=""></option>
									<option selected="selected" value="crunchbutton">crunchbutton</option>
									<option value="restaurant">restaurant</option>
									<option value="promotional">promotional</option>
									<option value="other_restaurant">other_restaurant</option>
								</select>
							</span>
						</li>

					</ul>
				</div>

			</div>

		<?php if( $this->user ) { ?>
		<!--notification box-->
		<div class="box">
				<div class="box-header">
					<span class="title">Notifications</span>
				</div>
				<div class="box-content ">
					<ul class="box-list">
						<li>
							<span>Notify by email (<?php  echo $this->user->email; ?>)</span>
							<span class="pull-right">
								<input type="checkbox" name="notify_by_email" id="notify_by_email" class="iButton-icons">
							</span>
						</li>
						<li>
							<span>Notify by phone (<?php  echo $this->user->phone; ?>)</span>
							<span class="pull-right">
								<input type="checkbox" name="notify_by_sms" id="notify_by_sms" class="iButton-icons">
							</span>
						</li>
						<li>
							<span>Add as credit</span>
							<span class="pull-right">
								<input type="checkbox" name="add_as_credit" id="add_as_credit">
							</span>
							<hr />
							<div class="note large">
								If you check this option the user will receive the credit without the need to redeem it. The user will not be notified.
							</div>
						</li>
					</ul>
				</div>

			</div>
			<?php } ?>
<!--end notification box-->
			<div class="box">
				<div class="box-header">
					<span class="title">Promo code</span>
				</div>
				<div class="box-content ">
					<ul class="box-list">

						<li>
							<span>Number of giftcards</span>
							<span class="pull-right">
								<input type="number" name="total" maxlength="5" id="total" value="1" />
								<div>
									<i class="icon-exclamation-sign"></i>
									Print no more than 1000 at a time.
								</div>
							</span>
						</li>
						<?php if( $this->user ) { ?>
						<li>
							<span>User</span>
							<span class="pull-right">
								<input type="text" readonly="readonly" id="user_name" name="user_name" value="<?php echo $this->user->name;?>">
								<input type="hidden" id="id_user" name="id_user" value="<?php echo $this->user->id_user;?>">
							</span>
						</li>
						<li>
							<span>Order reference</span>
							<span class="pull-right">
								<input type="text" id="id_order_reference" name="id_order_reference" value="<?php echo $this->id_order_reference;?>">
							</span>
						</li>
						<?php } ?>
						<li>
							<label>Restaurant</label>
							<span>
								<select id="id_restaurant" multiple class="chzn-select" name="id_restaurant" data-placeholder="Choose a restaurant" style="width:100%;">
								<?php
								$restaurants = Restaurant::q('SELECT * FROM restaurant WHERE giftcard = 1 ORDER BY name');
								if( $hasPermissionToAllRestaurants ){
									echo '<option value="*" selected>[ All ]</option>';
								} else {
									foreach ( $restaurants as $restaurant ) {
										if( c::admin()->permission()->check( [ "gift-card-restaurant-{$restaurant->id_restaurant}", "gift-card-create-restaurant-{$restaurant->id_restaurant}" ] ) ){
											?><option value="<?=$restaurant->id_restaurant?>" <?php if( $this->id_restaurant == $restaurant->id_restaurant ){ echo 'selected="selected"'; } ?> ><?=$restaurant->name?></option><?php
										}
									}
								}
								?>
								</select>
							</span>
						</li>
						<li>
							<label>Group</label>
							<span>
								<select id="id_group" class="chzn-select" multiple name="id_group" data-placeholder="Choose a group" style="width:100%;">
									<? foreach (Crunchbutton_Promo_Group::q('SELECT * FROM promo_group ORDER BY name') as $group) : ?>
										<option value="<?=$group->id_promo_group?>"><?=$group->name?></option>
									<? endforeach ; ?>
								</select>
							</span>
						</li>

						<li id="list_id_restaurant_paid_by" style="display:none;">
							<label>Restaurant that paid the credit</label>
							<span>
								<select id="id_restaurant_paid_by" class="chzn-select" name="id_restaurant_paid_by" data-placeholder="Choose a restaurant" style="width:100%;">
									<option value="" selected></option>
									<? /*each (Restaurant::q('SELECT * FROM restaurant ORDER BY name') as $restaurant) : ?>
										<option value="<?=$restaurant->id_restaurant?>" <?php if( $this->id_restaurant == $restaurant->id_restaurant ){ echo 'selected="selected"'; } ?> ><?=$restaurant->name?></option>
									<? endforeach ; */ ?>
								</select>
							</span>
						</li>
					</ul>
				</div>
			</div>
			<div class="box">
				<div class="box-header">
					<span class="title">Track and Notes</span>
				</div>
				<div class="box-content ">
					<ul class="box-list">
						<li>
							<span>Created by</span>
							<span class="pull-right">
								<input type="text" id="created_by" name="created_by" value="<?=c::admin()->login?>">
							</span>
						</li>
						<li>
							<span>Track this gift card</span>
							<span class="pull-right">
								<input type="checkbox" name="track" id="track" class="">
							</span>
						</li>
						<li class="track-fields">
							<span>Notify phone</span>
							<span class="pull-right">
								<input type="text" id="notify_phone" value="<?=c::admin()->testphone?>" name="notify_phone">
							</span>
							<hr/>
							<div class="note large">
								Phone number that will receive a sms notification when this gift card is redeemed.
							</div>
						</li>
						<li class="track-fields">
							<span>Customer Name</span>
							<span class="pull-right">
								<input type="text" id="name" name="name" value="">
							</span>
						</li>
						<li class="track-fields">
							<span>Contact Info:</span>
							<span class="pull-right">
								<textarea name="contact" id="contact"></textarea>
							</span>
						</li>
						<li>
							<span>Notes + Follow-up?</span>
							<span class="pull-right">
								<textarea name="notes" id="notes"></textarea>
							</span>
						</li>
						<li class="input">
							<button type="submit" class="btn btn-blue admin-giftcard-save"><i class="icon-save"></i> Save </button>
							<button type="submit" class="btn btn-green admin-giftcard-print"><i class="icon-print"></i> Save &amp; Print </button>
							<button type="submit" class="btn btn-blue admin-giftcard-print-flyer"><i class="icon-picture"></i> Save &amp; Print Flyer </button>
							<button type="submit" class="btn btn-red admin-giftcard-print-brown"><i class="icon-picture"></i> Save &amp; Print Brown </button>
						</li>
					</ul>
				</div>
			</div>
		</div>
		<div class="span6">
			<div class="box">
				<div class="box-header">
					<span class="title">Advanced options</span>
				</div>
				<div class="box-content ">
					<ul class="box-list">
						<li>
							<span>Message</span>
							<span class="pull-right">
								<input type="text" name="giftcard-message" maxlength="50" id="giftcard-message" value="Gift Card" />
							</span>
						</li>
						<li>
							<span>Gift card length</span>
							<span class="pull-right">
								<input type="number" name="length" maxlength="2" id="length" value="7" />
								<div>
									<i class="icon-exclamation-sign"></i>
									It should be at least 7!
								</div>
							</span>
						</li>
						<li>
							<span>Gift card prefix</span>
							<span class="pull-right">
								<input type="text" name="prefix" id="prefix" value="" />
							</span>
						</li>
						<li>
							<span>Include the Gift Card ID at the code</span>
							<span class="pull-right">
								<input type="checkbox" name="include_gift_card_id" id="include_gift_card_id" checked="checked">
							</span>
						</li>
						<li>
							<span>Use numbers</span>
							<span class="pull-right">
								<input type="checkbox" name="use_numbers" id="use_numbers" checked="checked">
							</span>
						</li>
						<li>
							<span>Use letters</span>
							<span class="pull-right">
								<input type="checkbox" name="use_letters" id="use_letters">
							</span>
						</li>
						<li>
							<span>Exclude chars</span>
							<span class="pull-right">
								<input type="text" id="exclude_chars" name="exclude_chars" value="0O">
							</span>
						</li>
						<li>
							<label>Chars will be used</label>
							<input type="text" id="chars_to_use" name="chars_to_use" value="" style="width:99%;" readonly="readonly" />
							<span class="chars-alert" style="display:none;">
								<hr />
								<div class="note large">
									<i class="icon-warning-sign"></i>
									Oops! Something wrong! Please review the options!
								</div>
							</span>
						</li>
						<li>
							<span>Possible combinations</span>
							<span class="pull-right" id="possible-combinations">
							</span>
						</li>
						<li>
							<span>Sample of codes</span>
							<span class="pull-right" id="sample">
							</span>
						</li>
					</ul>
				</div>
			</div>
		</div>
	</div>
	<div class=" giftcard-preview">
		<div class="box">
			<div class="box-header">
				<span class="title">Preview</span>
			</div>
			<div class="box-content ">
				<br />
				<div class="giftcard">
					<div class="giftcard-message"></div>
					<div class="help">Questions: Call or text us anytime at (646) 783-1444</div>
					<div class="dollar">$</div><div class="value"><span class="preview-value"></span></div><div class="info">To redeem, go to<span class="url">crunchbutton.com/giftcard/<span class="preview-code"></span></span></div><div class="call">Free Food from
					<br/>Crunchbutton</div><img src="/assets/images/giftcard/print-default.svg?<?php echo date( 'dHis' ); ?>"/>
				</div>
				<br />
			</div>
		</div>
	</div>
</div>
<script>
var lastID = '<?php echo $this->lastone->id_promo; ?>';
function updateValue(){
	var value = $( '#value' ).val();
	if( value == '' ){
		value = 0;
	}
	$( '.preview-value' ).html( value );
}
function updateMessage(){
	$( '.giftcard-message' ).html( $( '#giftcard-message' ).val() );
}
function updateUsedChars(){
	var letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
	var numbers = '0123456789';
	var exclude_chars = $( '#exclude_chars' ).val();
	var chars_to_use = '';
	var prefix = $( '#prefix' ).val();
	if( $( '#use_numbers' ).is( ':checked' ) ){
		chars_to_use += numbers;
	}
	if( $( '#use_letters' ).is( ':checked' ) ){
		chars_to_use += letters;
	}
	var exclude_chars = $( '#exclude_chars' ).val();
	exclude_chars = exclude_chars.split('');
	$( exclude_chars ).each( function( k, v ){
		chars_to_use = chars_to_use.replace( v.toUpperCase(), '' );
	} );
	$( '#chars_to_use' ).val( chars_to_use );
	var length = $( '#length' ).val();
	if( parseInt( length ) > 12 ){
		length = 12;
	}
	if( parseInt( length ) < 4 ){
		length = 4;
	}
	$( '#length' ).val( length );

	length = length - prefix.length;
	if( $( '#include_gift_card_id' ).is( ':checked' ) ){
		length -= lastID.toString().length;
	}

	var possible_combinations = Math.pow( chars_to_use.length, length );

	if( $( '#include_gift_card_id' ).is( ':checked' ) ){
		possible_combinations = parseInt( possible_combinations ) + parseInt( $( '#total' ).val() );
	}
	if( possible_combinations >= 1 && possible_combinations > $( '#total' ).val() ){
		$( '.chars-alert' ).hide();
		$( '.input' ).show();
		$( '.giftcard-preview' ).show();
	} else {
		$( '.chars-alert' ).show();
		$( '.input' ).hide();
		$( '.giftcard-preview' ).hide();
	}

	if( possible_combinations < 1 ){
		possible_combinations = 0;
		$( '#sample' ).html( '' );
	} else {
		sample();
	}
	$( '#possible-combinations' ).html( possible_combinations );
}
function sample(){
	var possible = $( '#chars_to_use' ).val();
	var prefix = $( '#prefix' ).val();
	var length = $( '#length' ).val() - prefix.length;
	var include_gift_card_id = $( '#include_gift_card_id' ).is( ':checked' );
	if( include_gift_card_id ){
		var gift_card_id = lastID;
		length -= gift_card_id.toString().length;
	}
	var text = '';
	var out = '';
	var commas = '';
	for( j = 0; j < 10; j++  ){
		var text = prefix;
		if( include_gift_card_id ){
			text += gift_card_id.toString();
			gift_card_id++;
		}
		for( i=0; i < length; i++ ){
			text += possible.charAt( Math.floor( Math.random() * possible.length ) );
		}
		$( '.preview-code' ).html( text );
		out += commas + text;
		commas = ', ';
	}
	$( '#sample' ).html( out );
}
$(function() {
	updateUsedChars();
	updateMessage();
	updateValue();
	$( '#track' ).iButton( { 	labelOn :"<i class='icon-ok'></i>",
														labelOff :"<i class='icon-remove'></i>",
														handleWidth :30,
														change : function( self ){
																				if(self.is( ':checked' )){
																					$( '.track-fields' ).show();
																				} else {
																					$( '.track-fields' ).hide();
																				}
																			}
												} );

	$( '#use_numbers, #use_letters, #include_gift_card_id' ).iButton( { 	labelOn :"<i class='icon-ok'></i>",
																								labelOff :"<i class='icon-remove'></i>",
																								handleWidth :30,
																								change : function( self ){
																														updateUsedChars();
																												}
																						} );
	$( document ).on( 'keyup, change', '#exclude_chars, #length, #total, #prefix', function() {
		updateUsedChars();
	} );

	$( document ).on( 'keyup, change', '#giftcard-message', function() {
		updateMessage();
	} );

	$( document ).on( 'keyup, change', '#value', function() {
		updateValue();
	} );

	$( '#paid_by' ).change( function(){
		var self = $( this );
		$( '#list_id_restaurant_paid_by' ).hide();
		if( self.val() == 'other_restaurant' ){
			$( '#list_id_restaurant_paid_by' ).show();
		}
	} );

	$(document).on('click', '.admin-giftcard-save', function() {
		App.giftcards.print = false;
		sendForm();
	} );

	$(document).on('click', '.admin-giftcard-print', function() {
	App.giftcards.print = 'default';
	sendForm();
	} );
	$(document).on('click', '.admin-giftcard-print-flyer', function() {
	App.giftcards.print = 'flyer';
	sendForm();
	} );
	$(document).on('click', '.admin-giftcard-print-brown', function() {
	App.giftcards.print = 'brown';
	sendForm();
	} );
});

function sendForm(){
	var value = $.trim( $( '#value' ).val() );
	var id_restaurant = $( '#id_restaurant' ).val();
	var id_group = $( '#id_group' ).val();
	var id_user = $( '#id_user' ).val();
	var total = $( '#total' ).val();
	var message = $( '#giftcard-message' ).val();
	var print = ( App.giftcards.print ) ? App.giftcards.print : 0;

	var id_order_reference = $( '#id_order_reference' ).val();
	var prefix = $( '#prefix' ).val();
	var paid_by = $( '#paid_by' ).val();
	var id_restaurant_paid_by = $( '#id_restaurant_paid_by' ).val();
	var created_by = $.trim( $( '#created_by' ).val() );
	var track = ( $('#track').is(':checked') ? 1 : 0 );
	var notify_phone = $.trim( $( '#notify_phone' ).val() );
	var name = $.trim( $( '#name' ).val() );
	var how_delivery = $.trim( $( '#how_delivery' ).val() );
	var contact = $.trim( $( '#contact' ).val() );
	var note = $.trim( $( '#notes' ).val() );
	var add_as_credit = ( $( '#add_as_credit' ).is( ':checked' ) ) ? 1 : 0;
	var notify_by_email = ( $( '#notify_by_email' ).is( ':checked' ) ) ? 1 : 0;
	var include_gift_card_id = ( $( '#include_gift_card_id' ).is( ':checked' ) ) ? 1 : 0;
	var notify_by_sms = ( $( '#notify_by_sms' ).is( ':checked' ) ) ? 1 : 0;
	var chars_to_use = $.trim( $( '#chars_to_use' ).val() );
	var length = $.trim( $( '#length' ).val() );


	if( value == '' ){
		alert( 'Please type a value!' );
		$( '#value' ).focus();
		return;
	}

	if( total == '' ){
		alert( 'Please type the number of gift cards!' );
		$( '#total' ).focus();
		return;
	}

	if( paid_by == '' ){
		alert( 'Please choose the field "paid by"!' );
		$( '#paid_by' ).focus();
		return;
	}

	if( id_restaurant == '' ){
		alert( 'Please choose a restaurant!' );
		$( '#id_restaurant' ).focus();
		return;
	}

	if( parseInt( length ) < 4 || parseInt( length ) > 12  ){
		alert( 'The gift card length must to be between 4 and 12!' );
		$( '#id_restaurant' ).focus();
		return;
	}

	if( track > 0 ){
		if( notify_phone == '' ){
			alert( 'Please type the phone number that will receive a sms!' );
			$( '#notify_phone' ).focus();
			return;
		}
	}

	var data = { 'value' : value, 'id_user' : id_user, 'id_restaurant' : id_restaurant, 'total' : total,'id_order_reference':id_order_reference, 'paid_by':paid_by, 'id_restaurant_paid_by':id_restaurant_paid_by, 'note' : note, 'created_by' : created_by, 'track' : track, 'notify_phone' : notify_phone, 'name' : name, 'how_delivery' : how_delivery, 'contact' : contact, 'add_as_credit' : add_as_credit, 'notify_by_sms' : notify_by_sms, 'notify_by_email' : notify_by_email, 'chars_to_use' : chars_to_use, 'include_gift_card_id' : include_gift_card_id, 'length' : length, 'print' : print, 'prefix' : prefix, 'id_group' : id_group, 'message' : message };
	var url = App.service + 'giftcard/generate';
	$.ajax({
		type: "POST",
		dataType: 'json',
		data: data,
		url: url,
		success: function( json ) {
			if( json.error ){
				alert( 'Error at adding a new giftcard!' );
			} else {
				alert( 'Gift card(s) created!' );
				if( App.giftcards.print ){
					if( App.giftcards.print == 'flyer' ){
						location.href = '/giftcards/print-flyer/' + json.success;
					} else if( App.giftcards.print == 'brown' ){
						location.href = '/giftcards/print-brown/' + json.success;
					} else {
						location.href = '/giftcards/print/' + json.success;
					}
				} else {
					location.href = '/giftcards';
				}
			}
		},
		error: function( ){
			alert( 'Error at adding a new gift card!' );
		}
	});
}
</script>