<?php
include '../config.php';
$type=$_POST["type"];
$count=0;
$firstweelList = array();
$secondweekList = array();
$thirdweekList = array();
$fouthList = array();
$fifthList = array();
$marketIdarray = array();
$marketnamearray = array();
$datearray=array();

$mainresultarray=array();


if($type=="1")
{

    $qry="Select * from market where market_type='Delhi' AND active_status!='Removed'";
    $result = mysqli_query($conn,$qry);
    $maxrows=mysqli_num_rows($result);
    if (mysqli_num_rows($result)) {
    
    	while($row =mysqli_fetch_array($result))
    	{
         
    		array_push($marketIdarray ,$row["market_id"]);
    		array_push($marketnamearray ,$row["market_name"]);
    	}
    }
    
    $lastmonth= date('Y-m-d', strtotime('-30 days', strtotime(date('Y-m-d'))));
    
    $datearray=createDateRangeArray($lastmonth,date('Y-m-d'));
    
    $tempresultarray=array();
    $jodivalue=$currdate; 
    $temp=["ResultColumn"=> "Date"];
    array_push($tempresultarray ,$temp); 
    
    // echo json_encode($marketname);
    for($i=0;$i<sizeof($marketnamearray);$i++)
    {
       $marketname=$marketnamearray[$i];
        $temp=["ResultColumn"=> $marketname];
        array_push($tempresultarray ,$temp); 
    }
    $temp=["ResultRow"=> $tempresultarray];
    array_push($mainresultarray ,$temp);
    
     $currdate="";
    for($i=sizeof($datearray)-1;$i>0;$i--)
    {
        $currdate=$datearray[$i];
        
        $resultarray=array();
        $jodivalue=$currdate; 
        $temp=["ResultColumn"=> $jodivalue];
        array_push($resultarray ,$temp);
        
        for($j=0;$j<sizeof($marketIdarray);$j++)
        {
            $marketId=  $marketIdarray[$j];
         $sql = "SELECT * FROM   winningbetting_detail WHERE  Date(opening_date) =Date($currdate)  AND  market_Id='$marketId'";
            $result2 = mysqli_query($conn,$sql);
            $maxrows=mysqli_num_rows($result2);
            if (mysqli_num_rows($result2))
            {
            
                while($row =mysqli_fetch_array($result2))
                {
                    $jodivalue=$row["winning_number_second"].$row["winning_number_third"]; 
                    if(empty($jodivalue) || $jodivalue==null)
                    {
                       $jodivalue="**"; 
                    }
                    $temp=["ResultColumn"=> $jodivalue];
                    array_push($resultarray ,$temp);
                    
                }
            }
            else
            {
                    $jodivalue="**"; 
                    $temp=["ResultColumn"=> $jodivalue];
                    array_push($resultarray ,$temp);  
            }
            
            
        }   
        
        $temp=["ResultRow"=> $resultarray];
        array_push($mainresultarray ,$temp);
        
        
    }
    $response=["status"=>"success","LastData"=>$currdate,"DelhiChartList"=>$mainresultarray];
    echo json_encode($response);
}
else
{
    
    
   $qry="Select * from market where market_type='Delhi' AND active_status!='Removed'";
    $result = mysqli_query($conn,$qry);
    $maxrows=mysqli_num_rows($result);
    if (mysqli_num_rows($result)) {
    
    	while($row =mysqli_fetch_array($result))
    	{
         
    		array_push($marketIdarray ,$row["market_id"]);
    		array_push($marketnamearray ,$row["market_name"]);
    	}
    }
    
  $lastDate=$_POST["lastDate"];  
  $lastmonth= date('Y-m-d', strtotime('-30 days', strtotime($lastDate)));
  $datearray=createDateRangeArray($lastmonth,$lastDate);
  
  

  
       $currdate="";
    for($i=sizeof($datearray)-1;$i>0;$i--)
    {
         $currdate=$datearray[$i];

        $resultarray=array();
        $jodivalue=$currdate; 
        $temp=["ResultColumn"=> $jodivalue];
        array_push($resultarray ,$temp);
        
        for($j=0;$j<sizeof($marketIdarray);$j++)
        {
            $marketId=  $marketIdarray[$j];
         $sql = "SELECT * FROM   winningbetting_detail WHERE  Date(opening_date) =Date($currdate)  AND  market_Id='$marketId'";
            $result2 = mysqli_query($conn,$sql);
            $maxrows=mysqli_num_rows($result2);
            if (mysqli_num_rows($result2))
            {
            
                while($row =mysqli_fetch_array($result2))
                {
                    $jodivalue=$row["winning_number_second"].$row["winning_number_third"]; 
                    if(empty($jodivalue) || $jodivalue==null)
                    {
                       $jodivalue="**"; 
                    }
                    $temp=["ResultColumn"=> $jodivalue];
                    array_push($resultarray ,$temp);
                    
                }
            }
            else
            {
                    $jodivalue="**"; 
                    $temp=["ResultColumn"=> $jodivalue];
                    array_push($resultarray ,$temp);  
            }
            
            
        }   
        
        $temp=["ResultRow"=> $resultarray];
        array_push($mainresultarray ,$temp);
        
        
    }
    $response=["status"=>"success","LastData"=>$currdate,"DelhiChartList"=>$mainresultarray];
    echo json_encode($response);
  
  
  
  
}

function createDateRangeArray($strDateFrom,$strDateTo)
{
    $aryRange = [];

    $iDateFrom = mktime(1, 0, 0, substr($strDateFrom, 5, 2), substr($strDateFrom, 8, 2), substr($strDateFrom, 0, 4));
    $iDateTo = mktime(1, 0, 0, substr($strDateTo, 5, 2), substr($strDateTo, 8, 2), substr($strDateTo, 0, 4));

    if ($iDateTo >= $iDateFrom) {
        array_push($aryRange, date('Y-m-d', $iDateFrom)); // first entry
        while ($iDateFrom<$iDateTo) {
            $iDateFrom += 86400; // add 24 hours
            array_push($aryRange, date('Y-m-d', $iDateFrom));
           
        }
    }
    return $aryRange;
}




?>