<?php
	include_once(__DIR__."/../../core.php");
	$module_name = "Magazzini";
	
	$idmagazzino = save( $_GET['idmagazzino'] );
	
	
	//Seleziona articolo
	$query = "SELECT * FROM mg_articoli WHERE attivo=1 ORDER BY id_categoria ASC, id_sottocategoria ASC";
	$rs = $dbo->fetchArray( $query );
	$n = sizeof($rs);


	/*
		Form di inserimento riga documento
	*/
	echo "<form id=\"link_form\" action=\"".$rootdir."/editor.php?id_module=".$modules_info['Magazzini']['id']."&id_record=".$idmagazzino."\" method=\"post\">\n";
	echo "	<input type='hidden' name='op' value='addrow'>\n";
	echo "	<input type='hidden' name='backto' value='record-edit'>\n";
	echo "	<input type='hidden' name='id_record' value='$idmagazzino'>\n";
	
	echo "	<div class='form'>\n";


	//Seleziona articolo
	echo "		<div class='col-md-8'>\n";
	echo "			<div class='form-group'>\n";
	echo "				<label>Articolo</label>\n";
	
	
	if( $n==0 ){
		echo "				"._("Non ci sono articoli")."...\n";
		echo "				<input type=\"hidden\" id=\"idarticolo\" value=\"0\">\n";
	}
	
	else{
			
			
		//Elenco articoli raggruppati per gruppi e sottogruppi
		echo "				<select id=\"idarticolo\" name=\"idarticolo\" class=\"superselect\" onchange=\"$('#qta').keyup(); $.get( '".$rootdir."/ajax_autocomplete.php?module=Articoli&op=getlotti&idarticolo='+$(this).find('option:selected').val(), function(data){ $('select[name=lotto]').html(data) }); $('#descrizione_art').val( $(this).find('option:selected').attr('descrizione') ); $other_js\">\n";
		echo "					<option value=\"0\">- Seleziona un articolo -</option>\n";
		for( $i=0; $i<$n; $i++ ){
			if( $rs[$i-1]['categoria'] != $rs[$i]['categoria'] ){
				echo "					<optgroup label='".$rs[$i]['categoria']."'>\n";
			}
			
			if( $rs[$i-1]['subcategoria'] != $rs[$i]['subcategoria'] ){
				echo "					<optgroup label='&nbsp;&nbsp;".$rs[$i]['subcategoria']."'>\n";
			}


			echo "						<option value=\"".$rs[$i]['id']."\" qta_magazzino='".floatval($rs[$i]['qta'])."' prezzo=\"".floatval($rs[$i]['prezzo_vendita'])."\" guadagno=\"".($rs[$i]['prezzo_vendita']/100*$prc_guadagno)."\" idiva_vendita=\"".intval($rs[$i]['idiva_vendita'])."\" descrizione=\"".$rs[$i]['descrizione']."\">".$rs[$i]['codice']." - ".$rs[$i]['descrizione']." (".floatval($rs[$i]['qta'])." ".$rs[$i]['um'].")</option>\n";

			if( $rs[$i]['categoria'] != $rs[$i+1]['categoria'] ){
				echo "					</optgroup>\n";
			}
			
			if( $rs[$i]['subcategoria'] != $rs[$i+1]['subcategoria'] ){
				echo "					</optgroup>\n";
			}
		}
	}
	
	echo "				</select><br><br>\n";
	echo "			</div>\n";
	echo "		</div>\n";
	
	
	
	//Quantità
	echo "		<div class='col-md-4'>\n";
	echo "			<div class='form-group'>\n";
	echo "				<label>Q.t&agrave; su questo magazzino</label>\n";
	echo "				<div class='input-group'>\n";
	echo "					<input type='text' class='form-control inputmask-decimal' id='qta' maxlength='8' name='qta' value=\"1\">\n";
	echo "				</div>\n";
	echo "			</div>\n";
	echo "		</div>\n";

	
	?>
	
	
	<script type="text/javascript">
		function addarticolo(){
			if(($('#qta').val()!='') && ($('#idarticolo option:selected').val()!=0 )){
				$("#link_form").submit();
			}
		}
		
		$(document).ready( function(){			
			start_superselect();
			start_inputmask();
		});
	</script>

	<br/><br/><br/>
	<div class="text-center">
		<a onclick="addarticolo();" class="btn btn-primary pull-right"><i class="fa fa-plus"></i> Aggiungi</a>
	</div>
	<br/>
	<div class="clearfix"></div>

</div>
</form>
<br/>
