<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<!-- (c) Copyright 2020 by ZiLoX IT - Heiko Zimmermann & Stephan Lorsbach GbR -->
<!-- DHL A6 auf A4 - Quervorlage auf Hoch gedreht-->

<form version="0.1">
	<options>
		<input type="text" id="anzahl" description="Anzahl Aufkleber" />
		<select id="start" description="Startposition">
			<option value="0">oben links</option>
			<option value="1">oben rechts</option>
			<option value="2">unten links</option>
			<option value="3">unten rechts</option>
		</select>
		
		<script><![CDATA[
			console.debug("Updating Options based on current record");
			var Form = this.parent;
			
			var anzahl = this.option("anzahl");
			var pakete = Form.dataRecord().getValue("paketanzahl");
			anzahl.setValue(pakete);
		]]></script>
	</options>
	
	<fields>
		<field type="text" name="absender" description="Absender des Pakets">Weingut Mustermann
			00000 Musterstadt
			Musterstraße 1</field>
	</fields>
	
	<page width="210" height="297">
		<section id="label" x="0" y="0" width="105" height="148.5">
			<!-- Absender Adresse -->
			<text id="absender" x="25" y="34" width="60" height="35" angle="90"><![CDATA[
				<p style="font:40pt 'Arial';">
					VALUE
				</p>
			]]>
			</text>	
						
			<!-- Empfänger -->
			<text id="empfaenger" x="-15" y="90" height="30" width="70" angle="90"><![CDATA[
				<p style="font:60pt 'Arial';">
					VALUE
				</p>
			]]>
			</text>
			
			<script><![CDATA[
				var Form = this.page.form;
				var anschrift = Form.dataRecord().getValue("anschrift");
				var lieferadresse = Form.dataRecord().getValue("lieferadresse");
				
				var empfaenger = lieferadresse;
				if (empfaenger=="") empfaenger = anschrift;
				
				var absender = Form.fields("absender");
				
				this.getElementById("empfaenger").replace("VALUE", empfaenger.nl2br() );
				this.getElementById("absender").replace("VALUE", absender.nl2br() );
				
			]]></script>
		</section>
	</page>
	
	<script execute="afterAppend"><![CDATA[
		console.debug("After append called!");
		
		var pageCount = this.getPageCount();
		if (pageCount>0) {
			var doSimplification = true;
			var w = this.getPage(0).width();
			var h = this.getPage(0).height();
			for(var i=1; i<pageCount; ++i) {
				if (this.getPage(i).width()!=w || this.getPage(i).height()!=h) {
					doSimplification = false;
					break;
				}
			}
			
			if (doSimplification===true) {
				// 1st collect all sections and pages
				var allSections = [];
				var allPages = [];
				for(var i=0; i<pageCount; ++i) {
					var page = this.getPage(i);
					var sections = page.getSections();
					allSections = allSections.concat( sections );
					allPages.push( page );
				}
				console.debug("There are "+allPages.length+" pages with "+allSections.length+" sections.");
				
				// 2nd remove all pages
				console.debug("Removing "+allPages.length+" pages.");
				for(var i=0; i<allPages.length; ++i) {
					this.remove( allPages[i] );
				}
				
				// 3rd build new with all sections
				var pos = 0;
				page = new ScriptPage(w, h);
				this.add(page);
				console.debug("adding new page");
				for(var i=0; i<allSections.length; ++i) {
					var sec = allSections[i].clone();
					if (pos>3) {
						pos=0;
						page = new ScriptPage(page.width(), page.height());
						this.add(page);
						console.debug("adding new page");
					}
					if (pos==0) {
						sec.setX(0);
						sec.setY(0);
					} else if (pos==1) {
						sec.setX(sec.width());
						sec.setY(0);
					} else if (pos==2) {
						sec.setX(0);
						sec.setY(sec.height());
					} else if (pos==3) {
						sec.setX(sec.width());
						sec.setY(sec.height());
					}
					page.add(sec);
					console.debug("adding section at position "+pos);
					pos++;
				}
			}
		}
		
	]]></script>
	
	<script><![CDATA[
		// do the magic
		
		var page = this.getPage(0);
		var sec2Clone = page.getSectionById("label");
		page.remove(sec2Clone);
		
		var repeat = this.dataRecord().getValue("paketanzahl");
		var pos = 0;
		
		var oAnzahl = this.option("anzahl");
		if (oAnzahl!==null && oAnzahl.value()!==undefined && oAnzahl.value()>0) repeat = oAnzahl.value().toInt();
		
		var oStart = this.option("start");
		if (oStart!==null && oStart.value()!==undefined) pos = this.option("start").value().toInt();
		
		console.log("Wiederhole Etikett " + repeat + " mal.");
		
		for(var i=0; i<repeat; ++i) {
			var sec = sec2Clone.clone();
			
			if (pos>3) {
				pos=0;
				page = new ScriptPage(page.width(), page.height());
				this.add(page);
			}
			
			if (pos==0) {
				sec.setX(0);
				sec.setY(0);
			} else if (pos==1) {
				sec.setX(sec.width());
				sec.setY(0);
			} else if (pos==2) {
				sec.setX(0);
				sec.setY(sec.height());
			} else if (pos==3) {
				sec.setX(sec.width());
				sec.setY(sec.height());
			}
			page.add(sec);
			
			pos++;
		}
		
	]]></script>
</form>
