<?php
  require_once('includes/load.php');
  // Pastikan user memiliki hak akses yang diperlukan
  page_require_level(4); 
  
  // Periksa apakah ada ID aset yang dikirim melalui URL
  if(isset($_GET['id']) && (int)$_GET['id'] > 0){
      $asset_id = (int)$_GET['id'];
      $asset = find_by_sql("SELECT a.*, c.category_name, e.first_name, e.last_name FROM assets a LEFT JOIN assets_categories c ON a.category_id = c.id LEFT JOIN employees e ON a.employee_id = e.id WHERE a.id = '{$asset_id}' LIMIT 1");
      
      // Jika aset ditemukan, siapkan data
      if($asset){
          $asset_data = $asset[0];
          $asset_name = $asset_data['asset_name'];
          $asset_serial = $asset_data['serial_number'];
          $asset_location = $asset_data['location'];
          $asset_category = $asset_data['category_name'];
          $employee_name = $asset_data['first_name'] . ' ' . $asset_data['last_name'];
          
          // Data yang akan dienkode dalam QR code (misalnya: ID atau Nomor Seri)
          $qr_data_json = json_encode([
              'id' => $asset_data['id'],
              'nama' => $asset_name,
              'sn' => $asset_serial,
              'lokasi' => $asset_location,
              'kategori' => $asset_category
          ]);

      } else {
          // Aset tidak ditemukan, beri pesan error
          $session->msg("d", "Aset tidak ditemukan.");
          redirect('asset_management.php');
      }
  } else {
      // ID tidak valid, beri pesan error
      $session->msg("d", "ID aset tidak valid.");
      redirect('asset_management.php');
  }
?>

<!DOCTYPE html>
<html>
<head>
    <title>Cetak QR Code Aset</title>
    <style>
        @media print {
            body { font-family: Arial, sans-serif; }
            .qrcode-container {
                width: 300px;
                height: auto;
                padding: 10px;
                border: 1px solid #000;
                margin: 5mm;
                box-sizing: border-box;
                page-break-inside: avoid;
            }
            .qrcode-label {
                font-size: 14px;
                font-weight: bold;
                margin-bottom: 5px;
            }
            .qrcode-info {
                font-size: 12px;
                text-align: left;
                margin-top: 5px;
            }
        }
    </style>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script>
</head>
<body>

<div class="qrcode-container">
    <div class="qrcode-label"><?php echo remove_junk($asset_name); ?></div>
    <div class="qrcode-info">
        <div><strong>Kategori:</strong> <?php echo remove_junk($asset_category); ?></div>
        <div><strong>Lokasi:</strong> <?php echo remove_junk($asset_location); ?></div>
        <?php if(!empty($employee_name)): ?>
        <div><strong>PJ:</strong> <?php echo remove_junk($employee_name); ?></div>
        <?php endif; ?>
    </div>
    <div id="qrcode"></div>
</div>

<script>
    // Data yang akan dienkode dalam QR code
    const qr_data = '<?php echo $qr_data_json; ?>';

    // Buat instance QRCode
    new QRCode(document.getElementById("qrcode"), {
        text: qr_data,
        width: 128,
        height: 128,
        colorDark : "#000000",
        colorLight : "#ffffff",
        correctLevel : QRCode.CorrectLevel.H
    });
    
    // Langsung mencetak halaman ini setelah dimuat
    window.onload = function() {
        window.print();
    }
</script>

</body>
</html>