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

	$qta_altrimagazzini = query_secca2("","sum(qta) as tot_mag","mg_articoli_magazzini","where idarticolo ='".$id_record."'","tot_mag");
	
?><form action="" method="post" role="form" enctype="multipart/form-data">
	<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 ?>">

	
	
	
	<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>										
				<button type="button" class="btn-app btn-primary" data-href="<?php echo $rootdir ?>/modules/articoli/duplica_articolo.php?idordine=<?php echo $id_record ?>&dir=<?php echo $dir ?>" data-toggle="modal" data-title="Duplica articolo" data-target="#bs-popup"><i class="fa fa-files-o"></i> Duplica </a>
			
			</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>			
		<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="#dettaglio" aria-controls="dettaglio" role="tab" data-toggle="tab">Principale</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="#magazzini" aria-controls="magazzini" role="tab" data-toggle="tab">Magazzini</a></li>
	</ul>
	
	
	<div class="tab-content">
		<!-- Dettaglio -->
		<div role="tabpanel" class="tab-pane active" id="dettaglio">
			<div class="panel panel-<?php echo $_SESSION['profilo_pannello'] ?>">
				<div class="panel-body">
					<div class="row">
						<div class="col-md-3">
							<?php
							( $records[0]['immagine01'] == '' ) ? $immagine01='' : $immagine01=$rootdir."/files/articoli/".$records[0]['immagine01'];
							?>
							{[ "type": "image", "label": "Immagine", "name": "immagine01", "required": 0, "class": "img-thumbnail", "value": "<?php echo $immagine01 ?>", "extra": "" ]}
						</div>
						
						<div class="col-md-4">
							{[ "type": "text", "label": "Codice", "name": "codice", "required": 1, "class": "", "value": "$codice$", "extra": "" ]}
							<br>							
							{[ "type": "text", "label": "Barcode", "name": "pro_barcode", "required": 0, "class": "", "value": "$pro_barcode$", "extra": "" ]}														
						</div>				
						<div class="col-md-5">
							{[ "type": "select", "label": "<?php echo _("Categoria"); ?>", "name": "categoria", "required": 0, "class": "", "value": "$id_categoria$", "ajax-source": "categorie" ]}
							<br>
							{[ "type": "select", "label": "<?php echo _("Subcategoria"); ?>", "name": "subcategoria", "required": 0, "class": "", "value": "$id_sottocategoria$", "ajax-source": "sottocategorie" ]}									
						</div>
					</div>
					<div class="row">
						<div class="col-md-3">
							{[ "type": "select", "label": "Fornitore", "name": "idfornitore", "required": 0, "class": "", "values": "query=SELECT an_anagrafiche.idanagrafica AS id, ragione_sociale AS descrizione FROM an_anagrafiche INNER JOIN (an_tipianagrafiche_anagrafiche INNER JOIN an_tipianagrafiche ON an_tipianagrafiche_anagrafiche.idtipoanagrafica=an_tipianagrafiche.idtipoanagrafica) ON an_anagrafiche.idanagrafica=an_tipianagrafiche_anagrafiche.idanagrafica WHERE descrizione='Fornitore' AND deleted=0 ORDER BY ragione_sociale", "value": "$idfornitore$", "extra": "" ]}
						</div>
						<div class="col-md-3">
							{[ "type": "text", "label": "Codice fornitore", "name": "cod_fornitore", "required": 0, "class": "", "value": "$cod_fornitore$", "extra": "" ]}
						</div>	
					
						<div class="col-md-3">
							<?php 
								$des_conto=query_secca("","descrizione","co_pianodeiconti3","where id='".$records[0]['id_conto'] ."'")
							?>
							{[ "type": "text", "label": "Conto predefinito entrata",  "name": "id_conto", "required": 0, "class": "", "values": "", "value": "<?php echo $des_conto ?>", "extra": "readonly" ]}								
						</div>	
						<div class="col-md-3">
							<?php 
								$des_conto=query_secca("","descrizione","co_pianodeiconti3","where id='".$records[0]['id_conto_u'] ."'")
							?>
							{[ "type": "text", "label": "Conto predefinito uscita",  "name": "id_conto_u", "required": 0, "class": "", "values": "", "value": "<?php echo $des_conto ?>", "extra": "readonly" ]}								
						</div>				
					</div>	
					<div class="row">
						<div class="col-md-12">
							{[ "type": "textarea", "label": "Descrizione", "name": "descrizione", "required": 1, "class": "", "value": "$descrizione$", "extra": "" ]}
						</div>
					</div>

					<div class="row">
						<div class="col-md-12">
							{[ "type": "textarea", "label": "Note", "name": "note", "required": 0, "class": "", "value": "$note$", "extra": "" ]}
						</div>
					</div>			
				</div>
			</div>
			
			<!-- Creazione Pannelli -->
			<ul class="nav nav-pills nav-justified" role="tablist">
				<li role="presentation" class="active"><a href="#principale" aria-controls="principale" role="tab" data-toggle="tab">Principale</a></li>
				<li role="presentation"><a href="#scorte" aria-controls="scorte" role="tab" data-toggle="tab">Scorte & Ubicazioni</a></li>
				<li role="presentation"><a href="#listini" aria-controls="listini" role="tab" data-toggle="tab">Listini</a></li>
			</ul>
			<div class="tab-content">
				<!-- Principale -->
				<div role="tabpanel" class="tab-pane active" id="principale">
					<div class="panel panel-<?php echo $_SESSION['profilo_pannello'] ?>">							
						<div class="panel-body">		
							<div class="row">
								<div class="col-md-1">
									{[ "type": "text", "label": "Quantità", "name": "qta", "required": 1, "class": "", "value": "$qta$", "extra": "readonly='true'" ]}
								</div>	
								<div class="col-md-2">
									{[ "type": "checkbox", "label": "Modifica manualmente quantità", "name": "qta_manuale", "required": 0, "class": "", "value": 0, "extra": "", "help": "seleziona per modificare manualmente la quantità", "placeholder": "Quantità manuale" ]}
									<script type="text/javascript">
										$('#qta_manuale').click(function(){
											$("#qta").attr("readonly", !$('#qta_manuale').is(":checked"));
										});
									</script>
								</div>
								<div class="col-md-2">
									{[ "type": "checkbox", "label": "Visibile", "name": "attivo", "required": 0, "class": "", "values": "$attivo$", "value": "$attivo$", "extra": "", "help": "", "placeholder": "ATTIVO" ]}
								</div>
								<div class="col-md-3">
									{[ "type": "checkbox", "label": "Seriale obbligatorio", "name": "abilita_serial", "value": "$abilita_serial$", "help": "", "placeholder": "<?php echo tr('Abilita serial number in fase di aggiunta articolo in documento'); ?>" ]}
								</div>
								<div class="col-md-3">
									{[ "type": "checkbox", "label": "Articolo Ordine di Servizio", "name": "servizio", "value": "$servizio$", "help": "", "placeholder": "<?php echo tr('Abilita se articolo non e fisico'); ?>" ]}
								</div>
							</div>
							<div class="row">	
								<div class="col-md-1">							
								</div>
								<div class="col-md-2">
									{[ "type": "checkbox", "label": "Magazzino", "name": "ges_magazzino", "value": "$ges_magazzino$", "help": "", "placeholder": "<?php echo tr('Abilita gestione magazzino'); ?>" ]}
								</div>
								
							</div>
									
							
							<div class="row">
								<div class="col-md-6">
									<div class="panel panel-<?php echo $_SESSION['profilo_pannello'] ?>">
										<div class="panel-heading">
											<h3 class="panel-title">Vendita</h3>
										</div>
										<div class="panel-body">
											<div class="row">
												<div class="col-md-3">
													{[ "type": "text", "label": "Prezzo di vendita base", "name": "prezzo_vendita", "required": 0, "class": "inputmask-decimal", "value": "$prezzo_vendita$", "extra": "", "icon-after": "&euro;" ]}
												</div>
												<div class="col-md-6">
													{[ "type": "select", "label": "Iva di vendita", "name": "idiva_vendita", "required": 0, "class": "", "values": "query=SELECT * FROM co_iva ORDER BY descrizione ASC", "value": "$idiva_vendita$", "extra": "", "valore_predefinito": "Iva predefinita" ]}
												</div>
												<div class="col-md-3">
													{[ "type": "select", "label": "Um", "name": "idum", "required": 0, "class": "", "values": "query=SELECT id, valore AS descrizione FROM mg_unitamisura ORDER BY valore ASC", "value": "$idum$", "extra": "" ]}
												</div>										
											</div>
										</div>
									</div>
								</div>
								<div class="col-md-6">
									<div class="panel panel-<?php echo $_SESSION['profilo_pannello'] ?>">
										<div class="panel-heading">
											<h3 class="panel-title">Acquisto</h3>
										</div>
										<div class="panel-body">
											<div class="row">
												<div class="col-md-3">
													{[ "type": "text", "label": "Prezzo di acquisto", "name": "prezzo_acquisto", "required": 0, "class": "inputmask-decimal", "value": "$prezzo_acquisto$", "extra": "", "icon-after": "&euro;" ]}
												</div>	
											</div>
										</div>
									</div>
								</div>
							</div>
							<div class="row">
								<div class="col-md-3">
									<div class="panel panel-<?php echo $_SESSION['profilo_pannello'] ?>">
										<div class="panel-heading">
											<h3 class="panel-title">Giacenza complessiva</h3>
										</div>
										<div class="panel-body">
											<div class="row">
												<div class="col-md-6">
													{[ "type": "text", "label": "Quantità", "name": "qta_tot", "required": 1, "class": "", "value": "<?php echo $qta_altrimagazzini + $records[0]['qta'] ?>", "extra": "readonly='true'" ]}
												</div>	
											</div>
										</div>
									</div>
								</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>
				
				<!-- Listini -->
				<div role="tabpanel" class="tab-pane" id="scorte">
					<div class="panel panel-<?php echo $_SESSION['profilo_pannello'] ?>">			
						<div class="panel-body">
							<div class="row">						
								<div class="col-md-2">
									{[ "type": "text", "label": "Scorta.Min", "name": "threshold_qta", "required": 0, "class": "", "value": "$threshold_qta$", "extra": "" ]}
								</div>
								<div class="col-md-2">
									{[ "type": "text", "label": "Scorta.Max", "name": "pro_smax", "required": 0, "class": "", "value": "$pro_smax$", "extra": "" ]}
								</div>
								<div class="col-md-2">
									{[ "type": "text", "label": "Lotto E.Acq", "name": "pro_lotto",  "required": 0, "class": "", "value": "$pro_lotto$", "extra": ""  ]}
								</div>
								<div class="col-md-2">
									{[ "type": "text", "label": "Peso lordo", "name": "peso_lordo", "required": 0, "class": "inputmask-decimal", "value": "$peso_lordo$", "extra": "", "icon-after": "KG" ]}
								</div>
								<div class="col-md-2">
									{[ "type": "text", "label": "Volume", "name": "volume", "required": 0, "class": "inputmask-decimal", "value": "$volume$", "extra": "", "icon-after": "M<sup>3</sup>" ]}
								</div>
								<div class="col-md-1">
									{[ "type": "text", "label": "GG di garanzia", "name": "gg_garanzia", "required": 0, "class": "", "value": "$gg_garanzia$", "extra": "" ]}
								</div>	
								<div class="col-md-1">
									{[ "type": "text", "label": "Qta per Colli", "name": "qta_colli", "required": 0, "class": "", "value": "$qta_colli$", "extra": "" ]}
								</div>									
							</div>
							<div class="row">
								<div class="col-md-4">							
									{[ "type": "select", "label": "Ubicazione", "name": "id_ubicazione", "required": 0, "class": "", "values": "query=SELECT * FROM mg_ubicazione ORDER BY descrizione", "value": "$id_ubicazione$", "extra": "" ]}
								</div>
								<div class="col-md-4">							
									{[ "type": "select", "label": "Scansia", "name": "id_scansia", "required": 0, "class": "", "values": "query=SELECT * FROM mg_scansia ORDER BY descrizione", "value": "$id_scansia$", "extra": "" ]}
								</div>	
								<div class="col-md-4">
									{[ "type": "select", "label": "Reparto", "name": "id_reparto", "required": 0, "class": "", "values": "query=SELECT * FROM mg_reparti ORDER BY descrizione", "value": "$id_reparto$", "extra": "" ]}
								</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>
				
				
				
				<!-- Listini -->
				<div role="tabpanel" class="tab-pane" id="listini">
					<div class="panel panel-<?php echo $_SESSION['profilo_pannello'] ?>">
						
						<div class="panel-body">
						<?php
							$rsl = $dbo->fetchArray("SELECT * FROM mg_listini ORDER BY id ASC");		
							$rsart = $dbo->fetchArray("SELECT id, prezzo_vendita FROM mg_articoli WHERE id=$id_record");
							if( sizeof($rsl)>0 ){
								echo "<div class=\"row\">\n";
								echo "	<div class=\"col-md-12 col-lg-6\">\n";
								echo "		<table class=\"table table-striped table-condensed table-bordered\">\n";
								echo "			<tr>\n";
								echo "				<th>Listino</th>\n";
								echo "				<th>Prezzo di vendita Listino</th>\n";
								echo "				<th>Prezzo modificato</th>\n";
								echo "			</tr>\n";
							// listino base
								echo "			<tr>\n";
								echo "				<td>Base</td>\n";
								echo "				<td>".number_format( $rsart[0]['prezzo_vendita'], get_var("Cifre decimali"), ",", "." )." &euro;</td>\n";
								echo "			</tr>\n";
								for( $i=0; $i<sizeof($rsl); $i++ ){				
									echo "		<tr>\n";
									echo "			<td>".$rsl[$i]['nome']." ( ".$rsl[$i]['prc_guadagno']."% )</td>\n";
									
									echo "			<td>".number_format( $rsart[0]['prezzo_vendita'] + $rsart[0]['prezzo_vendita']/100*$rsl[$i]['prc_guadagno'], get_var("Cifre decimali"), ",", "." )." &euro;</td>\n";
									$rslist = $dbo->fetchArray("SELECT prezzo , prc_guadagno FROM mg_listini_art WHERE idlistino='".$rsl[$i]['id']."' and idarticolo='".$id_record."'");
									if( $rslist[0]['prezzo'] > '0' && $rslist[0]['prezzo'] <> ($rsart[0]['prezzo_vendita'] + $rsart[0]['prezzo_vendita']/100*$rsl[$i]['prc_guadagno'])){
										echo "			<td> ".number_format( $rslist[0]['prezzo'] , get_var("Cifre decimali"), ",", "." )." &euro; ( ".$rslist[0]['prc_guadagno']."% )</td>\n";				
									}		
									echo "		</tr>\n";
								}
								echo "		</table>\n";
								echo "	</div>\n";
								echo "</div>\n";
							}
						?>
						</div>
					</div>
				</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>		
	
		<!-- magazzini -->
		<div role="tabpanel" class="tab-pane" id="magazzini">
			<div class="panel panel-<?php echo $_SESSION['profilo_pannello'] ?>">
				<div class="panel-body">
					<?php
					// Quantità nell'magazzino
					$rsa = $dbo->fetchArray("SELECT qta, (SELECT nome FROM mg_magazzini WHERE id=idmagazzino) AS nome, (SELECT sigla FROM mg_magazzini WHERE id=idmagazzino) AS sigla FROM mg_articoli_magazzini WHERE idarticolo='".$id_record."'");		
					if( sizeof($rsa)>0 ){
						echo "<div class=\"row\">\n";
						echo "	<div class=\"col-md-12 col-lg-6\">\n";
						echo "		<table class=\"table table-striped table-condensed table-bordered\">\n";
						echo "			<tr>\n";
						echo "				<th>Nome Magazzino</th>\n";
						echo "				<th>Sigla</th>\n";
						echo "				<th>Q.tà</th>\n";
						echo "			</tr>\n";
						for( $i=0; $i<sizeof($rsa); $i++ ){			
							echo "		<tr>\n";
							echo "			<td>".$rsa[$i]['nome']."</td>\n";
							echo "			<td>".$rsa[$i]['sigla']."</td>\n";
							echo "			<td>".$rsa[$i]['qta']." ".$rs[0]['unita_misura']."</td>\n";
							echo "		</tr>\n";
						}
						echo "		</table>\n";
						echo "	</div>\n";
						echo "</div>\n";
					}
					?>
				</div>	
				<?php include "grafico_magazzini.php" ?>						
			</div>
		</div>

	</div>
	
	<?php include ($docroot . "/lib/campi_personalizzati.php");	?>
	
	<div role="tabpanel" class="tab-pane " id="allegati">	
		<div class="panel panel-<?php echo $_SESSION['profilo_pannello'] ?>">
			<?php 
				include_once ($docroot . "/modules/articoli/edit_grafico.php");	
			?>
		</div>
	</div>	


	
</form>
<script>
$('#categoria').change( function(){
	session_set( 'superselect,id_categoria', $(this).val(), 0 );
	$("#subcategoria").val(null).trigger("change");
});




$('.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>



	