<?php 
  session_start();
  date_default_timezone_set("Asia/Jakarta");
  $tanggalSekarang = date("Y-m-d");
  $jamSekarang = date("h:i a");

	require 'fungsi.php';
	global $koneksi;

  $no_inv = isset($_GET['no_inv']) ? mysqli_real_escape_string($koneksi, $_GET['no_inv']) : '';
  if (!$no_inv) {
    ?>
    <!DOCTYPE html>
    <html lang="id">
    <head>
        <meta charset="UTF-8">
        <title>Cetak Ulang Struk</title>
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
        <style>
            body {
                background: #f5f5f5;
                display: flex;
                align-items: center;
                justify-content: center;
                min-height: 100vh;
                font-family: 'Plus Jakarta Sans', sans-serif;
            }
            .reprint-card {
                background: white;
                border-radius: 20px;
                padding: 40px;
                box-shadow: 0 4px 20px rgba(0,0,0,0.08);
                width: 100%;
                max-width: 420px;
            }
        </style>
    </head>
    <body>
        <div class="reprint-card">
            <div style="text-align:center;margin-bottom:24px;">
                <i class="fas fa-receipt" style="font-size:2.5rem;color:#1A3A2A;margin-bottom:12px;display:block;"></i>
                <h2 style="font-weight:800;color:#1A3A2A;">Cetak Ulang Struk</h2>
                <p style="color:#6B7280;font-size:0.9rem;">Masukkan nomor invoice untuk mencetak ulang struk.</p>
            </div>
            <form action="print.php" method="GET">
                <div class="mb-3">
                    <label style="font-weight:600;font-size:0.85rem;margin-bottom:6px;">Nomor Invoice</label>
                    <input type="text" class="form-control form-control-lg" name="no_inv" placeholder="Contoh: INV/20260715/0001" required style="border-radius:12px;">
                </div>
                <button type="submit" class="btn btn-primary w-100" style="border-radius:12px;padding:12px;font-weight:700;">
                    <i class="fas fa-print"></i> Cetak Ulang
                </button>
            </form>
            <div style="text-align:center;margin-top:16px;">
                <a href="index.php" style="color:#6B7280;font-size:0.85rem;">Kembali ke Kasir</a>
            </div>
        </div>
    </body>
    </html>
    <?php
    exit;
  }

  $select = mysqli_query($koneksi, "SELECT * FROM laporan_penjualan WHERE no_inv='$no_inv'");
  $items = [];
  $total = 0;
  $kasir = '';
  while ($row = mysqli_fetch_array($select)) {
    $items[] = $row;
    $total += (int)$row['total'];
    $kasir = $row['kasir'];
  }

  if (empty($items)) {
    echo "Data tidak ditemukan.";
    exit;
  }
 ?>
<!DOCTYPE html>
<html lang="id">
<head>
	<meta charset="UTF-8">
	<title>Nota Penjualan</title>
	<style>
		@page { margin: 0; size: 58mm auto; }
		body { 
			color: #000; 
			font-family: 'Courier New', Courier, monospace; 
			font-size: 11px; 
			line-height: 1.2; 
			margin: 0; 
			padding: 0;
			width: 58mm; 
			max-width: 58mm;
		}
		h1 { font-size: 14px; margin: 0 0 4px; text-align: center; }
		p { margin: 0; text-align: center; }
		table { border-collapse: collapse; width: 100%; }
		.header, .footer { text-align: center; margin-bottom: 5px; }
		.separator { border-top: 1px dashed #000; margin: 4px 0; }
		.meta td { padding: 1px 0; vertical-align: top; font-size: 10px; }
		.meta td:first-child { width: 35%; }
		.items td { padding: 2px 0; vertical-align: top; font-size: 11px; }
		.item-name { font-weight: bold; }
		.right { text-align: right; }
		.total td { border-top: 1px dashed #000; font-size: 12px; font-weight: bold; padding-top: 4px; }
	</style>
</head>
<body onload="window.print()">
	<div style="padding: 2mm;">
	<div class="header">
		<h1>TOKO MASTOR</h1>
		<p>Jl. Contoh Alamat Toko No 1</p>
		<p>Telp. 08123456789</p>
	</div>
	<div class="separator"></div>
	<table class="meta">
		<tr><td>No. Invoice</td><td>: <?= $no_inv; ?></td></tr>
		<tr><td>Tanggal</td><td>: <?= $tanggalSekarang; ?></td></tr>
		<tr><td>Jam</td><td>: <?= $jamSekarang; ?></td></tr>
		<tr><td>Kasir</td><td>: <?= $kasir; ?></td></tr>
	</table>
	<div class="separator"></div>
	<table class="items">
		<?php foreach ($items as $item): ?>
			<tr><td colspan="2" class="item-name"><?= $item['nm_barang']; ?></td></tr>
			<tr>
				<td><?= $item['jumlah_beli']; ?> x Rp <?= number_format((int)$item['total']/(int)$item['jumlah_beli'], 0, ',', '.') ?></td>
				<td class="right">Rp <?= number_format($item['total'], 0, ',', '.') ?></td>
			</tr>
		<?php endforeach; ?>
		<tr class="total">
			<td>TOTAL</td>
			<td class="right">Rp <?= number_format($total, 0, ',', '.') ?></td>
		</tr>
	</table>
	<div class="separator"></div>
	<div class="footer">
		<p>Terima kasih telah berbelanja</p>
		<p>Barang yang sudah dibeli tidak dapat ditukar</p>
	</div>
	</div>
    <script type="text/javascript">
      window.onafterprint = function() {
        window.close();
      }
      
      window.addEventListener('focus', function() {
        setTimeout(function() {
            window.close();
        }, 1000);
      });
    </script>
</body>
</html>
