<?php
//$page = $_SERVER['PHP_SELF'];
//$sec = "10";
//header("Refresh: $sec; url=$page");
$tableName="Wines2";
date_default_timezone_set("America/Belize"); 
  $con = mysqli_connect('localhost', 'mayabeac_wine', '3]eGo-r(Scru',"mayabeac_WineList");
 session_start();
  if(isset($_SESSION['reloadPage'])) {
  unset($_SESSION['reloadPage']);
  echo'
  <script>
  window.location.replace("/****/****/*****.php");
  </script>
  ';
}
$happyHourCount = 0;
$sql="SELECT * FROM settings";
$records3 = mysqli_query($con, $sql);
while($wines=mysqli_fetch_assoc($records3)){
    $happyHourStart = $wines['HappyHourStart'];
    $happyHourEnd = $wines['HappyHourEnd'];
    $happyhourwineprice = $wines['HappyHourPrice'];
    $happyHourShowStart = strtotime('-'.$wines['ShowBeforeHH'].' minutes', strtotime($happyHourStart));
    $happyHourShowStop = strtotime('+'.$wines['ShowAfterHH'].' minutes', strtotime($happyHourEnd));
    $happyHourOffer = $wines['HappyHourOffer'];
}

$sql="SELECT * FROM ".$tableName." WHERE OutOfStock=0 AND Archived=0 AND LeaveOffMenu=0 AND AvailableHappyHour = -1";
$records3 = mysqli_query($con, $sql);
while($wines=mysqli_fetch_assoc($records3)){
$happyHourCount =$happyHourCount +1;
}



$redWineCount=0;
$whiteWineCount=0;
$sql="SELECT * FROM ".$tableName." WHERE WineType != 'Glass' AND QtyInStock > 0 AND OutOfStock=0 AND Archived=0 AND LeaveOffMenu=0";
$records2 = mysqli_query($con, $sql);
while($wines=mysqli_fetch_assoc($records2)){

if($wines['WineType']=='Red'){
$redWineCount=$redWineCount+1;
}
elseif($wines['WineType']=='White'){
$whiteWineCount=$whiteWineCount+1;
}

}

echo "<div style='text-align: center; font-size: 1.5em;'>Our current selection has ".$whiteWineCount." <a href='/byprofile/?Listing=WhitePrice'>Whites</a> and ".$redWineCount."  <a href='/byprofile/?Listing=RedPrice'>Reds</a> by the bottle</div><br>";
echo "<div style='text-align: center; font-size: 1.25em;'>Plus <a href='/byprofile/?Listing=Sparkling'>Champagne/Sparkling Wines</a>, <a href='/byprofile/?Listing=Rose'>Rosés</a> and <a href='/byprofile/?Listing=Dessert Wines and Port'>Dessert Wines</a></div><br>";

//$happyhourwineprice=14;

if (time() >= $happyHourShowStart AND time() <= $happyHourShowStop AND $happyHourCount >0){
$happyhour = True;
echo "<head><meta http-equiv='refresh' content='120'></head>";
}
else{
$happyhour = False;
}


if(!isset($_GET['screen_check']))
{
 /* This code will be executed if screen resolution has not been detected.*/
 echo "<script language='JavaScript'>
 <!-- 
document.location=\"$PHP_SELF?screen_check=done&Width=\"+screen.width+\"&Height=\"+screen.height;
 //-->
 </script>";
}
else 
{    
 /* This code will be executed after screen resolution is detected.*/
    if(isset($_GET['Width']) && isset($_GET['Height'])) {
 // Resolution  detected
 //echo "<h1>Your screen resolution is ".$_GET['Width']." x ".$_GET['Height'].".</h1><br />";
 //Display page as per resolution
 if(($_GET['Width']>1024)&&($_GET['Height']>768)) {
 //echo "<b>This will be displayed when screen resolution is greater than 1024x768.</b>";
 }
 else {
 //Display page as per resolution
 //echo "<b>This will be displayed when screen resolution is either equal to or less than 1024x768.</b>";
 }
     }
     else {
               // Resolution not detected
     }
}


$screenWidth = $_GET['Width'];
$screenHeight = $_GET['Height'];
//echo $screenWidth;
//echo "<br>";
//echo $screenHeight;
//echo "<br>";

if($screenWidth > 1000){
$leftPad = 75; //$screenWidth/2-400;
$rightPad = 0; //$screenWidth/2-400;
}
elseif($screenWidth > 700){
$leftPad = 30; //$screenWidth/2-400;
$rightPad = 0; //$screenWidth/2-400;

}

else{
$leftPad = 0; //$screenWidth/2-400;
$rightPad = 0; //$screenWidth/2-400;

}

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

//echo"<style> span {width: 100%; max-width: 800px; margin-right: auto; margin-left: auto; padding-right: 100px; padding-left: 100px;}</style>";




$variable = 'Glass';

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


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

$sortButton="";
$wineCount=0;
for($x=2; $x<=3; $x++){ 

$wineCount1=0;
if($x==2){

if($happyhour==True){
$sql="SELECT * FROM ".$tableName." WHERE  ((WineType='Glass' AND (AvailableToday = -1 OR AvailableHappyHour = -1))) AND QtyInStock >0 AND OutOfStock=0 AND Archived=0 AND LeaveOffMenu=0 ORDER BY WineType2, WinePrice";
}
else{
$sql="SELECT * FROM ".$tableName." WHERE ((WineType='Glass' AND AvailableToday = -1))  AND QtyInStock >0 AND OutOfStock=0 AND Archived=0 AND LeaveOffMenu=0 ORDER BY WineType2, WinePrice";
}

}
elseif($x==1 AND $happyhour==True){

$sql="SELECT * FROM ".$tableName."  WHERE WineType='Glass' AND AvailableOnRequest = 0 AND AvailableHappyHour = -1  AND QtyInStock <> -1 ORDER BY WineType2, WinePrice"; //currently not used

}
elseif($x==3){
$sql="SELECT * FROM ".$tableName."  WHERE WineType!='Glass' AND ShowOnGlassList=-1 AND QtyInStock >0 AND OutOfStock=0 AND Archived=0 AND LeaveOffMenu=0 ORDER BY WineType2, WinePrice";

}
else{
$sql="SELECT * FROM ".$tableName." WHERE WineName <> 'Mimosa' AND WineType='Glass' AND AvailableToday = 0 AND AvailableOnRequest = -1  AND QtyInStock <> -1 ORDER BY WineType2, WinePrice"; //currently not used


}
//echo "<div style='text-align: center'>";
$records1=mysqli_query($con, $sql);
while($wines=mysqli_fetch_assoc($records1)){
  if($wines['WineName']!='Mimosa'){    
        $wineCount=$wineCount+1;
        $wineCount1=$wineCount1+1;
    }
        if($wineCount1 == 1){
            if($x==2){
                      $prevType= 0;
                      if($happyhour==True){
                        if (date("H:00:00", strtotime($happyHourStart)) == date("H:i:00", strtotime($happyHourStart))){
                          $timeStr = "Happy Hour ". date("ga", strtotime($happyHourStart)).' - ';
                        }
                        else{
                          $timeStr = "Happy Hour ". date("g:ia", strtotime($happyHourStart)).' - ';

                        }

                        if (date("H:00:00", strtotime($happyHourEnd)) == date("H:i:00", strtotime($happyHourEnd))){
                          $timeStr = $timeStr. date("ga", strtotime($happyHourEnd));
                        }
                        else{
                          $timeStr = $timeStr.date("g:ia", strtotime($happyHourEnd));

                        }


                        
                      echo "<p class = 'listView' style='padding-bottom: 10px;'><span style='opacity: 1.0; font-weight: bold; font-size: 1.5em; display: inline-block;'>Today's Wine By the Glass (5 oz pour)</span></p>";
 echo "<p class = 'listView' style='padding-bottom: 10px;'><span style='opacity: 1.0; font-weight: normal; font-size: 1.25em; text-align: right; '>".$timeStr."</span></p>";
                      }
                      else{
                      echo "<p class = 'listView' style = 'padding-bottom: 10px;'><span style='opacity: 1.0; font-weight: bold; font-size: 1.5em;'>Today's Wine By the Glass (5 oz pour)</span></p>";
                      }
                      $opacity = 1.0;
             }
             elseif($x==1 AND $happyhour==True){
                       $prevType= 0;
                       echo "<p class = 'listView'><span style='opacity: 1.0; font-weight: bold; font-size: 1.5em;'>Happy Hour 3pm - 5pm ($12 while they last)</span></p>";
                     $opacity = 1.0;
             }
             elseif($x==3){
                  $prevType= 0;
                  echo "<div style='opacity: 1.0; text-align: left; font-weight: bold; font-size: 1.5em;'><p class = 'listView' style='padding-top: 10px;'>Sparkling (small bottles)</p></div>";
                  $opacity = 1.0;

             }

              else{
                    $prevType= 0;
                    echo "<p class = 'listView'><span style='opacity: 0.5; font-weight: bold; font-size: 1.5em;'>By the Glass (on request - 2 glass minimum)</span></p>";
                    $opacity = 0.5;
              }
         } 	
          
          if($wines['WineVintage']==0){
          	
            $vintage=NULL;	
          }
          else{
          	$vintage=$wines['WineVintage'];
          }
            if($wines['QtyInStock'] < 3 AND $wines['WineType']!='Glass'){
          $lowStock = " ";  
//don't use with by the glass
          }
          else{
          $lowStock = "";
           }
          if($wines['WineSize']<750 AND $wines['WineSize']!=0){
                 $sizeinfo="";    //$sizeinfo="&nbsp;(small bottle)";
          }
          else{
               $sizeinfo="";
          }	

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


if($happyhour==True AND $wines['AvailableHappyHour'] == -1){
$happyhourtext="     <font color='red'>(Happy Hour)</font>";
} 
else{
$happyhourtext="";
}
           $wineID="<b><span style= 'opacity: ".$opacity.";'><a  href='http://www.bistrowinelist.com/".$singlePage."?WineID=".$wines['ID']."&listType=byGlass&listName=".$variable."&linkCount=1#wine".$wineCount."'>".$lowStock.$wines['WineName'].$sizeinfo."&emsp;".$vintage."&emsp;".$happyhourtext."</a></span></b>";



   if($prevID != $wines['WineType2']){
      $prevID = $wines['WineType2'];
      if($prevID == 1){
         echo "<span style='opacity: ".$opacity."; font-weight: bold; font-size: 1.5em; line-height: 50%;'><p class = 'listView'>White</p></span>";
       }
       elseif($prevID == 2){
        // echo "<br>";   
         echo "<br><span style='opacity: ".$opacity."; font-weight: bold; font-size: 1.5em;  line-height: 50%;'><p class = 'listView'>Red</p></span>";
       }
       elseif($prevID == 3){
          // echo "<br>";  
          if($wines['WineSize'] != 187){ 
          echo "<br><span style='opacity:  ".$opacity."; font-weight: bold; font-size: 1.5em;  line-height: 50%;'><p class = 'listView'>Sparkling</p></span>";
       }
       }
       elseif($prevID == 4){
            // echo "<br>";
             echo "<br><span style='opacity:  ".$opacity."; font-weight: bold; font-size: 1.5em;  line-height: 50%;'><p class = 'listView'>Rosé</p></span>";
       }
    }

   echo "<p class= 'listView' style = 'padding-top: 8px;'><span style='font-size: 1.25em; text-align: left; width:85%; line-height: 100%; display: inline-block;'>".$wineID."</span>";
	        
    if($x==2 AND $happyhour==True AND $wines['AvailableHappyHour'] == -1){
                if($happyHourOffer=='Fixed Price'){
                  $happyHourFinal=$happyhourwineprice;
                }
                else if($happyHourOffer=='Amount Off'){
                  $happyHourFinal=$wines['WinePrice'] - $happyhourwineprice;
                }
                else if($happyHourOffer=='Percent Off'){
                  $happyHourFinal=round($wines['WinePrice'] * (1-$happyhourwineprice/100));
                }
                 $winePrice="<del><b>$".$wines['WinePrice']."</del>&nbsp;<font color='red'>$".$happyHourFinal."</font></b>";
    }
                 

    else{
    $winePrice="<b>$".$wines['WinePrice']."</b>";
    }

                 echo "<span style='font-size: 1.25em; text-align: left; width:85%; line-height: 100%;'>".$winePrice."</span></p>"; 

  		   if($wines['WineVarietal']!=""){
                              
                       $wineVarietal=$wines['WineVarietal'];
                       echo "<p  class = 'listView' style = 'font-size:1.1em; padding-top: 0px; line-height: 120%'>".$wineVarietal."</p>";

                  }
                 
	
  		$wineCountry=$wines['WineCountry'].">".$wines['WineRegion'];
             
                 if(substr($wineCountry, strlen($wineCountry)-1,1)==">"){

                       $wineCountry=substr($wineCountry, 0, strlen($wineCountry)-1);
                }
                      $wineCountry=$wineCountry;

		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'><font size='-1'>All prices are in Belize Dollars and include 12.5% GST (Sales Tax).  BZ$2 = US$1</p><font>";
//echo "</div>";