<?php

	$check_restrict_customer="";
		$customer_group_id=0;
		$check_restrict_customer=" and ((p.restrict_to_customers='' OR ISNULL(p.restrict_to_customers)) AND (p.restrict_to_groups='' OR ISNULL(p.restrict_to_groups)))";
		if($FSESSION->is_registered('customer_id'))
		{
			$customer_group_id=get_customer_group_id();
			$check_restrict_customer=substr($check_restrict_customer,0,-1)." OR (p.restrict_to_customers like '%". tep_db_input($FSESSION->customer_id) ."%' OR p.restrict_to_groups like '%". tep_db_input($customer_group_id) ."%'))";
		}
	if(!defined('ALT_GA_TEMPLATE'))
	{
	define('ALT_GA_TEMPLATE', 'no');
	}		
		if(SHOW_GA_SOLDOUT=='yes')
		{
			$soldout=' and products_status<2 ';
		}else
		{
			$soldout=' and products_status>0 ';
		}
		$check_pda_query = tep_db_query("select * from " . TABLE_PRODUCTS . " where products_id = '" . $pid . "'");
        $check_pda = tep_db_fetch_array($check_pda_query);
		//$soldout='and products_status=1 ';
		//We get the actual server date and time
		$serverDate = date('Y-m-d H:i',getServerDate(false));
		//Let's add 45 minutes
		$newtimestamp = strtotime($serverDate . ' + '.ADD_MINUTES.' minutes');
		//Now we have a newdatetime+45
		$stamp= date('Y-m-d H:i', $newtimestamp);
		//If the products_date_available is more than ....we kill the product.
		if($check_pda['products_date_available']>0)
		{
		$expires=" and  p.products_date_available >'" . $stamp . "' ";
		}else
		{
		$expires='';	
		}
		$g_query = tep_db_query("select 
		p.products_id,
		p.color_code,
		p.products_quantity, 
		p.products_price_break,
		pd.products_name,
		pd.products_description,
		p.products_quantity,
		p.products_model,
		p.section_id,
		p.parent_id,p.
		products_sku, 
		p.products_season,
		p.products_image_1,
		p.products_price, 
		p.products_weight, 
		p.products_sort_order, 
		date_format(p.products_date_added,'%Y-%m-%d') as products_date_added, 
		date_format(p.products_last_modified,'%Y-%m-%d') as products_last_modified, 
		date_format(p.products_date_available, '%Y-%m-%d %H:%i') as products_date_available, 
		p.products_status, 
		p.products_tax_class_id,
		p.manufacturers_id,
		p.restrict_to_groups,
		p.restrict_to_customers,
		p.product_type,
		p.master_quantity 
		from " . TABLE_PRODUCTS . " p join ".TABLE_PRODUCTS_TO_CATEGORIES . " p2c on p2c.products_id=p.products_id join ".TABLE_PRODUCTS_DESCRIPTION . " pd on pd.products_id=p.products_id where p.product_type !='P' and p.products_id >'0' " . $expires . " " . $soldout . " and p2c.categories_id = '" . $cPath . "' and pd.language_id = '" . (int)$FSESSION->languages_id . "' ". $check_restrict_customer." order by p.products_id asc");
		if(tep_db_num_rows($g_query) > 0)
		{
	
		$category_ga_query = tep_db_query("select categories_GA,categories_quantity_remaining from " . TABLE_CATEGORIES . " where categories_id = '" . $cPath. "' limit 1");

			if (tep_db_num_rows($category_ga_query)) 
			{ 
			$category_ga = tep_db_fetch_array($category_ga_query);
			if($category_ga['categories_GA']>0)
				{//this is a GA category with master quantity?>
					<div>
					<?php 

							if(HIDE_GA_ONLY_QTY=="no")
							{
							echo TEXT_GA_REMAIN_LEFT . ' <span style="color:#ff0000">' . $category_ga['categories_quantity_remaining'] .'</span> ' . TEXT_GA_REMAIN_RIGHT;
							}else
							{
							echo '<strong>There are tickets remaining for this session.</strong>';
							}
					?>
					</div>
				<?php 
				}
			}

		echo tep_draw_form('add_multi', 'shopping_cart.php?action=add_multi');	
		
		$serverDateNow = date(EVENTS_DATE_FORMAT,getServerDate(false));
				
		// if ($cdate>$serverDateNow)
		// {
		// //$customer_time='Date now is ' . date('M d, Y');
		// $customer_time='Date now is ' . date('M d, Y').' <br>  Time now is ' .date("g:i:a");
		// }elseif($cdate<$serverDateNow)
		// {
		// $customer_time='These ' . ITEMS . ' are done!';
		// }else
		// {	
		// $customer_time='Time now is ' .date("g:i:a");
		// }

		// echo '<h1>' . $customer_time . '</h1>';
		//echo $cdate;
		?>
		
	<div id="ga_listing" class="container  mt-5 pb-2 mb-3">
      <div class="row p-3 text-dark bg-light">  
        <div class="col-md-6">
        <strong><?php echo TEXT_TYPE;?></strong>
        </div>

        <div class="col-md-2">
		<strong><?php echo TEXT_PRICE;?></strong>
        </div>
        <div class="col-md-2">
		<strong><?php echo TEXT_QUANTITY;?></strong>
        </div>
      </div>
<?php 
		
			while ($g = tep_db_fetch_array($g_query)) 
			{
				$pid = $g['products_id'];
				$qty = $g['products_quantity'];
				$color = $g['color_code'];
				$status = $g['products_status'];
				$type = $g['product_type'];
				$pda = $g['products_date_available'];
				$pname = $g['products_name'];
				$pdesc = mb_strimwidth($g['products_description'], 0, 150, "...");
				$img1 = "small/" . $g['products_image_1'];
				$price = $currencies->format(tep_add_tax($g['products_price'], tep_get_tax_rate($g['products_tax_class_id'])));
				//$price1 = $currencies->format($g['products_price']);
				//$price = substr($price1,0,-1);
				$salemaker_text = '';
				//multi buy check for salemaker
	            	$sDate=getServerDate(true);
					$sale_query = tep_db_query("select sale_id,sale_specials_condition, sale_deduction_value, sale_deduction_type,choice_text,choice_warning from " . TABLE_SALEMAKER_SALES . " where sale_discount_type='C'  and ((sale_categories_all='' and sale_products_selected='') or sale_categories_all like '%," . tep_db_input($current_category_id) . ",%' or sale_products_selected like '%," . tep_db_input($pid) .",%') and sale_status = '1' and (sale_date_start <='" . tep_db_input($sDate) . "' or sale_date_start = '0000-00-00') and (sale_date_end >= '" . tep_db_input($sDate) . "' or sale_date_end = '0000-00-00') and (sale_pricerange_from <= " . $g['products_price'] . " or sale_pricerange_from = '0') and (sale_pricerange_to >= " . $g['products_price'] . " or sale_pricerange_to = '0') order by sale_deduction_value");
					
					if(tep_db_num_rows($sale_query) > 0)
					{ 
					$salemaker_text = TEXT_DISCOUNT_ALERT3;
					}
					// if  ($g['products_date_available']>$serverDate){
					// echo "hello  world";
					// }else{
					// echo $serverDate;
					//break should stay enabled!
					 echo '<br>';
					 //echo $stamp;
					 //echo $newtimestamp;
					// }
				?>
				<?php if($status!=2)
				{ 
			
			if (($status == 0) or ($category_ga['categories_GA'] > 0) && ($category_ga['categories_quantity_remaining'] < 1)) 
			{
				$plink = '#';
			}
			else 
			{
				$plink = tep_href_link(FILENAME_PRODUCT_INFO, 'cPath=' . $cPath . '&products_id=' . $pid);
			}
			
				}
			
			?>
		
		<?php if($status!=2)
				{ ?>
			<?php echo'<!-- general admission //-->' . "\n"; ?>

				  <div class="row p-3 effects">
					<div class="col-md-2"><?php
					  echo '<a href="' . $plink . '"><img src="images/' . $img1 . '" width=' . SMALL_IMAGE_WIDTH . ' alt=' . $pname . ' /></a>'; ?>
					</div>
					<div class="col-md-4">
					  <h4><?php echo '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'cPath='.$cPath.'&products_id=' . $pid) . '"><strong>' .  $pname . '</strong></a>';?>
							<?php 
							if(HIDE_GA_QTY=="no")
							{
							echo "<small>(" . $qty . ")</small>"; 
							}
							?></h4>
					  <?php 
					  echo $pdesc; 
					  //echo $pda;
					  ?>

					</div>
					<div class="col-md-2">
					  <h3><?php echo $price;?></h3>
					  <small><?php echo $salemaker_text; ?></small>

					</div>
					<div class="col-md-2">
					  <?php
							if($status == 0) 
							{
								echo tep_template_image_button('button_checkout.gif', SOLD_OUT);
							}
							else 
							{
								if(ALT_GA_TEMPLATE=='yes')
								{
									echo '<table id="gaq">';
									echo '<thead>';
									echo '<tr>';
									echo '<th>';
									echo '<a href="javascript:add_'.$pid.'(-1)"><span class="btn btn-primary"><i class="fa fa-minus"></i></span></a>';
									echo '</th>';
									echo '<th style="float:left">';
									echo tep_draw_input_field('qty['.$pid.']',0,'id="qty['.$pid.']'.'" 
									value="0" 
									style="margin:0;padding:0px; text-align: center;min-width:75px" size="3" onfocus="this.value=\'\';" onBlur="javascript:checkStock(\'\');" onKeyDown="javascript:return numericOnly(event);"');
									echo '</th>';
									echo '<th>';
									echo '<a href="javascript:add_'.$pid.'(1)"><span class="btn btn-primary"><i class="fa fa-plus"></i></span></a>'; 
									echo '</th>';
									echo '</tr>';
									echo '</thead></table>';
									echo'
									  <script type="text/javascript">
										var currentValue_'.$pid.' = 0;
										var add_'.$pid.' = function(valueToAdd){
											currentValue_'.$pid.' += valueToAdd;
											if (currentValue_'.$pid.' < 1){
											 currentValue_'.$pid.' =0;
											// return;
											}
											document.getElementById(\'qty['.$pid.']'.'\').value = currentValue_'.$pid.';
											};
									</script>
									';
									
								}
								else
								{
									echo '<a href="' . $plink . '">' . tep_template_image_button('', IMAGE_BUTTON_BUY_NOW) . '</a>';	
								}
							}
				
						?>
					</div>
					
				</div>
				<?php
					if(ALT_GA_TEMPLATE=='yes')
					{
					?>
					<div class="pull-right"><strong>Please select QUANTITY of tickets and click ADD to Cart button below</strong></div>
					<?php   
					}
				}
			} ?>    
				
			</div>
			  <div class="row">
			<div class="col">
					<?php	
					if ($salemaker_text != '')
					{
					echo '<span  class="discount_link">';
					echo TEXT_DISCOUNT_ALERT4.' ';
					echo '</span>';
					}
					
					if(ALT_GA_TEMPLATE=='yes')
					{
					echo "<span style=\"float:right;cursor:pointer\" onClick='document.forms[\"add_multi\"].submit()'><strong>" .tep_template_image_button('', IMAGE_BUTTON_IN_CART) . "</strong></span>
					&nbsp;";
					}
					//echo " </form>";
					?>
			</div>
  </div></form><!-- eof general admission //-->

<?php
}
?>
