<?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']);

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 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.")";

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



// Now read all the wines using $sql and determine which is the previous and which is the next. These will be the url for the prev and 
// next buttons. Also need to add the listType and listName variables to the url so that this page can know how and what to display.
       $IDarray=array();
       $wineCount=0;
      
       // echo $sql;
       $records1=mysqli_query($con, $sql);
       while ($row1 = mysqli_fetch_assoc($records1)) {
            
             $wineCount = $wineCount+1;
             $IDarray[$wineCount]=$row1['ID'];
                  if($IDarray[$wineCount]==$variable){
                          $offset= $wineCount;        // need to know how many to go back in browser history
                  }
             // echo $IDarray[$wineCount].", ".$variable."<br>";
             

       }
       $offset=-1*($wineCount-$offset);

     // echo $wineCount;
       
      for ($x = 1; $x <= $wineCount; $x++) {

         if($IDarray[$x] == $variable){
		if($x == 1){
	
		$prevID=0;
		$currentID=$array[$x];
		$nextID=$IDarray[$x+1];
		}
		elseif($x == $winecount){
	
		$prevID=$IDarray[$x-1];
		$currentID=$array[$x];
		$nextID=0;
		}
		else{
		$prevID=$IDarray[$x-1];
		$currentID=$array[$x];
		$nextID=$IDarray[$x+1];
	
		}
	}

      }
    

      
            // *disabled for now*  the first time this page is called we load all the urls for every wine in this profile, varietal or country
            // into the browser history then jump to the page for the selected wine
            // this allows the slider to scroll through all pages.
            // the first page in the history will be the listing page that called this so you can swipe back to that.

             //for ($x = 1; $x <= $wineCount; $x++) {
               
              //     $url="http://www.bistrowinelist.com/single-item?WineID=".$IDarray[$x]."&listType=".$listType."&listName=".$listName."&linkCount=2";
               //    echo "<script>push('".$url."'); </script>";
                //  echo $url."<br>";
            // }
  
       
 
// code here to assign $prevID and $nextID to the prev and next buttons




   
      if($prevID !=0){
           if($listType != 'byFood'){      
                   $prevWine = "[maxbutton name='Previous' url='http://www.bistrowinelist.com/".$singlePage."? WineID=".$prevID."&listType=".$listType."&listName=".$listName."']";
           }
           else{
                  $prevWine = "[maxbutton name='Previous' url='http://www.bistrowinelist.com/".$singlePage."? WineID=".$prevID."&listType=byFood&listName=Food&food1=".$food1."&food2=".$food2."&food3=".$food3."&food4=".$food4."']";


           }
      }
      else{
             $prevWine="[maxbutton name='EmptyButton']";
       }
      
      // $homeWine = "[maxbutton id='3' url='http://www.bistrowinelist.com/".$listType."/?Listing=".$listName."']";
     
      if($linkCount==1){
          $homeWine = "[maxbutton name='back']";
       }
       else{

          $homeWine = "[maxbutton name='back' url='http://www.bistrowinelist.com/".$listType."/?Listing=".$listName."']";
       }

      if($nextID !=0){
            if($listType != 'byFood'){    
                   $nextWine = "[maxbutton name='Next' url='http://www.bistrowinelist.com/".$singlePage."?WineID=".$nextID."&listType=".$listType."&listName=".$listName."']";     
             }
              else{
                   $nextWine = "[maxbutton name='Next' url='http://www.bistrowinelist.com/".$singlePage."? WineID=".$nextID."&listType=byFood&listName=Food&food1=".$food1."&food2=".$food2."&food3=".$food3."&food4=".$food4."']";

             }
      }
      else{
      $nextWine="[maxbutton name='EmptyButton']";
      }


	$sql="SELECT * FROM Wines WHERE ID='".$variable."'"; 
	$records=mysqli_query($con, $sql);

    

while ($row = mysqli_fetch_assoc($records)) {

         if($listType=="byCountry"){

               $listTypeName=$row['WineNationality'];
          

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

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

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


//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($listType=='byCountry'){

           if($linkCount==1){
              $browsing=$wineCount+$offset." of ".$wineCount."&nbsp;<a href='http://www.bistrowinelist.com/".$listType."/?Listing=".$listName."'><b>".$listTypeName." Wines</b></a>";
           }
           else{
            $browsing=$wineCount+$offset." 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=$wineCount+$offset." of ".$wineCount."&nbsp;<a href='http://www.bistrowinelist.com/".$listType."/?Listing=".$listName."'><b>".$listTypeName."</b></a>";
                 // $browsing=$wineCount+$offset." of ".$wineCount." ".$listTypeName;
                  $showName=$listName;
         }
         elseif($listType=='byProfile' AND $showName==""){
            $browsing=$wineCount+$offset." of ".$wineCount."&nbsp;<a href='http://www.bistrowinelist.com/".$listType."/?Listing=".$listName."'><b>".$listTypeName." </b></a>";
          //  $browsing=$wineCount+$offset." of ".$wineCount." ".$listTypeName;
            $showName=$listName;
         }
         elseif($listType=="byGlass"){
         $browsing=$wineCount+$offset." of ".$wineCount."&nbsp;<a href='http://www.bistrowinelist.com/".$listType."/?Listing=".$listName."'><b> Wines By The Glass</b></a>";
         // $browsing=$wineCount+$offset." of ".$wineCount." Wines By The Glass";
         }
         
         elseif($listType=="byFood"){

           $browsing=$wineCount+$offset." 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=$wineCount+$offset." of ".$wineCount."&nbsp;<a href='http://www.bistrowinelist.com/".$listType."/?Listing=".$listName."'><b>".$listTypeName."</b></a>";
          // $browsing=$wineCount+$offset." of ".$wineCount." ".$listTypeName." Wines";
}
        
        echo "<body>";
// display: flex; justify-content: center; align-items: center;
 
// echo "<span style=' display: flex;  font-size: 0.8em; font-weight: normal; justify-content: center; align-items: center; padding-top: 0px;'>".$browsing." </span>";


        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;'>".do_shortcode($prevWine)."&emsp;".$browsing."&emsp;".do_shortcode($nextWine)."</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;'>".do_shortcode($prevWine)."&emsp;".$browsing."&emsp;".do_shortcode($nextWine)."</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;'>".do_shortcode($prevWine)."&emsp;".$browsing."&emsp;".do_shortcode($nextWine)."</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;'>".do_shortcode($prevWine)."&emsp;".$browsing."&emsp;".do_shortcode($nextWine)."</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; '>".do_shortcode($prevWine)."&emsp;".$browsing."&emsp;".do_shortcode($nextWine)."</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 "</body>";