<!DOCTYPE html>
<?php 
    $marketId = $_GET['market_id']; 
    include('includes/config.php');
    // $marketId = $_REQUEST['market_id'];
    $marketIdName = "SELECT * FROM market WHERE market_id = '$marketId'";
    $marketIdResult = mysqli_query($conn,$marketIdName);
    $marketNameTable = mysqli_fetch_array($marketIdResult);
?>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Chart</title>

    <link rel="icon" type="image/x-icon" href="assest/images/fav.png" />

    <link rel="stylesheet" href="assest/css/bootstrap.min.css">
    <link rel="stylesheet" href="assest/css/style.css">
    
    <style>
        .table td
        {
            border: 1px solid #ff870d;
            text-align:center;
            padding: 0.25rem !important;
        }
        .table td, .table th
        {
            font-size: 10px;
        } 
        .datefontsmall
        {
            font-size: 12px;
        }
    </style>

</head>

<body>

<!--<input type="hidden" value="<?php echo $market_id;?>" class="mkid" id="mkid">-->

    <!-- Page Content-->
    <!--<div class="container-fluid">-->
    <!--    <div class="row">-->
    <!--        <h1 style="text-align: center;">KALYAN PANEL CHART</h1>-->

    <!--    </div>-->
    <!--</div>-->


    <!--<div class="container-fluid">-->
        <!--<div class="row jodi-chat">-->
            <div class="container-fluid p-0">
                <div class="col-md-12 p-0">
                    <div class="jodi-chat-single">
                        <!--<h2>KALYAN</h2>-->
                        <table class="table table-striped table-hover">
                            <thead>
                                <tr  style="color: white; background-color: #ff870d; text-align: center;"> 
                                    <th>Date</th>
                                    <th>MON</th>
                                    <th>TUE</th>
                                    <th>WED</th>
                                    <th>THU</th>
                                    <th>FRI</th>
                                    <th>SAT</th> 
                                    <th>SUN</th> 
                                </tr> 
                            </thead>
                            <tbody>
                                <?php 
                                    $name = '';
                                    $myArray = '';
                                    $myArrayThird = ''; 
                                    $firstDateSql = "SELECT opening_date FROM winningbetting_detail WHERE market_Id='$marketId' ORDER BY Id ASC LIMIT 0,1";
                                    $firstDateresult = mysqli_query($conn,$firstDateSql);
                                    $maxrows=mysqli_num_rows($firstDateresult);
                                    // print_r($maxrows);die;
                                    if($maxrows <= 0){
                                        echo "<p class='text-center text-danger'><span>No Market Record Found</span></p>";
                                    }
                                    else
                                    {
                                        $firstDateData = mysqli_fetch_assoc($firstDateresult);
                                        $firstDate = $firstDateData['opening_date'];
                                        $currentDate = date('Y-m-d');
    
                                        // echo date('D',strtotime($firstDate));die();
    
                                        if (date('D',strtotime($firstDate)) == "Mon") {
                                            $firstDate = $firstDate;
                                        }else if(date('D',strtotime($firstDate)) == "Tue"){
                                            $firstDate = date('Y-m-d',strtotime('-1 days',strtotime($firstDate)));
                                        }else if(date('D',strtotime($firstDate)) == "Wed"){
                                            $firstDate = date('Y-m-d',strtotime('-2 days',strtotime($firstDate)));
                                        }else if(date('D',strtotime($firstDate)) == "Thu"){
                                            $firstDate = date('Y-m-d',strtotime('-3 days',strtotime($firstDate)));
                                        }else if(date('D',strtotime($firstDate)) == "Fri"){
                                            $firstDate = date('Y-m-d',strtotime('-4 days',strtotime($firstDate)));
                                        }else if(date('D',strtotime($firstDate)) == "Sat"){
                                            $firstDate = date('Y-m-d',strtotime('-5 days',strtotime($firstDate)));
                                        }else if(date('D',strtotime($firstDate)) == "Sun"){
                                            $firstDate = date('Y-m-d',strtotime('-6 days',strtotime($firstDate)));
                                        }else{
                                            $firstDate = $firstDate;
                                        }
                                    
                                        function week_between_two_dates($date1, $date2)
                                        {
                                            $first = DateTime::createFromFormat('Y-m-d', $date1);
                                            $second = DateTime::createFromFormat('Y-m-d', $date2);
    
                                            if($date1 > $date2) return week_between_two_dates($date2, $date1);
                                            return floor($first->diff($second)->days/7);
                                        }
                                        $lastDate = date('Y-m-d', strtotime('+6 days',strtotime($firstDate)));
                                        $totalWeek = week_between_two_dates($firstDate, $currentDate);
                                        for ($i=0; $i <= $totalWeek; $i++) {
                                            echo "<tr>";
                                            echo "<td class='datefontsmall'>".$firstDate."<br/><span class='text-center'>To</span><br/>".$lastDate."</td>";
                                            $mydate = $firstDate;
                                            for ($j=1; $j <=7; $j++) {
                                                $SQL = "SELECT * FROM winningbetting_detail WHERE date(opening_date)='$mydate' AND market_Id='$marketId' ORDER BY date(opening_date) ASC ";
                                                $dataResult = mysqli_query($conn,$SQL); 
                                                $numSql = mysqli_num_rows($dataResult);
                                                if ($numSql == 0) {
                                                        $firstNum = "<span>***</span><br/>";
                                                        $jodi = "<span  class='text-center'>**</span><br/>";
                                                        $lastNum = "<span>***</span>";
                                                        echo "<td>".$firstNum.$jodi.$lastNum."</td>";
                                                    $mydate = date('Y-m-d',strtotime($j." days",strtotime($firstDate)));
                                                }else{
                                                    while ($chartRow = mysqli_fetch_assoc($dataResult)) {
                                                        $firstNum = "<span>".$chartRow['winning_number_first']."</span><br/>";
                                                        $jodi = "<span  class='text-center'>".$chartRow['winning_number_second'].$chartRow['winning_number_third']."</span><br/>";
                                                        $lastNum = "<span>".$chartRow['winning_number_fouth']."</span>";
                                                        echo "<td>".$firstNum.$jodi.$lastNum."</td>";
                                                    }
                                                    $mydate = date('Y-m-d',strtotime($j." days",strtotime($firstDate)));
                                                }
                                                
                                            }
                                            
                                            $firstDate = date('Y-m-d',strtotime('+1 days', strtotime($lastDate)));
                                            $lastDate = date('Y-m-d', strtotime('+6 days',strtotime($firstDate)));
                                            echo "</tr>";
                                        }
                                    }
                                  ?>
                            </tbody> 
                        </table>
                    </div>
                </div>
            </div>
        <!--</div>-->
    <!--</div>-->

    <script src="../assets/js/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.5.1/gsap.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.5.1/ScrollTrigger.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.2.5/ScrollToPlugin.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
    <!-- ./custom -->
    <script src="../assets/js/custom.js"></script>
    

    <script>
        
        // // var mkid = $(this).attr('mkid');
        // var mkid = $('.mkid').val();
        
        // if (mkid != null || mkid != ''){
    
        // $.ajax({
        //   url:"/skofficial/api/getMarketChartList.php?market_id="+mkid,
        //   type:'GET',
        //   dataType: 'JSON',
        //   success: function(response){
        //       var data = JSON.stringify(response)
        //       var newaa = $.parseJSON(JSON.stringify(response));
        //                 for(var e = 0; e < response.MarketDateChartList.length; e++) {
        //                   var ne = '<tr class="nrow"><td>' + response.MarketDateChartList[e] + '</td></tr>'
        //                 //   console.log(response.MarketDateChartList[e]);
        //                     $('#datee').append(ne);
        //                 }
        //             for(var i = 0; i < response.MarketDayChartList.length; i++) {
        //                 $('#days').append('<td align="center" >' + response.MarketDayChartList[i] + '</td>');
        //             }
                    
        //             for(var f = 0; f < response.MarketChartList.length; f++) {
        //                     console.log(response.MarketChartList[f])
        //                     // var k = response.MarketChartList[f].length
        //                     var q = response.MarketChartList[f]
                            
        //                     let i = 0;
                            
        //                   while (i < response.MarketChartList[f].length) {
        //                         var panelfirst = response.MarketChartList[f][i]['panelfirst']
        //                         var panelsecond = response.MarketChartList[f][i]['panelsecond']
        //                         var panelthird = response.MarketChartList[f][i]['panelthird']
        //                         var vDate = response.MarketChartList[f][i]['Date']
        //                         var pan = panelfirst + ' - ' + panelsecond + ' - ' + panelthird 
        //                         // console.log(pan)
        //                         $('.nrow').append('<td>' + pan + '<br>' + vDate + '</td>');
        //                       i++;
        //                     }
                            
        //                     // for(var a = 0; a < response.MarketChartList[f].length; a++) {
        //                     //     var panelfirst = response.MarketChartList[f][a]['panelfirst']
        //                     //     var panelsecond = response.MarketChartList[f][a]['panelsecond']
        //                     //     var panelthird = response.MarketChartList[f][a]['panelthird']
        //                     //     var vDate = q[a]['Date']
        //                     //     var pan = panelfirst + ' - ' + panelsecond + ' - ' + panelthird 
        //                     //     // console.log(pan)
        //                     //     $('.nrow').append('<td>' + pan + '<br>' + vDate + '</td>');
        //                     //     // $('.nrow').append('<td>' + response.MarketChartList[a] + '</td>');
        //                     //     // $('.nrow').append('<td>' + response.MarketChartList[q] + '</td>');
        //                     // }
        //                 }
                        
                    
                    
           
                    
        //         //   for(var e = 0; e < response.MarketDateChartList.length; e++) {
        //         //       var ne = '<tr class="nrow"><td>' + response.MarketDateChartList[e] + '</td></tr>'
        //         //         $('#datee').append(ne);
        //         //         for(var f = 0; f < response.MarketChartList.length; f++) {
        //         //             var k = response.MarketChartList[f].length
        //         //             var q = response.MarketChartList[f]
        //         //             for(var a = 0; a < q.length; a++) {
        //         //                 var panelfirst = q[a]['panelfirst']
        //         //                 var panelsecond = q[a]['panelsecond']
        //         //                 var panelthird = q[a]['panelthird']
        //         //                 var vDate = q[a]['Date']
        //         //                 var pan = panelfirst + ' - ' + panelsecond + ' - ' + panelthird 
        //         //                 // console.log(pan)
        //         //                 $('.nrow').append('<td>' + pan + '<br>' + vDate + '</td>');
        //         //                 // $('.nrow').append('<td>' + response.MarketChartList[a] + '</td>');
        //         //                 // $('.nrow').append('<td>' + response.MarketChartList[q] + '</td>');
        //         //             }
        //         //         }
        //         //     }
        //   }
        // });
        // }else {
        //     alert('No Market');
        // }
       
    </script>

</body>

</html>