<?php
//define this module's name and path
$mn = "odyshop_cart_menu";
include("modules/modules.php");

/*
Available variables:
All system classes
All request variables
$p (the template path)
$mp (the module path)
$sufixID (id sufix for this module's divs)
*/

if($view == "basket" && $section == "users") return; // do not display module in basket page

$marginTop = ($moduleParameters['marginTop'] == "__EMPTY__" || empty($moduleParameters['marginTop'])) ? 0 : $moduleParameters['marginTop'];
$marginRight = ($moduleParameters['marginRight'] == "__EMPTY__" || empty($moduleParameters['marginRight'])) ? 0 : $moduleParameters['marginRight'];
$marginBottom = ($moduleParameters['marginBottom'] == "__EMPTY__" || empty($moduleParameters['marginBottom'])) ? 0 : $moduleParameters['marginBottom'];
$marginLeft = ($moduleParameters['marginLeft'] == "__EMPTY__" || empty($moduleParameters['marginLeft'])) ? 0 : $moduleParameters['marginLeft'];
$paddingTop = ($moduleParameters['paddingTop'] == "__EMPTY__" || empty($moduleParameters['paddingTop'])) ? 0 : $moduleParameters['paddingTop'];
$paddingRight = ($moduleParameters['paddingRight'] == "__EMPTY__" || empty($moduleParameters['paddingRight'])) ? 0 : $moduleParameters['paddingRight'];
$paddingBottom = ($moduleParameters['paddingBottom'] == "__EMPTY__" || empty($moduleParameters['paddingBottom'])) ? 0 : $moduleParameters['paddingBottom'];
$paddingLeft = ($moduleParameters['paddingLeft'] == "__EMPTY__" || empty($moduleParameters['paddingLeft'])) ? 0 : $moduleParameters['paddingLeft'];
$useModuleContainer = ($moduleParameters['useModuleContainer'] == "__EMPTY__" || empty($moduleParameters['useModuleContainer'])) ? 0 : (int)$moduleParameters['useModuleContainer'];
$widthInPixels = ($moduleParameters['widthInPixels'] == "__EMPTY__" || empty($moduleParameters['widthInPixels'])) ? null : (int)$moduleParameters['widthInPixels'];

if(empty($marginTop) && empty($marginRight) && empty($marginBottom) && empty($marginLeft)){
	$Mmargin = "0";
}else{
	$Mmargin = $marginTop." ".$marginRight." ".$marginBottom." ".$marginLeft." ";
}
if(empty($paddingTop) && empty($paddingRight) && empty($paddingBottom) && empty($paddingLeft)){
	$Mpadding = "0";
}else{
	$Mpadding = $paddingTop." ".$paddingRight." ".$paddingBottom." ".$paddingLeft." ";
}

$MenuTheme = $moduleParameters["MenuTheme"];

if(!$parameters->getParameter("basket_enabled"))	return;  // basket disabled, exit cart menu module

$auth = new authentication(false);
$login = $auth->getLoginStatus() != 2;
$userID = ($login) ? $_SESSION['userID'] : NULL;


if($useModuleContainer ){
?>
<div id="<?php echo $mn.$sufixID;?>" data-editable="editable" data-menu="cart" class="responsive_float">
<?php
}
?>

<style  type="text/css">
<?php
if($useModuleContainer ){
	?>
#<?php echo $mn.$sufixID;?>{
	<?php 
	$alignRule = ($moduleParameters['moduleAlign'] == "center") ? "text-align" : "float";
	echo $alignRule.": ".$moduleParameters['moduleAlign'].";".PHP_EOL;
	?>
	margin : <?php echo $Mmargin;?>;
	padding : <?php echo $Mpadding;?>;
<?php
if($widthInPixels){
	?>
	width: <?php echo $widthInPixels;?>px;
	<?php 
}	

if($moduleParameters['moduleAlign'] && $moduleParameters['moduleHR']){ // there are system parameters...

if($moduleParameters['moduleHR'] == "top" || $moduleParameters['moduleHR'] == "bottom"){
?>
	border-<?php echo $moduleParameters['moduleHR'];?> : 1px solid black;
<?php 
}
}
?>
}
<?php
}
if($MenuTheme == "light"){
?>
.open_cart{
	background-position: left bottom; 
	color: #e9e9e9;
}
.open_cart:hover{
	color: #e9e9e9; /* overidde general link behavior*/
}
.open_cart_down_arrow{
	color: #e9e9e9;
}
<?php	
}
?>
#cartMenu<?php echo $sufixID;?>{
	display: none;
	position: absolute;
	top: 25px;
	right:0;
	border: 1px solid black;
	background-color: white;
	z-index: 9999;
	min-width: 400px;
	box-shadow: 2px 2px 16px 0px rgba(0, 0, 0, 0.52);	
}
</style>

<div class="dropDownMenuContainer">
    <a href="javascript:void(0)" onclick="dropDownMenu($(this).attr('id'), 'cartMenu<?php echo $sufixID;?>')" id="cartMenuButton<?php echo $sufixID;?>" class="open_cart">
    <?php echo t("Το καλάθι μου");?><span class="open_cart_down_arrow">▼</span>
    </a>
    <div id="cartMenu<?php echo $sufixID;?>" data-menu="dropdown" class="cartMenu">
    <?php
    $unit_text = $parameters->getParameter('unit_text');
    if($userID){ // registered client
		$basketType = $basket->getBasketTypeID("basket");
		$basketItems = $content->getContent("basket", " active=1 and userID='".$userID."' and basket_type='".$basketType."'", "*", false, $language->getDefaultLanguage(), "content_basket.title");

		if(!count($basketItems)){
			?>
            <div class="empty_basket_text menu_content"><?php echo t("Το καλάθι είναι άδειο");?></div>
            <?php
		}else{
			?>
            <div class="basketItemsContainer menu_content">
            <ul class="dropDownBasketItems">
            <?php
			$totalBasketItems = 0;
			$totalPrice = 0;
			$include_vat = (int)$parameters->getParameter("include_vat");
			foreach($basketItems as $basketItem){
				$product = $content->getContentByID("products", $basketItem->productID, array('id', 'price', 'vat'), array('title'), $langID);
				$vat = $content->getValue("fpa_cat", $product->vat, "percent", $langID);
				$quantity = $basketItem->quantity;
				$price = $eshop->getProductPrice($basketItem->productID, $basketItem->combination, $quantity);
				if($include_vat) $price = $eshop->includeVat($price, $vat);
				$image = $eshop->getProductImage($basketItem->productID, $basketItem->combination);
				$totalBasketItems += $quantity;
				if(empty($include_vat)){
					$totalPrice+= ($eshop->includeVat($price, $vat) * $quantity);
				}else{
					$totalPrice+= ($price * $quantity);
				}
				$taxText = (empty($include_vat)) ? " (+".(float)$vat."% ".t("ΦΠΑ").")" : " (".t("με ΦΠΑ").")";
				if(empty($vat)) $taxText = " (".t("με ΦΠΑ").")";
				?>
            <li><div class="cart_menu_thumb_wrapper">
            	<?php
				if($image){
					?>
                    <img src="<?php echo "mediabank/thumbs/small_".$image;?>">
                    <?php
				}
				?>
                </div>
                <a class="cart_menu_title" href="<?php $sef->url("index.php?section=products&view=node&id=".$product->id);?>"><?php echo $product->title; ?></a>
                <div class="cart_menu_price clearfix"><?php echo $quantity." X ".$eshop->formatPrice($price).$taxText;?></div>
            </li>
            	<?php
			}
			?>
            </ul>
            </div>
            <?php
			$totalText = "&nbsp;&nbsp;(".t("με ΦΠΑ").")";
			?>
            <div class="basketItemsFooter">
            	<div class="cart_menu_results"><?php echo t("Ποσότητα") . " " . $totalBasketItems." | ".t("Σύνολο").": <b>".$eshop->formatPrice($totalPrice)."</b>".$totalText;?></div>
                <a class="dd_button checkout cart_menu_right" href="index.php?section=users&view=basket&langID=<?php echo $langID;?>&checkout=auto"><?php echo t("Ταμείο"); ?></a>
                <a class="dd_button rainbow cart_menu_right" href="index.php?section=users&view=basket&langID=<?php echo $langID;?>"><?php echo t("Μετάβαση στο καλάθι"); ?></a>
            </div>
            <?php
		}
		
	}else{ // visitor
		$basketItems = $basket->sessionBasket;
		if(!count($basketItems)){
			?>
            <div class="empty_basket_text menu_content"><?php echo t("Το καλάθι είναι άδειο");?></div>
            <?php			
		}else{
			?>
            <div class="basketItemsContainer menu_content">
            <ul class="dropDownBasketItems">
            <?php
			$totalBasketItems = 0;
			$totalPrice = 0;
			foreach($basketItems as $basketItem){
				list($uid, $productID, $quantity, $combination) = explode("~|~", $basketItem);
				$product = $content->getContentByID("products", $productID, array('id', 'price', 'vat'), array('title'), $langID);
				$vat = $content->getValue("fpa_cat", $product->vat, "percent", $langID);
				$price = $eshop->getProductPrice($productID, $combination, $quantity);
				if($include_vat) $price = $eshop->includeVat($price, $vat);
				$image = $eshop->getProductImage($productID, $combination);
				$totalBasketItems += $quantity;
				if(empty($include_vat)){
					$totalPrice+= ($eshop->includeVat($price, $vat) * $quantity);
				}else{
					$totalPrice+= ($price * $quantity);
				}
				$taxText = (empty($include_vat)) ? " (+".(float)$vat."% ".t("ΦΠΑ").")" : " (".t("με ΦΠΑ").")";
				if(empty($vat)) $taxText = " (".t("με ΦΠΑ").")";
				?>
            <li><div class="cart_menu_thumb_wrapper">
            	<?php
				if($image){
					?>
                    <img src="<?php echo "mediabank/thumbs/small_".$image;?>">
                    <?php
				}
				?>
                </div>
                <a class="cart_menu_title" href="<?php $sef->url("index.php?section=products&view=node&id=".$product->id);?>"><?php echo $product->title; ?></a>
                <div class="cart_menu_price clearfix"><?php echo $quantity." X ".$eshop->formatPrice($price).$taxText;?></div>
            </li>
            	<?php
			}
			?>
            </ul>
            </div>
            <?php
			$totalText = "&nbsp;&nbsp;(".t("με ΦΠΑ").")";
			?>
            <div class="basketItemsFooter">
            	<div class="cart_menu_results"><?php echo t("Ποσότητα") . " " . $totalBasketItems." | ".t("Σύνολο").": <b>".$eshop->formatPrice($totalPrice)."</b>".$totalText;?></div>
                <a class="dd_button checkout cart_menu_right" href="index.php?section=users&view=basket&langID=<?php echo $langID;?>&checkout=auto"><?php echo t("Ταμείο"); ?></a>
                <a class="dd_button rainbow cart_menu_right" href="index.php?section=users&view=basket&langID=<?php echo $langID;?>"><?php echo t("Μετάβαση στο καλάθι"); ?></a>
            </div>
            <?php			
		}
	}
	?>
    </div>
</div>


<?php
if($useModuleContainer ){
?>
</div>
<?php 
}
?>
