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

	$this->title2 = 'Gift card #' . $this->giftcard->id_promo;
	$this->title2icon = 'file';
?>
<?php
$wasUsed = Crunchbutton_Promo::giftWasAlreadyUsed( $this->giftcard->id_promo );
$credit = $this->giftcard->credit();
if ( $credit->id_credit ) {
	$credit = Crunchbutton_Credit::o( $credit->id_credit );
}
?>
<div class="container-fluid padded">
	<div class="row-fluid">
		<div class="span6">
			<!-- box gift card start -->
			<div class="box">
				<div class="box-header">
					<span class="title">Gift Card</span>
					<ul class="box-toolbar">
						<li>
							<?php if( $wasUsed ) { ?>
								<span class="badge badge-green">Redeemed</span>
							<?php } else { ?>
								<span class="badge badge-black">Not redeemed</span>
							<?php } ?>
						</li>
					</ul>
				</div>
				<div class="box-content ">
					<table class="table table-striped ">
						<tr>
							<td>Code</td>
							<td><?=$this->giftcard->code;?></td>
						</tr>
						<tr>
							<td>Value</td>
							<td>$<?=$this->giftcard->value;?></td>
						</tr>
						<tr>
							<td>Number of views</td>
							<td><?=$this->giftcard->viewed;?></td>
						</tr>
						<?php if( $credit->id_credit ) { ?>
						<tr>
							<td>Total Spent</td>
							<td>$<?php if( $credit->creditSpent() ){ echo $credit->creditSpent(); } else { echo '0'; } ;?></td>
						</tr>
						<tr>
							<td>Credit Left</td>
							<td>$<?php echo $credit->creditLeft();?></td>
						</tr>
						<?php } ?>
						<?php if( $this->giftcard->id_order ) { ?>
						<tr>
							<td>Order</td>
							<td>#<?=$this->giftcard->id_order;?></td>
						</tr>
						<?php } ?>
						<?php if( $this->giftcard->restaurant()->name ) { ?>
						<tr>
							<td>Restaurant</td>
							<td><?=$this->giftcard->restaurant()->name;?></td>
						</tr>
						<?php } ?>
					</table>
				</div>
			</div>
			<!-- box gift card end -->
			<!-- box more info start -->
			<div class="box">
				<div class="box-header">
					<span class="title">More info / Actions</span>
				</div>
				<div class="box-content ">
					<input type="hidden" name="id_promo" id="id_promo" value="<?=$this->giftcard->id_promo;?>" />
					<table class="table table-striped ">
						<?php if( $this->giftcard->user()->id_user ) { ?>
						<tr>
							<td>User ID:
							<input type="hidden" name="id_user" id="id_user" value="<?=$this->giftcard->user()->id_user;?>" />
							</td>
							<td><?=$this->giftcard->user()->id_user;?></td>
						</tr>
						<tr>
							<td>User name:</td>
							<td><?=$this->giftcard->user()->name;?></td>
						</tr>
						<?php } else if( !$this->giftcard->phone && !$this->giftcard->email ) { ?>
						<tr>
							<td>User:</td>
							<td>
								<select id="id_user" class="chzn-select" name="id_user" data-placeholder="Choose an user" style="width:365px;" >
								<option></option>
								<?php foreach( $this->users as $user ) { ?>
									<option value="<?php echo $user->id_user; ?>"><?php echo $user->name; ?> <?php if( $user->phone != '' ) { echo ' - ' . $user->phone ; } ?> <?php if( $user->email != '' ) { echo ' - ' . $user->email; } ?></option>
								<?php } ?>
								</select>
								</td>
						</tr>
						<?php } ?>
						<?php if( $this->giftcard->phone ) { ?>
						<tr>
							<td>Phone:</td>
							<td><?=$this->giftcard->phone;?></td>
						</tr>
						<?php } ?>
						<?php if( $this->giftcard->email ) { ?>
						<tr>
							<td>Email:</td>
							<td><?=$this->giftcard->email;?></td>
						</tr>
						<?php } ?>
						<?php if( $this->giftcard->email_subject ) { ?>
						<tr>
							<td>Subject:</td>
							<td><?=$this->giftcard->email_subject;?></td>
						</tr>
						<?php } ?>
						<?php if( $this->giftcard->email_content ) { ?>
						<tr>
							<td>Content:</td>
							<td><?=$this->giftcard->email_content;?></td>
						</tr>
						<?php } ?>
						<?php if( $this->giftcard->created_by ) { ?>
						<tr>
							<td>Created by:</td>
							<td><?=nl2br( $this->giftcard->created_by );?></td>
						</tr>
						<?php } ?>
						<tr>
							<td>Date:</td>
							<td><?=$this->giftcard->date()->format('M jS Y')?> - <?=$this->giftcard->date()->format('g:i:s A')?></td>
						</tr>
						<?php if( $this->giftcard->paid_by ) { ?>
						<tr>
							<td>Paid by:</td>
							<td><?=$this->giftcard->paid_by;?></td>
						</tr>
						<?php } ?>
						<?php if( $this->giftcard->restaurant_paid_by()->id_restaurant ) { ?>
						<tr>
							<td>Paid by restaurant:</td>
							<td><?=$this->giftcard->restaurant_paid_by()->name;?></td>
						</tr>
						<?php } ?>
						<?php if( $this->giftcard->note ) { ?>
						<tr>
							<td>Note:</td>
							<td><?=nl2br( $this->giftcard->note );?></td>
						</tr>
						<?php } ?>
						<tr>
							<td colspan="2">
								<?php if( !CrunchButton_Promo::giftWasAlreadyUsed( $this->giftcard->id_promo ) ){ ?>
									<a target="_blank" class="btn btn-gray" href="/giftcards/print/<?=$this->giftcard->id_promo;?>"><i class="icon-print"></i> Print</a>&nbsp;
									<a target="_blank" class="btn btn-blue" href="/giftcards/print-flyer/<?=$this->giftcard->id_promo;?>"><i class="icon-picture"></i> Print flyer</a>&nbsp;
								<?php } ?>
								<?php
								if( c::admin()->permission()->check( [ 'global', 'gift-card-all', 'gift-card-create-all', "gift-card-restaurant-{$this->giftcard->id_restaurant}", "gift-card-restaurant-create-{$this->giftcard->id_restaurant}" ] ) ){
								?>
									<?php if( $this->giftcard->phone ) { ?>
										<div class="btn btn-blue admin-giftcard-sms"><i class="icon-phone"></i> Send by sms</div>&nbsp;
									<?php } ?>
									<?php if( $this->giftcard->email ) { ?>
										<div class="btn btn-blue admin-giftcard-email"><i class="icon-envelope"></i> Send by email</div>&nbsp;
									<?php } ?>
									<?php if( !$this->giftcard->user()->id_user && !$this->giftcard->note ) { ?>
										<div class="btn btn-green admin-giftcard-save"><i class="icon-user"></i> Relate user</div>&nbsp;
									<?php } ?>
								<?php } ?>
							</td>
						</tr>
					</table>
				</div>
			</div>
			<!-- box more info end -->
		</div>
		<div class="span6">
			<!-- box history start -->
			<?php
			$debits = $credit->debitHistory();
				if( $debits->count() > 0 ){ ?>
			<div class="box">
				<div class="box-header">
					<span class="title">History</span>
				</div>
				<div class="box-content ">
					<table class="table table-striped ">
						<?php foreach( $debits as $debit ){ ?>
							<?php if( $debit->order()->id_order ) { ?>
								<tr>
									<td>Order:</td>
									<td><a href="/vieworder/<?php echo $debit->order()->uuid; ?>"><?=$debit->order()->id_order;?></a></td>
								</tr>
							<?php } ?>
							<tr>
								<td>Date:</td>
								<td><?=$debit->date()->format('M jS Y')?> - <?=$debit->date()->format('g:i:s A')?></td>
							</tr>
							<tr>
								<td>Charged:</td>
								<td>$<?php if( $debit->value ){ echo $debit->value; } else { echo 0;};?></td>
							</tr>
							<?php if( $debit->note ) { ?>
							<tr>
								<td>Note:</td>
								<td><?php echo $debit->note;?></td>
							</tr>
							<?php } ?>
							<tr>
								<td colspan="2"><hr/></td>
							</tr>
						<?php } ?>
					</table>
				</div>
			</div>
			<?php } ?>
			<!-- box history end -->
			<!-- box track info start -->
			<div class="box">
				<div class="box-header">
					<span class="title">Track info</span>
				</div>
				<div class="box-content ">
					<table class="table table-striped ">
						<?php if( $this->giftcard->track ) { ?>
							<?php if( $this->giftcard->name ) { ?>
							<tr>
								<td>Name:</td>
								<td><?=$this->giftcard->name;?></td>
							</tr>
							<?php } ?>
							<?php if( $this->giftcard->contact ) { ?>
							<tr>
								<td>Contact:</td>
								<td><?=$this->giftcard->contact;?></td>
							</tr>
							<?php } ?>
							<?php if( $this->giftcard->how_delivery ) { ?>
							<tr>
								<td>How delivery:</td>
								<td><?=$this->giftcard->how_delivery;?></td>
							</tr>
							<?php } ?>
							<?php if( $this->giftcard->notify_phone ) { ?>
							<tr>
								<td>Notify phone:</td>
								<td><?=$this->giftcard->notify_phone;?></td>
							</tr>
							<?php } ?>
						<?php } else { ?>
							<tr>
								<td>This gift card is not being tracked.</td>
							</tr>
						<?php } ?>
					</table>
				</div>
			</div>
			<!-- box track info end -->
			<!-- box danger zone start -->
			<?php
			if (c::admin()->permission()->check( [ 'global','gift-card-all', 'gift-card-delete'])) {
			?>
			<div class="box">
				<div class="box-header">
					<span class="title">Danger Zone</span>
				</div>
				<div class="box-content padded">
					<?php if( $wasUsed ){ ?>
						<?php if( $credit->creditLeft() > 0 ) { ?>
							<div class="btn btn-red admin-giftcard-remove-credit">Remove the credit left</div>&nbsp;
							<hr />
							<div class="note large">
								<i class="icon-warning-sign"></i> As this gift card was already redemeed it do could not be deleted! But you can remove the $<?php echo $credit->creditLeft();?> credit left.
							</div>
						<?php } else { ?>
							<div class="note large">
								<i class="icon-warning-sign"></i> This gift card was already redemeed and all its credit was already spend. You could not do nothing anymore.
							</div>
						<?php } ?>
					<?php } else { ?>
						<div class="btn btn-red admin-giftcard-delete">Delete this gift card</div>&nbsp;
					<?php } ?>
				</div>
			</div>
			<?php } ?>
			<!-- box danger zone end -->
		</div>
	</div>
</div>
<script>
$(function() {
	App.giftcards.prepareFormAddUser();
	App.giftcards.prepareFormSendSMS();
	App.giftcards.prepareFormSendEMAIL();
	/* Delete gift card methods */
	$(document).on('click', '.admin-giftcard-delete', function() {
		if( !confirm( 'Are you sure you want to delete this gift card?' ) ){
			return;
		}
		var url = App.service + 'giftcard/delete';
		$.ajax({
			type: "POST",
			dataType: 'json',
			data: { 'id_promo' : '<?=$this->giftcard->id_promo;?>' },
			url: url,
			success: function( json ) {
				if( json.error ){
					if( json.error == 'already used' ){
						alert( 'Oops, this gift card was already redeemed and could not be deleted!' );
					}
				} else {
					alert( 'Gift card removed!' );
					window.location.href = '/giftcards/';
				}
			},
			error: function( ){
				alert( 'Oops!' );
			}
		});
	} );
	/* Delete gift card methods */
	$(document).on('click', '.admin-giftcard-remove-credit', function() {
		if( !confirm( 'Are you sure you want to remove the credit of <?php echo $credit->creditLeft(); ?>?' ) ){
			return;
		}
		var url = App.service + 'giftcard/removecredit';
		$.ajax({
			type: "POST",
			dataType: 'json',
			data: { 'id_promo' : '<?=$this->giftcard->id_promo;?>' },
			url: url,
			success: function( json ) {
				if( json.error ){
					alert( 'Oops, something bad happened!' );
				} else {
					alert( 'Credit removed!' );
					location.reload();
				}
			},
			error: function( ){
				alert( 'Oops!' );
			}
		});
	} );
	$(".chosen-select").chosen();
});
</script>