<?php
$jsondata   = file_get_contents("jadwal.json");
$data       = json_decode($jsondata,true);    
$jsondata   = file_get_contents("data.json");
$data1       = json_decode($jsondata,true);   



function countStatus($arg){
    global $data1;
    $count = isset(array_count_values(array_column($data1, 'status'))[$arg])?array_count_values(array_column($data1, 'status'))[$arg]:0;
    return $count;
}

function date_compare($arg1,$arg2){
    return (int)$arg1['date'] - (int)$arg2['date'];
    // return (int)$arg2['date'] - (int)$arg1['date'];
}
usort($data, 'date_compare');
//   ========== START HOME PAGE ==========
?>
<div class="card">
    <div class="card-header ">
        <h4>HOME</h4>
    </div>
    <div class="card-body ">
        <form class="form" method="post">
            <div class="form-group">
                <div class="card-columns">
                    <div class="card bg-success"style="height: 8rem;">
                        <div class="card-body text-center">
                            <p class="card-text font-weight-bolder text-white text-uppercase" style="font-size:25px">
                                band active <br> <span style="font-size:35px"><?php echo countStatus(1);?></span>
                            </p>
                        </div>
                    </div>
                    <div class="card bg-primary"style="height: 8rem;">
                        <div class="card-body text-center">
                            <p class="card-text font-weight-bolder text-white text-uppercase" style="font-size:25px">
                                band trial <br> <span style="font-size:35px"><?php echo countStatus(0);?></span>
                            </p>
                        </div>
                    </div>
                    <div class="card bg-danger"style="height: 8rem;">
                        <div class="card-body text-center">
                            <p class="card-text font-weight-bolder text-white text-uppercase" style="font-size:25px">
                                band off <br> <span style="font-size:35px"><?php echo countStatus(2);?></span>
                            </p>
                        </div>
                    </div>
                    
                </div>
                <hr>
                <h3 class="text-center">ON GOING SCHEDULE</h3>
                <!-- <hr> -->
                <div class="row justify-content-center">
                    <div class="col-md-12">
                        <div class="table-responsive">
                            <table class="table table-striped" style="text-align: center;">
                            <thead>
                                <tr>
                                <th scope="col">#</th>
                                <th scope="col">Band Name</th>
                                <th scope="col">PIC</th>
                                <th scope="col">EVENT</th>
                                <th scope="col">DATE</th>
                                </tr>
                            </thead>
                            <tbody>
                            <?php $no=1;foreach($data as $value){
                                $date = date("Y-m-d",(int)$value['date']);
                                $now = date("Y-m-d"); 

                                if($date >= $now) {
                                    foreach ($data1 as $key => $d) {
                                        if ($d['id'] == $value['bandId']) {
                                            $nama       = $d['nama'];
                                            $pic        = $d['pic'];
                                            $keterangan = $d['keterangan'];
                                            $kontak     = $d['kontak'];
                                            $event      = eventName($value['event']);
                                            $tanggal = dateFormat($value['date']);
                                            echo "<tr>";
                                            echo "<th scope='row'>$no</th>";
                                            echo "<td style='text-align: left;''>".$nama."</td>";
                                            echo "<td>".$pic."</td>";
                                            echo "<td>".$event."</td>";
                                            // echo "<td>".$value["date"]."</td>";
                                            echo "<td>".$tanggal."</td>";
                                            echo "</tr>";
                                            $no++;
                                        }
                                    }
                                    
                                }        
                            }
                            ?>
                            
                            </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>