<?php
$variable = $_GET['Listing'];
$filter = $_GET['Filter'];
//echo $filter;
  $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{


}
$variable = str_replace('%20',' ', $variable);
 
mysqli_query($con, "SET NAMES 'utf8'");

if(empty($filter)){

$sql="SELECT DISTINCT Category FROM Wines WHERE ((QtyInStock > 0 AND WineType<>'Glass') OR (QtyInStock = 0 AND SoldAs = 1 AND WineType2 = 6)) AND WineCountry='".$variable."' ORDER BY WineType";
 
$records = mysqli_query($con, $sql); 

$linecount = 1;
$category = array();
$categoryCount=0;


while($wine=mysqli_fetch_assoc($records)){
     if(!empty($wine['Category'])){
          $categoryCount = $categoryCount + 1;
          $category[$categoryCount] = $wine['Category'];
        
    }
}

$sql="SELECT DISTINCT Category2 FROM Wines WHERE ((QtyInStock > 0 AND WineType<>'Glass') OR (QtyInStock = 0 AND SoldAs = 1 AND WineType2 = 6)) AND WineCountry='".$variable."' ORDER BY WineType";
 
$records = mysqli_query($con, $sql); 
while($wine=mysqli_fetch_assoc($records)){
     if(!empty($wine['Category2'])){
          $categoryFound = 0;
           for ($x = 1; $x <= $categoryCount; $x++) {
                 if($category[$x] == $wine['Category2']){
                  
                        $categoryFound = 1;
                  }
           }
           if($categoryFound == 0){
              $categoryCount = $categoryCount + 1;
              $category[$categoryCount] = $wine['Category2'];
          }
    }
}

sort($category);


$categoryList = "<a href='http://www.bistrowinelist.com/byCountry/?Listing=".$variable."&Filter=".$category[0]."'>".$category[0]."</a>" ;
for ($x = 1; $x <= $categoryCount-1; $x++) {
$temp = "<a href='http://www.bistrowinelist.com/byCountry/?Listing=".$variable."&Filter=".$category[$x]."'>".$category[$x]."</a>" ;;
$categoryList = $categoryList."&emsp;".$temp;
//echo $category[$x]."<br>";
}
//echo $categoryList;
}



     echo "<body>";

if(!empty($filter)){
      echo "<p><span style=' display: inline-block; font-weight: bold; text-align: left; width:85%; line-height: 150%; font-size: 200%'>".$variable." (".$filter.")</span>";
}
else{
  echo "<p><span style=' display: inline-block; font-weight: bold; text-align: left; width:85%; line-height: 150%; font-size: 200%'>".$variable."</span>";

echo "<span style=' display: inline-block; font-weight: bold; text-align: left; width:85%; line-height: 150%; font-size: 100%'>".$categoryList."</span>";
}

echo "</p>";
       $wineTypes = array();
       $wineTypes[1] = "Sparkling";
       $wineTypes[2] = "White";
       $wineTypes[3] = "Rose";
       $wineTypes[4] = "Red";
       $wineTypes[5] = "Dessert";
       $wineCount=0;
        for ($x = 1; $x <= 5; $x++) {
	
if(empty($filter)){
     	$sql="SELECT * FROM Wines WHERE (QtyInStock>0 OR SoldAs = 1) AND WineCountry='".$variable."' AND WineType='".$wineTypes[$x]."' ORDER BY WinePrice";
}
else{
     	$sql="SELECT * FROM Wines WHERE (QtyInStock>0 OR SoldAs = 1) AND WineCountry='".$variable."' AND WineType='".$wineTypes[$x]."' AND (Category = '".$filter."' OR Category2 = '".$filter."') ORDER BY WinePrice";
//echo $sql;
}
      	$records1=mysqli_query($con, $sql);
 
      	$recordcount=1;
        
      	while($wines=mysqli_fetch_assoc($records1)){
              $wineNationality = $wines['WineNationality'];
              $wineCount=$wineCount+1;
               if($recordcount == 1){
               
              if($wineTypes[$x]=="Rose"){
                 $wineType2="Rosé";
               }
               elseif($wineTypes[$x]=="Sparkling"){
               $wineType2="Sparkling and Champagne";
                }
                elseif($wineTypes[$x]=="Dessert"){
               $wineType2="Dessert Wines & Port"; 
                }
               else{
               $wineType2=$wineTypes[$x];
               }

              echo "<span style='font-weight: bold; text-align: left; width:85%; line-height: 150%; display: inline-block; font-size: 1.5em'>".$wineType2."</span><br>";

	       $recordcount = 2;
               }
          //echo "<div style='line-height: 100%;'>";
          if($wines['WineVintage']==0){
          	
            $vintage=NULL;	
          }
          else{
          	$vintage=$wines['WineVintage'];
          }
         if($wines['QtyInStock'] < 3 AND $wines['WineType']!='Glass' AND $wines['QtyInStock'] !=0 ){  
          $lowStock = "* ";
          }
          else{
          $lowStock = "";
           }

           if($wines['WineSize']<750 AND $wines['WineSize']!=0){
           $sizeinfo="&nbsp (small bottle)";
            }
            else{
            $sizeinfo="";
             }


  if (wonderplugin_is_device('Windows,Mac,Linux,iPad')){
$singlePage="single-item/";
}
else{
$singlePage="singleitem-smallscreen/";
}

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

            
            
     echo "<div style='line-height: 140%;'>";
            echo "<span style='display: inline-block; font-size: 1.25em; text-align: left; width:85%;'>".$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%;'>".$winePrice."</span><br>"; 
             
               $wineCountry=$wines['WineRegion'];

               if($wines['WineVarietal']!=""){
                              
                       $wineVarietal=$wines['WineVarietal'];
                      
                       echo "<span style=' display: inline-block; text-align: left; width:85%;'>".$wineVarietal."</span><br>";

                  }

		echo "<span style=' display: inline-block; text-align: left; width:85%;'>".$wineCountry."</span><br>";
                
                
               echo "<br>";
                        	
      	  echo "</div>";
        }
      
  
}

if(!empty($filter)){

$filterList1 = "<a href='http://www.bistrowinelist.com/byCountry/?Listing=".$variable."&Filter=".$category[0]."'>".$wineNationality." Wines</a>" ;
$filterList2 = "<a href='http://www.bistrowinelist.com/byVarietal/?Listing=".$filter.">All ".$filter."</a>" ;

echo "<span style=' display: inline-block; text-align: left; width:85%;'>".$filterList1."&emsp;".$filterList2."</span><br>";
}

echo "<br><font size='-1'>All prices are in Belize Dollars and include 12.5% GST (Sales Tax).&nbsp;BZ$2 = US$1<font>";
echo "<br><font size='-1'>* Limited Quantities Available<font>";
echo "</body>";