<?php
// invoice_preview.php
$nama_customer = $_POST['nama_customer'];
$nama_usaha = $_POST['nama_usaha'];
$alamat = $_POST['alamat'];
$telp = $_POST['telp'];
$no_invoice = $_POST['no_invoice'];
$barang = $_POST['barang'];
$qty = $_POST['qty'];
$uom = $_POST['uom'];
$harga = $_POST['harga'];
$keterangan = $_POST['keterangan'];

$total = 0;
for ($i = 0; $i < count($barang); $i++) {
    $total += $harga[$i];
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Invoice <?= htmlspecialchars($no_invoice) ?></title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css">
    <style>
        body { font-family: Arial, sans-serif; font-size: 13px; color: #000; }
        .invoice-box { max-width: 900px; margin: 20px auto; padding: 20px; border: 1px solid #ddd; }
        .header { text-align: center; margin-bottom: 10px; }
        .header h4 { font-weight: bold; margin-bottom: 3px; }
        .header p { margin: 0; font-size: 12px; line-height: 1.4; }
        .table td, .table th { vertical-align: middle; padding: 6px; }
        .table-bordered th, .table-bordered td { border: 1px solid #000 !important; }
        .no-border td { border: none !important; padding: 2px 6px; }
        .text-end { text-align: right; }
        .note { font-size: 12px; margin-top: 10px; line-height: 1.5; }
        .footer { margin-top: 30px; text-align: center; font-size: 12px; }
        .logo { width: 80px; margin: 8px 0; }
        hr { border-top: 1px solid #000; margin: 6px 0 10px 0; }
        .right-info { width: 300px; }
        .note-table td { padding: 3px 8px; }
        .bank-box {
            border: 1px solid #000; 
            padding: 6px 10px; 
            display: inline-block; 
            text-align: left;
            margin-top: 8px;
        }
    </style>
</head>
<body>
<div class="invoice-box">

    <!-- Header Perusahaan -->
    <div class="header">
        <h4>PILARIAN GLOBAL SOLUTION</h4>
        <p>
            Jl. Cendana III No.18 Permata Puri Harmoni 1<br>
            Sitisari - Cileungsi, Kab. Bogor – Jawa Barat 16820<br>
            Site B : Terondol, Kota Serang – Banten 42119<br>
            Telp : 08 22107 22400 | Email : admin@sadayajasa.com | Website : www.sadayajasa.com
        </p>
        <hr>
        <h5><b>INVOICE</b></h5>
        <p><b>No. <?= htmlspecialchars($no_invoice) ?></b></p>
    </div>

    <!-- Informasi Penerima -->
    <table class="table no-border" style="width:100%;">
        <tr>
            <td width="110">Kepada Yth</td>
            <td>: <?= htmlspecialchars($nama_customer) ?></td>
            <td class="right-info">Sales</td>
            <td>: -</td>
        </tr>
        <tr>
            <td></td>
            <td><b><?= htmlspecialchars($nama_usaha) ?></b></td>
            <td>PO</td>
            <td>: Confirm By Quotation</td>
        </tr>
        <tr>
            <td></td>
            <td><?= htmlspecialchars($alamat) ?></td>
            <td>GRN</td>
            <td>: -</td>
        </tr>
        <tr>
            <td>Telpon</td>
            <td>: <?= htmlspecialchars($telp) ?></td>
            <td></td>
            <td></td>
        </tr>
    </table>

    <!-- Daftar Barang -->
    <table class="table table-bordered mt-2">
        <thead class="table-light">
            <tr>
                <th style="width: 40px;">No.</th>
                <th>Nama Barang</th>
                <th style="width: 60px;">Qty</th>
                <th style="width: 60px;">UOM</th>
                <th style="width: 100px;">Harga</th>
                <th style="width: 120px;">Subtotal</th>
            </tr>
        </thead>
        <tbody>
        <?php 
        $grandTotal = 0;
        for ($i = 0; $i < count($barang); $i++):
            $subtotal = $harga[$i];
            $grandTotal += $subtotal;
        ?>
            <tr>
                <td><?= $i+1 ?></td>
                <td><?= htmlspecialchars($barang[$i]) ?></td>
                <td><?= htmlspecialchars($qty[$i]) ?></td>
                <td><?= htmlspecialchars($uom[$i]) ?></td>
                <td>Rp <?= number_format($harga[$i], 0, ',', '.') ?></td>
                <td>Rp <?= number_format($subtotal, 0, ',', '.') ?></td>
            </tr>
        <?php endfor; ?>
        </tbody>
    </table>

    <!-- Note dan Total (2 Kolom) -->
    <table style="width:100%; margin-top:10px;">
        <tr valign="top">
            <td width="55%">
                <table class="note-table">
                    <tr><td colspan="2"><b>Note :</b></td></tr>
                    <tr><td>- Pembayaran</td><td>: Cash / Transfer / QRIS</td></tr>
                    <tr><td>- PIC Customer</td><td>: <?= htmlspecialchars($nama_customer) ?></td></tr>
                    <tr><td>- Keterangan</td><td>: <?= htmlspecialchars($keterangan) ?></td></tr>
                </table>
            </td>
            <td width="45%">
                <table style="width:100%;">
                    <tr><td class="text-end"><b>Sub Total :</b></td><td class="text-end">Rp <?= number_format($grandTotal, 0, ',', '.') ?></td></tr>
                    <tr><td class="text-end"><b>Diskon :</b></td><td class="text-end">Rp 0</td></tr>
                    <tr><td class="text-end"><b>Total :</b></td><td class="text-end">Rp <?= number_format($grandTotal, 0, ',', '.') ?></td></tr>
                    <tr><td class="text-end"><b>Grand Total :</b></td><td class="text-end"><b>Rp <?= number_format($grandTotal, 0, ',', '.') ?></b></td></tr>
                </table>
            </td>
        </tr>
    </table>

    <!-- Footer -->
    <div class="footer">
        <p><b>Bogor, <?= date("d F Y") ?></b><br>Hormat Kami</p>
        <img src="https://www.sadayajasa.com/assets/logo.png" alt="Logo" class="logo"><br>
        <b>SADAYA JASA</b><br><br>

        <div class="bank-box">
            <b>Pembayaran transfer ke :</b><br>
            Bank Central Asia (BCA)<br>
            Cepi Ruswandi<br>
            A/C : <b>18 1039 6837</b>
        </div>
        <p style="margin-top:5px;">
            Mohon konfirmasi bukti pembayaran via email ke <b>admin@sadayajasa.com</b><br>
            atau Whatsapp ke nomor <b>08 22107 22400</b>
        </p>
    </div>

</div>
</body>
</html>
