/* Parcel Selector — Label print-popup helper.
 * Opens a new window with the PDF preview + print/download toolbar. */

			(function(){
				if (window.psleOpenPrintWindow) return;
				window.psleOpenPrintWindow = function(pdfUrl) {
					var w = 620, h = 880;
					var left = Math.max(0, (screen.availWidth  - w) / 2);
					var top  = Math.max(0, (screen.availHeight - h) / 2);
					var win = window.open('about:blank', '_blank',
						'width='+w+',height='+h+',top='+top+',left='+left+
						',menubar=no,toolbar=no,location=no,status=no,scrollbars=no,resizable=yes');
					if (!win) { alert('Wyłącz blokadę popup, aby otworzyć etykietę.'); return; }
					// NS-70 2026-04-30: dodano DOWNLOAD button obok PRINT.
					// Klik DOWNLOAD: fetch + blob + auto-click <a download> = zapis na dysku.
					// Fallback (cross-origin block): window.open + ?download=1 query param.
					var pdfFilename = 'etykieta-' + new Date().toISOString().slice(0,10).replace(/-/g,'') + '-' + Math.random().toString(36).slice(2,8) + '.pdf';
					var html = ''+
						'<!DOCTYPE html><html lang="pl"><head><meta charset="UTF-8">'+
						'<title>Etykieta — drukuj / pobierz</title>'+
						'<style>'+
						'@page { size: A4 portrait; margin: 0; }'+
						'html,body { margin:0; padding:0; height:100%; background:#f1f5f9; font-family:-apple-system,sans-serif; overflow:hidden; }'+
						'.toolbar { position:fixed; top:0; left:0; right:0; height:54px; background:linear-gradient(135deg,#10b981 0%,#059669 100%); display:flex; align-items:center; justify-content:center; gap:10px; box-shadow:0 4px 14px rgba(0,0,0,.12); z-index:999; }'+
						'.tb-btn { padding:10px 22px; border:0; border-radius:999px; font-weight:800; font-size:14px; cursor:pointer; box-shadow:0 4px 12px rgba(0,0,0,.18); letter-spacing:.04em; transition:transform .15s ease, background .15s ease; font-family:inherit; display:inline-flex; align-items:center; gap:6px; }'+
						'.tb-btn:hover { transform:scale(1.06); }'+
						'.tb-print { background:#fff; color:#059669; }'+
						'.tb-download { background:#1e293b; color:#fff; }'+
						'.tb-download:hover { background:#0f172a; }'+
						'.tb-download.done { background:#10b981; color:#fff; }'+
						'.label-frame { position:absolute; top:54px; left:0; right:0; bottom:0; background:#fff; }'+
						'.label-frame iframe { width:100%; height:100%; border:0; display:block; background:#fff; }'+
						'@media print {'+
						'  @page { size: A4 portrait; margin: 0; }'+
						'  .toolbar { display:none !important; }'+
						'  html,body { background:#fff !important; overflow:visible !important; margin:0 !important; padding:0 !important; }'+
						/* NS-81 2026-04-30: etykieta w ćwiartce A4 (105×148mm = A6) z marginesem
						   bezpieczeństwa 7mm na każdej stronie żeby drukarka nic nie obcięła.
						   Inner iframe area: 91×134mm (po odjęciu 2×7mm). PDF page A6 (105×148mm)
						   skaluje się przez view=Fit & zoom=page-fit żeby zmieścić całość bez obcięcia. */
						'  .label-frame { position:absolute !important; top:0 !important; left:0 !important; right:auto !important; bottom:auto !important; width:105mm !important; height:148mm !important; padding:7mm !important; box-sizing:border-box !important; background:#fff !important; }'+
						'  .label-frame iframe { width:91mm !important; height:134mm !important; display:block !important; }'+
						'}'+
						'</style></head><body>'+
						'<div class="toolbar">'+
						'  <button class="tb-btn tb-print" onclick="window.print()">🖨️ DRUKUJ A6 (1/4 A4 — lewy górny róg, 7mm safe margin)</button>'+
						'  <button class="tb-btn tb-download" id="tbDownload">📥 POBIERZ PDF</button>'+
						'</div>'+
						'<div class="label-frame"><iframe src="' + pdfUrl + '#toolbar=0&navpanes=0&scrollbar=0&view=Fit&zoom=page-fit"></iframe></div>'+
						'<script>'+
						'(function(){'+
						'  var pdfUrl = ' + JSON.stringify(pdfUrl) + ';'+
						'  var fileName = ' + JSON.stringify(pdfFilename) + ';'+
						'  var btn = document.getElementById("tbDownload");'+
						'  btn.addEventListener("click", function(){'+
						'    var orig = btn.innerHTML;'+
						'    btn.innerHTML = "⏳ Pobieranie...";'+
						'    btn.disabled = true;'+
						'    fetch(pdfUrl, { credentials: "same-origin" })'+
						'      .then(function(r){ if (!r.ok) throw new Error("HTTP " + r.status); return r.blob(); })'+
						'      .then(function(blob){'+
						'        var a = document.createElement("a");'+
						'        a.href = URL.createObjectURL(blob);'+
						'        a.download = fileName;'+
						'        document.body.appendChild(a);'+
						'        a.click();'+
						'        setTimeout(function(){'+
						'          document.body.removeChild(a);'+
						'          URL.revokeObjectURL(a.href);'+
						'          btn.innerHTML = "✓ POBRANE — " + fileName;'+
						'          btn.classList.add("done");'+
						'          setTimeout(function(){ btn.innerHTML = orig; btn.classList.remove("done"); btn.disabled = false; }, 3000);'+
						'        }, 200);'+
						'      })'+
						'      .catch(function(e){'+
						'        var sep = pdfUrl.indexOf("?") > -1 ? "&" : "?";'+
						'        window.open(pdfUrl + sep + "download=1", "_blank");'+
						'        btn.innerHTML = orig;'+
						'        btn.disabled = false;'+
						'      });'+
						'  });'+
						'})();'+
						'<\/script>'+
						'</body></html>';
					win.document.open();
					win.document.write(html);
					win.document.close();
				};
			})();