$variable = $_GET['WineID'];
$listType = $_GET['listType'];
$listName = $_GET['listName'];
$linkCount = $_GET['linkCount'];

 $prevWine = "[maxbutton id='1' url='http://192.168.104.254/wordpress/single-item?WineID=".$prevID."&listType=".$listType."&listName=".$listName."']";

$connection = mysql_connect('localhost', 'root', 'admin');
mysql_query("SET NAMES 'utf8'");
mysql_select_db('WineList'); 
mysqli_set_charset($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";
      }
       elseif($listName=="WhitePrice"){
            $sql="SELECT * FROM Wines WHERE WineType = 'White' AND QtyInStock>0 ORDER BY WinePrice";
      }
      elseif($listName=="RedsAZ"){
            $sql="SELECT * FROM Wines WHERE WineType = 'Red' AND QtyInStock>0 ORDER BY WineName";
      }
      elseif($listName=="RedPrice"){
            $sql="SELECT * FROM Wines WHERE WineType = 'Red' AND QtyInStock>0 ORDER BY WinePrice";
      }
      else{
      
            $sql="SELECT * FROM Wines WHERE (WineProfile1 = '".$listName."' OR WineProfile2 = '".$listName."') AND QtyInStock>0 AND WineType != 'Glass' ORDER BY WinePrice";

      } 
}

// 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=mysql_query($sql);
       while ($row1 = mysql_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://192.168.104.254/wordpress/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){
      $prevWine = "[maxbutton id='1' url='http://192.168.104.254/wordpress/single-item?WineID=".$prevID."&listType=".$listType."&listName=".$listName."']";
      }
      else{
             $prevWine="";
       }
      
      // $homeWine = "[maxbutton id='3' url='http://192.168.104.254/wordpress/".$listType."/?Listing=".$listName."']";
     
      if($linkCount==1){
          $homeWine = "[maxbutton id='3']";
       }
       else{

          $homeWine = "[maxbutton id='3' url='http://192.168.104.254/wordpress/".$listType."/?Listing=".$listName."']";
       }

      if($nextID !=0){
      $nextWine = "[maxbutton id='7' url='http://192.168.104.254/wordpress/single-item?WineID=".$nextID."&listType=".$listType."&listName=".$listName."']";
            
      }
      else{
      $nextWine="";
      }


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

    

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


        if(strlen($row['WineName']."&emsp".$row['WineVintage'])>50){
         $nameFont="1.1em";
         }
        else{
        $nameFont="1.25em";
        }

        if (wonderplugin_is_device('Windows, Mac')){
	
        echo "<table>";
        echo "<colgroup>";
        echo "<col span='1' style='width: 15%;'>";
        echo "<col span='1' style='width: 85%;'>";
        echo "</colgroup>";

        echo "<tr>";
  	echo "<td style='vertical-align:top; text-align:center; font-size: 1.25em'>";
        echo do_shortcode($prevWine);
        echo "&emsp;<b>$".$row['WinePrice']."</b>&emsp;";
        echo do_shortcode($nextWine);
        echo "</td>";
         

         if($row['WineVintage'] != 0){
  	         echo "<td style='vertical-align:top; text-align:left; font-size:".$nameFont."'><b>".$row['WineName']."&emsp; ".$row['WineVintage']."</b></td></tr>";
        }
        else{
               echo "<td style='vertical-align:top; text-align:left; font-size:".$nameFont."'><b>".$row['WineName']."</b></td></tr>";
         }
         echo "</tr>"; 
         echo "<tr>";
         echo "<td style='vertical-align: top; text-align: center;'><img style='width: 20vw; height: 50vh; object-fit: contain;' src='".$row['Image1']."' alt='WineImage'>";
         echo "<br>";
        echo do_shortcode($homeWine);

       echo "</td>";

        }

        else if(wonderplugin_is_device('iPad')){
       
        echo "<table>";
       
        echo "<colgroup>";
        echo "<col span='1' style='width: 22%;'>";
        echo "<col span='1' style='width: 78%;'>";
        echo "</colgroup>";

       echo "<tr>";
  	echo "<td style='vertical-align:top; text-align:center; font-size: 1.25em'>";
 
        echo do_shortcode($prevWine);
 
        echo "&emsp; <b>$".$row['WinePrice']."</b> &emsp;";
        echo do_shortcode($nextWine);
        echo "</td>";
       

         if($row['WineVintage'] != 0){
  	         echo "<td style='vertical-align:top; text-align:left; font-size:".$nameFont."'><b>".$row['WineName']."&emsp; ".$row['WineVintage']."</b></td></tr>";
        }
        else{
               echo "<td style='vertical-align:top; text-align:left; font-size:".$nameFont."'><b>".$row['WineName']."</b></td></tr>";
         }
         echo "</tr>"; 
         echo "<tr>";
         echo "<td style='text-align:center; vertical-align:top;' ><img style='width: 30vw; height: 50vh; object-fit: contain;' src='".$row['Image1']."' alt='WineImage'>";
         echo "<br>";      
          echo do_shortcode($homeWine);

       echo "</td>";


        }

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

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

       echo "<tr>";
  	echo "<td style='vertical-align:top; text-align:center; font-size: 1.25em '>";
        echo do_shortcode($prevWine);
        echo "&nbsp; <b>$".$row['WinePrice']."</b> &nbsp;";
        echo do_shortcode($nextWine);
        echo "</td>";

         if($row['WineVintage'] != 0){
  	         echo "<td style='vertical-align:top; text-align:left; font-size:".$nameFont."'><b>".$row['WineName']."&emsp; ".$row['WineVintage']."</b></td></tr>";
        }
        else{
               echo "<td style='vertical-align:top; text-align:left; font-size:".$nameFont."'><b>".$row['WineName']."</b></td></tr>";
         }
         echo "</tr>"; 
         echo "<tr>";
         echo "<td style='text-align:center; vertical-align:top;' ><img style='width: 30vw; height: 70vh; object-fit: contain;' src='".$row['Image1']."' alt='WineImage'>";
echo "<br>"; 
 echo do_shortcode($homeWine);

       echo "</td>";


        }
         

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

         if($varietalText!=""){
         echo "<td style='vertical-align: top;'><font size='+0'>".$varietalText."</font>";
               echo "<br>";
         }
         else{
        echo "<td style='vertical-align: top;'>";
         }
   
    
       echo "<font size='+1'><b>".$row['WineCountry'].">".$row['WineRegion']."</b></font><br><br>";
    
    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['Desc3'])) {
       echo "<font size='+1'><b>Ratings: &nbsp;</b>".$row['Desc3']."</b><br><font>";
      }
    
  	if (!empty($row['Desc2'])) {
       echo "<font size='+1'><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>";
    
}




