<?php
include 'config.php'; // koneksi ke database


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

$total = 0;
$data_barang = mysqli_query($conn, "SELECT * FROM keranjang");
while ($item = mysqli_fetch_array($data_barang)) {
    $total += $item['subtotal'];
}

$toko = mysqli_query($conn,"SELECT * FROM login ORDER BY nama_toko ASC");
      while($dat = mysqli_fetch_array($toko)){
        $user = $dat['user'];
      }

$total = 0;
$total_item = 0;
$data_barang = mysqli_query($conn, "SELECT * FROM keranjang");
while ($item = mysqli_fetch_array($data_barang)) {
    $total += $item['subtotal'];
    $total_item += $item['quantity'];
}
$bayar = 0;
$kembalian = 0; 
$ambilBayar = mysqli_query($conn, "SELECT bayar FROM keranjang LIMIT 1");
$datBayar = mysqli_fetch_array($ambilBayar);
$bayar = is_numeric($datBayar['bayar']) ? (float)$datBayar['bayar'] : 0;
$kembalian = $bayar - $total;
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link rel="icon" type="image/png" href="assets/img/kasir.ico">
    <title>Nota Pembelian</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
      <style>
       @media print {
            @page {
                size: auto;   /* ikut ukuran printer */
                margin: auto;    /* hilangkan margin default browser */
            }

            body {
                font-family: monospace;
                font-size: 7px;
                margin: 0 auto;     /* supaya tetap center */
                padding: 0 2mm;     /* beri spasi kanan-kiri biar tidak mepet */
                width: 38mm;
                box-sizing: border-box;
            }

            #nota {
                padding: 5px;
            }
        }
        body {
            width: 38mm;       /* dikurangi 2mm biar ada space di kanan */
            font-family: monospace;
            font-size: 7px;
            margin: 0 auto;    /* biar tetap rata tengah */
            padding-right: 11mm; /* tambahan ruang di kanan */
        }

        #nota {
            padding: 5px;
        }

       table {
            width: 100%;
            border-collapse: collapse; /* biar rapat */
        }

        td {
            vertical-align: top;
            padding: 1px 0; /* jarak antar baris diperkecil */
        }


         hr {
            border: none;
            border-top: 1px dashed #000;
            margin: 4px 0;
        }
    </style>
</head>
<body>
<div id="nota">
    <div style="text-align: center;">
        <?php
        $toko = mysqli_query($conn, "SELECT * FROM login ORDER BY nama_toko ASC LIMIT 1");
        while ($dat = mysqli_fetch_array($toko)) {
            echo "<b>{$dat['nama_toko']}</b><br>";
            echo "{$dat['alamat']}<br>";
            echo "Telp. {$dat['telp']}<br>";
        }
        ?>
        <hr>
    </div>
    <table>
        <tr>
            <td style="width: 40px;">Nota</td>
            <td style="width: 10px; text-align: center;">:</td>
            <td>
                <?php
                $notrans = mysqli_query($conn, "SELECT * FROM keranjang ORDER BY no_transaksi ASC LIMIT 1");
                while ($dat2 = mysqli_fetch_array($notrans)) {
                    echo $dat2['no_transaksi'];
                }
                ?>
            </td>
        </tr>
        <tr>
            <td>Kasir</td>
            <td style="text-align: center;">:</td>
            <td><?= $user ?></td>
        </tr>
        <tr>
            <td>Tanggal</td>
            <td style="text-align: center;">:</td>
            <td><?= date("d-m-Y") ?> <?= date("H:i:s") ?></td>
        </tr>
    </table>
    <hr>

    <table>
        <tbody>
        <?php
        $total = 0;
        $total_diskon = 0;
        $data_barang = mysqli_query($conn, "SELECT * FROM keranjang");
        while ($d = mysqli_fetch_array($data_barang)) {
            $qty = is_numeric($d['quantity']) ? (int)$d['quantity'] : 0;
            $harga = is_numeric($d['harga_barang']) ? (float)$d['harga_barang'] : 0;
            $diskon = is_numeric($d['diskon']) ? (float)$d['diskon'] : 0;

            $subtotal_asli = $harga * $qty;
            $subtotal_setelah_diskon = $subtotal_asli - $diskon;

            $total += $subtotal_setelah_diskon;
            $total_diskon += $diskon;

            $ambilBayar = mysqli_query($conn, "SELECT bayar FROM keranjang LIMIT 1");
            $datBayar = mysqli_fetch_array($ambilBayar);
            $bayar = is_numeric($datBayar['bayar']) ? (float)$datBayar['bayar'] : 0;
            $kembalian = $bayar - $total;
            ?>
            <!-- Baris utama item -->
             <tr>
                <td colspan="4"><?= $d['nama_barang'] ?></td>
            </tr>
            <tr>
                <td colspan="2"><?= $qty ?> x <?= format_ribuan($harga) ?></td>
                <td colspan="2" align="right"><?= format_ribuan($subtotal_asli) ?></td>
            </tr>
            <!-- Baris diskon (selalu tampil dan lurus dengan harga) -->
            <tr>
                <td colspan="2"></td>
                <td align="right"><i>Disc</i></td>
                <td align="right"><i><?= format_ribuan($diskon) ?></i></td>
            </tr>
    <?php } ?>
    </tbody>
    </table>
    <hr>
    <table>
        <tr>
            <td style="width: 15px;"><b>Total</b></td>
            <td style="width: 10px; text-align: center;">:</td>
            <td style="text-align: right;"><b><?= format_ribuan($total) ?></b></td>
        </tr>
        <tr>
            <td>Bayar</td>
            <td style="text-align: center;">:</td>
            <td style="text-align: right;"><?= format_ribuan($bayar) ?></td>
        </tr>
        <tr>
            <td>Kembali</td>
            <td style="text-align: center;">:</td>
            <td style="text-align: right;"><?= format_ribuan($kembalian) ?></td>
        </tr>
    </table>
    <hr>
    <table>
        <tr>
            <td style="width: 16px;">Item</td>
            <td style="width: 5px;">:</td>
            <td style="text-align: left;"><?= $total_item ?></td>
        </tr>
    </table>

    <div style="text-align: center;">
        <b style="font-size: 7px; margin: 0;">TERIMA KASIH TELAH BERBELANJA</b>
    </div>
</div>

<script>
    window.print();
    window.onafterprint = function () {
        window.close();
    };
</script>
</body>
</html>
