<script src="<?php echo $rootdir ?>/modules/contratti/js/contratti_helper.js"></script>

<form action="" method="post" role="form">
	<input type="hidden" name="backto" value="record-edit">
	<input type="hidden" name="op" value="update">
	<input type="hidden" name="id_record" value="<?php echo $id_record ?>">

	
	<?php	
		$rs_ana = $dbo->fetchArray("SELECT * FROM an_anagrafiche WHERE idanagrafica=\"".$records[0]['idanagrafica']."\"");
		// ricerca ordini di servizio associati a questo canone
// ricerco eventuali canoni in stato 	
		$contratti_attivi=cerca_contratti_documenti($records[0]['id']);
		if ( $contratti_attivi > 0 ) echo "<div class='alert alert-warning'><i class='fa fa-warning'></i> Impossibile rimozione o modifica Tipologia del seguente Canone.  Vi sono ".$contratti_attivi." Ordini di Servizio associati</a></div>\n";					
	?>
	
	
	<div class="panel panel-<?php echo $_SESSION['profilo_pannello'] ?>">
		<div class="clearfix"></div>
		</br>	
		<div class="col-md-4">				
			<div class="pull-left">														
				<button type="submit" class="btn-app btn-success"><i class="fa fa-check"></i> Salva </button>
				<?php
					if(  $records[0]['id_documento'] == 0  ){
				?>
					<a class="btn btn-app btn-primary" data-href="<?php echo $rootdir ?>/modules/contratti/add_figlio.php?iddocumento=<?php echo $id_record ?>&dir=<?php echo $dir ?>&numerocon=<?php echo $records[0]['numero'] ?>" data-toggle="modal" data-title="Genera documento" data-target="#bs-popup"><i class="fa fa-copy"></i> Genera documento</a>								
				<?php } ?>
				
				<?php
					if(  $records[0]['id_documento'] != 0  ){
				?>
					<a  class="btn btn-app btn-info" target="_blank" href="<?php echo $rootdir ?>/editor.php?id_module=14&id_record=<?php echo $records[0]['id_documento'] ?>"><i class="fa fa-line-chart"></i>Documento Generato</a>	
				<?php } ?>

				<?php
					if( $records[0]['rinnovabile'] ){
					echo "<button type=\"button\" class=\"btn btn-app btn-primary\" onclick=\"if( confirm('Rinnovare questo contratto?') ){ location.href='".$rootdir."/editor.php?op=renew&id_module=".$id_module."&id_record=".$id_record."'; }\"><i class=\"fa fa-refresh\"></i> Rinnova...</button>\n";
					}
				?>					
			</div>
		</div>
		<div class="col-md-4">				
			<div class="pull-right">								
				<div class="pull-left showcase sweet"> 		
				<?php
				if( ( $records[0]['id_documento'] == 0 ) && ( $contratti_attivi == "0" )  ){
				?>
				<button type="button" class="btn-app btn-danger" aria-label="Show SweetAlert2 success message" ><i class="fa fa-trash-o"></i> Elimina</span></button>		
				<?php } ?>	
				</div>				
			</div>	
		</div>
		
		<div class="col-md-4">				
			<a  class="btn btn-app btn-info" target="_blank" href="<?php echo $rootdir ?>/editor.php?id_module=2&id_record=<?php echo $records[0]['idanagrafica'] ?>"><i class="fa fa-user"></i>Anagrafica</a>	
			<a class="btn btn-app btn-info" href="<?php echo $rootdir ?>/templates/pdfgen.php?ptype=contratti&idcontratto=<?php echo $id_record ?>" target="_blank"><i class="fa fa-print"></i> Stampa contratto</a>	
			<a class="btn btn-app btn-info" href="<?php echo $rootdir ?>/templates/pdfgen.php?ptype=contratti_storico&idcontratto=<?php echo $id_record ?>" target="_blank"><i class="fa fa-print"></i> Consuntivo</a>				
		</div>			
		<div class="clearfix"></div>
		
	</div>
	

	
	<!-- DATI INTESTAZIONE -->
	<div class="panel panel-<?php echo $_SESSION['profilo_pannello'] ?>">
		<div class="panel-heading">
			<h3 class="panel-title">Dettagli Canone Assistenza</h3>
		</div>
		<div class="panel-body">      							
			<!-- Dati Anagrafica -->
		   <div class="row">
		   <div class="col-md-4">			
				<div class="box box-primary">											
					<div class="box-body">													
						<div class="form-group">									
							{[ "type": "text", "label": "Ragione Sociale", "class": "form-control", "value": "<?php echo $rs_ana[0]['ragione_sociale']?>", "extra": "readonly" ]}								
						</div>
						<div class="form-group">														
							{[ "type": "text", "label": "Localita", "class": "form-control", "value": "<?php echo $rs_ana[0]['citta']?>", "extra": "readonly" ]}							
						</div>
						<div class="form-group">
							{[ "type": "text", "label": "Telefono", "class": "form-control", "value": "<?php echo $rs_ana[0]['telefono']?>", "extra": "readonly" ]}		
						</div>
						<div class="form-group">
							{[ "type": "text", "label": "Email", "class": "form-control", "value": "<?php echo $rs_ana[0]['email']?>", "extra": "readonly" ]}		
						</div>							           
					</div>        
				</div>								
 			</div>	
			<div class="col-md-4">			
				<div class="box box-primary">											
					<div class="box-body">													
						<div class="form-group">									
							{[ "type": "text", "label": "Numero", "name": "numero", "required": 1, "class": "text-center", "value": "$numero$", "extra": "readonly" ]}					
						</div>
						<div class="form-group">									
							{[ "type": "text", "label": "Nome", "name": "nome", "required": 1, "class": "", "value": "$nome$", "extra": "" ]}		
						</div>						
						<div class="form-group">
							{[ "type": "select", "label": "Tipologia", "name": "id_tipocanone", "required": 1, "class": "", "values": "query=SELECT id, descrizione FROM co_contratti_tipi", "value": "$id_tipocanone$", "extra": "" ]}
						</div>
						<div class="form-group">
							{[ "type": "select", "label": "Metodo di pagamento", "name": "idpagamento", "required": 0, "class": "", "values": "query=SELECT id, descrizione FROM co_pagamenti GROUP BY descrizione ORDER BY descrizione", "value": "$idpagamento$", "extra": "" ]}
						</div>
											
					</div>        
				</div>								
 			</div>	
			<div class="col-md-2">			
				<div class="box box-primary">											
					<div class="box-body">													
						<div class="form-group">									
							{[ "type": "date", "label": "Data bozza", "maxlength": 10, "name": "data_bozza", "required": 0, "class": "text-center date-mask", "value": "$data_bozza$", "extra": "" ]}
						</div>
						<div class="form-group">														
							{[ "type": "date", "label": "Data accettazione", "maxlength": 10, "name": "data_accettazione", "required": 0, "class": "text-center date-mask", "value": "$data_accettazione$", "extra": "" ]}
						</div>
						<div class="form-group">
								{[ "type": "date", "label": "Data conclusione", "maxlength": 10, "name": "data_conclusione", "required": 0, "class": "text-center date-mask", "value": "$data_conclusione$", "extra": "" ]}
						</div>
						<div class="form-group">
							{[ "type": "date", "label": "Data rifiuto", "maxlength": 10, "name": "data_rifiuto", "required": 0, "class": "text-center date-mask", "value": "$data_rifiuto$", "extra": "" ]}
						</div>	
										           
					</div>        
				</div>								
 			</div>	
			<div class="col-md-2">			
				<div class="box box-primary">											
					<div class="box-body">													
						<div class="form-group">
							{[ "type": "select", "label": "Stato", "name": "idstato", "required": 1, "class": "", "values": "query=SELECT id, descrizione FROM co_staticontratti", "value": "$idstato$", "extra": "" ]}
						</div>						
						<div class="form-group">
							{[ "type": "checkbox", "label": "Rinnovabile", "name": "rinnovabile", "required": 0, "class": "", "value": "$rinnovabile$", "extra": "" ]}
						</div>
						<div class="form-group">														
							{[ "type": "text", "label": "Validità Offerta", "name": "validita", "required": 0, "class": "inputmask-integer", "value": "$validita$", "extra": "", "icon-after": "giorni" ]}	
						</div>
						<div class="form-group">
							{[ "type": "text", "label": "Preavviso per rinnovo", "name": "giorni_preavviso_rinnovo", "required": 0, "class": "inputmask-integer", "value": "$giorni_preavviso_rinnovo$", "extra": "", "icon-after": "giorni" ]}				
						</div>					           
					</div>        
				</div>								
 			</div>
			</div>	
		</div>	
		<div class="panel-body">
			<div class="row">
				<div class="col-md-4">			
					<div class="box box-primary">											
						<div class="box-body">													
							<div class="form-group">
								{[ "type": "textarea", "label": "Esclusioni", "name": "esclusioni", "required": 0, "class": "autogrow", "value": "$esclusioni$", "extra": "rows='4'" ]}
							</div>	
						</div>        
					</div>								
				</div>
				<div class="col-md-4">			
					<div class="box box-primary">											
						<div class="box-body">													
							<div class="form-group">
								{[ "type": "textarea", "label": "Descrizione", "name": "descrizione", "required": 0, "class": "autogrow", "value": "$descrizione$", "extra": "rows='4'" ]}
							</div>	
						</div>        
					</div>								
				</div>			
				<div class="col-md-4">
					<div class="box box-primary">											
						<div class="box-body">
							<div class="form-group">
								{[ "type": "select", "multiple": "1", "label": "Installazioni", "name": "matricolaimpianto[]", "required": 0, "class": "", "values": "query=SELECT idanagrafica, matricola AS id, nome AS descrizione FROM my_impianti WHERE idanagrafica='$idanagrafica$' ORDER BY descrizione", "value": "$matricoleimpianti$", "extra": "" ]}
							</div>						
						</div>        
					</div>		
				</div>						
			</div>			
		</div>
	
	</div>

	<!-- Creazione Pannelli -->
	<ul class="nav nav-pills nav-justified" role="tablist">
		<li role="presentation" class="active"><a href="#residuo" aria-controls="residuo" role="tab" data-toggle="tab">Storico</a></li>		
		<li role="presentation"><a href="#allegati" aria-controls="allegati" role="tab" data-toggle="tab">Allegati</a></li>
		<li role="presentation"><a href="#personalizza" aria-controls="personalizza" role="tab" data-toggle="tab">Moduli personalizzati</a></li>	
    </ul>

	<div class="tab-content">
		<div role="tabpanel" class="tab-pane active" id="residuo">	
			<div class="panel panel-<?php echo $_SESSION['profilo_pannello'] ?>">					
				<div class="panel-body">											
					<?php
						include_once($docroot."/modules/contratti/pannello_canoni.php");						
					?>															
				</div>
			</div>
		</div>
	
	
		<div role="tabpanel" class="tab-pane" id="allegati">	
			<div class="panel panel-<?php echo $_SESSION['profilo_pannello'] ?>">					
				<div class="panel-body">											
					<?php
						echo filelist_and_upload( $module_name, $id_record );					
					?>															
				</div>
			</div>
		</div>
		
		<!-- Moduli personalizzati -->
		<div role="tabpanel" class="tab-pane" id="personalizza">
			<div class="panel panel-<?php echo $_SESSION['profilo_pannello'] ?>">
				
				<div class="panel-body">
					
					<?php
	
						/* necesario per funzione get_componenti */
						include($docroot."/modules/my_impianti/modutil.php");
						echo "<div class='row'>\n";
						echo "	<div class='col-md-4'>\n";
						echo "		<div class='form-group'>\n";
						echo "		<label for='componente_filename'>Seleziona un componente:</label>\n";
						echo "		<select class=\"form-control\" id=\"componente_filename\" name=\"componente_filename\" onchange=\"$.post( '".$rootdir."/modules/my_impianti/actions.php', {op: 'load_componente', idarticolo: '".$id_record."', filename: $(this).find('option:selected').val() }, function(response){ $('#info_componente').html( response ); } );\">\n";
						echo "			<option value=\"0\">- Collega ad un componente -</option>";
						$cmp = get_componenti( $docroot."/files/my_impianti/", '' );
						if( sizeof($cmp)>0 ){	
							for( $c=0; $c<sizeof($cmp); $c++ ){
								( $records[0]['componente_filename'] == $cmp[$c][0] ) ? $attr='selected="selected"' : $attr='';
								echo "<option value=\"".$cmp[$c][0]."\" $attr>".$cmp[$c][1]."</option>\n";
							}
						}	
						echo "		</select>\n";
						echo "		</div>\n";
						echo "	</div>\n";
						echo "</div>\n";
						echo "<div id='info_componente'>\n";
						genera_form_componente( $records[0]['componente_filename'], $records[0]['contenuto'] );	
						echo "</div>\n";
					?>
					<div class="pull-right">
						<button type="submit" class="btn btn-app btn-success"><i class="fa fa-check"></i> Salva </button>			
					</div>	
					</div>
						
				</div>
		</div>		
	
	</div>
</form>


<?php
if( $records[0]['idcontratto_prev'] != '0' ){
?>
	<!-- RIGHE -->
	<div class="panel panel-<?php echo $_SESSION['profilo_pannello'] ?>">
		<div class="panel-heading">
			<h3 class="panel-title">Rinnovi precedenti</h3>
		</div>

		<div class="panel-body">
			<div class="row">
				<div class="col-md-12">
					<?php
					$idcontratto_prev = $records[0]['idcontratto_prev'];

					echo "<table class='table table-hover table-condensed table-bordered table-striped'>\n";
					echo "<tr><th>Descrizione</th><th width='100'>Totale</th><th width='150'>Data inizio</th><th width='150'>Data conclusione</th></tr>\n";

					while( $idcontratto_prev != 0 ){
						$rs = $dbo->fetchArray("SELECT nome, numero, data_accettazione, data_conclusione, budget, idcontratto_prev FROM co_contratti WHERE id=\"".$idcontratto_prev."\"");

						echo "	<tr>\n";
						echo "		<td><a href='".$rootdir."/editor.php?id_module=".$id_module."&id_record=".$idcontratto_prev."' target='_blank' title='Apri in una nuova finestra'>Contratto n<sup>o</sup> ".$rs[0]['numero']."<br><small class='text-muted'>".$rs[0]['nome']."</small></a></td>\n";
						echo "		<td align='right'>".number_format( $rs[0]['budget'], 2, ",", "." )." &euro;</td>\n";
						echo "		<td align='center'>".date("d/m/Y", strtotime($rs[0]['data_accettazione']))."</td>\n";
						echo "		<td align='center'>".date("d/m/Y", strtotime($rs[0]['data_conclusione']))."</td>\n";

						echo "	</tr>\n";

						$idcontratto_prev = $rs[0]['idcontratto_prev'];
					}

					echo "</table>\n";
					?>
				</div>
			</div>
		</div>
	</div>
<?php
}
?>


<form action="" method="post" role="form" id="form-delete">
	<input type="hidden" name="backto" value="record-list">
	<input type="hidden" name="op" value="delete">	
</form>



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

	$('.showcase.sweet button').on('click', () => {
		swal({
		  title: 'Sei sicuro?',
		  text: "Eliminare questo elemento!",
		  type: 'warning',
		  showCancelButton: true,
		  confirmButtonColor: '#3085d6',
		  cancelButtonColor: '#d33',
		  confirmButtonText: 'Conferma!'
		}).then((result) => {
		  if (result.value) {			
			{ $('#form-delete').submit(); }
		  }
		})
	})	
	
	
	
</script>
