<?php include 'template/header.php';?>
<br>

<div class="row">
    <div class="col-6 col-sm-6 col-md-6 col-lg-3 mb-3 pr-0">
        <div class="card-body bg-white py-2 px-1 border-laporan">
            <div class="row mx-auto align-items-center">
            <div class="col-auto m-pr-1">
                <div class="bg-icon">
                    <i class="fa fa-shopping-cart"></i>
                </div>
            </div>
            <div class="col-auto pl-0 pt-2">
                <div class="text-muted" style="font-size:11px;">
                    Transaksi Hari Ini
                </div>
                <h4 class="1"><?php 
                $tanggal_hari_ini = date('Y-m-d');
                $itungtransaksi = mysqli_query($conn,"SELECT COUNT(*) as jumlahtransaksi FROM laporan WHERE DATE(tgl_sub) = '$tanggal_hari_ini'");
                $cekrow1 = mysqli_num_rows($itungtransaksi);
                $itungtransaksi1 = mysqli_fetch_assoc($itungtransaksi);
                $itungtransaksi2 = $itungtransaksi1['jumlahtransaksi'];
                if($cekrow1 > 0){
                    echo  $itungtransaksi2;
                    } ?></h4>
            </div>
            </div>
        </div>
    </div>

    <div class="col-6 col-sm-6 col-md-6 col-lg-3 mb-3 m-pr-0">
        <div class="card-body bg-white py-2 px-1 border-laporan">
            <div class="row mx-auto align-items-center">
            <div class="col-auto m-pr-1">
                <div class="bg-icon">
                    <i class="fa fa-dollar-sign"></i>
                </div>
            </div>
            <div class="col-auto pl-0 pt-2">
                <div class="text-muted" style="font-size:11px;">
                    Pendapatan Hari Ini
                </div>
                <h4 class="1"><?php 
                $tanggal_hari_ini = date('Y-m-d');
                $itungpendapatan = mysqli_query($conn,"SELECT SUM(totalbeli) as jumlahpendapatan FROM laporan WHERE DATE(tgl_sub) = '$tanggal_hari_ini'");
                $cekrow = mysqli_num_rows($itungpendapatan);
                $itungpendapatan1 = mysqli_fetch_assoc($itungpendapatan);
                $itungpendapatan2 = $itungpendapatan1['jumlahpendapatan'];
                if($cekrow > 0 && $itungpendapatan2){
                    echo 'Rp. '.ribuan($itungpendapatan2);
                } else {
                    echo 'Rp. 0';
                } ?></h4>
            </div>
            </div>
        </div>
    </div>

    <div class="col-6 col-sm-6 col-md-6 col-lg-3 mb-3 pr-0">
        <div class="card-body bg-white py-2 px-1 border-laporan">
            <div class="row mx-auto align-items-center">
            <div class="col-auto m-pr-1">
                <div class="bg-icon">
                    <i class="fa fa-box"></i>
                </div>
            </div>
            <div class="col-auto pl-0 pt-2">
                <div class="text-muted" style="font-size:11px;">
                    Produk Terjual Hari Ini
                </div>
                <h4 class="1"><?php 
                $tanggal_hari_ini = date('Y-m-d');
                $itungpeterjual = mysqli_query($conn,"SELECT SUM(quantity) as jumlahterjual FROM tb_nota t JOIN laporan l ON t.no_nota = l.no_nota WHERE DATE(l.tgl_sub) = '$tanggal_hari_ini'");
                $cekrow = mysqli_num_rows($itungpeterjual);
                $itungpeterjual1 = mysqli_fetch_assoc($itungpeterjual);
                $itungpeterjual2 = $itungpeterjual1['jumlahterjual'];
                if($cekrow > 0 && $itungpeterjual2){
                    echo $itungpeterjual2;
                } else {
                    echo '0';
                } ?></h4>
            </div>
            </div>
        </div>
    </div>

    <div class="col-6 col-sm-6 col-md-6 col-lg-3 mb-3">
        <div class="card-body bg-white py-2 px-1 border-laporan">
            <div class="row mx-auto align-items-center">
            <div class="col-auto m-pr-1">
                <div class="bg-icon">
                    <i class="fa fa-user"></i>
                </div>
            </div>
            <div class="col-auto pl-0 pt-2">
                <div class="text-muted" style="font-size:11px;">
                    Pelanggan Hari Ini
                </div>
                <h4 class="1"><?php 
                $tanggal_hari_ini = date('Y-m-d');
                $itungpelanggan = mysqli_query($conn,"SELECT COUNT(DISTINCT idpelanggan) as jumlahpelanggan FROM laporan WHERE DATE(tgl_sub) = '$tanggal_hari_ini'");
                $cekrow = mysqli_num_rows($itungpelanggan);
                $itungpelanggan1 = mysqli_fetch_assoc($itungpelanggan);
                $itungpelanggan2 = $itungpelanggan1['jumlahpelanggan'];
                if($cekrow > 0){
                    echo $itungpelanggan2;
                } else {
                    echo '0';
                } ?></h4>
            </div>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-lg-8 mb-4">
        <div class="card">
            <div class="card-header">
                <div class="card-tittle"><i class="fa fa-chart-bar me-2"></i> Grafik Penjualan Harian</div>
            </div>
            <div class="card-body">
                <canvas id="dailySalesChart" width="400" height="150"></canvas>
            </div>
        </div>
    </div>
    <div class="col-lg-4 mb-4">
        <div class="card">
            <div class="card-header">
                <div class="card-tittle"><i class="fa fa-boxes me-2"></i> Produk Terlaris</div>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-sm">
                        <thead>
                            <tr>
                                <th>Produk</th>
                                <th>Terjual</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            // Get top 5 best selling products
                            $top_produk = mysqli_query($conn, "SELECT p.nama_produk, SUM(t.quantity) as total_terjual 
                                                              FROM tb_nota t 
                                                              JOIN produk p ON t.idproduk = p.idproduk 
                                                              GROUP BY t.idproduk 
                                                              ORDER BY total_terjual DESC 
                                                              LIMIT 5");
                            while($produk = mysqli_fetch_assoc($top_produk)) {
                                echo "<tr>";
                                echo "<td>".$produk['nama_produk']."</td>";
                                echo "<td>".$produk['total_terjual']."</td>";
                                echo "</tr>";
                            }
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-lg-12">
        <div class="card">
            <div class="card-header">
                <div class="card-tittle"><i class="fa fa-receipt me-2"></i> Transaksi Terakhir</div>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-striped table-sm table-bordered dt-responsive nowrap" id="table" width="100%">
                        <thead>
                        <tr>
                            <th>No. Nota</th>
                            <th>Pelanggan</th>
                            <th>Total</th>
                            <th>Tanggal</th>
                            <th>Aksi</th>
                        </tr>
                        </thead>
                        <tbody>
                            <?php
                            $data_transaksi = mysqli_query($conn,"SELECT * FROM laporan l, pelanggan e
                            WHERE e.idpelanggan=l.idpelanggan ORDER BY idlaporan DESC LIMIT 10");
                            while($d=mysqli_fetch_array($data_transaksi)){
                                ?>
                                
                                <tr>
                                    <td><?php echo $d['no_nota'] ?></td>
                                    <td><?php echo $d['nama_pelanggan'] ?></td>
                                    <td>Rp.<?php echo ribuan($d['totalbeli']) ?></td>
                                    <td><?php echo $d['tgl_sub'] ?></td>
                                    <td>
                                        <a class="btn btn-primary btn-xs" href="detail.php?invoice=<?php echo $d['no_nota'] ?>">
                                            <i class="fa fa-eye fa-xs mr-1"></i>Detail</a>
                                    </td>
                                </tr>		
                        <?php }?>
					</tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<!-- Chart.js library -->
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>

<script>
// Daily Sales Chart
document.addEventListener('DOMContentLoaded', function() {
    // Get data for the chart (last 7 days)
    <?php
    $chart_data = array();
    for($i = 6; $i >= 0; $i--) {
        $date = date('Y-m-d', strtotime("-$i days"));
        $tanggal_format = date('d M', strtotime($date));
        
        $query = mysqli_query($conn, "SELECT SUM(totalbeli) as daily_total FROM laporan WHERE DATE(tgl_sub) = '$date'");
        $result = mysqli_fetch_assoc($query);
        $daily_total = $result['daily_total'] ? $result['daily_total'] : 0;
        
        $chart_data[] = array(
            'date' => $tanggal_format,
            'total' => $daily_total
        );
    }
    ?>
    
    var ctx = document.getElementById('dailySalesChart').getContext('2d');
    var dailySalesChart = new Chart(ctx, {
        type: 'line',
        data: {
            labels: [<?php foreach($chart_data as $data) { echo '"'.$data['date'].'",'; } ?>],
            datasets: [{
                label: 'Penjualan Harian (Rp)',
                data: [<?php foreach($chart_data as $data) { echo $data['total'].','; } ?>],
                backgroundColor: 'rgba(54, 162, 235, 0.2)',
                borderColor: 'rgba(54, 162, 235, 1)',
                borderWidth: 2,
                fill: true
            }]
        },
        options: {
            responsive: true,
            scales: {
                y: {
                    beginAtZero: true,
                    ticks: {
                        callback: function(value) {
                            return 'Rp. ' + value.toLocaleString();
                        }
                    }
                }
            }
        }
    });
});
</script>

<?php include 'template/footer.php';?>