<?php
$jsondata   = file_get_contents("feedback.json");
$data       = json_decode($jsondata,true);   
// var_dump($data);
count($data);


function countStatus($arg){
    global $data;
    if($arg==0){
        $count  = count($data);
    }else{
        $count = isset(array_count_values(array_column($data, 'status'))[$arg])?array_count_values(array_column($data, '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">
                                TOTAL REVIEW <br> <span style="font-size:35px"><?php echo countStatus(0);?></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">
                                GOOD REVIEW <br> <span style="font-size:35px"><?php echo countStatus(1);?></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">
                                BAD REVIEW <br> <span style="font-size:35px"><?php echo countStatus(2);?></span>
                            </p>
                        </div>
                    </div>
                    
                </div>
                <hr>
                <h3 class="text-center">LATEST REVIEW SHOWING HERE</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">Tanggal</th>
                                <th scope="col">Nama</th>
                                <th scope="col">Komentar</th>
                                <th scope="col">Action</th>
                                </tr>
                            </thead>
                            <tbody>
                            <?php $no=1;foreach($data as $value){
                                $date = date("Y-m-d",(int)$value['date']);
                                $now = date("Y-m-d"); 
                                // var_dump($value);
                                    $name       = $value['name'];
                                    $phone      = $value['phone'];
                                    $comment    = $value['comment'];
                                    $tanggal    = dateFormat($value['timestamp']);
                                            echo "<tr>";
                                            echo "<th scope='row'>$no</th>";
                                            echo "<td>".$tanggal."</td>";
                                            echo "<td style='text-align: left;''>".$name."</td>";
                                            echo "<td>".$comment."</td>";
                                            echo "</tr>";
                                            $no++;
                                             
                            }
                            ?>
                            
                            </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>