<?php
session_start();
include 'koneksi.php';

if(!isset($_SESSION['user'])){
    header("Location: index.php");
    exit;
}

$username = $_SESSION['user'];
$tanggal  = $_GET['tgl'];

// ambil data order + produk + alamat
$data = mysqli_query($conn, "
    SELECT o.*, p.nama_produk, p.gambar, u.alamat
    FROM orders o
    JOIN produk p ON o.produk_id = p.id
    JOIN users u ON o.username = u.username
    WHERE o.username='$username'
    AND o.tanggal='$tanggal'
");

// ambil data pertama (alamat)
$first = mysqli_fetch_assoc($data);

// nomor invoice otomatis
$invoice = "INV-".date('Ymd')."-".substr(md5($tanggal),0,5);
?>

<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Invoice</title>

<style>
body{
    font-family:'Segoe UI',sans-serif;
    background:#f4f6f9;
    padding:20px;
}

/* CONTAINER */
.invoice-box{
    max-width:900px;
    margin:auto;
    background:white;
    padding:30px;
    border-radius:20px;
    box-shadow:0 10px 30px rgba(0,0,0,0.1);
}

/* HEADER */
.header{
    display:flex;
    justify-content:space-between;
    align-items:center;
    border-bottom:2px solid #eee;
    padding-bottom:15px;
}
.logo{
    height:70px;
}
.title{
    text-align:right;
}
.title h2{
    margin:0;
    color:#f44336;
}
.title small{
    color:#777;
}

/* INFO */
.info{
    margin-top:20px;
    display:flex;
    justify-content:space-between;
}
.info div{
    font-size:14px;
}

/* TABLE */
table{
    width:100%;
    border-collapse:collapse;
    margin-top:25px;
}
th{
    background:#f44336;
    color:white;
    padding:12px;
}
td{
    padding:12px;
    border-bottom:1px solid #eee;
}
.total{
    text-align:right;
    font-size:18px;
    font-weight:bold;
    color:#2e7d32;
}

/* BUTTON */
.btn{
    margin-top:20px;
    padding:10px 20px;
    border:none;
    border-radius:10px;
    background:#28a745;
    color:white;
    cursor:pointer;
    font-weight:bold;
}
.btn:hover{
    background:#218838;
}

/* PRINT */
@media print{
    .btn{
        display:none;
    }
}
</style>

</head>
<body>

<div class="invoice-box">

    <!-- HEADER -->
    <div class="header">
        <img src="images/wpo.png" class="logo">
        <div class="title">
            <h2>INVOICE</h2>
            <small><?= $invoice ?></small><br>
            <small><?= date('d M Y H:i', strtotime($tanggal)) ?></small>
        </div>
    </div>

    <!-- INFO -->
    <div class="info">
        <div>
            <b>Dari:</b><br>
            Warung Pojok Online
        </div>

        <div>
            <b>Kepada:</b><br>
            <?= $username ?><br>
            <?= $first['alamat'] ?>
        </div>
    </div>

    <!-- TABLE -->
    <table>
        <tr>
            <th>Produk</th>
            <th>Qty</th>
            <th>Harga</th>
            <th>Subtotal</th>
        </tr>

        <?php
        mysqli_data_seek($data,0);
        $total = 0;

        while($d = mysqli_fetch_assoc($data)){
            $harga = $d['total_harga'] / $d['qty'];
            $subtotal = $d['total_harga'];
            $total += $subtotal;
        ?>
        <tr>
            <td><?= $d['nama_produk'] ?></td>
            <td><?= $d['qty'] ?></td>
            <td>Rp <?= number_format($harga) ?></td>
            <td>Rp <?= number_format($subtotal) ?></td>
        </tr>
        <?php } ?>

        <tr>
            <td colspan="3" class="total">TOTAL</td>
            <td class="total">Rp <?= number_format($total) ?></td>
        </tr>
    </table>

    <!-- BUTTON PRINT -->
    <button class="btn" onclick="window.print()">🖨️ Print / Save PDF</button>

</div>

</body>
</html>