<?php
include 'header.php';
include '../koneksi/koneksi.php';

// Validasi invoice
if (!isset($_GET['inv']) || empty($_GET['inv'])) {
    echo "<div style='padding:20px; color:red; font-weight:bold;'>Invoice tidak ditemukan. <a href='produksi.php'>Kembali</a></div>";
    exit;
}

$inv = mysqli_real_escape_string($conn, $_GET['inv']);
$d_order = mysqli_query($conn, "SELECT * FROM produksi WHERE invoice='$inv' LIMIT 1");
$t_order = mysqli_fetch_assoc($d_order);

if (!$t_order) {
    echo "<div style='padding:20px; color:red; font-weight:bold;'>Data order tidak ditemukan. <a href='produksi.php'>Kembali</a></div>";
    exit;
}

// Ambil data customer
$cs = mysqli_query($conn, "SELECT * FROM customer WHERE kode_customer='".$t_order['kode_customer']."' LIMIT 1");
$t_cs = mysqli_fetch_assoc($cs);

// Ambil item pesanan
$total = 0;
$items = [];
$order_items = mysqli_query($conn, "SELECT * FROM produksi WHERE invoice='$inv'");
while ($row = mysqli_fetch_assoc($order_items)) {
    $row['subtotal'] = $row['harga'] * $row['qty'];
    $total += $row['subtotal'];
    $items[] = $row;
}

// Format teks untuk RawBT (rapi dan sejajar)
$struk_text  = "";
$struk_text .= "         UMKM GALERI 3D\n";
$struk_text .= "Jl. Jend. Sudirman No 60 RT.08 RW.01\n";
$struk_text .= "Sumberpucung - Malang\n";
$struk_text .= "Telp: 0831-7495-6776\n";
$struk_text .= "--------------------------------\n";
$struk_text .= "Invoice : ".$t_order['invoice']."\n";
$struk_text .= "Tanggal : ".date("d-m-Y H:i")."\n";
$struk_text .= "Customer: ".$t_cs['nama']."\n";
$struk_text .= "--------------------------------\n";
foreach ($items as $it) {
    $nama = strtoupper($it['nama_produk']);
    $harga = number_format($it['harga'], 0, ',', '.');
    $sub = number_format($it['subtotal'], 0, ',', '.');
    $qty = $it['qty'];
    
    // format dengan padding agar sejajar
    $line1 = sprintf("%-32.32s", $nama);
    $line2 = sprintf("%-15s %17s", "{$qty} x Rp {$harga}", "Rp {$sub}");
    $struk_text .= $line1."\n".$line2."\n";
}
$struk_text .= "--------------------------------\n";
$struk_text .= sprintf("%-15s %17s\n", "TOTAL", "Rp ".number_format($total,0,',','.'));
$struk_text .= "--------------------------------\n";
$struk_text .= "Terima Kasih Telah Berbelanja\n";
$struk_text .= "       UMKM GALERI 3D\n\n\n";
?>

<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Struk Belanja</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
    background: #f0f2f5;
    font-family: 'Poppins', sans-serif;
    margin: 0;
    padding: 0;
}

.container-struk {
    max-width: 480px;
    margin: 40px auto;
    background: white;
    border-radius: 16px;
    box-shadow: 0 6px 14px rgba(0,0,0,0.1);
    padding: 25px;
}

.header {
    text-align: center;
    font-weight: bold;
    font-size: 18px;
    color: #2d3436;
}
.header small {
    font-size: 13px;
    color: #636e72;
}
.line {
    border-top: 1px dashed #000;
    margin: 10px 0;
}
table {
    width: 100%;
    border-collapse: collapse;
    font-size: 14px;
}
td {
    padding: 4px 0;
}
.right {
    text-align: right;
}
.center {
    text-align: center;
}

/* Tombol area */
.btn-area {
    display: flex;
    justify-content: center;
    gap: 12px;
    margin-top: 25px;
    flex-wrap: wrap;
}

/* Style dasar tombol */
.btn-rawbt, .btn-back {
    display: inline-block;
    border: none;
    outline: none;
    cursor: pointer;
    padding: 14px 25px;
    border-radius: 10px;
    font-size: 15px;
    font-weight: bold;
    text-decoration: none; /* hilangkan garis bawah */
    color: white;
    box-shadow: 0 3px 6px rgba(0,0,0,0.15);
    user-select: none;
    transition: background 0.2s ease;
}

/* Warna tombol */
.btn-rawbt {
    background: linear-gradient(90deg,#6c5ce7,#4834d4);
}
.btn-back {
    background: #b2bec3;
    color: #2d3436;
}

/* Hover (opsional, tidak wajib ubah warna) */
.btn-rawbt:hover {
    background: linear-gradient(90deg,#5b4ce0,#3b2fb3);
}
.btn-back:hover {
    background: #a4b0be;
}

/* Hilangkan efek active / fokus */
.btn-rawbt:active,
.btn-back:active,
.btn-rawbt:focus,
.btn-back:focus {
    outline: none;
    box-shadow: none;
    transform: none;
    opacity: 1;
    background: inherit; /* warna tetap sama */
}

/* Responsif */
@media (max-width: 767px) {
    .container-struk {
        margin: 15px;
        padding: 18px;
    }
    table {
        font-size: 13px;
    }
    .btn-area {
        flex-direction: column;
    }
    .btn-rawbt, .btn-back {
        width: 100%;
        text-align: center;
    }
}

/* Mode cetak */
@page {
    size: 80mm auto;
    margin: 0;
}
@media print {
    body { background:#fff; }
    .btn-area { display: none; }
    .container-struk {
        box-shadow: none;
        border-radius: 0;
        margin: 0;
        padding: 5px;
        font-size: 11px;
    }
}
</style>
</head>
<body>

<div class="container-struk">
    <div class="header">
        UMKM GALERI 3D<br>
        <small>Jl. Jend. Sudirman No 60 RT.08 RW.01<br>
        Sumberpucung - Malang<br>
        Telp: 0831-7495-6776</small>
    </div>

    <div class="line"></div>

    <table>
        <tr><td>No. Invoice</td><td>: <?= htmlspecialchars($t_order['invoice']) ?></td></tr>
        <tr><td>Tanggal</td><td>: <?= date("d-m-Y H:i") ?></td></tr>
        <tr><td>Customer</td><td>: <?= htmlspecialchars($t_cs['nama']) ?></td></tr>
    </table>

    <div class="line"></div>

    <table>
        <?php foreach($items as $it): ?>
        <tr><td colspan="2"><b><?= htmlspecialchars($it['nama_produk']) ?></b></td></tr>
        <tr>
            <td><?= $it['qty'] ?> x Rp <?= number_format($it['harga'],0,',','.') ?></td>
            <td class="right">Rp <?= number_format($it['subtotal'],0,',','.') ?></td>
        </tr>
        <?php endforeach; ?>
    </table>

    <div class="line"></div>
    <table>
        <tr>
            <td><b>Total</b></td>
            <td class="right"><b>Rp <?= number_format($total,0,',','.') ?></b></td>
        </tr>
    </table>

    <div class="line"></div>
    <div class="center" style="margin-top:10px;">
        <b>Terima Kasih</b><br>
        --- Telah Berbelanja di ---<br>
        <b>UMKM GALERI 3D</b>
    </div>

    <div class="btn-area">
        <a href="rawbt:<?= urlencode($struk_text) ?>" class="btn-rawbt">🖨 Cetak Struk</a>
        <a href="produksi.php" class="btn-back">⬅ Kembali</a>
    </div>
</div>

</body>
</html>
