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

function format_ribuan($nilai) {
    return number_format($nilai, 0, ',', '.');
}

// Filter tanggal
$tanggal_filter = "";
if (isset($_GET['tanggal']) && !empty($_GET['tanggal'])) {
    $tanggal_filter = mysqli_real_escape_string($conn, $_GET['tanggal']);
} else {
    $tanggal_filter = date('Y-m-d');
}
$where_clause = "WHERE laporanku.tgl_input = '$tanggal_filter'";


$data_barang = mysqli_query($conn, "
    SELECT * FROM laporanku
    $where_clause
");
$tot_bayar= 0;

while ($d = mysqli_fetch_array($data_barang)) {
    // Pastikan semua nilai numerik
    $harga = is_numeric($d['harga_barang']) ? (float)$d['harga_barang'] : 0;
    $qty = is_numeric($d['quantity']) ? (int)$d['quantity'] : 0;
    $diskon = isset($d['diskon']) && is_numeric($d['diskon']) ? (float)$d['diskon'] : 0;

    $subtotal = ($harga * $qty) - $diskon;
    if ($subtotal < 0) $subtotal = 0;

    $tot_bayar += $subtotal;
}
?>

<style>
  body {
    background: linear-gradient(to left, #b3d9ff, #e6f2ff);
  }
</style>

<div class="col-md-9 mb-2">
    <div class="row">
        <div class="col-md-12 mb-2">
            <div class="card">
                <div class="card-header bg-purple">
                    <div class="card-tittle text-white"><i class="fa fa-table"></i> <b>Data Laporan Transaksi</b></div>
                </div>
                <div class="card-body">
                    <div class="mb-4">
                        <form method="get" action="" class="form-inline">
                            <input type="date" name="tanggal" id="tanggal" class="form-control form-control-sm mr-2"
                                value="<?php echo $tanggal_filter; ?>">
                        </form>
                    </div>

                    <table class="table table-striped table-bordered table-sm dt-responsive nowrap" id="table" width="100%">
                        <thead class="thead-purple" style="font-size: 12px;">
                            <tr>
                                <th>No</th>
                                <th>Kode Barang</th>
                                <th>Tgl Input</th>
                                <th>Nama Barang</th>
                                <th>Quantity</th>
                                <th>Harga</th>
                                <th>Sub-Total</th>
                                <th>Opsi</th>
                            </tr>
                        </thead>
                        <tbody style="font-size: 12px;">
                            <?php 
                            $no = 1;
                            $data_barang = mysqli_query($conn, "
                                SELECT * FROM laporanku 
                                $where_clause
                            ");
                            while ($d = mysqli_fetch_array($data_barang)) {
                                $subtotal = ($d['harga_barang'] * $d['quantity']) - $diskon;
                            ?>
                                <td><?php echo $no++; ?></td>
                                <td><?php echo $d['no_transaksi']; ?></td>
                                <td><?php echo $d['tgl_input']; ?></td>
                                <td><?php echo $d['nama_barang']; ?></td>
                                <td><?php echo $d['quantity']; ?></td>
                                <td>Rp. <?php echo format_ribuan($d['harga_barang']); ?>,-</td> 
                                <td>Rp. <?php echo format_ribuan($subtotal); ?>,-</td>
                                <td>
                                    <a class="btn btn-danger btn-xs" href="?id=<?php echo $d['id_cart']; ?>"
                                        onclick="return confirm('Hapus Data Barang ?');">
                                        <i class="fa fa-trash fa-xs"></i> Hapus
                                    </a>
                                </td>
                            </tr>
                            <?php } ?>
                            <?php if ($no === 1): ?>
                            <tr >
                                <td colspan="9" class="text-center text-muted">Tidak ada transaksi pada tanggal ini.</td>
                            </tr>
                            <?php endif; ?>
                        </tbody>
                        <tfoot style="font-size:12px;">
                            <tr>
                                <th colspan="6" class="text-right"><b>TOTAL :</b></th>
                                <th><b>Rp. <?php echo format_ribuan($tot_bayar); ?>,-</b></th>
                                <th></th>
                            </tr>
                        </tfoot>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<?php
// Hapus data jika ada parameter ID
if (!empty($_GET['id'])) {
    $id = $_GET['id'];
    $hapus_data = mysqli_query($conn, "DELETE FROM laporanku WHERE id_cart ='$id'");
    echo '<script>window.location="laporan.php"</script>';
}
?>

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

<script>
$(document).ready(function() {
    $('#table').DataTable();

    $('#tanggal').on('change', function() {
        const tanggalValue = $(this).val();
        window.location.href = `laporan.php?tanggal=${tanggalValue}`;
    });
});
</script>
