<?php
$variable = $_GET['WineID'];
$listType = $_GET['listType'];
$listName = $_GET['listName'];
$linkCount = $_GET['linkCount'];
$food1 = trim($_GET['food1']);
$food2 = trim($_GET['food2']);
$food3 = trim($_GET['food3']);
$food4 = trim($_GET['food4']);
$wineNumber = 0;

if($food1!=""){
$xfood1=substr($food1, (strlen($food1)-2)*-1);
}
if($food2!=""){
$xfood2=substr($food2, (strlen($food2)-2)*-1);
}
if($food3!=""){
$xfood3=substr($food3, (strlen($food3)-2)*-1);
}
if($food4!=""){
$xfood4=substr($food4, (strlen($food4)-2)*-1);
}


   $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{


}

if (wonderplugin_is_device('iPhone')){
$singlePage="singleitem-smallscreen/";
}
else{
$singlePage="single-item/";
}


mysqli_query($con, "SET NAMES 'utf8'");
// mysql_select_db('WineList'); 
// mysqli_set_charset($con, $connection, 'utf8');

if($listType=="byVarietal"){
      $sql="SELECT * FROM Wines WHERE (Category = '".$listName."' OR Category2 = '".$listName."' OR Category3 = '".$listName."') AND QtyInStock>0 AND WineType != 'Glass' ORDER BY WinePrice";
}
elseif($listType=="byCountry"){

      $sql="SELECT * FROM Wines WHERE WineCountry = '".$listName."' 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";
          

}
elseif($listType=="byProfile"){

      if($listName=="WhitesAZ"){
            $sql="SELECT * FROM Wines WHERE WineType = 'White' AND QtyInStock>0 ORDER BY WineName";
            $showName="All Whites A - Z";
            $listTypeName="White";
      }
       elseif($listName=="WhitePrice"){
            $sql="SELECT * FROM Wines WHERE WineType = 'White' AND QtyInStock>0 ORDER BY WinePrice";
            $showName="All Whites by Price";
            $listTypeName="White";
      }
      elseif($listName=="RedsAZ"){
            $sql="SELECT * FROM Wines WHERE WineType = 'Red' AND QtyInStock>0 ORDER BY WineName";
            $showName="All Reds A - Z";
            $listTypeName="Red";
      }
      elseif($listName=="RedPrice"){
            $sql="SELECT * FROM Wines WHERE WineType = 'Red' AND QtyInStock>0 ORDER BY WinePrice";
            $showName="All Reds by Price";
            $listTypeName="Red";
      }    
      
       else{
      
            $sql="SELECT * FROM Wines WHERE (WineProfile1 = '".$listName."' OR WineProfile2 = '".$listName."') AND QtyInStock>0 AND WineType != 'Glass' ORDER BY WinePrice";
     $showName="";
      } 
}
elseif($listType=="byGlass"){

            $sql="SELECT * FROM Wines WHERE WineType = 'Glass' AND AvailableToday = -1 ORDER BY WinePrice";
            $showName="Wines by Glass";
            $listTypeName="Glass";

     
}
elseif($listType=="byFood"){

$foodList = "('".$food1."', '".$food2."', '".$food3."', '".$food4."')";

$sql="SELECT * FROM Wines WHERE ((WineType != 'Glass' AND QtyInStock >0) OR (WineType = 'Glass' AND AvailableToday = -1))  AND (FoodPairing1 IN ".$foodList." OR FoodPairing2 IN ".$foodList." OR FoodPairing3 IN ".$foodList." OR FoodPairing4 IN ".$foodList." OR FoodPairing5 IN ".$foodList." OR FoodPairing6 IN ".$foodList." OR FoodPairing7 IN ".$foodList." OR FoodPairing8 IN ".$foodList." OR FoodPairing9 IN ".$foodList." OR FoodPairing10 IN ".$foodList."  OR FoodPairing11 IN ".$foodList."  OR FoodPairing12 IN ".$foodList."  OR FoodPairing13 IN ".$foodList."  OR FoodPairing14 IN ".$foodList."  OR FoodPairing15 IN ".$foodList.")";

$showName="Food Pairings";
$listTypeName="Food Matches";
}



// Read all wines to determine length of list.
// Need to sort items based on match score (desc) then price (asc). Wines by the glass will appear first as they have 6 added
// to the base score whereas other wines start at 0

       $IDarray=array();
       $wineCount=0;
      $foodWines = array();      //two dimensional array 1= name, 2 = food1, 3 = food2, 4 = food3, 5 = food4, 6 = score, 7 = country, 8 = varietal, 9 = btl or glass, 10 = WineID, 11 = Price. 12 = Desc1, 13 = Desc2, 14= Desc3, 15 = Desc 4, 16 = Image1, 17= Image2, 18 = QtyInStock, 19 = Eyes, 20 = Nose, 21 = Mouth, 22 = Vintage, 23 = Alcohol Content, 24 = Pairing Suggestion 


       $records1=mysqli_query($con, $sql);
       while ($row1 = mysqli_fetch_assoc($records1)) {
            
      $wineCount = $wineCount+1;
     if($listType=="byFood"){
      $foodWines[$wineCount][1] = $wines['WineName'];
      $foodWines[$wineCount][6] = 0;
      $foodWines[$wineCount][7]  = $wines['WineCountry'];
      $foodWines[$wineCount][8] = $wines['WineVarietal'];
      $foodWines[$wineCount][10] = $wines['ID'];
      $foodWines[$wineCount][11] = $wines['WinePrice'];
     
      if($wines['WineType'] == "Glass"){
          $foodWines[$wineCount][6] = 6;
          $foodWines[$wineCount][9] = 0;
          }
      else{
            $foodWines[$wineCount][9] = 1;
      }

if($food1!=""){
if($wines['FoodPairing1']==$food1 OR $wines['FoodPairing2']==$food1 OR $wines['FoodPairing3']==$food1 OR $wines['FoodPairing4']==$food1 OR $wines['FoodPairing5']==$food1 OR $wines['FoodPairing6']==$food1 OR $wines['FoodPairing7']==$food1 OR $wines['FoodPairing8']==$food1 OR $wines['FoodPairing9']==$food1 OR $wines['FoodPairing10']==$food1 OR $wines['FoodPairing11']==$food1 OR $wines['FoodPairing12']==$food1 OR $wines['FoodPairing13']==$food1 OR $wines['FoodPairing14']==$food1 OR $wines['FoodPairing15']==$food1){

$foodWines[$wineCount][2] = 'Yes';
$foodWines[$wineCount][6] =$foodWines[$wineCount][6]+1;
} 
else{
$foodWines[$wineCount][2] = 'No';
}
}

if($food2!=""){
if($wines['FoodPairing1']==$food2 OR $wines['FoodPairing2']==$food2 OR $wines['FoodPairing3']==$food2 OR $wines['FoodPairing4']==$food2 OR $wines['FoodPairing5']==$food2 OR $wines['FoodPairing6']==$food2 OR $wines['FoodPairing7']==$food2 OR $wines['FoodPairing8']==$food2 OR $wines['FoodPairing9']==$food2 OR $wines['FoodPairing10']==$food2 OR $wines['FoodPairing11']==$food2 OR $wines['FoodPairing12']==$food2 OR $wines['FoodPairing13']==$food2 OR $wines['FoodPairing14']==$food2 OR $wines['FoodPairing15']==$food2){

$foodWines[$wineCount][3] = 'Yes';
$foodWines[$wineCount][6] =$foodWines[$wineCount][6]+1;
} 
else{
$foodWines[$wineCount][3] = 'No';
}
}

if($food3!=""){
if($wines['FoodPairing1']==$food3 OR $wines['FoodPairing2']==$food3 OR $wines['FoodPairing3']==$food3 OR $wines['FoodPairing4']==$food3 OR $wines['FoodPairing5']==$food3 OR $wines['FoodPairing6']==$food3 OR $wines['FoodPairing7']==$food3 OR $wines['FoodPairing8']==$food3 OR $wines['FoodPairing9']==$food3 OR $wines['FoodPairing10']==$food3 OR $wines['FoodPairing11']==$food3 OR $wines['FoodPairing12']==$food3 OR $wines['FoodPairing13']==$food3 OR $wines['FoodPairing14']==$food3 OR $wines['FoodPairing15']==$food3){

$foodWines[$wineCount][4] = 'Yes';
$foodWines[$wineCount][6] =$foodWines[$wineCount][6]+1;
} 
else{
$foodWines[$wineCount][4] = 'No';
}
}

if($food4!=""){
if($wines['FoodPairing1']==$food4 OR $wines['FoodPairing2']==$food4 OR $wines['FoodPairing3']==$food4 OR $wines['FoodPairing4']==$food4 OR $wines['FoodPairing5']==$food4 OR $wines['FoodPairing6']==$food4 OR $wines['FoodPairing7']==$food4 OR $wines['FoodPairing8']==$food4 OR $wines['FoodPairing9']==$food4 OR $wines['FoodPairing10']==$food4 OR $wines['FoodPairing11']==$food4 OR $wines['FoodPairing12']==$food4 OR $wines['FoodPairing13']==$food4 OR $wines['FoodPairing14']==$food4 OR $wines['FoodPairing15']==$food4){

$foodWines[$wineCount][5] = 'Yes';
$foodWines[$wineCount][6] =$foodWines[$wineCount][6]+1;
} 
else{
$foodWines[$wineCount][5] = 'No';
}
}
       
//sort Array

foreach ($foodWines as $key => $row) {
    $score[$key]  = $row[6];
    $winePrice[$key] = $row[11];
}

// With the use of the column keys you can now sort entire array by these columns and keep the row index value intact if they are non-numeric. This is good if your using some kind of hashed index.

array_multisort($score, SORT_DESC, $winePrice, SORT_DESC, $foodWines);

    
       }

}
     // echo $wineCount;
       

if($listType!='byFood'){


   
	$records=mysqli_query($con, $sql);

         while ($row = mysqli_fetch_assoc($records)) {
         $wineNumber=$wineNumber + 1;

         if($listType=="byCountry"){

               $listTypeName=$row['WineNationality'];
          

         }
         elseif($listType=="byProfile" AND $listTypeName!=""){


         }
        elseif($listType=="byProfile" AND $listTypeName==""){

          $listTypeName=$listName;
         }
        elseif($listType=="byVarietal"){
          $listTypeName=$listName;
        }


       if($listType=='byCountry'){

           if($linkCount==1){
              $browsing=$wineNumber." of ".$wineCount."&nbsp;<a href='http://www.bistrowinelist.com/".$listType."/?Listing=".$listName."'><b>".$listTypeName." Wines</b></a>";
           }
           else{
            $browsing=$wineNumber." of ".$wineCount."&nbsp;<a href='http://www.bistrowinelist.com/".$listType."/?Listing=".$listName."'><b>".$listTypeName." Wines</b></a>";
            }
           
             if($row['WineType']=='Rose'){
             $showName=$listName."&nbsp;(Rosé)";
             }
             else{
             $showName=$listName."&nbsp;(".$row['WineType']. " Wines)";
             }
        }
        elseif($listType=='byVarietal'){
                  $browsing=$wineNumber." of ".$wineCount."&nbsp;<a href='http://www.bistrowinelist.com/".$listType."/?Listing=".$listName."'><b>".$listTypeName."</b></a>";

                  $showName=$listName;
         }
         elseif($listType=='byProfile'){
            $browsing=$wineNumber." of ".$wineCount."&nbsp;<a href='http://www.bistrowinelist.com/".$listType."/?Listing=".$listName."'><b>".$listTypeName." </b></a>";

            $showName=$listName;
         }
         elseif($listType=="byGlass"){
         $browsing=$wineNumber." of ".$wineCount."&nbsp;<a href='http://www.bistrowinelist.com/".$listType."/?Listing=".$listName."'><b> Wines By The Glass</b></a>";

         }
         
         elseif($listType=="byFood"){

           $browsing=$wineNumber." of ".$wineCount."&nbsp;<a href='http://www.bistrowinelist.com/foodmatches/?food1=".$food1."&food2=".$food2."&food3=".$food3."&food4=".$food4."'><b> Food Matches</b></a>";
           
          }

if($browsing==""){

        // $browsing=$wineNumber." of ".$wineCount."&nbsp;<a href='http://www.bistrowinelist.com/".$listType."/?Listing=".$listName."'><b>".$listTypeName."</b></a>";
}

        
        echo "<body>";


echo "<a class='anchor' id='wine".$wineNumber."'></a>";

//set to 500 now so it does not change - effectively disabling

        if(strlen($row['WineName']."&emsp".$row['WineVintage'])>500){
         $nameFont="1.1em";
         }
        else{
        $nameFont="1.35em";
        }
        
if (wonderplugin_is_device('Windows, Mac')){
        $bodytext="justify";
     
       echo "<table>";
       echo "<colgroup>";
        echo "<col span='1' style='width: 15%;'>";
        echo "<col span='1' style='width: 84%;'>";
        echo "</colgroup>";


        if($row['WineType']=="Glass"){
        $winePrice = "<b>$".$row['WinePrice']."/glass</b>"; 
        }
        else{
        $winePrice= "<b>$".$row['WinePrice']."</b>";
        }

       if($linkCount==1){

  echo "<tr><td style='text-align: center; font-size: 1.5em;  font-weight: bold;'>".$winePrice."</td><td style='text-align: left; font-size: 1.0em;  font-weight: normal;'>".$browsing."</td></tr>";

 }
else{

echo "<tr><td style='text-align: center; font-size: 1.5em;  font-weight: bold;'>".$winePrice."</td><td style='text-align: left; font-size: 1.0em;  font-weight: normal;'>".$browsing."</td></tr>";

 }
      
      

         echo"<tr>";
   
      echo "<td style='vertical-align: top; text-align: center;'><img style='width: 28vw; height: 60vh; object-fit: contain;' src='".$row['Image1']."' alt='WineImage'></td>";
       
         

         if($row['WineVintage'] != 0){
  	         echo "<td style='vertical-align:top; font-weight: normal; text-align: ".$bodytext."; line-height: 1.25em; padding-right: 20px; font-size:".$nameFont."'><b>".$row['WineName']."&emsp; ".$row['WineVintage']."<br>";
        }
        else{
               echo "<td style='vertical-align:top; fonr-weight: normal; text-align: ".$bodytext."; line-height: 1.25em; padding-right: 20px; font-size:".$nameFont."'><b>".$row['WineName']."<br>";
         }


        }

        else if(wonderplugin_is_device('iPad')){

  $bodytext="left";
      echo "<table>";

       echo "<colgroup>";
        echo "<col span='1' style='width: 30%;'>";
        echo "<col span='1' style='width: 69%;'>";
        echo "</colgroup>";


        if($row['WineType']=="Glass"){
        $winePrice = "<b>$".$row['WinePrice']."/glass</b>"; 
        }
        else{
        $winePrice= "<b>$".$row['WinePrice']."</b>";
        }

       if($linkCount==1){

  echo "<tr><td style='text-align: center; font-size: 1.5em;  font-weight: normal;'>".$winePrice."</td><td style='text-align: left; font-size: 1.0em;  font-weight: normal;'>".$browsing."</td></tr>";

 }
else{

echo "<tr><td style='text-align: center; font-size: 1.5em;  font-weight: bold;'>".$winePrice."</td><td style='text-align: left; font-size: 1.0em;  font-weight: normal;'>".$browsing."</td></tr>";

 }
      
      

         echo"<tr>";
   
      echo "<td style='vertical-align: top; text-align: center;'><img style='width: 37vw; height: 65vh; object-fit: contain;' src='".$row['Image1']."' alt='WineImage'></td>";
       
         

         if($row['WineVintage'] != 0){
  	         echo "<td style='vertical-align:top; text-align: ".$bodytext."; line-height: 1.25em; padding-right: 20px; font-size:".$nameFont."'><b>".$row['WineName']."&emsp; ".$row['WineVintage']."</b><br>";
        }
        else{
               echo "<td style='vertical-align:top; text-align: ".$bodytext."; line-height: 1.25em; padding-right: 20px; font-size:".$nameFont."'><b>".$row['WineName']."</b><br>";
         }


        }

      else if(wonderplugin_is_device('iPhone, Android')){

   $bodytext="left";
      


        if($row['WineType']=="Glass"){
        $winePrice = "<b>$".$row['WinePrice']."/glass</b>"; 
        }
        else{
        $winePrice= "<b>$".$row['WinePrice']."</b>";
        }

       if($linkCount==1){


//on small screen, spread the $browsing text and buttons across the whole screen , the price lines up with the first row on the description

 echo "<table>";

echo "<tr><td style='display:block-inline; text-align: center; font-size: 1.1em;  font-weight: normal;'>".do_shortcode($prevWine)."&emsp;".$browsing."&emsp;".do_shortcode($nextWine)."</td></tr></table>";

 }
else{

echo "<table><tr><td style='display:block; text-align: center; font-size: 1.1em;  font-weight: bold; '>".$browsing."</td></tr></table>";

 }
      
      echo "<table>";
       echo "<colgroup>";
        echo "<col span='1' style='width: 35%;'>";
        echo "<col span='1' style='width: 64%;'>";
        echo "</colgroup>";  

         echo"<tr>";
   
      echo "<td style='vertical-align: top; text-align: center; font-size: 1.25em;'><b>".$winePrice."</b><br><br><img style='width: 30vw; height: 70vh; object-fit: contain;' src='".$row['Image1']."' alt='WineImage'></td>";
       
         

         if($row['WineVintage'] != 0){
  	         echo "<td style='vertical-align:top; text-align: ".$bodytext."; line-height: 1.25em; padding-right: 20px; font-size:".$nameFont."'><b>".$row['WineName']."&emsp; ".$row['WineVintage']."</b><br>";
        }
        else{
               echo "<td style='vertical-align:top; text-align: ".$bodytext."; line-height: 1.25em; padding-right: 20px; font-size:".$nameFont."'><b>".$row['WineName']."</b><br>";
         }


        }
         

         //  ///Common Code Here
        

         
  	if($listType=="byFood"){

 $foodarray = array($row['FoodPairing1'], $row['FoodPairing2'], $row['FoodPairing3'], $row['FoodPairing4'], $row['FoodPairing5'], $row['FoodPairing6'], $row['FoodPairing7'], );
         $goodfoods = "";
         $badfoods = "";

          if(in_array($food1, $foodarray)) {
           
                $goodfoods = $goodfoods.", ".$xfood1;
           }
           elseif($food1!='none'){
                  $badfoods = $badfoods.", ".$xfood1;
            }

           if(in_array($food2, $foodarray)) {
           
                $goodfoods = $goodfoods.", ".$xfood2;
           }
           elseif($food2!='none'){
                  $badfoods = $badfoods.", ".$xfood2;
            }

   if(in_array($food3, $foodarray)) {
           
                $goodfoods = $goodfoods.", ".$xfood3;
           }
           elseif($food4!='none'){
                  $badfoods = $badfoods.", ".$xfood3;
            }

   if(in_array($food4, $foodarray)) {
           
                $goodfoods = $goodfoods.", ".$xfood4;
           }
           elseif($food4!='none'){
                  $badfoods = $badfoods.", ".$xfood4;
            }
            
        if($goodfoods!=""){
        $goodfoods=substr($goodfoods, (strlen($goodfoods)-2)*-1);
         echo "<font size='+1'><b>We suggest with: </b>".$goodfoods."<br><font>";
        }

//alternate good food list - show all food listed as pairing with this wine
      





        if($badfoods!=""){
        $badfoods=substr($badfoods, (strlen($badfoods)-2)*-1);
 // leave off for now    echo "<font size='+1'><b>Mabye not so good with: </b>".$badfoods."<br><font>";
        }


          

         }
         if (strpos($row['WineName'], $row['WineVarietal']) !== false){
              $varietalText="";
        }
        else{
               $varietalText=$row['WineVarietal'];
         }

         if($varietalText!=""){
         echo "</b>".$varietalText;
               echo "<br>";
         }
         else{
        // echo "<td style='vertical-align: top; padding-top: 0px; text-align: ".$bodytext."; padding-right: 5em; line-height: 1.75em'>";
         }
   
    
       echo "<font size='+1'><b>".$row['WineCountry'].">".$row['WineRegion']."</b></font><br><br>";
    
if($row['WineType']!='Glass'){   
 echo "<font size='+1'><b>In our Cellar: &nbsp;</b>".$row['QtyInStock']."<br><font>";
}

    if (!empty($row['Eyes'])) {
       echo "<font size='+1'><b>Eyes: &nbsp;</b>".$row['Eyes']."</b><br><font>";
      }
  
  	if (!empty($row['Nose'])) {
       echo "<font size='+1'><b>Nose: &nbsp;</b>".$row['Nose']."</b><br><font>";
      }
  
  	if (!empty($row['Mouth'])) {
       echo "<font size='+1'><b>Mouth: &nbsp;</b>".$row['Mouth']."</b><br><font>";
      }
   
      if (!empty($row['AlcoholContent']) AND $row['AlcoholContent']!=0) {
       echo "<font size='+1'><b>Alcohol Content: &nbsp;</b>".$row['AlcoholContent']."%</b><br><font>";
      }
  	if (!empty($row['Desc3'])) {
       echo "<font size='+1'><b>Ratings: &nbsp;</b>".$row['Desc3']."</b><br><font>";
      }
    
  	if (!empty($row['Desc2'])) {
       echo "<font size='+1'><text align='justify'><b>Why we like it: &nbsp;</b>".$row['Desc2']."</b><br><font>";
      }
    
  	if (!empty($row['PairingSuggestion'])) {
       echo "<br><font size='+1'><b>Pairs with: &nbsp;</b>".$row['PairingSuggestion']."</b><br><font>";
      }
    
  	if (!empty($row['Desc1'])) {
       echo "<br><font size='+1'><b>From the Winemaker: &nbsp;</b>".$row['Desc1']."</b><br><font>";
      }
      echo "<br><font size='-1'>All prices are in Belize Dollars and include 12.5% GST (Sales Tax). BZ$2 = US$1<font>";
    
    echo "</td>";

    // echo "<td valign='top' align='left'>";
    // echo do_shortcode($nextWine);
    // echo "</td>";

  	echo "</tr>";
	echo "</table>";
     echo "<br><br><br><br>";
}
echo "</body>";

}
else{
echo "FOOD TEST";
}