<?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.
**/

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');

// Getting menu id(Itemid)
$itemid = JRequest::getVar('Itemid');
?>

<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','name', $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:%M:%S");
		$deal_date1 = JHTML::_('date',  $split_date[0]." 23:59:00", "%Y-%m-%d %H:%M:%S");
		$today_date1 = JHTML::_('date',  time(), "%Y-%m-%d %H:%M:%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");
		$query = "SELECT count(*) FROM #__jegroupbuy_payment AS p 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&task=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" cellpadding="0" cellspacing="0" style="padding:5px;">
							<tr>
								<td colspan="2" align="center">
									<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" align="center">
									<table width="100%" align="center" border="0" cellpadding="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 JURI::base().'images/jegroupbuy/category/'.$items->imageurl; ?>"  border="0">
														<?php } ?>
												</a>
											</td>
										</tr>
									</table>
								</td>
							</tr>
							<tr>
								<td colspan="2" style="border:solid 2px #FFFFFF;height:39px;" align="center">
									<table width="100%" align="center" border="0" cellpadding="0" cellspacing="0" align="center">
										<tr>
											<td align="center"  class="deal_price_msg" valign="middle" width="60%" style="height:39px;">
												<h2 style="margin:0px;padding:0px;color:#FFFFFF;text-align:center;"><?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;text-align:center;"><?php if($items->currencysymbol == "1") $symbol = "&#36";
												elseif($items->currencysymbol == "2") $symbol = "&#163";
												elseif($items->currencysymbol == "3") $symbol = "&#165";
												elseif($items->currencysymbol == "4") $symbol = "&#128";
												else $symbol = "INR";
												 echo $symbol.' '.$items->deal_price; ?></h2>
											</td>
										</tr>
									</table>
								</td>
							</tr>
							<tr>
								<td colspan="2" style="padding-top:3px;" align="center">
									<table width="100%" align="center" border="0" cellpadding="0" cellspacing="0" style="border:solid 2px #E8E8E8;">
										<tr>
											<td style="height:35px;padding:3px;" width="100%">
												<table width="100%" align="center" border="0" cellpadding="0" cellspacing="0" >
													<tr>
														<td width="100%">
															<table width="100%" class="value_discount_yousave_box" align="center" border="0" cellpadding="0" cellspacing="0" style="color:#FFFFFF;">
																<tr>
																	<td width="33.3%" style="height:33px;" align="center">
																		<?php  echo JText::_("COM_JEGROUPBUY_DEAL_VALUE"); ?>
																	</td>
																	<td width="33.3%" style="height:33px;" align="center">
																		<?php echo JText::_("COM_JEGROUPBUY_DEAL_DISCOUNT"); ?>
																	</td>
																	<td width="33.3%" style="height:33px;" align="center">
																		<?php echo JText::_("COM_JEGROUPBUY_DEAL_YOUSAVE"); ?>
																	</td>
																</tr>
															</table>
														</td>
													</tr>
													<tr >
														<td width="100%" align="center">
															<table width="100%" align="center" border="0" cellpadding="0" cellspacing="0" >
																<tr>
																	<td width="33.3%" style="height:9px;" class="deal_price_arrow_down" >
																		&nbsp;
																	</td>
																	<td width="33.3%" style="height:9px;" class="deal_price_arrow_down">
																		&nbsp;
																	</td>
																	<td width="33.3%" style="height:9px;" class="deal_price_arrow_down">
																		&nbsp;
																	</td>
																</tr>
															</table>
														</td>
													</tr>
													<tr>
														<td width="100%">
															<table width="100%" align="center" border="0" cellpadding="0" cellspacing="0" >
																<tr>
																	<td width="33.3%" style="height:20px;" align="center">
																		<b><?php echo $symbol.' '.$items->product_price; ?></b>
																	</td>
																	<td width="33.3%" style="height:20px;" align="center">
																		<b><?php echo $items->product_discount."%"; ?></b>
																	</td>
																	<td width="33.3%" style="height:20px;" align="center">
																		<b><?php echo $symbol.' '. $items->you_save; ?></b>
																	</td>
																</tr>
															</table>
														</td>
													</tr>
												</table>
											</td>
										</tr>
									</table>
								</td>
							</tr>
							<tr>
								<td colspan="2" style="padding:5px;" align="center">
									<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" align="center">
								<?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
	}
} else {
	JError::raiseWarning('', JText::_('NOT_ADDED_PRODUCTS'));
}
?>
</td>
</tr>
<tr>
	<td>
		<div id="je-pagination" >
			 <?php echo $this->pageNav->getListFooter(); ?>
		</div>
	</td>
</tr>
<tr>
	<td>&nbsp;</td>
</tr>
<?php if($this->settings == '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>