<?php
session_start();
require '../config.php';
require '../lib/session_user.php';
require '../lib/header.php';
?>
<div class="container-fluid">
    <div class="card">
        <div class="card-header">
            <h4 class="card-title m-0"><i class="fas fa-desktop me-1"></i> Monitoring Layanan</h4>
        </div>
        <div class="card-body">
            <form class="form-horizontal" method="GET">
                <input type="hidden" name="csrf_token" value="<?php echo $config['csrf_token'] ?>">
                <div class="row">
                    <div class="col-md">
                        <div class="mb-3">
                            <select class="form-control" name="tampil">
                                <option value="0" selected disabled>- Tampilkan Data -</option>
                                <option value="10">10</option>
                                <option value="20">10</option>
                                <option value="50">50</option>
                                <option value="100">100</option>
                            </select>
                        </div>
                    </div>
                    <div class="col-md">
                        <div class="mb-3">
                            <div class="input-group">
                                <input type="text" class="form-control" name="cari" placeholder="Cari layanan.." value="">
                                <button class="btn btn-primary bg-gradient" type="submit"><i class="fa fa-fw fa-search"></i></button>
                            </div>
                        </div>
                    </div>
                </div>
            </form>
            <pre></pre>
            <div class="table-responsive">
                <table class="table table-bordered table-striped">
                    <thead>
                        <tr class="table-light">
                            <th>Layanan</th>
                            <th>Jumlah</th>
                            <th>Dibuat</th>
                            <th>Sukses</th>
                            <th>Waktu Sukses</th>
                        </tr>
                    </thead>
                    <tbody> 
<?php
$thirty_days_ago = date('Y-m-d', strtotime('-30 days'));

// Jika ada pencarian
if (isset($_GET['cari'])) {
    $cari_layanan = $conn->real_escape_string(filter($_GET['cari']));
    $cek_pesanan = "SELECT * FROM pembelian_sosmed 
                    WHERE layanan LIKE '%$cari_layanan%' 
                    AND updated_aw >= '$thirty_days_ago' 
                    AND status = 'Success' 
                    ORDER BY id DESC";
} else {
    // Jika tidak ada pencarian
    $cek_pesanan = "SELECT * FROM pembelian_sosmed 
                    WHERE updated_aw >= '$thirty_days_ago' 
                    AND status = 'Success' 
                    ORDER BY id DESC";
}

$records_per_page = isset($_GET['tampil']) ? $conn->real_escape_string(filter($_GET['tampil'])) : 10;
$starting_position = isset($_GET["halaman"]) ? ($conn->real_escape_string(filter($_GET["halaman"]))-1) * $records_per_page : 0;

$new_query = $cek_pesanan . " LIMIT $starting_position, $records_per_page";
$new_query = $conn->query($new_query);

while ($data_layanan = $new_query->fetch_assoc()) {
    $datee = $data_layanan['date'];
    $timee = $data_layanan['time'];
    $kelardate = $data_layanan['updated_aw'];
    $kelartime = $data_layanan['updated_at'];

    $awal  = "$datee $timee";
    $done = "$kelardate $kelartime";

    $hehe = date_create($awal);
    $akhir = date_create($done);
    $diff  = date_diff($hehe, $akhir);
?>
                        <tr>
                            <td><?= $data_layanan['layanan'];?></td>
                            <td><?php echo number_format($data_layanan['jumlah'],0,',','.'); ?></td>
                            <td><?php echo tanggal_indo($data_layanan['date']); ?> <?= $data_layanan['time']; ?></td>
                            <td><?php echo tanggal_indo($data_layanan['updated_aw']); ?> <?= $data_layanan['updated_at']; ?></td>
                            <td><?php echo $diff->h . ' Jam, ' ?><?php echo $diff->i . ' Menit, ' ?><?php echo $diff->s . ' Detik' ?></td>
                        </tr>
<?php } ?>
                    </tbody>
                </table>
            </div>
            
            <ul class="pagination pagination-split">
<?php
// start paging link
    if (isset($_GET['cari'])) {
        $cari_urut = $conn->real_escape_string(filter($_GET['tampil']));
    } else {
        $cari_urut =  10;
    }  
    if (isset($_GET['cari'])) {
        $cari_oid = $conn->real_escape_string(filter($_GET['cari']));
        $cari_status = $conn->real_escape_string(filter($_GET['status']));
        $cari_urut = $conn->real_escape_string(filter($_GET['tampil']));
    } else {
        $self = $_SERVER['PHP_SELF'];
    }
    $cek_pesanan = $conn->query($cek_pesanan);
    $total_records = mysqli_num_rows($cek_pesanan);
    echo "<li class='disabled page-item'><a class='page-link' href='#'>Total: ".$total_records."</a></li>";
    if($total_records > 0) {
        $total_pages = ceil($total_records/$records_per_page);
        $current_page = 1;
        if(isset($_GET["halaman"])) {
            $current_page = $conn->real_escape_string(filter($_GET["halaman"]));
            if ($current_page < 1) {
                $current_page = 1;
            }
        }
        if($current_page > 1) {
            $previous = $current_page-1;
            if (isset($_GET['cari'])) {
                $cari_oid = $conn->real_escape_string(filter($_GET['cari']));
                $cari_status = $conn->real_escape_string(filter($_GET['status']));
                $cari_urut = $conn->real_escape_string(filter($_GET['tampil']));
                echo "<li class='page-item'><a class='page-link' href='".$self."?halaman=1&tampil=".$cari_urut."&status=".$cari_status."&cari=".$cari_oid."'><<</a></li>";
                echo "<li class='page-item'><a class='page-link' href='".$self."?halaman=".$previous."&tampil=".$cari_urut."&status=".$cari_status."&cari=".$cari_oid."'><</a></li>";
            } else {
                echo "<li class='page-item'><a class='page-link' href='".$self."?halaman=1'><<</a></li>";
                echo "<li class='page-item'><a class='page-link' href='".$self."?halaman=".$previous."'><</a></li>";
            }
        }
// limit page
        $limit_page = $current_page+3;
        $limit_show_link = $total_pages-$limit_page;
        if ($limit_show_link < 0) {
            $limit_show_link2 = $limit_show_link*2;
            $limit_link = $limit_show_link - $limit_show_link2;
            $limit_link = 3 - $limit_link;
        } else {
            $limit_link = 3;
        }
        $limit_page = $current_page+$limit_link;
// end limit page
// start page
        if ($current_page == 1) {
            $start_page = 1;
        } else if ($current_page > 1) {
            if ($current_page < 4) {
                $min_page  = $current_page-1;
            } else {
                $min_page  = 3;
            }
            $start_page = $current_page-$min_page;
        } else {
            $start_page = $current_page;
        }
// end start page
        for($i=$start_page; $i<=$limit_page; $i++) {
            if (isset($_GET['cari'])) {
                $cari_oid = $conn->real_escape_string(filter($_GET['cari']));
                $cari_status = $conn->real_escape_string(filter($_GET['status']));
                $cari_urut = $conn->real_escape_string(filter($_GET['tampil']));
                if($i==$current_page) {
                    echo "<li class='active page-item'><a class='page-link' href='#'>".$i."</a></li>";
                } else {
                    echo "<li class='page-item'><a class='page-link' href='".$self."?halaman=".$i."&tampil=".$cari_urut."&status=".$cari_status."&cari=".$cari_oid."'>".$i."</a></li>";
                }
            } else {
                if($i==$current_page) {
                    echo "<li class='active page-item'><a class='page-link' href='#'>".$i."</a></li>";
                } else {
                    echo "<li class='page-item'><a class='page-link' href='".$self."?halaman=".$i."'>".$i."</a></li>";
                }        
            }
        }
        if($current_page!=$total_pages) {
            $next = $current_page+1;
            if (isset($_GET['cari'])) {
                $cari_oid = $conn->real_escape_string(filter($_GET['cari']));
                $cari_status = $conn->real_escape_string(filter($_GET['status']));
                $cari_urut = $conn->real_escape_string(filter($_GET['tampil']));
                echo "<li class='page-item'><a class='page-link' href='".$self."?halaman=".$next."&tampil=".$cari_urut."&status=".$cari_status."&cari=".$cari_oid."'>></a></li>";
                echo "<li class='page-item'><a class='page-link' href='".$self."?halaman=".$total_pages."&tampil=".$cari_urut."&status=".$cari_status."&cari=".$cari_oid."'>>></a></li>";
            } else {
                echo "<li class='page-item'><a class='page-link' href='".$self."?halaman=".$next."'>></i></a></li>";
                echo "<li class='page-item'><a class='page-link' href='".$self."?halaman=".$total_pages."'>>></a></li>";
            }
        }
    }
// end paging link
    ?>
</ul>

        </div>
    </div>
</div>
<?php
require '../lib/footer.php';
?>