<?php
include_once ("../../core.php");

	$module_name="Fatture di vendita";
	$cartella_salvataggio="interventi";
if (isset($_GET['anteprima'])) {
	$idintervento = save($_GET['idintervento']);
	$iddocumento = save($_GET['idintervento']);

	// Lettura dati intervento
	$query = "SELECT idintervento, idanagrafica, (SELECT MIN(DATE_FORMAT(`orario_inizio`, '%d/%m/%Y')) FROM in_interventi_tecnici WHERE in_interventi_tecnici.`idintervento`=in_interventi.idintervento ) AS data_inizio, (SELECT MAX(DATE_FORMAT(`orario_inizio`, '%d/%m/%Y')) FROM in_interventi_tecnici WHERE in_interventi_tecnici.`idintervento`=in_interventi.idintervento )  AS data_fine FROM in_interventi WHERE in_interventi.idintervento=\"$idintervento\" ";
	$rs = $dbo->fetchArray($query);
	$n = sizeof($rs);
	if ($n == 0) {
		echo "Intervento inesistente!";
		exit();
	}

	$idanagrafica = $rs[0]['idanagrafica'];
	$idcliente = $rs[0]['idanagrafica'];
	$data_intervento = $rs[0]['data_inizio'];

	$rapportino_nome = sanitizeFilename("Rapportino" . $idintervento . ".pdf");
	$rapportino_path = $docroot . '/files/interventi/' . $rapportino_nome;

	$dst_dir = $docroot . "/files/" . strtolower($cartella_salvataggio) . "/";

	// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// creazione file da allegare

	$id_record = $idintervento;
	$id_module = save($_GET['id_module']);
	$ptype = "interventi";

	// Se la destinazione non esiste la creo
	if (! is_dir($dst_dir)) {
		if (! mkdir($dst_dir)) {
			$dir_ok = false;
			echo "<p align='center'><br/><br/>Non hai i permessi per creare directory e files in: $dst_dir </p>\n";
			exit();
		}
	}

	$n_pages = 1;
	$orientation = 'P';
	$body_table_params = "style='width:210mm;'";
	$font_size = "10pt";

	// questa chiamata dentro pdfgen.".$ptype.".php" non funziona, devo richiamare lo script da qua!
	include ($docroot . "/templates/pdfgen_variables.php");

	// Decido se usare la stampa personalizzata (se esiste) oppure quella standard
	if (@file_exists($docroot . "/templates/" . $ptype . "/custom/pdfgen." . $ptype . ".php")) {
		include ($docroot . "/templates/" . $ptype . "/custom/pdfgen." . $ptype . ".php");
	}

	else {
		include ($docroot . "/templates/" . $ptype . "/pdfgen." . $ptype . ".php");
	}

	// questa chiamata dentro pdfgen.".$ptype.".php" non funziona, devo richiamare lo script da qua!
	include ($docroot . "/templates/pdfgen_variables.php");

	// Completamento e output del report
	$report = str_replace('$body$', $body, $report);
	$report = str_replace('$footer$', $footer, $report);
	$report = str_replace('$body_table_params$', $body_table_params, $report);
	$report = str_replace('$font_size$', $font_size, $report);
	$report = str_replace('$docroot$', $docroot, $report);

	// html
	$formato = get_var('Formato report');
	$html = ($formato == 'html') ? true : false;

	try {
		$html2pdf = new HTML2PDF($orientation, 'A4', 'it', true, 'UTF-8');
		$html2pdf->writeHTML($report, $html);
		$html2pdf->Output($rapportino_path, 'F');
	}
	catch (HTML2PDF_exception $e) {
		echo $e;
		exit();
	}
	// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	echo '
	<button type="button" class="btn btn-success btn-block btn-lg" id="firma" onclick="$(\'.canvas\').removeClass(\'hide\'); $(this).addClass(\'hide\'); $(\'#pdf\').addClass(\'hide\');  $(\'#canvas\').signature();">
		<i class="fa fa-pencil"></i> ' . _("Firma") . '
	</button>
	<div class="clearfix" ></div><br><br>

		<div class="hide" id="pdf">
			<object data="' . $rootdir . '/files/interventi/' . $rapportino_nome . '#view=fitH&scrollbar=0&toolbar=0&navpanes=0" id ="rapportino_pdf"  type="application/pdf" width="100%">
				alt : <a href="' . $rootdir . '/files/interventi/' . $rapportino_nome . '">' . $rapportino_nome . '</a>
				<span>plugin PDF mancante.</span>
			</object>
		</div>';
}

?>
	<script src="<?php echo $js ?>/jquery.ui.touch-punch.min.js"></script>
	<script src="<?php echo $js ?>/jquery.signature.min.js"></script>

	<div id="canvas" class="canvas" style="position:relative; width:680px; height:202px; border:1px solid #888; margin:auto;"></div>
	<br>

	<form class="canvas" action="<?php echo $rootdir ?>/editor.php?id_module=<?php echo $_GET['id_module']; ?>&id_record=<?php echo $_GET['id_record']; ?>" method="post" id="form-firma">

		<div class="row">
			<div class="col-md-12">
				{[ "type": "text", "label": "<?php echo _("Nome e cognome"); ?>", "name": "firma_nome", "required": 1 ]}
			</div>
		</div>


		<input type="hidden" name="op" value="firma">
		<input type="hidden" name="backto" value="record-edit">
		<input type="hidden" name="id_record" value="<?php echo $_GET['idintervento']; ?>">
		<input type="hidden" name="firma_base64" id="firma_base64" value="">

		<div class="btn-group pull-right">
			<button type="button" class="btn btn-danger btn-lg" onclick="$('#canvas').signature('clear');"><i class="fa fa-eraser"></i> <?php echo _("Cancella firma"); ?></button>
			<button type="button" class="btn btn-success btn-lg" onclick="if ($('#firma_nome').val()!='') { signatureSave(); $('#form-firma').submit(); } else { alert ('Inserisci nome e cognome');   $( '#firma_nome' ).focus();  };"><i class="fa fa-check"></i> <?php echo _("Salva firma"); ?></button>
		</div>

	</form>
	<div class="clearfix"></div>

	<script type="text/javascript" >
		$(document).ready( function(){
			$('#canvas').signature();

			$('button').removeClass('hide');
			$('#pdf').removeClass('hide');
			$('.canvas').addClass('hide');
			$('#firma').removeClass('hide');
			$('#rapportino_pdf').css('height', ($(window).height()-200));
		});

		function signatureSave(){
			image_data = document.querySelector('#canvas > canvas').toDataURL("image/png");
			$('#firma_base64').val( image_data );
		}
	</script>
