<?php
@session_start();
$vg_login=$_SESSION["usuario"];
if($vg_login<>""){}
else
{echo "<script>";
 echo "window.location='../index.php';";
 echo "</script>";
}	
include "../valida_permisos.php";
$vg_programa = F_programa();
include "../seguridad.php";
$sql_entrada= mysqli_query($conseg,"select login,nombre,perfil,programa,cedula,pais from permiso     
					  where login='".trim($vg_login)."'  and programa=".$vg_programa."");
while($datos= mysqli_fetch_row($sql_entrada))
	  { $vg_nombre=$datos[1];
		$vg_perfil=$datos[2];				
	  } 

//permisos a formularios
$modulo="Seguridad";
$permisos=validar_permiso($vg_login,$modulo,$vg_perfil,$vg_programa);
include "../permisos.php";
$valpermiso=F_permiso($permisos);

if($valpermiso[0]==1){}
else
{echo "<script>";
echo "window.location='../erroracceso.php?formulario=Parametros_Usuarios';";
echo "</script>";
}

?>



<html>

<head>

<title>SEGURIDAD</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<link rel="icon" type="image/png" href="../icono.ico"/>

<link href="../general.css" rel="stylesheet" type="text/css">

<script src="../js/jquery.min.js"></script>

<link href="../bootstrap/css/bootstrap.min.css" rel="stylesheet">

<script src="../bootstrap/js/bootstrap.min.js"></script>

</head>

<body  onFocus="document.forms.form1.login.focus();">

<?php

 @session_start();

  $vg_login=$_SESSION["usuario"];

  if($vg_login<>""){}

 else

  {echo "<script>";

   echo "window.location='../index.php';";

   echo "</script>";

  }	



include "../seguridad.php";



$rs_programa=mysqli_query($conseg,"SELECT id,nombre FROM programa  where estado='A' 

						  ORDER BY nombre asc");



$rs_modulo=mysqli_query($conseg,"SELECT codigo as id,nombre FROM modulo  

								where estado='A' 

								union select 0,'' from modulo

						       ORDER BY nombre asc");

						  

 $rs_perfil=mysqli_query($conseg,"SELECT id,nombre from perfil

							   where programa=1							   

							   and activa='S'

							   union 

							   select 0,'' from perfil

							    order by nombre asc");							  

   

if(isset($_REQUEST['oculto']))

  { if(isset ($_REQUEST['programa'])) {$vp_programa=$_REQUEST['programa']; }

    if(isset ($_REQUEST['perfil'])) {$vp_perfil=$_REQUEST['perfil']; }

	if(isset ($_REQUEST['modulo'])) {$modulo=$_REQUEST['modulo']; }

    if(isset ($_REQUEST['estado'])) {$vp_estado=$_REQUEST['estado']; }

	

  

    #opcion para guardar un nuevo usuario

     if(isset ($_REQUEST['guardar']))

        {   $sql_contar = mysqli_query($conseg,"SELECT count(*) as cantidad FROM modulo_perfil

				 		               where modulo='".trim($modulo)."'

									   and perfil=".$vp_perfil."");

				 while($datos= mysqli_fetch_row($sql_contar)){ $contar=$datos[0];}					

				if($contar>=1)

				 {  echo "<script>";

				 	echo "alert('Ya existe el perfil con el modulo')";  echo "</script>";  

				 }

				else

				 {  $sql_insert = mysqli_query($conseg,"insert into modulo_perfil (modulo,perfil,programa,sn)

													   values ('".$modulo."',".$vp_perfil.",

													           ".$vp_programa.",'".$vp_estado."')");

							   if ($sql_insert)

								 { echo "<script>";  

								 echo "alert('se proceso')";  echo "</script>"; }

							   else

								 { echo "<script>";   

								 echo "alert('Error al procesar')"; echo "</script>"; }

						 }

        }//fin guarda

		

     if(isset ($_REQUEST['modificar']))

        {  $sql_contar = mysqli_query($conseg,"SELECT count(*) as cantidad FROM modulo_perfil

				 		  where modulo='".$modulo."'

						  and perfil='".$vp_perfil."'");

 					while($datos= mysqli_fetch_row($sql_contar)){ $contar=$datos[0];}			

					if($contar>=1)

						 {    $sql_update = mysqli_query($conseg,"update modulo_perfil set 

						   					 sn='".$vp_estado."'

											 where modulo='".$modulo."'

				 							 and perfil='".$vp_perfil."'");

							   if ($sql_update)

								 { echo "<script>";  

								 echo "alert('Se actualizo el perfil del usuario')";  echo "</script>"; }

							   else

								 { echo "<script>";  

								  echo "alert('Error al actualizar el perfil del usuario')"; echo "</script>"; } 

						 }

						else

						 { echo "<script>";  

						  echo "alert('No existe el perfil de usuario a modificar')"; echo "</script>";}	 

        }//fin modifica

		

   }//fin  oculto



?>

<?php

// if($vp_vvalida[0]==1)

 //{

?>

<form name="form1" method="post" action="">

<div class="panel panel-primary" >

    <div class="panel-heading">REGISTRO DE MODULOS CON PERFIL</div>

    <TABLE 

      width="60%" border=0 align=center cellPadding=10 cellSpacing=0 borderColor=#111111 >

      <TBODY>

        <TR>

          <TD  align=left><table width="65%"  border="0" cellspacing="0" cellpadding="0">

              <tr>

                <td width="14%"><strong><font color="#000000" >PROGRAMA</font><font color="#000000"> </font></strong> </td>

                <td width="27%"><strong><font color="#FFFFFF" size="2">

                  <select name="programa" class="form-control" id="programa" style="width: 150px">

                    <?php 

							while ($row=mysqli_fetch_array($rs_programa))

									{

									 if ($vp_programa==$row['id'])

										{echo "<option value=".$row['id']." selected>".$row['nombre']."</option>";}

									 else

										{echo "<option value=".$row['id'].">".$row['nombre']."</option>";}		

									}

					  ?>

                  </select>

                  <input name="obusca" type="hidden" id="obusca">

                </font><font color="#000000"> </font></strong></td>

                <td width="59%"><strong><font color="#FFFFFF" size="2">

                  <input name="proceso" type="submit" id="proceso"  value=">>"  onClick="form1.obusca.value=1;" class="btn btn-primary">

                </font></strong></td>

              </tr>

              <tr>

                <td><strong><font color = #000000>PERFIL</font><font color="#000000"> </font><font color="#FFFFFF" size="2"> </font><font color="#000000"> </font><font color="#000000"> </font></strong></td>

                <td colspan="2"><strong><font color="#FFFFFF" size="2">

                  <select name="perfil" class="form-control"id="perfil" style="width: 100%" onChange="obusca.value=1;submit();">

                    <?php 

							while ($row=mysqli_fetch_array($rs_perfil))

									{

									 if ($vp_perfil==$row['id'])

										{echo "<option value=".$row['id']." selected>".$row['nombre']."</option>";}

									 else

										{echo "<option value=".$row['id'].">".$row['nombre']."</option>";}		

									}

					  ?>

                  </select>
                </font></strong></td>

              </tr>

              <tr>

                <td><strong><font color = #000000>MODULO</font></strong></td>

                <td colspan="2"><strong><font color="#FFFFFF" size="2">

                  <select name="modulo" class="form-control"id="modulo" style="width: 100%">

                    <?php 

							while ($row=mysqli_fetch_array($rs_modulo))

									{

									 if ($modulo==$row['id'])

										{echo "<option value=".$row['id']." selected>".$row['nombre']."</option>";}

									 else

										{echo "<option value=".$row['id'].">".$row['nombre']."</option>";}		

									}

					  ?>

                  </select>

                </font></strong></td>

              </tr>

              <tr>

                <td><strong><font color = #000000>ACTIVAR</font></strong></td>

                <td colspan="2"><strong><font color="#000000">

                  <select name="estado" class="form-control" onFocus="this.style.background='#ccffff'" onBlur="this.style.background='#ffffff'" style="width: 150px">

                    <?php

					   if($vp_estado=="S")

						  {echo "<option value='S' selected>ACTIVO</option>";}

					   else

						  {echo "<option value='S'>ACTIVO</option>";}		   

					   if($vp_estado=="N")  	  

						  {echo "<option value='N' selected>INACTIVO</option>";}

					   else

						  {echo "<option value='N'>INACTIVO</option>";}		   

					 ?>

                  </select>

                </font></strong></td>

              </tr>

              <tr>

                <td colspan="3"><div align="center">

                    <?php

 // if($btn_guardar==1)

//	{

?>

                    <input name="guardar" type="submit" id="guardar"  value="Guardar" class="btn btn-primary">

                    <?php

   //}

?>

                    <?php

  //if($btn_modificar==1)

   // {

?>

                    <input name="modificar" type="submit" id="modificar"  value="Modificar" class="btn btn-primary">

                    <?php

     //}

?>

                    <input type="hidden" name="oculto">

                </div></td>

              </tr>

          </table></TD>

        </TR>

      </TBODY>

    </TABLE>

    

    <?php

	 if($_REQUEST['obusca']==1)
       { 

   
								
	?>
    <div class="card-body">
                <table id="example1" class="table table-bordered table-striped" style="font-size:11px;">
                  <thead>
                  <tr>
                    <th class="btn-primary" width="10%">MODULO</th>
                    <th class="btn-primary">MODULO</th>
                    <th class="btn-primary">ACTIVAR</th>
                  </tr>
                  </thead>                 
                  <tbody>
                  <?php
				 
				 
				  $sql_datosmenu =mysqli_query($conseg,"select menu,modulo,count(*) from menu_sistema group by menu order by menu asc");
				  while($datosmenu= mysqli_fetch_row($sql_datosmenu))//01
				  {
					  $menu=$datosmenu[0]; 
					  $nmodulo=$datosmenu[1]; 
					  
					   echo "<tr>
								<td colspan='3'>".strtoupper($nmodulo)."</td>
						</tr>";
					  
					  $sql_datosmodulo =mysqli_query($conseg,"select modulof,titulo from menu_sistema where menu='".$menu."' order by menu asc");
					  while($datosmodulo= mysqli_fetch_row($sql_datosmodulo))//01
					  {
						  $id=$datosmodulo[0]; 
						  $titulo=$datosmodulo[1]; 
						  
						    $sql_contar=mysqli_query($conseg,"SELECT count(*) FROM empresa_usuario 
															  where empresa='".$id."' and usuario='".$usuario."'");
					  		while($datoscontar= mysqli_fetch_row($sql_contar)){ $contar=$datoscontar[0];}
					  		if($contar>=1){$checked="checked"; } else { $checked=""; } 
					  
						  echo "<tr>
								<td width='10%'></td>
								<td width='80%'>".strtoupper($titulo)."</td>
								<td width='10%'>
									<input name='id$c' type='hidden' id='id$c'  value='$id'>
									<input type='checkbox' name='ch$c' style='width: 15px' $checked >
								</td>
								</tr>";
						$c++;
					  }
				  }
					  
					 $canti=$c-1;
					 echo "<input type='text' name='cantir' value='$canti' readonly size='10' style='width:1px; height=1px;' readonly>";	
					
				  ?>
                  </tbody>
                 
                </table>
              </div>
    <?php
		}	
	  ?>
  </div>
</form>
</body>
</html>
