<?php
session_start();
// reset cookie
$site->setCookie('rezgo_gift_card_'.REZGO_CID, '');
if (in_array($_REQUEST['card'], FAKE_GIFT_CARDS)) {

		// give a dummy response
		$card = (object) array(
			'first_name' => 'Info',
			'email' => 'asd@gmail.com',
			'created' => date("U"),
			'sent' => (object) array(
					'to' => 'qwe@gmail.com',
					'message' => 'Lorem ipsum dolor!',
					),
			'amount' => 50,
			'billing' => (object) array(
					'first_name' => 'qwerty',
					'last_name' => 'ptyuio',
					'address_1' => '23 42nd Avenue',
					'address_2' => '',
					'city' => 'Brookfield',
					'state' => 'Columbia',
					'country' => 'ca',
					'postal' => '43212',
					'email' => 'asd@gmail.com',
					'phone' => '604-884-4526',
				),
			);

	$billing = $card->billing;

} else {
	// continue with a real success
	$res = $site->getGiftCard($site->decode(sanitize_text_field($_REQUEST['card']))); 
	$card = $res->card;
	$billing = $card->billing;
}

// continue with response
$company = $site->getCompanyDetails();
$site->readItem($company);
$debug = 0;
unset($_SESSION['gift-card']);
?>

<div class="container-fluid rezgo-container">
	<div class="row">
		<div class="col-12">
			<div class="rezgo-gift-card-container gift-card-receipt">
				<div class="master-heading">
					<p><span>Your Gift Card Receipt</span></p>
				</div>

				<div class="rezgo-gift-card-group balance-section clearfix">
					
					<div class="rezgo-gift-card-head text-center">
						 <i class="fad fa-badge-check" id="badge"></i>
						<p id="rezgo-gift-emailed"><span>Thank you for your gift card purchase. <br> We've sent the gift card details to <?php echo stripslashes($card->sent->to); ?></span></p>
					</div>

					<div class="rezgo-gift-card-head">
						<h3><span>Gift Card Details</span></h3>
					</div>

					<div class="flex-row">

						<div class="flex-50 billing-payment-info-box">
							<p class="rezgo-receipt-pax-label"><span>Gift Card Value</span></p>
							<p class="rezgo-receipt-pax-info"><span><?php echo $site->formatCurrency((float) $card->amount); ?></span></p>
						</div>

						<div class="flex-50 billing-payment-info-box">
							<p class="rezgo-receipt-pax-label"><span>Date Purchased</span></p>
							<p class="rezgo-receipt-pax-info"><span><?php echo date((string) $company->date_format, (int) $card->created); ?></span></p>
						</div>

						<div class="flex-50 billing-payment-info-box">
							<p class="rezgo-receipt-pax-label"><span>Sent To</span></p>
							<p class="rezgo-receipt-pax-info"><span><?php echo $card->first_name; ?> <?php echo $card->last_name; ?> <?php echo (($card->sent->to) ? '('.$card->sent->to.')' : ''); ?></span></p>
						</div>

						<?php if((string) $card->sent->message) { ?>
						<div class="flex-50 billing-payment-info-box">
							<p class="rezgo-receipt-pax-label"><span>Message</span></p>
							<p class="rezgo-receipt-pax-info"><span><?php echo nl2br((string)$card->sent->message); ?></span></p>
						</div>
						<?php } ?>
					</div>
					
				</div>

				<div class="rezgo-gift-card-group balance-section clearfix">
					<div class="rezgo-gift-card-head">
						<h3><span>Billing Information</span></h3>
					</div>
					<div id="billing-info-row" class="flex-row">

						<?php if ($site->exists($billing->first_name)){ ?>
						<div class="flex-50 billing-payment-info-box" id="rezgo-receipt-name">
							<p class="rezgo-receipt-pax-label"><span>Name</span></p>
							<p class="rezgo-receipt-pax-info"><?php echo esc_html($billing->first_name); ?> <?php echo esc_html($billing->last_name); ?></p>
						</div>
						<?php } ?>
						
						<?php if ($site->exists($billing->phone)){ ?>
						<div class="flex-50 billing-payment-info-box" id="rezgo-receipt-phone">
							<p class="rezgo-receipt-pax-label"><span>Phone Number</span></p>
							<p class="rezgo-receipt-pax-info"><?php echo esc_html($billing->phone); ?></p>
						</div>
						<?php } ?>

						<?php if ($site->exists($billing->email)){ ?>
						<div class="flex-50 billing-payment-info-box" id="rezgo-receipt-email">
							<p class="rezgo-receipt-pax-label"><span>Email Address</span></p>
							<p class="rezgo-receipt-pax-info"><?php echo esc_html($billing->email); ?></p>
						</div>
						<?php } ?>

						<?php 
							$billing_address = array((string) $billing->address_1, 
												 	 (string) $billing->address_2,
												 	 (string) $billing->city,
												 	 (string) $billing->address_2,
												 	 (string) $billing->state,
												 	 (string) $billing->country,
												 	 (string) $billing->postal
												);
						?>
						<?php if (array_filter($billing_address)){ ?>
						<div class="flex-33" id="rezgo-receipt-address">
							<p class="rezgo-receipt-pax-label"><span>Address</span></p>
							<p class="rezgo-receipt-pax-info">
								<?php echo esc_html($billing->address_1); ?>
								<?php echo ($site->exists($billing->address_2)) ? '<br>'.esc_html($billing->address_2) : ''; ?>
								<?php echo ($site->exists($billing->city)) ? '<br>'.esc_html($billing->city) : ''; ?>
								<?php echo ($site->exists($billing->state)) ? esc_html($billing->state) : ''; ?>
									<?php foreach ($site->getRegionList() as $iso => $name) { ?>
										<?php if ($iso == $billing->country) { ?>
											<?php echo ucwords($name); ?>
										<?php } ?>
									<?php } ?>
								<?php echo ($site->exists($billing->postal)) ? '<br>'.esc_html($billing->postal) : ''; ?>
							</p>
						</div>
						<?php } ?>

					</div>


					<div class="rezgo-company-info">
						<p>
							<span>Only one gift card may be used per order.</span>

							<br/>

							<a href="javascript:void(0);" onclick="javascript:window.open('<?php echo esc_js($site->base); ?>/terms',null,'width=800,height=600,status=no,toolbar=no,menubar=no,location=no,scrollbars=1');">Click here to view the terms and conditions.</a>
						</p>

						<br/>

						<h3 id="rezgo-receipt-head-provided-by">
							<span>Valid At</span>
						</h3>

						<address>
							<?php $company = $site->getCompanyDetails($booking->cid ?? ''); ?>
							<strong><?php echo esc_html($company->company_name); ?></strong><br />
							<?php echo esc_html($company->address_1); ?><?php if($site->exists($company->address_2)) { ?>, <?php echo esc_html($company->address_2); ?><?php } ?>
							<br />
							<?php echo esc_html($company->city); ?>,
							<?php if ($site->exists($company->state_prov)) { ?><?php echo esc_html($company->state_prov); ?>, <?php } ?>
							<?php echo esc_html($site->countryName($company->country)); ?><br />
							<?php echo esc_html($company->postal_code); ?><br />
							<?php echo esc_html($company->phone); ?><br />
							<?php echo esc_html($company->email); ?>
							<?php if ($site->exists($company->tax_id)) { ?><br />Tax ID: <?php echo esc_html($company->tax_id); ?><?php } ?>
						</address>
					</div>
				</div>
			</div>
		</div>

		<?php if ($debug) { ?>
			<div class="col-12">
				<pre><?php var_dump($card); ?></pre>
			</div>
		<?php } ?>
	</div>	
</div>