<?php if (Auth::check() and Auth::user()->isOsaaUser()): ?>

<?php
	$quantities_list = array();
	for ($i = 1; $i <= 20; $i++)
	{
		$quantities_list[$i] = $i;
	}
?>

	
<?php /*  Include the Ticket Policies  */ ?>
<?php echo $__env->make('layouts.ticket_policies', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>


<?php
	// Get the config file path
	$config_file = storage_path() . '/config/tickets.json';

	// Make sure the file exists
	if (!file_exists($config_file)) throw new \Exception ("The tickets configuration file cannot be found.");

	// Read the JSON config file
	$raw_data = file_get_contents($config_file);
	$data = json_decode($raw_data);
	if (is_null($data)) throw new \Exception ("The tickets configuration file cannot be parsed because it is an invalid JSON file.  Refer to this JSON Validator (http://jsonlint.com/) for the specifics on the error, or ask Gibby for assistance.");
?>

<?php if (count($data) > 0): ?>

	<?php foreach ($data as $act): ?>

		<?php /*  Search for this activity  */ ?>
		<?php if (!Helpers::strEqual($activity_slug, $act->activity)) continue;	?>

		<!-- Title -->
		<h2><?php echo $activity_name; ?> Tickets</h2>

		<!-- General Information -->
		<h3 style="color: #990000;">Information</h3>
		<?php echo $act->description; ?>		

		<br class="clear" /> 

		<!-- Events -->
		<?php if (count($act->events) > 0): ?>

			<?php foreach ($act->events as $event): ?>

				<!-- Event -->
				<div class="ui-corner-all" style="border: 2px solid #d9e1e0; width: 100%; height: 185px; background-color: #fcfcfc; margin-bottom: 1em; background-image: url('<?php echo asset('images/icons/vertical_dots.png'); ?>'); background-repeat: repeat-y; background-position: 75% 0; font-size: 9pt;">

					<!-- Left Side -->
					<div style="width: 74%; height: 95%; float: left; padding: 0.15em 0 0.25em 0.25em; margin-right: 1%;">
						
						<!-- Event Title -->
						<div style="font-family: Lucida Console; width: 100%; text-align: center; font-weight: bold; letter-spacing: 4px; font-size: 9pt; text-transform: uppercase; background-color: #eeeeee;">
							- <?php echo $event->title; ?> -
						</div>
						
						<!-- Sub-Title Area -->
						<div style="width: 100%;">
							
							<!-- Date -->
							<div style="float: left; width: 50%;">
								<?php echo date('l, F j', strtotime($event->date)); ?>
							</div>

							<!-- Location -->
							<div style="float: left; width: 50%; text-align: right;">
								<?php if (!is_null($event->location_url)): ?>
									<a href="<?php echo $event->location_url; ?>" target="_blank"><?php echo $event->location; ?></a>
								<?php else: ?>
									<?php echo $event->location; ?>
								<?php endif; ?>
							</div>
						</div>

						<!-- Description -->
						<div style="width: 100%; height: 10.5em; clear: both; overflow: hidden;">
							<?php echo $event->description; ?>									
						</div>

						<!-- Links -->
						<?php if (count($event->links) > 0): ?>
							<div style="">
								<?php for ($i = 0; $i < count($event->links); $i++): ?>
									<a href="<?php echo $event->links[$i]->url; ?>" target="_blank"><?php echo $event->links[$i]->name; ?></a>
									<?php if ($i + 1 < count($event->links)): ?>
										|
									<?php endif; ?>
								<?php endfor; ?>
							</div>
						<?php endif; ?>

					</div>
					<!-- End Left Side -->

					<!-- Right Side -->
					<div style="width: 24%; height: 95%; display: inline-block; padding: 0.15em 0 0.25em 0.25em;">
						<div style="width: 100%; text-align: center; font-style: italic; font-family: Lucida Console; letter-spacing: 0px;">
							Purchase Tickets Online
						</div>

						<?php
							$open = strtotime($event->tickets->open);
							$close = strtotime($event->tickets->close);
							$now = time();

							/* Ticket Statuses:
							 *   -1 = Early
							 *    0 = Available  
							 *    1 = Expired
							 */
							if ($now < $open) $status = -1;
							if ($now >= $open and $now <= $close) $status = 0;
							if ($now > $close) $status = 1;

							if (Input::has('override-status'))
							{
								$status = Input::get('override-status');
							}
						?>

						<?php if ($status == -1): ?>
							<?php /*  Early  */ ?>

							<div style="text-align: center; margin: 1em 0 2em 0; font-weight: bold; font-size: 10pt; color: #c49331; font-family: Lucida Console; letter-spacing: 1px;  text-transform: uppercase;">
								Coming Soon								
							</div>

							<div style="text-align: center; margin-top: 1em;">
								Available Starting<br />
								<?php echo date('g:i A n/d/Y', $open); ?>
							</div>

						<?php elseif ($status == 0): ?>
							<?php /*  Available  */ ?>

							<div style="text-align: center; margin: 0.5em 0 1em 0; font-weight: bold; font-size: 10pt; color: #147300;">
								Available Now Through<br />								
								<?php echo date('g:i A n/d/Y', $close); ?>
							</div>

							<?php if (count($event->tickets->items) > 0): ?>

								<?php foreach ($event->tickets->items as $item): ?>

									<?php echo Form::open(array('url'    => 'https://www.paypal.com/cgi-bin/webscr',
														'method' => 'post')); ?>			

										<?php echo Form::hidden('cmd', '_cart'); ?>
										<?php echo Form::hidden('add', '1'); ?>
										<?php echo Form::hidden('business', 'tickets@osaa.org'); ?>
										<?php echo Form::hidden('bn', 'OSAA_AddToCart_WPS_US'); ?>
										<?php echo Form::hidden('currency_code', 'USD'); ?>
										<?php echo Form::hidden('handling_cart', '3.00'); ?>
										<?php echo Form::hidden('paymentaction', 'sale'); ?>
										<?php echo Form::hidden('lc', 'US'); ?>
										<?php echo Form::hidden('no_shipping', '0'); ?>
										<?php echo Form::hidden('shipping', '0.00'); ?>
										<?php echo Form::hidden('return', url('/activities/' . $activity_slug . '/tickets?test=true')); ?>
										<?php echo Form::hidden('rm', '1'); ?>
										<?php echo Form::hidden('cancel_return', url('/activities/' . $activity_slug . '/tickets?test=true')); ?>
										<?php echo Form::hidden('cpp_cart_border_color', '005fa9'); ?>


										<?php echo Form::hidden('amount', $item->price); ?>
										<?php echo Form::hidden('item_name', $item->name . ' ' . $activity_name . ' Ticket'); ?>
										<?php echo Form::hidden('item_number', strtoupper($event->short . '-' . $activity_slug . '-' . $item->name . '-TIX')); ?>

										<div style="font-weight: bold;">
											<?php echo $item->name; ?> Ticket ($<?php echo $item->price; ?>)
										</div>
										Qty: <?php echo Form::select('quantity', $quantities_list, array('style' => 'font-size: 8pt;')); ?>
										

										<?php echo Form::submit('Add to Cart', array('class' => 'button',
																			 'style' => 'margin-left: 1em; font-size: 8pt;')); ?>

									<?php echo Form::close(); ?>

									<br />

								<?php endforeach; ?>
							<?php else: ?>
								There are no ticket items to sell.
							<?php endif; ?>							

						<?php elseif ($status == 1): ?>
							<?php /*  Expired  */ ?>

							<div style="text-align: center; margin: 1em 0 2em 0; font-weight: bold; font-size: 10pt; color: #8a0000; font-family: Lucida Console; letter-spacing: 1px; text-transform: uppercase;">
								Closed
							</div>

							<div style="text-align: center; margin-top: 1em;">
								<?php echo $event->tickets->closed_note; ?>
							</div>

						<?php endif; ?>								
						
					</div>
					<!-- End Right Side -->
					
				</div>
				<!-- End Event -->

			<?php endforeach; ?>

		<?php else: ?>

			There are no events setup to sell <?php echo $activity_name; ?> tickets online.

		<?php endif; ?>

	<?php endforeach; ?>

<?php endif; ?>


<?php else: ?>
	<h2>OSAA Online Tickets</h2>
	<p>
		&raquo; Online tickets will be available beginning Tuesday, October 22. 
	</p>
<?php endif; ?>