<?php
$tableName="Wines2";
$variable = $_GET['Listing'];
//echo $variable;


if(wonderplugin_is_device('Windows, Mac, Linux')){
$leftPad = 40;
$rightPad = 40;
}
elseif(wonderplugin_is_device('iPad')){
$leftPad = 20;
$rightPad = 20;
}
else{
$leftPad = 0; 
$rightPad = 0;
}

//echo $leftPad;

$leftPad = 40;
$rightPad = 40;
$singlePage="single-item/";

echo"<style> .listView {width: 100%; max-width: 800px; text-align: left; padding-right: ".$rightPad."px; padding-left: ".$leftPad."px; padding-top: 0px; padding-bottom: 0px; margin: auto; }</style>";



  $con = mysqli_connect('localhost', 'mayabeac_wine', '3]eGo-r(Scru',"mayabeac_WineList");
        if (mysqli_connect_errno()) {
            echo"
                Failed to connect to the MySQL DB:".mysqli_connect_error();
            }
           else{


}
 
//echo "<body>";

mysqli_query($con, "SET NAMES 'utf8'");

$sortButton="";

      if($variable=="WhitePrice" || $variable=="RedPrice" ||  $variable=="WhitesAZ" ||  $variable=="RedsAZ" || $variable=="Inventory" || $variable=="All"){

           if($variable=="WhitePrice"){
                 $winetype="White";
                 $sorttype="WinePrice";
                 $winetitle="White Wines (by price)&emsp;<a href='/byProfile/?Listing=WhitesAZ'>Sort A-Z</a>";
                 //$sortButton="[maxbutton id='5' url='http://www.bistrowinelist.com/byProfile/?Listing=WhitesAZ']";
                 $newvariable = "WhitePrice";
           }
           elseif($variable=="RedPrice"){
                $winetype="Red";
                $sorttype = "WinePrice";
               $winetitle="Red Wines (by price)&emsp;<a href='/byProfile/?Listing=RedsAZ'>Sort A-Z</a>";
               //$sortButton="[maxbutton id='5' url='http://www.bistrowinelist.com/byProfile/?Listing=RedsAZ']";
               $newvariable = "RedPrice";
           } 
           elseif($variable=="WhitesAZ"){
                $winetype="White";
                $sorttype="WineName";
               $winetitle="White Wines (A - Z)&emsp;<a href='/byProfile/?Listing=WhitePrice'>Sort by Price</a>";
               //$sortButton="[maxbutton id='4' url='http://www.bistrowinelist.com/byProfile/?Listing=WhitePrice']";
               $newvariable = "WhitesAZ";
           }
           elseif($variable=="RedsAZ"){
                $winetype="Red";
                $sorttype="WineName";
               $winetitle="Red Wines (A - Z)&emsp;<a href='/byProfile/?Listing=RedPrice'>Sort by Price</a>";
               //$sortButton="[maxbutton id='4' url='http://www.bistrowinelist.com/byProfile/?Listing=RedPrice']";
               $newvariable = "RedsAZ";
           }
           elseif($variable=="Inventory"){
                $winetype="All";
                $sorttype="WineType, WineName";
               $winetitle="All Wines A-Z (Reds/Rose/Sparkling/White)";
               $newvariable = "Inventory";
           }
           elseif($variable=="All"){
               $winetype="All";
                $sorttype="WineType, WinePrice";
               $winetitle="All Wines (Sparkling/White/Rose/Red)";
               $newvariable = "All";

           }
           
           //echo "<p class='listView' style='padding-top: 0px;'><b><font size='+2'>".$winetitle."</font></b></p>";
           echo "<p class='listView'><span style=' padding-top: 0px; display: inline-block; font-weight: bold; text-align: left; width:85%; line-height: 150%; font-size: 1.75em'>".$winetitle."</span></p>"; 

           if($variable=="Inventory"){

            $sql="SELECT * FROM ".$tableName." WHERE OutOfStock=0 AND LeaveOffMenu=0 AND Archived=0 AND QtyInStock>0  AND WineType!= 'Glass' ORDER BY ".$sorttype;

            }
            elseif($variable=="All"){
               $sql="SELECT * FROM ".$tableName." WHERE OutOfStock=0 AND LeaveOffMenu=0 AND Archived=0 AND QtyInStock>0  AND WineType!= 'Glass' ORDER BY CASE 
               WHEN WineType = 'Sparkling' THEN 1
               WHEN WineType = 'White' THEN 2
               WHEN WineType = 'Rose' THEN 3
               WHEN WineType = 'Red' THEN 4
               ELSE 5
         END,  WinePrice";
             }

           else{
           $sql="SELECT * FROM ".$tableName." WHERE OutOfStock=0 AND LeaveOffMenu=0 AND Archived=0 AND QtyInStock>0 AND WineType = '".$winetype."' ORDER BY ".$sorttype;
            }

      	   $records=mysqli_query($con, $sql);
      	   $wineCount = 0;
      	   while($wines=mysqli_fetch_assoc($records)){
           
       if(is_null($wines['WhosFavorite'])){
         	$favorite="";
          	}   
          else{
          $favorite="<a href='/byperson/?Listing=".$wines['WhosFavorite']."&listType=byProfile&identifier=".$variable."'>".$wines['FavoriteImage']."</a>";
          	}

          $wineCount=$wineCount+1;
        	
          
          if($wines['WineVintage']==0){
          	
            $vintage=NULL;	

          }
          else{
          	$vintage=$wines['WineVintage'];
              
          }
         if($wines['QtyInStock'] < 3 AND $wines['WineType']!='Glass'){
          $lowStock = "* ";
          }
          else{
          $lowStock = "";
           }

            if($wines['WineSize']<750 AND $wines['WineSize']!=0){
            if($wines['WineSize']==375){    
                $sizeinfo="&nbsp;(half bottle)";
            }
            else{
                  $sizeinfo="&nbsp;(small bottle)";
          }
          }
          elseif($wines['ServingSize'] != 0 AND $wines['ServingSize'] !=5){
            
               $sizeinfo=" (".$wines['ServingSize']." oz)";
            }
          else{
               $sizeinfo="";
          }
                



if($variable=="Inventory"  or $variable=="All"){
  
if($prevWine!=$wines['WineType']){
          echo"<br>";
          echo "<b><p class='listView'><span style='font-size: 1.5em; text-align: left; width:85%; line-height: 150%; display: inline-block;'>".$wines['WineType']."</span></p></b>";  

         }
   
$prevWine=$wines['WineType'];
 
if($variable=="Inventory"){
          $wineID="<a href='/".$singlePage."?WineID=".$wines['ID']."&listType=byProfile&listName=".$newvariable."&linkCount=1#wine".$wineCount."'>".$wines['WineName'].$sizeinfo."&emsp;".$vintage."&emsp; ( ".$wines['QtyInStock']." ) &emsp;".$wines['CellarLocation']."</a>";
}

else{
  $wineID="<b><a href='/".$singlePage."?WineID=".$wines['ID']."&listType=byProfile&listName=".$newvariable."&linkCount=1#wine".$wineCount."'>".$wines['WineName'].$sizeinfo."&emsp;".$vintage."</a></b>";
}

}
else{

           $wineID="<b><a href='/".$singlePage."?WineID=".$wines['ID']."&listType=byProfile&listName=".$newvariable."&linkCount=1#wine".$wineCount."'>".$lowStock.$wines['WineName'].$sizeinfo."&emsp;".$vintage."&emsp;".$favorite."</a></b>";
}
         
             echo "<p class='listView'><span style='font-size: 1.25em; text-align: left; width:85%; line-height: 150%; display: inline-block;'>".$wineID."</span>";
             echo "<a class='anchor' id='wine".$wineCount."'></a>";

                $winePrice="<b>$".$wines['WinePrice']."</b>";
                echo "<span style='display: inline-block; font-size: 1.25em; text-align: right; width:15%; line-height: 150%; '>".$winePrice."</span></p>"; 
               
                 if($wines['WineVarietal']!=""){
                              
                       $wineVarietal=$wines['WineVarietal'];
                     if($variable!="Inventory"){ 
                    echo "<p  class = 'listView' style = 'font-size:1.1em; padding-top: 0px; line-height: 120%'>".$wineVarietal."</p>";
                    }
                  }
               




 $wineCountry=$wines['WineCountry'];
               if(!empty($wines['WineRegion'])){
                  $wineCountry = $wineCountry.">".$wines['WineRegion'];
                  if(!empty($wines['WineSubRegion'])){
                  $wineCountry = $wineCountry.">".$wines['WineSubRegion']; 
                  }
                }
if($variable!="Inventory"){
		echo "<p class = 'listView' style = 'font-size:1.1em; padding-top: 0px; padding-bottom: 5px;  line-height: 120%'>".$wineCountry."</p>"; 

             }
              
               

         }
 

          
             

         

}
elseif($variable=="Glass"){

$sql="SELECT * FROM ".$tableName." WHERE WineType='Glass' ORDER BY WinePrice";
      	$records1=mysqli_query($con, $sql);

      	 echo "<p class='listView'><b><font size='+2'>".$variable."</font></b></p>";
         $wineCount=0;
         while($wines=mysqli_fetch_assoc($records1)){
        $wineCount=$wineCount+1;
        	
          
          if($wines['WineVintage']==0){
          	
            $vintage=NULL;	
          }
          else{
          	$vintage=$wines['WineVintage'];
          }
            if($wines['QtyInStock'] < 3 AND $wines['WineType']!='Glass'){
          $lowStock = "* ";
          }
          else{
          $lowStock = "";
           }
          if($wines['WineSize']<750 AND $wines['WineSize']!=0){
                
          if($wines['WineSize']==375){  
            $sizeinfo="&nbsp;(half bottle)";
           }
           else{
             $sizeinfo="&nbsp;(small bottle)";
          }
          }
          else{
               $sizeinfo="";
          }	

//*******************************************************here

     
           $wineID="<b><a href='/".$singlePage."?WineID=".$wines['ID']."&listType=byProfile&listName=".$variable."&linkCount=1#wine".$wineCount."'>".$lowStock.$wines['WineName'].$sizeinfo."&emsp;".$vintage."&emsp;".$favorite."</a></b>";

   echo "<p class='listView'><span style='font-size: 1.25em; text-align: left; width:85%; line-height: 150%; padding-bottom: 11px; display: inline-block;'>".$wineID."</span>";
	        
                 $winePrice="<b>$".$wines['WinePrice']."</b>";
                 echo "<p class='listView'><span style='font-size: 1.25em; text-align: right; width:15%; line-height: 150%; display: inline-block;'>".$winePrice."</span></p>"; 
  		   if($wines['WineVarietal']!=""){
                              
                       $wineVarietal=$wines['WineVarietal'];
                       echo "<p class='listView'><span style='text-align: left; width:85%; line-height: 40%;'>".$wineVarietal."</span></p>";

                  }
	
  		 $wineCountry=$wines['WineCountry'];
               if(!empty($wines['WineRegion'])){
                  $wineCountry = $wineCountry.">".$wines['WineRegion'];
                  if(!empty($wines['WineSubRegion'])){
                  $wineCountry = $wineCountry.">".$wines['WineSubRegion']; 
                  }
                }

		echo "<p class='listView'><span style='text-align: left; width:85%; line-height: 40%; display:'>".$wineCountry."</span></p>";
                        
             
                  //echo "<br>"; 
    
                  

        }

}


else{
       if($variable=="Dessert Wines and Port"){
          $stockLimit = -10000;
       }
       else{
        $stockLimit = 0;
        }
      

     	$sql="SELECT * FROM ".$tableName." WHERE (WineProfile1='".$variable."' OR WineProfile2='".$variable."') AND QtyInStock !=0  AND WineType!='Glass' AND OutOfStock=0 AND LeaveOffMenu=0 AND Archived=0 ORDER BY WinePrice";
$printBottleHeader = False;
      	$records1=mysqli_query($con, $sql);

         if($variable=='Rose'){
            $winetitle = "Rosé";
          }
         elseif($variable=='Sparkling'){
          $winetitle = "Sparkling & Champagne";
         }
         else{
          $winetitle = $variable;
         }
      	 echo "<p class='listView' style='padding-bottom: 5px;'><b><font size='+2'>".$winetitle."</font></b></p>";
        $wineCount = 0;
        $headerPrinted = False;

      	while($wines=mysqli_fetch_assoc($records1)){
  if(is_null($wines['WhosFavorite'])){
         	$favorite="";
          	}   
          else{
          $favorite="<a href='/byperson/?Listing=".$wines['WhosFavorite']."&listType=byProfile&identifier=".$variable."'>".$wines['FavoriteImage']."</a>";
          	}      

$wineCount=$wineCount+1;
        	
          
          if($wines['WineVintage']==0){
          	
            $vintage=NULL;	
          }
          else{
          	$vintage=$wines['WineVintage'];
          }
            if($wines['QtyInStock'] < 3 AND $stockLimit==0 AND $wines['WineType']!='Glass' AND $wines['QtyInStock'] !=0){
          $lowStock = "* ";
          }
          else{
          $lowStock = "";
           }
           if($wines['WineSize']<750 AND $wines['WineSize']!=0){
           if($wines['WineSize']==375){ 
                $sizeinfo="&nbsp;(half bottle)";
           }
           else{
                 $sizeinfo="&nbsp;(small bottle)";
          }
          }
           elseif($wines['ServingSize'] != 0 AND $wines['ServingSize'] !=5){
            
               $sizeinfo=" (".$wines['ServingSize']." oz)";
            }
          else{
               $sizeinfo="";
          }	


if($headerPrinted==False AND $wines['SoldAs'] == 1){
$printBottleHeader = True;
$headerPrinted=True;
echo "<p class='listView'><span style='font-size: 1.5em; text-align: left; font-weight: bold; width:85%; line-height: 150%; display: inline-block;'>By The Glass</span></p>";
}
elseif($wines['SoldAs'] == 0 AND $printBottleHeader==True){
$printBottleHeader=False;
echo "<p class='listView'><span style='font-size: 1.5em; text-align: left; font-weight: bold; width:85%; line-height: 150%; display: inline-block;'>By The Bottle</span></p>";
}
           $wineID="<b><a href='/".$singlePage."?WineID=".$wines['ID']."&listType=byProfile&listName=".$variable."&linkCount=1#wine".$wineCount."'>".$lowStock.$wines['WineName'].$sizeinfo."&emsp;".$vintage."&emsp;".$favorite."</a></b>";

   echo "<p class='listView'><span style='font-size: 1.25em; text-align: left; width:85%; line-height: 150%; display: inline-block;'>".$wineID."</span>";
   
echo "<a class='anchor' id='wine".$wineCount."'></a>";
        
                 $winePrice="<b>$".$wines['WinePrice']."</b>";
                 echo "<span style='display: inline-block; font-size: 1.25em; text-align: right; width:15%; line-height: 150%;'>".$winePrice."</span></p>"; 
  		   if($wines['WineVarietal']!=""){
                              
                       $wineVarietal=$wines['WineVarietal'];
                       //echo "<p class='listView'><span style='text-align: left; width:85%; line-height: 120%; display: inline-block;'>".$wineVarietal."</span></p>";
                         echo "<p  class = 'listView' style = 'font-size:1.1em; padding-top: 0px; line-height: 120%'>".$wineVarietal."</p>";
                  }
	
  		 $wineCountry=$wines['WineCountry'];
               if(!empty($wines['WineRegion'])){
                  $wineCountry = $wineCountry.">".$wines['WineRegion'];
                  if(!empty($wines['WineSubRegion'])){
                  $wineCountry = $wineCountry.">".$wines['WineSubRegion']; 
                  }
                }

		
                  echo "<p class = 'listView' style = 'font-size:1.1em; padding-top: 0px; padding-bottom: 5px;  line-height: 120%'>".$wineCountry."</p>";      
             
                  //echo "<br>"; 
    
                  

        }
    
  }
echo "<br><p class = 'listView' style='line-spacing: 90%; font-size: .8em; padding-top: 0px; padding-bottom: 0px;'>All prices are in Belize Dollars and include 12.5% GST (Sales Tax).  BZ$2 = US$1</p>";
echo "<p class = 'listView' style='line-spacing: 90%; font-size: .8em; padding-top: 0px; padding-bottom: 0px;'>* Limited Quantities Available</p>";
//echo "</body>";

?>