import JsBarcode from "jsbarcode";
export function printBarcode(product) {
  const win = window.open("", "_blank", "width=650,height=500");
  if (!win) throw new Error("Allow the label preview popup, then try again.");
  win.document.title = `Stockroom label · ${product.sku}`;
  const style = win.document.createElement("style");
  style.textContent =
    "body{font:14px Arial;padding:32px;color:#101b2c}h1{font-size:23px}svg{max-width:100%;height:auto}button{padding:12px 22px;margin-top:20px;background:#101b2c;color:white;border:0;border-radius:5px}@media print{button{display:none}body{padding:0}}";
  win.document.head.append(style);
  const brand = win.document.createElement("p");
  brand.textContent = "STOCKROOM / GLASS HOUSE TECHNOLOGIES";
  const mark = win.document.createElement("img");
  mark.src = new URL("/brand/stockroom-mark.png", window.location.origin).href;
  mark.alt = "Stockroom"; mark.width = 64; mark.height = 64;
  const h = win.document.createElement("h1");
  h.textContent = product.name;
  const p = win.document.createElement("p");
  p.textContent = `${product.sku} · per ${product.unit}`;
  const svg = win.document.createElementNS("http://www.w3.org/2000/svg", "svg");
  JsBarcode(svg, product.barcode, {
    format: "CODE128",
    width: 2,
    height: 70,
    displayValue: true,
    margin: 15,
  });
  const button = win.document.createElement("button");
  button.textContent = "Print label";
  button.onclick = () => win.print();
  win.document.body.append(mark, brand, h, p, svg, button);
}
