<?php
/**
 * JE GroupBuy package
 * @author J-Extension <contact@jextn.com>
 * @link http://www.jextn.com
 * @copyright (C) 2011 - 2012 J-Extension
 * @license GNU/GPL, see LICENSE.php for full license.
**/

// no direct access
defined('_JEXEC') or die;
?>
<form action="<?php echo JRoute::_('index.php'); ?>" method="post" name="adminForm">
<table width="100%" cellpadding="0" cellspacing="0" class="searchBox">
<tr>
<td align="right" style="padding:5px 35px 5px 0px;">
<?php echo JText::_("COM_JEGROUPBUY_CATEGORY"); ?>&nbsp;
<?php
echo JHTML::_('select.genericlist', $this->cNameOption , 'cid', 'class="inputbox" size="1" onChange="document.adminForm.submit();"', 'id','title', $this->cid);
?>
</td>
</tr>
<tr>
<td>

<link rel="stylesheet" type="text/css" href="<?php echo JURI::root().'components/com_jegroupbuy/assets/css/style.css'?>">
<div id="respOutput"></div>

<input type="hidden" name="path" value="<?php echo JURI::base();?>">
<?php
if(count( $this->items ) )
{
	$db	= JFactory::getDBO();
	foreach ($this->items as $items)
	{
		$split_date = explode(" ",$items->deal_to_date);
		$deal_date1 = JHTML::_('date',  $split_date[0]." 23:59:00", "Y-m-d h:i:s");
		$today_date1 = JHTML::_('date',  time(), "Y-m-d h:i:s");


		$total_products = $items->total_bought;
		$deal_date = JHTML::_('date',  $items->deal_to_date, "Y-m-d");
		$today_date = JHTML::_('date',  time(), "Y-m-d");
		//echo $deal_date;
		$query		= $db->getQuery(true);
		$query->select('count(*)');
		$query->from('#__jegroupbuy_payment AS p');
		$query->where('p.p_id = '.$items->id);
		$db->setQuery($query);
		$number_of_bought		= $db->loadResult();

		if(($number_of_bought >= $total_products) || (strtotime($today_date1) > strtotime($deal_date1)))
		{

			if($items->expired == 0)
			{
				$sql = "UPDATE #__jegroupbuy_product SET expired = 1 WHERE id=".$items->id;
				$db->setQuery($sql);
				$db->query();
				$items->expired = 1;
			}
		}else
		{
			if($items->expired == 1)
			{
				$sql = "UPDATE #__jegroupbuy_product SET expired = 0 WHERE id=".$items->id;
				$db->setQuery($sql);
				$db->query();
				$items->expired = 0;
			}
		}

		if($items->expired > 0)
		{
			$product_rul = "javascript:void(0);";
		}else
		{
			$product_rul = JRoute::_('index.php?option=com_jegroupbuy&view=productdetails&pid='.$items->id);
		}
		?>
		<div style="width:250px;margin:5px;" class="prod_container" id="prod_container">
			<div>
				<div>
		      		<div >
						<table width="100%" align="center" border="0" cellapdding="0" cellspacing="0" style="padding:5px;">
							<tr>
								<td colspan="2">
									<span class="prod_title"><a href="<?php echo $product_rul; ?>" style="text-decoration:none;color:#000000;"><?php echo $this->limit_characters( $items->title, 15 ); ?></a></span>
								</td>
							</tr>
							<tr>
								<td colspan="2">
									<table width="100%" align="center" border="0" cellapdding="0" cellspacing="0">
										<tr>
											<td align="center" ><a href="<?php echo $product_rul; ?>" style="text-decoration:none;padding:0px;margin:0px;">
												<?php if($items->imageurl == '' || null) { ?>
													<img style="margin:0px;padding-top:10px;" width = "80%;" src="<?php echo JURI::base().'/components/com_jegroupbuy/assets/images/shopping.jpg'; ?>" border="0">
													<?php } else { ?>
													<img style="margin:0px;padding-top:10px;" width = "80%;" src="<?php echo $items->imageurl; ?>"  border="0">
												<?php } ?>
											</a>
											</td>
										</tr>
									</table>
								</td>
							</tr>
							<tr>
								<td colspan="2" style="border:solid 2px #FFFFFF;height:39px;">
									<table width="100%" align="center" border="0" cellapdding="0" cellspacing="0">
										<tr>
											<td align="center" class="deal_price_msg" valign="middle" width="60%" style="height:39px;">
												<h2 style="margin:0px;padding:0px;color:#FFFFFF;"><?php echo JText::_("COM_JEGROUPBUY_DEAL_PRICE"); ?></h2>
											</td>
											<td align="center" class="deal_price_arrow"  valign="middle" width="40%" style="height:39px;">
												<h2 style="margin:0px;padding:0px;color:#FFFFFF;"><?php echo  $items->currencysymbol.' '.$items->deal_price; ?></h2>
											</td>
										</tr>
									</table>
								</td>
							</tr>
							<tr>
								<td colspan="2" style="padding-top:3px;">
									<table width="100%" align="center" border="0" cellapdding="0" cellspacing="0" style="border:solid 2px #E8E8E8;">
										<tr>
											<td style="height:35px;padding:3px;" width="100%">
												<table width="100%" align="center" border="0" cellapdding="0" cellspacing="0" >
													<tr>
														<td width="100%">
															<table width="100%" class="value_discount_yousave_box" align="center" border="0" cellapdding="0" cellspacing="0" style="color:#FFFFFF;">
																<tr>
																	<td width="33.3%" style="height:33px;">
																		<?php echo JText::_("COM_JEGROUPBUY_DEAL_VALUE"); ?>
																	</td>
																	<td width="33.3%" style="height:33px;">
																		<?php echo JText::_("COM_JEGROUPBUY_DEAL_DISCOUNT"); ?>
																	</td>
																	<td width="33.3%" style="height:33px;">
																		<?php echo JText::_("COM_JEGROUPBUY_DEAL_YOUSAVE"); ?>
																	</td>
																</tr>
															</table>
														</td>
													</tr>
													<tr>
														<td width="100%">
															<table width="100%" align="center" border="0" cellapdding="0" cellspacing="0" >
																<tr>
																	<td width="33.3%" style="height:10px;" class="deal_price_arrow_down">
																		&nbsp;
																	</td>
																	<td width="33.3%" style="height:10px;" class="deal_price_arrow_down">
																		&nbsp;
																	</td>
																	<td width="33.3%" style="height:10px;" class="deal_price_arrow_down">
																		&nbsp;
																	</td>
																</tr>
															</table>
														</td>
													</tr>
													<tr>
														<td width="100%">
															<table width="100%" align="center" border="0" cellapdding="0" cellspacing="0" >
																<tr>
																	<td width="33.3%" style="height:20px;" >
																		<b><?php echo $items->currencysymbol.' '.$items->product_price; ?></b>
																	</td>
																	<td width="33.3%" style="height:20px;" >
																		<b><?php echo $items->product_discount."%"; ?></b>
																	</td>
																	<td width="33.3%" style="height:20px;" >
																		<b><?php
																		 echo $items->currencysymbol.' '.$items->you_save; ?></b>
																	</td>
																</tr>
															</table>
														</td>
													</tr>
												</table>
											</td>
										</tr>
									</table>
								</td>
							</tr>
							<tr>
								<td colspan="2" style="padding:5px;">
									<span class="prod_bought" ><?php echo sprintf(JText::_("COM_JEGROUPBUY_NUMBER_OF_COUPONS"),$number_of_bought,$total_products); ?></span>
								</td>
							</tr>
							<tr>
								<td colspan="2">
								<?php
								//$deal_date = JHTML::_('date',  $items->deal_to_date, "Y-m-d");
								unset($deal_date_sp);
								$deal_date_sp = explode("-",$deal_date);
								?>
					                <p><?php echo JText::_("COM_JEGROUPBUY_TIME_LEFT_TO_BUY"); ?></p>
									<span class="prod_timer">
						                <script language="JavaScript" type="text/javascript">
										TargetDate = "<?php echo $deal_date_sp[1]; ?>/<?php echo $deal_date_sp[2]; ?>/<?php echo $deal_date_sp[0]; ?> 11:59 PM";
						                BackColor = "palegreen";
						                ForeColor = "navy";
						                pID = "<?php echo $items->id; ?>";
						                eCheck = "<?php echo $items->expired; ?>";
						                CountActive = true;
						                CountStepper = -1;
						                LeadingZero = true;
						                number_of_bought = "<?php echo $number_of_bought; ?>";
						                total_products = "<?php echo $total_products; ?>";
						                DisplayFormat = "%%D%% <?php echo JText::_("COM_JEGROUPBUY_DAYS"); ?>, %%H%% <?php echo JText::_("COM_JEGROUPBUY_HOURS"); ?>, %%M%% <?php echo JText::_("COM_JEGROUPBUY_MINUTES"); ?>, %%S%% <?php echo JText::_("COM_JEGROUPBUY_SECONDS"); ?>.";
						                FinishMessage = "<span style='color:red;'><?php echo JText::_("COM_JEGROUPBUY_EXPIRED"); ?></span>";
					                </script>
					                <script language="JavaScript" src="components/com_jegroupbuy/assets/js/countdown.js" type="text/javascript"></script>
									</span>
								</td>
							</tr>
						</table>
 					</div>
		  		</div>
			</div>
		</div>
		<?php
	}
}
?>

</td>
<tr>
	<td>
		<!-- Area for pagination -->
			<div id="jefaqpro-paginationarea" style="text-align : center;">
				<!-- Limit Box Drop down -->
				<div class="je-limitbox">
					<label for="limit">
						<?php echo JText::_('JGLOBAL_DISPLAY_NUM'); ?>
					</label>
					<?php echo $this->pagination->getLimitBox(); ?>
				</div>

				<?php
				if( $this->pagination->get('pages.total') > 1) {
				?>
					<!-- Page counter display -->
					<div class="je-pagecounter">
						<?php
							echo $this->pagination->getPagesCounter();
						?>
					</div>

					<!-- Pagination with page links -->
					<div  id="je-pagination" class="pagination">
						<?php echo $this->pagination->getPagesLinks(); ?>
					</div>
				<?php
				}
				?>
			</div>
		</td>
	</tr>
</tr>
<tr>
	<td>&nbsp;</td>
</tr>
<?php
 if($this->params->get('show_footertext',1))
{  ?>
	<tr>
		<td>
			<p class="copyright" align="center">
				<?php require_once( JPATH_COMPONENT . DS . 'copyright' . DS . 'copyright.php' ); ?>
			</p>
		</td>
	</tr>
<?php } ?>
</table>



<input type="hidden" name="option" value="com_jegroupbuy"/>
<input type="hidden" name="view" value="jegroupbuy"/>
</form>
