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

<div class="row mb-3">
    <div class="col-lg-12">
        <div class="card">
            <div class="card-header">
                <div class="card-tittle">
                    <i class="fa fa-table me-2"></i> Filter Laporan
                </div>
            </div>
            <div class="card-body">
                <form method="GET" class="row">
                    <div class="col-md-4">
                        <label>Tanggal Awal:</label>
                        <input type="date" name="tgl_awal" class="form-control" value="<?php echo $_GET['tgl_awal'] ?? date('Y-m-01'); ?>">
                    </div>
                    <div class="col-md-4">
                        <label>Tanggal Akhir:</label>
                        <input type="date" name="tgl_akhir" class="form-control" value="<?php echo $_GET['tgl_akhir'] ?? date('Y-m-d'); ?>">
                    </div>
                    <div class="col-md-4">
                        <label>Jenis Laporan:</label>
                        <select name="jenis" class="form-control">
                            <option value="all" <?php echo ($_GET['jenis'] ?? 'all') == 'all' ? 'selected' : ''; ?>>Semua</option>
                            <option value="penjualan" <?php echo ($_GET['jenis'] ?? 'all') == 'penjualan' ? 'selected' : ''; ?>>Penjualan</option>
                            <option value="retur" <?php echo ($_GET['jenis'] ?? 'all') == 'retur' ? 'selected' : ''; ?>>Retur</option>
                        </select>
                    </div>
                    <div class="col-md-12 mt-3 text-right">
                        <button type="submit" class="btn btn-primary">Filter</button>
                        <button type="button" class="btn btn-success" onclick="printReport()">Cetak Laporan</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<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-user"></i>
                    </div>
                </div>
                <div class="col-auto pl-0 pt-2">
                    <div class="text-muted" style="font-size:11px;">
                        Pelanggan
                    </div>
                    <h4 class="1"><?php 
                    $tanggal_awal = $_GET['tgl_awal'] ?? date('Y-m-01');
                    $tanggal_akhir = $_GET['tgl_akhir'] ?? date('Y-m-d');
                    $itungpelanggan = mysqli_query($conn,"SELECT COUNT(idpelanggan) as jumlahpelanggan FROM pelanggan");
                    $cekrow1 = mysqli_num_rows($itungpelanggan);
                    $itungpelanggan1 = mysqli_fetch_assoc($itungpelanggan);
                    $itungpelanggan2 = $itungpelanggan1['jumlahpelanggan'];
                    if($cekrow1 > 0){
                        echo  $itungpelanggan2;
                        } ?></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-shopping-cart"></i>
                    </div>
                </div>
                <div class="col-auto pl-0 pt-2">
                    <div class="text-muted" style="font-size:11px;">
                        Terjual
                    </div>
                    <h4 class="1"><?php 
                    $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) BETWEEN '$tanggal_awal' AND '$tanggal_akhir'");
                    $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 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 Bersih
                    </div>
                    <h4 class="1">Rp.<?php 
                    // Hitung total penjualan
                    $data_produk=mysqli_query($conn,"SELECT * FROM tb_nota t, produk p
                    WHERE p.idproduk=t.idproduk AND t.no_nota IN (SELECT no_nota FROM laporan WHERE DATE(tgl_sub) BETWEEN '$tanggal_awal' AND '$tanggal_akhir') ORDER BY idnota ASC");
                    $total_penjualan = 0;
                    $x = mysqli_num_rows($data_produk);
                    if($x>0){
                    while($b=mysqli_fetch_array($data_produk)){
                        $total_penjualan += $b['harga_jual'] * $b['quantity'];
                    }
                } 
                
                // Hitung total retur dalam periode yang sama
                $retur_query = "SELECT SUM(subtotal) as total_retur FROM retur_barang WHERE DATE(tanggal_retur) BETWEEN '$tanggal_awal' AND '$tanggal_akhir'";
                $retur_result = mysqli_query($conn, $retur_query);
                $retur_data = mysqli_fetch_assoc($retur_result);
                $total_retur = $retur_data['total_retur'] ?? 0;
                
                $pendapatan_bersih = $total_penjualan - $total_retur;
                echo ribuan($pendapatan_bersih)?></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-file-invoice-dollar"></i>
                    </div>
                </div>
                <div class="col-auto pl-0 pt-2">
                    <div class="text-muted" style="font-size:11px;">
                        Total Penjualan Bersih
                    </div>
                    <h4 class="1">Rp.<?php 
                    $itungtotal = mysqli_query($conn,"SELECT SUM(totalbeli) as jumlahtotal FROM laporan WHERE DATE(tgl_sub) BETWEEN '$tanggal_awal' AND '$tanggal_akhir'");
                    $cekrow3 = mysqli_num_rows($itungtotal);
                    $itungtotal1 = mysqli_fetch_assoc($itungtotal);
                    $total_penjualan_kotor = $itungtotal1['jumlahtotal'] ?? 0;
                    
                    // Hitung total retur dalam periode yang sama
                    $retur_query = "SELECT SUM(subtotal) as total_retur FROM retur_barang WHERE DATE(tanggal_retur) BETWEEN '$tanggal_awal' AND '$tanggal_akhir'";
                    $retur_result = mysqli_query($conn, $retur_query);
                    $retur_data = mysqli_fetch_assoc($retur_result);
                    $total_retur = $retur_data['total_retur'] ?? 0;
                    
                    $total_penjualan_bersih = $total_penjualan_kotor - $total_retur;
                    
                    if($cekrow3 > 0 && $total_penjualan_kotor){
                        echo ribuan($total_penjualan_bersih);
                        } else {
                            echo '0';
                        } ?></h4>
                </div>
                </div>
            </div>
        </div>

</div><!-- end row -->

<div class="card">
    <div class="card-header">
        <div class="card-tittle">
            <i class="fa fa-table me-2"></i> Data Laporan 
        </div>
    </div>
        <div class="card-body">
            <table class="table table-striped table-sm table-bordered dt-responsive nowrap" id="table" width="100%">
                        <thead>
                        <tr>
                            <th>No.</th>
                            <th>No. Nota</th>
                            <th>Pelanggan</th>
                            <th>Qty</th>
                            <th>Catatan</th>
                            <th>SubTotal</th>
                            <th>Pembayaran</th>
                            <th>Kembalian</th>
                            <th>Tanggal</th>
                            <th>Opsi</th>
                        </tr>
                        </thead>
                        <tbody>
                            <?php
                                $no=1;
                                $where_clause = "WHERE e.idpelanggan=l.idpelanggan";
                                
                                if(isset($_GET['tgl_awal']) && isset($_GET['tgl_akhir'])) {
                                    $tanggal_awal = $_GET['tgl_awal'];
                                    $tanggal_akhir = $_GET['tgl_akhir'];
                                    $where_clause .= " AND DATE(l.tgl_sub) BETWEEN '$tanggal_awal' AND '$tanggal_akhir'";
                                }
                                
                                $data_produk=mysqli_query($conn,"SELECT * FROM laporan l, pelanggan e $where_clause ORDER BY idlaporan ASC");
                                while($d=mysqli_fetch_array($data_produk)){
                                    $idlaporan= $d['idlaporan'];
                                    $nota= $d['no_nota'];
                                    ?>
                                    
                                    <tr>
                                        <td><?php echo $no++ ?></td>
                                        <td><?php echo $d['no_nota'] ?></td>
                                        <td><?php echo $d['nama_pelanggan'] ?></td>
                                        <td><?php 
                                            $itungtrans = mysqli_query($conn,"SELECT SUM(quantity) as jumlahtrans
                                             FROM tb_nota where no_nota='$nota'");
                                            $itungtrans2 = mysqli_fetch_assoc($itungtrans);
                                            $itungtrans3 = $itungtrans2['jumlahtrans'];
                                            echo $itungtrans3;
                                        ?></td>
                                         <td class="catatan"><?php echo $d['catatan'] ?></td>
                                         <td>Rp.<?php echo ribuan($d['totalbeli']) ?></td>
                                         <td>Rp.<?php echo ribuan($d['pembayaran']) ?></td>
                                         <td>Rp.<?php echo ribuan($d['kembalian']) ?></td>
                                        <td><?php echo $d['tgl_sub'] ?></td>
                                        <td>
                                        <a class="btn btn-primary btn-xs" href="detail.php?invoice=<?php echo $nota ?>">
                                            <i class="fa fa-eye fa-xs mr-1"></i>View</a>
                                            <a class="btn btn-danger btn-xs" href="?hapus=<?php echo $nota ?>" 
                                            onclick="javascript:return confirm('Hapus Data laporan - <?php echo $d['no_nota'] ?> ?');">
                                            <i class="fa fa-trash fa-xs mr-1"></i>Hapus</a>
                                        </td>
                                    </tr>		
                        <?php }?>
					</tbody>
                </table>
        </div>
</div>

<!-- Retur Section -->
<div class="card mt-4">
    <div class="card-header">
        <div class="card-tittle">
            <i class="fa fa-undo me-2"></i> Data Retur Barang
        </div>
    </div>
        <div class="card-body">
            <table class="table table-striped table-sm table-bordered dt-responsive nowrap" id="returTable" width="100%">
                        <thead>
                        <tr>
                            <th>No.</th>
                            <th>No. Retur</th>
                            <th>No. Nota</th>
                            <th>Produk</th>
                            <th>Qty</th>
                            <th>Subtotal</th>
                            <th>Tanggal</th>
                            <th>Keterangan</th>
                        </tr>
                        </thead>
                        <tbody>
                            <?php
                                $no=1;
                                $where_retur = "";
                                
                                if(isset($_GET['tgl_awal']) && isset($_GET['tgl_akhir'])) {
                                    $tanggal_awal = $_GET['tgl_awal'];
                                    $tanggal_akhir = $_GET['tgl_akhir'];
                                    $where_retur = "WHERE DATE(tanggal_retur) BETWEEN '$tanggal_awal' AND '$tanggal_akhir'";
                                }
                                
                                $data_retur=mysqli_query($conn,"SELECT r.*, p.nama_produk FROM retur_barang r JOIN produk p ON r.idproduk = p.idproduk $where_retur ORDER BY idretur ASC");
                                while($d=mysqli_fetch_array($data_retur)){
                                    ?>
                                    
                                    <tr>
                                        <td><?php echo $no++ ?></td>
                                        <td>RET<?php echo str_pad($d['idretur'], 6, "0", STR_PAD_LEFT) ?></td>
                                        <td><?php echo $d['no_nota'] ?></td>
                                        <td><?php echo $d['nama_produk'] ?></td>
                                        <td><?php echo $d['quantity'] ?></td>
                                        <td>Rp.<?php echo ribuan($d['subtotal']) ?></td>
                                        <td><?php echo $d['tanggal_retur'] ?></td>
                                        <td><?php echo $d['keterangan'] ?></td>
                                    </tr>		
                        <?php }?>
					</tbody>
                </table>
        </div>
</div>

<?php 
	if(!empty($_GET['hapus'])){
		$nota = $_GET['hapus'];
		$hapus_data = mysqli_query($conn, "DELETE FROM laporan WHERE no_nota='$nota'");
        $hapus_data1 = mysqli_query($conn, "DELETE FROM tb_nota WHERE no_nota='$nota'");
        if($hapus_data&&$hapus_data1){
            echo '<script>alert("Berhasi Hapus data laporan");window.location="laporan.php"</script>';
        } else {
            echo '<script>alert("gagal Hapus data laporan");history.go(-1);</script>';
        }
	};
?>

<script>
function printReport() {
    // Get the filter parameters
    const tgl_awal = document.querySelector('input[name="tgl_awal"]').value;
    const tgl_akhir = document.querySelector('input[name="tgl_akhir"]').value;
    const jenis = document.querySelector('select[name="jenis"]').value;
    
    // Open print window with filtered data
    const printUrl = `print_laporan.php?tgl_awal=${tgl_awal}&tgl_akhir=${tgl_akhir}&jenis=${jenis}`;
    window.open(printUrl, '_blank');
}
</script>

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