<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">

		<!--
			Try to exclude the application CSS here, in order to
			avoid conflicts with the style of the template.
			<link rel="stylesheet" href="../assets/css/main.css">
		-->

		<!-- Insert this line above script imports  -->
		<script type="text/javascript">
			if (typeof module === 'object') {
				window.module = module;
				module = undefined;
			}
		</script>

		<script src="../assets/js/jquery.min.js"></script>

		<!-- Insert this line after script imports -->
		<script type="text/javascript">
			if (window.module) {
				module = window.module;
			}
		</script>

		<!-- Load system -->
		<script type="text/javascript">
			const ipc = require('electron').ipcRenderer;

			const UIConfig = require('../assets/js/system/config.js');
			const UIPrintFormat = require('../assets/js/print/format.js');

			const config = new UIConfig();
		</script>
	</head>

	<body>
		<div id="template"><!-- HTML goes here --></div>
	</body>

	<script type="text/javascript">

		ipc.on('receive-details', (event, arg) => {
			// wait for configuration loading
			config.onLoad(() => {
				// create a page format
				let format = new UIPrintFormat(config.get('pageformat', 'A4'), config.get('pagesize', {}));

				// include @page CSS declaration
				jQuery('head').append('<style> @page { ' + format.getPageSizeCSS() + ' } </style>');

				// get custom CSS
				let style = config.get('printstyle', '').trim();

				if (style) {
					jQuery('head').append('<style>\n' + style + '\n</style>');
				}

				// load template
				let template = '';
				let layout   = config.get('printlayout', 'plain');

				// check whether the print template exists
				if (arg.template.hasOwnProperty(layout)) {
					// use the specified template for printing
					template = arg.template[layout];
				}

				// set template to print
				jQuery('#template').html(template);
				// inform subscribers that the priner is ready
				ipc.send('printer-ready', {id: arg.id});
			});
		});

	</script>
</html>