<?php 
// --- Pastikan header dikirim sebelum HTML ---
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=Laporan_Penjualan_" . date('Y-m-d_His') . ".xls");
header("Pragma: no-cache");
header("Expires: 0");

// Koneksi ke database
$conn = mysqli_connect("localhost", "root", "", "db_roti");

// Ambil data filter tanggal
$date1 = isset($_POST['date1']) ? $_POST['date1'] : date('Y-m-d');
$date2 = isset($_POST['date2']) ? $_POST['date2'] : date('Y-m-d');

// Query data
$result = mysqli_query($conn, "SELECT * FROM produksi WHERE terima = 1 AND DATE(tanggal) BETWEEN '$date1' AND '$date2'");
$no = 1;
$total = 0;
?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
	<meta http-equiv="Content-Type" content="application/vnd.ms-excel; charset=UTF-8">
    <title>Laporan Penjualan</title>
</head>
<body>
    <table border="1">
        <tr style="background-color: #d3d3d3;">
            <th>No</th>
            <th>Nama Produk</th>
            <th>Tanggal</th>
            <th>Qty</th>
        </tr>

        <?php while ($row = mysqli_fetch_assoc($result)) { ?>
        <tr>
            <td><?= $no++; ?></td>
            <td><?= $row['nama_produk']; ?></td>
            <td><?= $row['tanggal']; ?></td>
            <td><?= $row['qty']; ?></td>
        </tr>
        <?php 
            $total += $row['qty'];
        } 
        ?>
        <tr>
            <td colspan="4" style="text-align:right;"><b>Total Jumlah Terjual = <?= $total; ?></b></td>
        </tr>
    </table>
</body>
</html>
