<?php
include '../config.php';
$marketid=$_POST["market_id"];
$count=0;
$firstweelList = array();
$secondweekList = array();
$thirdweekList = array();
$fouthList = array();
$fifthList = array();
$marketList = array();
  $sql = "SELECT * FROM   winningbetting_detail WHERE  Date(opening_date) <= NOW()  AND  market_Id='$marketid'";
$result = mysqli_query($conn,$sql);
$maxrows=mysqli_num_rows($result);
if (mysqli_num_rows($result)) {

while($row =mysqli_fetch_array($result))
{
      $openingdate=$row["opening_date"];
      $unixTimestamp = strtotime($openingdate);
      $dayOfWeek=date('D', strtotime($unixTimestamp));
      $date=date('d M y', $unixTimestamp);
      $date= "$dayOfWeek($date)";
      
      $jodivalue=$row["winning_number_second"].$row["winning_number_third"];
     $temp =[
	"panelfirst"=>$row["winning_number_first"],
	"panelsecond"=>$jodivalue,
	"panelthird"=>$row["winning_number_fouth"],
	"Date"=>$date
		]; 
		
	$count++;
	if($count>28)
	{
	   	array_push($fifthList,$temp);  
	}
	else if($count>21)
	{
	   array_push($fouthList,$temp);  
	}
	else if($count>14)
	{
	   array_push($thirdweekList,$temp); 
	}
	else if($count>7)
	{
	   array_push($secondweekList,$temp); 
	}
	else
	{
	   array_push($firstweelList,$temp); 
	}
	
	if($maxrows>7)
	{
	
	if($count==7)
	{
	    if(sizeof($firstweelList)>0)
	   
	   	array_push($marketList,$firstweelList);  
	}
	else if($count==14)
	{
	    if(sizeof($secondweekList)>0)
	   array_push($marketList,$secondweekList);  
	}
	else if($count==21)
	{
	    if(sizeof($thirdweekList)>0)
	   array_push($marketList,$thirdweekList); 
	}
	else if($count==28)
	{
	    if(sizeof($fouthList)>0)
	   array_push($marketList,$fouthList); 
	}
	else
	{
	    if(sizeof($fifthList)>0)
	   array_push($marketList,$fifthList); 
	}
	}
	else
	{
	    if(	$count==$maxrows)
	    {
	       	    if(sizeof($firstweelList)>0)
	   
	   	array_push($marketList,$firstweelList);   
	    }
	}
	

}
	$response = ["status"=>"success","MarketChartList"=>$marketList];
}
else
{
	$response = ["status"=>"failure"];
}
echo json_encode($response);


?>