<?php
	include_once(__DIR__."/../../core.php");
	unset($_SESSION["superselect"]["idanagrafica"]);
	$_SESSION["superselect"]["idanagrafica"] = $records[0]["idanagrafica"];


	// dati anagrafica
	$rs_ana = $dbo->fetchArray("SELECT *  FROM an_anagrafiche  WHERE idanagrafica=\"".$records[0]['idanagrafica']."\"");	
	$idanagrafica=$records[0]['idanagrafica'];

?><script type="text/javascript" src="<?php echo $rootdir ?>/modules/agenda/js/interventi_helperjs.php"></script>

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

	
	<div class="panel panel-primary">
		<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>										
			</div>
		</div>
		<div class="col-md-4">				
			<div class="pull-right">								
				<div class="pull-left showcase sweet"> 		
				<button type="button" class="btn-app btn-danger"  ><i class="fa fa-trash-o"></i> Elimina</span></button>	
				</div>				
			</div>	
		</div>
		
		<div class="col-md-4">
			<div class="pull-right">		
			</div>	
		</div>			
		<div class="clearfix"></div>
		
	</div>
	
	<div class="clearfix"></div>
	
	
	<!-- Creazione Pannelli -->
	<ul class="nav nav-pills nav-justified" role="tablist">
		<li role="presentation" class="active"><a href="#appuntamento" aria-controls="appuntamento" role="tab" data-toggle="tab">Appuntamento n° <?php echo $records[0]["idintervento"] ?></a></li>
		<li role="presentation"><a href="#personalizza" aria-controls="personalizza" role="tab" data-toggle="tab">Moduli personalizzati</a></li>	
		<li role="presentation"><a href="#allegati" aria-controls="allegati" role="tab" data-toggle="tab">Allegati</a></li>
    </ul>
	
	<div class="tab-content">
		<div role="tabpanel" class="tab-pane active" id="appuntamento">
			<div class="panel panel-primary">
				<div class="panel-body">
					<div class="col-md-4">			
						<div class="box box-primary">
							<div class="box-header with-border">
								<h3 class="box-title">Dettaglio Anagrafica</h3>
							</div>				           
							<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-header with-border">
								<h3 class="box-title">Tipologia</h3>
							</div>				           
							<div class="box-body">																						                             						
								<div class="form-group">														
									{[ "type": "select", "label": "<?php echo _("Sede"); ?>", "name": "idsede", "class": "", "values": "query=SELECT 0 AS id, 'Sede legale' AS descrizione UNION SELECT id, CONCAT_WS( ' - ', nomesede, citta ) AS descrizione FROM an_sedi WHERE idanagrafica='$idanagrafica$'", "value": "$idsede$", "extra": "" ]}
								</div>						
								<div class="form-group">		
									{[ "type": "text", "label": "<?php echo _("Data richiesta"); ?>", "name": "data_richiesta", "required": 1, "class": "datepicker text-center", "value": "<?php echo date('d/m/Y', strtotime($records[0]['data_richiesta'])) ?>", "extra": "" ]}
								</div>
								<div class="form-group">		
									{[ "type": "select", "label": "<?php echo _("Tipologia"); ?>", "name": "idtipoagenda", "required": 1, "class": "", "values": "query=SELECT idtipoagenda AS id, descrizione FROM agenda_tipi", "value": "$idtipoagenda$", "extra": "" ]}
								</div>
								<div class="form-group">		
									{[ "type": "select", "label": "<?php echo _("Stato"); ?>", "name": "idstatoagenda", "required": 1, "class": "", "values": "query=SELECT idstatoagenda AS id, descrizione, colore AS _bgcolor_ FROM agenda_stati", "value": "$idstatoagenda$", "extra": "" ]}
								</div>							           
							</div>        
						</div>
					</div>	
					<div class="col-md-4">			
						<div class="box box-primary">
							<div class="box-header with-border">
								<h3 class="box-title">Associazione documento</h3>
							</div>				           
							<div class="box-body">																						                             						
								<div class="form-group">														
									{[ "type": "select", "label": "<?php echo _("Tipologia documento"); ?>", "name": "idtipodoc", "class": "", "values": "query=SELECT id,descrizione FROM co_tipidocumento", "value": "$ass_idtipodocumento$", "extra": "" ]}
								</div>  

								<div class="form-group">														
									<?php
										if( $records[0]['ass_idtipodocumento'] == '' ){
										?>
											{[ "type": "select", "label": "<?php echo _("Numero"); ?>", "name": "iddocumento", "required": 0, "class": "", "values": "", "value": "Seleziona prima una tipologia di documento...", "extra": "", "ajax-source": "documenti" ]}
										<?php
										}
										else{
										?>
											{[ "type": "select", "label": "<?php echo _("Numero"); ?>", "name": "iddocumento", "required": 0, "class": "", "values": "query=SELECT id as id , numero_documento AS descrizione FROM co_documenti ", "value": "<?php echo $records[0]['ass_iddocumento'] ?>", "extra": "<?php if( in_array('iddocumento', $disabled_fields) ){ echo "disabled='true'"; } ?>",  "ajax-source": "documenti" ]}
										<?php
										}
										?>
								</div>  
								<div class="form-group">														
									<?php
										if( $records[0]['ass_data_documento'] != '0000-00-00 00:00:00' ){
									?>
										{[ "type": "text", "label": "<?php echo _("Creato il"); ?>", "name": "ass_data_documento", "required": 1, "class": "datepicker text-center", "value": "<?php echo date('d/m/Y', strtotime($records[0]['ass_data_documento'])) ?>", "extra": "readonly" ]}						
									<?php
									}
									?>
								</div>  
							</div>
						</div>	
					</div>
				</div>
			</div>
		
		
			<div class="panel panel-primary">
				<div class="panel-heading">
					<h3 class="panel-title"> Attività</h3>
				</div>
				<div class="panel-body">
					<div class="row">
						<div class="col-md-6">
							{[ "type": "textarea", "label": "<?php echo _("Richiesta"); ?>", "name": "richiesta", "class": "autosize", "value": "$richiesta$", "extra": "rows='10'" ]}
						</div>
						<div class="col-md-6">
							{[ "type": "textarea", "label": "<?php echo _("Appunti"); ?>", "name": "descrizione", "class": "autosize", "value": "$descrizione$", "extra": "rows='10'" ]}
						</div>
					</div>
				</div>
			</div>


			<div class="panel panel-primary">
				<div class="panel-heading">
					<h3 class="panel-title"> Assegnazione Operatore </h3>
				</div>

				<div class="panel-body">
					<div class="row">
						<div class="col-md-12" id="tecnici">
							<script>$('#tecnici').load( '<?php echo $rootdir ?>/modules/agenda/ajax_tecnici.php?idintervento=<?php echo $id_record ?>' );</script>
						</div>
					</div>
					<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 role="tabpanel" class="tab-pane" id="personalizza">	
			<div class="panel panel-primary">				
				<div class="panel-heading">
					<h3 class="panel-title"> Componente personalizzato</h3>
				</div>
				<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 role="tabpanel" class="tab-pane " id="allegati">	
			<div class="panel panel-primary">					
				<div class="panel-body">											
					<?php
						echo filelist_and_upload( $module_name, $id_record );					
					?>															
				</div>
			</div>
		</div>
		
		
	</div>
	


	<?php include ($docroot . "/lib/campi_personalizzati.php");	?>
	
	
	
	
		
																
		
	
	
	
</form>


<script>
	$('#idanagrafica').change( function(){
		session_set( 'superselect,idanagrafica', $(this).val(), 0 );
	});
	
	
	
	var ref = "<?php echo $_GET['ref'] ?>";
	var id_module = "<?php echo $_GET['id_module'] ?>";

	$('#idtipodoc').change( function(){
		session_set( 'superselect,idtipodoc', $(this).val(), 0 );
		$("#iddocumento").prop("disabled", false);
		$("#iddocumento").val(null).trigger("change");
	});

	$('#iddocumento').change( function(){
		session_set( 'superselect,iddocumento', $(this).val(), 0 );	
	});
	
	
	
	$('.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>

<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>
