<?php
require_once("../includes/academia_constantes.php");
require_once("../includes/funciones_generales.php");
require_once("../includes/clase_sesion.php");
$sesion=new sesion();
$sesion->set('permiso',permisos($_POST['privilegio']));
#error_reporting(1);
require_once("../includes/lib_lenguaje.php");
$clenguaje=new cLanguage();
	
if(!comprobar_sesion()){
	mensaje($clenguaje->Phrase('global','ses_cad'),$clenguaje->Phrase('project','institucion'));
	exit;	
}

require_once('../includes/MySQL.php');
$academia = new Servidor_Base_Datos(AC_CONN_HOST,AC_CONN_USER,AC_CONN_PASS,AC_CONN_DB."".$sesion->get('anio'));

$codigo_usr=$_GET['id_usr'];
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Asisgnar Permisos de administracion</title>

<!--<link type="text/css" rel="stylesheet" href="../css/jquery-ui-1.8.css" />
<link type="text/css" rel="stylesheet" href="../css/general.css" />
<link type="text/css" href="css/ui.multiselect.css" rel="stylesheet" />
<link type="text/css" href="../css/jquery.notifyBar.css" rel="stylesheet" />
<link type="text/css" href="../css/SyntaxHighlighter.css" rel="stylesheet" />

<script type="text/javascript" src="../js/jquery-1.6.js"></script>
<script type="text/javascript" src="../js/jquery-ui-1.8.js"></script>
<script type="text/javascript" src="js/ui.multiselect.js"></script>
<script type="text/javascript" src="../js/jquery.notifyBar.js"></script>
-->
<script type="text/javascript" src="js/styletable.jquery.plugin.js"></script>

<script type="text/javascript">
$(document).ready(function(){
	
	$("#guardar_per_menu").click(function(){
		datos=$("#form_per_menu").serialize();
		$.post("mod_admin_usuarios/g_asig_permisos.php", datos ,function(pag){
			if(pag==0){	
				$.fn.colorbox.close();							
				notifica_ok("<?php echo $clenguaje->Phrase("administrador",'guarda_per_ok') ?>")
			}else{
				jAlert("<?php echo $clenguaje->Phrase("administrador",'guarda_per_err') ?>",'<?php  echo $clenguaje->Phrase('project','institucion') ?>',function(){
					
				});
			}
		});
	})
	
	$(".togFila").each(function(){
		var num=$(this).attr('nx')
		$(this).toggle(function(){		
			$(".fila"+num).each(function(){
//				alert($(this).val());
				nval=$(this).val();
				if(!$(this).is(':checked')){
					
					valper=$("#txtvalper"+num).val();
					$("#txtvalper"+num).val(parseInt(valper) + parseInt(nval))
					$("#txtnper"+num).html(parseInt(valper) + parseInt(nval))
					this.checked = true;
				}
			})
		},function(){
			$(".fila"+num).each(function(x){
				nval=$(this).val();
				if($(this).is(':checked')){
					valper=$("#txtvalper"+num).val();
					$("#txtvalper"+num).val(parseInt(valper) - parseInt(nval))
					$("#txtnper"+num).html(parseInt(valper) - parseInt(nval))
					this.checked = false;
				}
			})
		})
	})
	
	
});

colorear_tb('lis_per_menu')

function  chktodos(){
$(document).ready(function(){
	$("input[name=txtpera[]]").change(function(){
		$('input[type=txtpera[]]').each( function() {			
			if($("input[name=txtpera[]]:checked").length == 1){
				this.checked = true;
			} else {
				this.checked = false;
			}
		});
	});
});
}

function colorear_tb(id){
$(document).ready(function(){
	$('#'+id).styleTable({
		th_bgcolor: '#BCD4EC',  
		th_color: '#fff',
		th_image: 'css/images/bg_hover.png',  
		tr_odd_image: 'css/images/bg.jpg',  
		tr_even_image: 'css/images/bg.jpg',  
		tr_hover_image: 'css/images/bg_topbar.gif' ,
		font_name: 'Verdana',
		font_size: '11'	
	});
})
}

function calper(num,id,valor){
$(document).ready(function(){
	val=parseInt($("#txtvalper"+num).val());	

	if($("#"+id).is(':checked')){
		val=val+parseInt(valor);
		$("#txtvalper"+num).val(val)	
	}else{
		val=val-parseInt(valor);
		$("#txtvalper"+num).val(val)	
	}
//	$("#txtnper"+num).html(val)
	
})
}

$("#txttodosA,#txttodosM,#txttodosE,#txttodosS, #txttodosEX").change(function(){
	clase=$(this).attr('idclas')
	
	if($(this).is(':checked')){
		$("."+clase).each(function(){
			nval=$(this).val();
			if(!$(this).is(':checked')){
				n=$(this).attr('n');
				valper=$("#txtvalper"+n).val();
				$("#txtvalper"+n).val(parseInt(valper) + parseInt(nval))
				//$("#txtnper"+n).html(parseInt(valper) + parseInt(nval))
				this.checked = true;
			}			
		})
	}else{
		$("."+clase).each(function(){
			nval=$(this).val();
			if($(this).is(':checked')){
				n=$(this).attr('n');
				valper=$("#txtvalper"+n).val();
				
				$("#txtvalper"+n).val(parseInt(valper) - parseInt(nval))
				//$("#txtnper"+n).html(parseInt(valper) - parseInt(nval))
				this.checked = false;
			}			
		})
	}
})



</script>
</head>

<body>
<form action="g_asig_permiso_menu.php" method="post" name="form_per_menu" id="form_per_menu">
<input type="hidden" name="txtcod_usu" id="txtcod_usu" value="<?php echo $codigo_usr ?>" />
<table border="0" align="center" cellpadding="2" cellspacing="0" id="lis_per_menu">
  <tr>
    <th width="200" rowspan="2" align="left"><span class="tit1">Menú</span></th>
    <th width="400" rowspan="2" align="left"><span class="tit1">Descripcion Menú</span></th>
    <th colspan="5"><span class="tit1">Permisos</span></th>
    </tr>
  <tr>
    <th  title="Añadir"><span class="tit1">
      <input type="checkbox" name="txttodosA" id="txttodosA" idclas='anadir'/>
    A</span></th>
    <th  title="Modificar"><span class="tit1">
      <input type="checkbox" name="txttodosM" id="txttodosM" idclas='edtitar'/>
    M</span></th>
    <th  title="Eliminar"><span class="tit1">
      <input type="checkbox" name="txttodosE" id="txttodosE" idclas='elimina' />
    E</span></th>
    <th align="center" title="Subir Archivos"><input type="checkbox" name="txttodosS" id="txttodosS" idclas='subir' />
      S</th>
    <th align="center" title="Exportar a excel"><input type="checkbox" name="txttodosEX" id="txttodosEX" idclas='exporta' />
      Ex</th>
    
  </tr >

<?php
	$x=0;
	$sqlmenu="SELECT * FROM conf_menu where trim(url_formulario)<>''";
	$academia->instruccion($sqlmenu);
	
	
	while($reg=$academia->extraer_registro()){
		$codfor=$reg['ide_formulario'];		
		$academia_per= new Servidor_Base_Datos(AC_CONN_HOST,AC_CONN_USER,AC_CONN_PASS,AC_CONN_DB."".$sesion->get('anio'));
		
		$sqlperasi="SELECT * FROM conf_permisos where ide_usuario='$codigo_usr' and ide_formulario='$codfor'";
		$academia_per->instruccion($sqlperasi);
		$regper=$academia_per->extraer_registro();
		
		$numreg=$academia_per->num_rows();
		
		$valtipo= $numreg==0 ? '' : $regper['ide_permiso']; // lleva en cuenta el codigo del permiso que ya existe para el formulario
		
		$valper=$regper['permiso']; // trae el valor del permiso para checarlo al mostrarlo
		
		$valper=($valper=='2' || $valper=='') ? 1 : $valper;
		
		$perx=str_split(permisos($regper['permiso']));
		$academia_per->liberar();
		$academia_per->cerrar();

		$cha=  in_array("A",$perx) ? 'checked' : '';
		$chm=in_array("M",$perx) ? 'checked' : '';
		$che=in_array("E",$perx) ? 'checked' : '';
		$chs=in_array("S",$perx) ? 'checked' : '';
		$chx=in_array("P",$perx) ? 'checked' : '';
		
		
?>

	<tr class="opera">
        <td class="togFila" nx='<?php echo $x ?>'>
		<?php 
			//echo "<input name='txttipact[]' id='txttipact$x' type='hidden' value='".$valtipo."'/>";
			echo "<input name='codigofor[]' id='codigofor$x' type='hidden' value='".$reg['cod_formulario']."'/>"; //codigo del formulario los que llevan punto
			echo "<input name='txtidefor[]' id='txtidefor$x' type='hidden' value='".$reg['ide_formulario']."'/>". $reg['nom_formulario'] ; //id autoincrmento del formuario
		//colocar otro campo oculto para llevar el codigo del formulario (nivel)
		?>
        
        </td>
        <td class="togFila" nx='<?php echo $x ?>'><?php echo $reg['des_formulario'] ?></td> 
        <td><?php echo "<input name='txtpera[]' id='txtpera$x' n='$x'  class='anadir  fila$x' type='checkbox'  value='2' $cha onchange=\"calper('$x','txtpera$x',2)\" />" ?></td>
        <td><?php echo "<input name='txtperm[]' id='txtperm$x' n='$x'  class='edtitar fila$x' type='checkbox' value='4' $chm onchange=\"calper('$x','txtperm$x',4)\" />" ?></td>
        <td><?php echo "<input name='txtpere[]' id='txtpere$x' n='$x'  class='elimina fila$x' type='checkbox' value='8' $che onchange=\"calper('$x','txtpere$x',8)\" /> " ?></td>
        <td><?php echo "<input name='txtpers[]' id='txtpers$x' n='$x'  class='subir fila$x' type='checkbox' type='checkbox' value='16' $chs onchange=\"calper('$x','txtpers$x',16)\" />" ?></td>
        <td><?php echo "<input name='txtperx[]' id='txtperx$x' n='$x'  class='exporta fila$x' type='checkbox' value='32' $chx onchange=\"calper('$x','txtperx$x',32)\" /> 
		<input name='txtvalper[]' id='txtvalper$x' type='hidden' value='$valper'/>"  ?></td>
	</tr>
<?php		
	$x++;
	}
	$academia->liberar();
	$academia->cerrar();
?>
</table>
<table align="center">
<tr>
        <td align="center">
        <img src="images/guardar.png" alt="Guardar" name="guardar_per_menu" width="20" height="20" class="opera" id="guardar_per_menu"/></td>
  </tr>
</table>
</form>
</body>
</html>