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


	$module_name = 'Listini';
//	include($docroot."/core.php");
//	include($docroot."/lib/user_check.php");
//	include($docroot."/lib/permissions_check.php");
	
	$id_listino = $html->form('idlistino');
	$idriga = $html->form('idriga');
	$idrigal = $html->form('idrigal');
	
	
	//Info riga inserita
	$q = "SELECT descrizione , prezzo_vendita FROM mg_articoli WHERE id='".$idriga."'";
	$rsr = $dbo->fetchArray($q);
	$prezzo_vendita=$rsr[0]['prezzo_vendita'];
	
	$q1="SELECT prezzo , prc_guadagno FROM mg_listini_art  WHERE idlistino='".$id_listino."' and idarticolo='".$idriga."'";
	$rsprr = $dbo->fetchArray($q1);
	/*
		Form di modifica riga kit
	*/
	echo "Prezzo base :".number_format($prezzo_vendita, get_var("Cifre decimali"));
	echo "<br><label>Modifica prezzo  Listino ( Prezzo=0 alimina associazione )</label><br><br>\n";
	echo "<form id=\"link_form\" action=\"".$rootdir."/editor.php?id_module=".$modules_info[$module_name]['id']."&id_record=".$id_listino."\" method=\"post\">\n";
	
	echo "	<input type='hidden' name='op' value='editriga'>\n";
	echo "	<input type='hidden' name='idriga' value='".$idrigal."'>\n";
	// echo "	<input type='hidden' name='subtotale' value='".$subtotale."'>\n";
	// echo "	<input type='hidden' name='subtotaleper' value='".$subtotaleper."'>\n";
	
	echo "	<input type='hidden' name='backto' value='record-edit'>\n";
	echo "	<input type='hidden' name='dir' value='".$dir."'>\n";
	
	
	
	//Descrizione
	echo "	<div class='row'>\n";
	echo "		<div class='col-md-12'>\n";
	echo "			<div class='form-group'>\n";
	echo "				<label>Descrizione</label>\n";
	echo "				<textarea name=\"descrizione\" readonly=\"true\" id=\"descrizione\" class='form-control' >".$rsr[0]['descrizione']."</textarea>\n";
	echo "			</div>\n";
	echo "		</div>\n";
	echo "	</div>\n";
// onkeyup=\"ricalcola_subtotale_riga(".$i.");\" />\n";
	// Percentuale
	echo "		<div class='col-md-4'>\n";
	echo "			<div class='form-group'>\n";
	echo "				<label>% </label>\n";
	echo "				<div class='input-group'>\n";	
	echo "					<input type='text' class='form-control inputmask-decimal' id='subtot_perc' name='subtot_perc' onkeyup=\"ricalcola_subtotale_riga('0');\" value=\"".$rsprr[0]['prc_guadagno']."\"><span class='input-group-addon'>&euro;</span>\n";
		
	echo "				</div>\n";
	echo "			</div>\n";
	echo "		</div>\n";
	
	//Prezzo Venedita
	echo "		<div class='col-md-4'>\n";
	echo "			<div class='form-group'>\n";
	echo "				<label>Prezzo Vendita Finale</label>\n";
	echo "				<div class='input-group'>\n";
	echo "					<input type='text' class='form-control inputmask-decimal' id='subtot' name='subtot' onkeyup=\"ricalcola_subtotale_riga1('0');\" value=\"".number_format($rsprr[0]['prezzo'], get_var("Cifre decimali"), ".", "")."\"><span class='input-group-addon'>&euro;</span>\n";
	echo "				</div>\n";
	echo "			</div>\n";
	echo "		</div>\n";
	
	//Prezzo Venedita
	echo "		<div class='col-md-2'>\n";
	echo "			<div class='form-group'>\n";
	echo "				<label>Nuovo Prezzo</label>\n";
	echo "				<div class='input-group'>\n";
		echo "<big id='subtotale'  >".number_format($subtotale, get_var("Cifre decimali"), ",", ".")." &euro;</big><br/>\n";
	echo "				</div>\n";
	echo "			</div>\n";
	echo "		</div>\n";
	//Prezzo Venedita
	echo "		<div class='col-md-2'>\n";
	echo "			<div class='form-group'>\n";
	echo "				<label>Nuova %</label>\n";
	echo "				<div class='input-group'>\n";
		echo "<big id='subtotaleper'  >".$subtotaleper."</big><br/>\n";
	echo "				</div>\n";
	echo "			</div>\n";
	echo "		</div>\n";
	
						
	
	echo "	<a onclick=\"addriga();\" class=\"btn btn-primary pull-right\"><i class=\"fa fa-edit\"></i> Modifica</a>\n";

	
	echo "</form>\n";
?>

	<script type="text/javascript">
	
		prezzo_base = '<?php echo $prezzo_vendita ?>';
	
		function addriga(){
			$("#link_form").submit();
		}
		
		
		$(document).ready( function(){		
			start_jquerychosen();
			start_inputmask();		
		});
		
		
		
		function ricalcola_subtotale_riga( r ){
			subtot_perc =  $("#subtot_perc").val() ;		
			subtot_perc=-subtot_perc;
			subtotale=prezzo_base - ((prezzo_base / 100 ) * subtot_perc);			
			$("#subtotale").html(subtotale);
			$("#subtotaleper").html(-subtot_perc);					
			
		}
		
		function ricalcola_subtotale_riga1( r ){			
			subtot = force_decimal( $("#subtot").val() );
			new_perc=((prezzo_base - subtot ) / prezzo_base)*100;	
			$("#subtotale").html(subtot);
			$("#subtotaleper").html(-new_perc);						
		}
		
	</script>

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

