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


}
 
$buttonset = False;
$foods = array();
$lunch = array();
$dinner = array();
$smallplates = array();
$happyhour = array();
$dessert = array();
$breakfast = array();
$lunchCount=0;
$dinnerCount=0;
$smallplateCount=0;
$happyhourCount=0;
$dessertCount=0;
$breakfastCount=0;

$foodCount = 0;

mysqli_query($con, "SET NAMES 'utf8'");
// mysqli_select_db('WineList'); 
$sql="SELECT DISTINCT FoodPairing1 from Wines";
 
$records = mysqli_query($con, $sql); 

while($wine=mysqli_fetch_assoc($records)){
    if(!empty($wine['FoodPairing1'])){
         $foodCount = $foodCount + 1;
         $foods[$foodCount] = $wine['FoodPairing1'];
    }
}
$foodFound = False;
for ($x = 1; $x <= 7; $x++) {
    $foodPairingNo =  "FoodPairing".$x;
    $sql="SELECT DISTINCT ".$foodPairingNo." from Wines";
    $records = mysqli_query($con, $sql); 
    while($wine=mysqli_fetch_assoc($records)){
        if(!empty($wine[$foodPairingNo])){
              
                for ($y = 1; $y <= $foodCount; $y++) {

                       if($foods[$y]==$wine[$foodPairingNo]){
                           $foodFound=True;
                       }

               }   
              if($foodFound==False){
                   $foodCount = $foodCount + 1; 
                    $foods[$foodCount] = $wine[$foodPairingNo];
                    
              }
              $foodFound=False;

       }
    }
}


// echo $foodCount;



$smallplatesCount = 1;

for ($x = 1; $x <= $foodCount; $x++) {

    if(substr($foods[$x],0,2)=="D:"){
    $dinnerCount = $dinnerCount + 1;
    $dinner[$dinnerCount] = substr($foods[$x],-(strlen($foods[$x])-2));
   }
   else if(substr($foods[$x],0,2)=="L:"){
         $lunchCount = $lunchCount + 1;
        $lunch[$lunchCount] = substr($foods[$x],-(strlen($foods[$x])-2));
   }
   else if(substr($foods[$x],0,2)=="Z:"){
         $dessertCount = $dessertCount + 1;
        $dessert[$dessertCount] = substr($foods[$x],-(strlen($foods[$x])-2));
   }
   else if(substr($foods[$x],0,2)=="S:"){
         $smallplateCount = $smallplateCount + 1;
        $smallplates[$smallplateCount] = substr($foods[$x],-(strlen($foods[$x])-2));
   }
   else if(substr($foods[$x],0,2)=="H:"){
         $happyhourCount = $happyhourCount + 1;
        $happyhour[$happyhourCount] = substr($foods[$x],-(strlen($foods[$x])-2));
   }
 else if(substr($foods[$x],0,2)=="B:"){
         $breakfastCount = $breakfastCount + 1;
        $breakfast[$breakfastCount] = substr($foods[$x],-(strlen($foods[$x])-2));
   }

}
sort($lunch);
sort($dinner);
sort($happyhour);
sort($smallplates);
sort($dessert);

//echo "Lunch: ".$lunchCount."<br>";
//echo "Dinner: ".$dinnerCount."<br>";
//echo "Small Plates: ".$smallplateCount."<br>";
//echo "Happy Hour: ".$happyhourCount."<br>";
//echo "Dessert: ".$dessertCount."<br>";

$lineCount = max($lunchCount, $dinnerCount, $smallplateCount, $happyhourCount, $dessertCount);

// echo $lineCount."<br>";
$lineCount1=0;
if(wonderplugin_is_device('Windows')){
$extraColumn="<th></th>";
}
else{
$extraColumn="";
}
echo "<table style='margin-left: auto; margin-right: auto;'>";
echo "<tr><th style='text-align: left; padding-left: 3em; font-size: 1.25em;'>Lunch</th><th  style='text-align: left;  padding-left: 3em; font-size: 1.25em;'>Small Plates</th>".$extraColumn."<th  style='text-align: left;  padding-left: 3em; font-size: 1.25em;'>Dinner</th></tr>";
for ($x = 0; $x <= $lineCount-1; $x++) {
echo "<tr>";


if($lunch[$x]!=""){
$lineCount1 = $lineCount1 + 1;
$checkname = "check";  //.$lineCount1;
echo "<td style = 'font-size: 1.2em;'><input class='messageCheckbox' type='checkbox' value='L:".$lunch[$x]."' name='check'>&nbsp;".$lunch[$x]."</td>";
}
else{
echo "<td></td>";
}


if($smallplates[$smallplatesCount]!=""){
$lineCount1 = $lineCount1 + 1;

$checkname = "check"; //.$lineCount1;
echo "<td style = 'font-size: 1.2em;'><input class='messageCheckbox' type='checkbox' value='S:".$smallplates[$smallplatesCount]."' 
name='check'>&nbsp;".$smallplates[$smallplatesCount]."</td>";
$smallplatesCount = $smallplatesCount + 1;
}
else{
echo "<td></td>";
}

if(wonderplugin_is_device('Windows')){
if($smallplates[$smallplatesCount]!=""){
echo "<td style = 'font-size: 1.2em;'><input class='messageCheckbox' type='checkbox' value='S:".$smallplates[$smallplatesCount]."' name='check'>&nbsp;".$smallplates[$smallplatesCount]."</td>";
$smallplatesCount = $smallplatesCount + 1;
}
else{
echo "<td></td>";
}
}

if($dinner[$x]!=""){
$lineCount1 = $lineCount1 + 1;
$checkname = "check"; //.$lineCount1;
echo "<td style = 'font-size: 1.2em;'><input class='messageCheckbox' type='checkbox' value='D:".$dinner[$x]."' name='check'>&nbsp;".$dinner[$x]."</td>";
}
elseif($buttonset==False){
echo "<td style='text-align: center;'><button onclick='test()'>Find Wines</button></td>";
$buttonset=True;
}



// if($happyhour[$x]!=""){
// echo "<td><input type='checkbox' name='".$happyhour[$x]. " value='".$happyhour[$x]."'>&nbsp;".$happyhour[$x]."</td>";
// }
// else{
// echo "<td></td>";
// }

echo "</tr>";

}
echo "<tr><td></td><td>";
// echo "<tr><td></td><td style='text-align: center;'><button onclick='test()'>Find Wines</button></td><td></td></tr>";
//$temp = "[maxbutton name='ChooseWine']";
//echo do_shortcode($temp);
echo "</td><td></td></tr>";

echo "</table>";