<?php
include_once ("../core/config.php");
$UTENTE = login();
$DIRITTI = diritti('Admin');
view_top();

$query = "	SELECT
			id,
			nome,
			max(accesso) AS accesso
		FROM
			(SELECT
				moduli.id AS id,
				moduli.nome AS nome,
				permessi.accesso AS accesso
			FROM
				permessi
			JOIN
				moduli ON permessi.id_moduli = moduli.id AND moduli.attivo = 1
			WHERE
				permessi.id_utenti = ".$_GET['id']."
			UNION DISTINCT
			SELECT 
				moduli.id AS id,
				moduli.nome AS nome,
				0 AS accesso
			FROM
				moduli
			WHERE
				moduli.attivo = 1) s
			GROUP BY
				nome
			ORDER BY
				nome
";

$permessi = mysql_query( $query, $DB_ID );

$query = "	SELECT 
			utente, 
			admin
		FROM 
			utenti
		WHERE
			id = ".$_GET['id']."
		AND
			attivo = 1
";
$res = mysql_query( $query, $DB_ID );
$dato = mysql_fetch_array ($res);

$livelli = get_levels();
?>
<script type="text/javascript" src="<?php print $CONF['base_url']?>/jquery/jquery-1.8.0.min.js"></script>

<?php 
$tabella = new html (0,"90%", array (0)); ?>
<tr>
	<td align="center">

<?php $tabella1 = new html (0,"40%", array (20,0)); ?>
<tr>
<?php	if ($dato['admin']) { ?>
		<td rowspan=2 class="col3btn"><img src="<?php print $CONF['base_url'] ?>/img/admin.png" ALT="Admin" TITLE="Admin"></td>
<?php	} else { ?>
		<td rowspan=2 class="col3btn"><img src="<?php print $CONF['base_url'] ?>/img/utente.png" ALT="Utente" TITLE="Utente"></td>
<?php	} ?>
		<td class="col3campo2"><?php print $dato['utente'] ?></td>
</tr>
<?php $tabella1->close(); ?>
	</td>
</tr>
<?php $tabella->riga(); ?>
<tr>
	<td align="center">
<?php 
$tabella2 = new html (0,"60%", array(30,0));
while ($dato = mysql_fetch_array ( $permessi )) { ?>
<tr>
<td class="col3campo2"><?php print $dato['nome']?></td>
<td>
	<select name='<?php print $dato['nome']?>' class="cambio" >
<?php 	foreach ($livelli as $i => $v) { ?>
		<option name='<?php print $dato['nome']?>' value='<?php print $i ?>' <?php if ($i == $dato['accesso']) { print "selected"; } ?>><?php print $livelli[$i] ?></option>
<?php } ?>
	</select>
</td>
</tr>
<?php 
$tabella2->riga();
} 
$tabella2->close();
?>
	</td>
</tr>
<?php $tabella->close(); ?>

<script type="text/javascript">
$(function(){
	$('.cambio').on('change', function(){
		var usr = <?php print $_GET['id'] ?>;
		var acc = $(this).val();
		var mod = $(this).attr('name');

	$.ajax({
		url: "updatepermessi.php",
		dataType: "json",
		data: {
			user_id: usr,
			accesso: acc,
			modulo: mod
		},
		success: function(data) {
//        data contiene i ritorni dalla pagina chiamata ma devono essere in json
		}
	}	);
	});
})
</script>

<?php view_footer(); ?>
