<?php
include ("../conexion.php");
include ("../funciones/fechas.php");

$dscfam=strtoupper($_POST["dscfam"]);
$accion=$_POST["accion"];
$cdgfam=$_POST["cdgfam"];
$cdgfam=sprintf("%03d",$cdgfam);

if ($accion == "alta"){

if ($dscfam <> "") {

			
	 ////////////////////////   Averigua el numero de FILAS ////////////////////////////
if ($result = $mysqli->query("SELECT * FROM familias WHERE   cdgfam = '$cdgfam' " )) {
   $filas = $result->num_rows;
   $result->close();
}
					
	if ($filas > 0 ) {
	
	
	?>
	<script>
	alert ("Favor verifique familia ya existe");
    </script>
	
    
	<?php
	
			echo "<script>parent.location.href='familias.php?cdgfam=".$cdgfam."';</script>";	
			
	} else{ 
	
	
	
	
	$query_operacion="INSERT INTO familias (cdgfam, dscfam) VALUES ('$cdgfam', '$dscfam' )";					
	$mysqli->query($query_operacion);
	
	

$consulta = "SELECT MAX(cdgfam) AS cdgfam FROM familias";
$resultado = $mysqli->query($consulta);
$fila = $resultado->fetch_array();
					$cdgfam =  $fila["cdgfam"];
                    $cdgfam= $cdgfam + 1 ;
					$cdgfam=sprintf("%03d",$cdgfam);



	echo "<script>parent.location.href='familias.php?cdgfam=".$cdgfam."';</script>";		

				

}}
}


if ($accion == "modificar"){

if ($dscfam <> "") {

 	$query="UPDATE familias SET dscfam='$dscfam' WHERE cdgfam='$cdgfam'";
	$mysqli->query($query);
	

	$consulta = "SELECT MAX(cdgfam) AS cdgfam FROM familias";
	$resultado = $mysqli->query($consulta);
	$fila = $resultado->fetch_array();
						$cdgfam =  $fila["cdgfam"];
						$cdgfam= $cdgfam + 1 ;
						$cdgfam=sprintf("%03d",$cdgfam);
						

	
	?>
	<script>
	

			parent.document.getElementById("form_familias").reset();
			parent.document.getElementById("form_lineas").reset();
			parent.cdgfam.disabled=false;
			parent.document.getElementById("dscfam").focus();
			parent.document.form_familias.accion.value="alta";
	
    </script>
	
	<?php


}
}




if ($result = $mysqli->query("SELECT * FROM familias " )) {
   $filas = $result->num_rows;
   $result->close();
}


?>
<html>
	<head>
		<title>Familias</title>
		<link href="../estilos/estilos.css" type="text/css" rel="stylesheet">
		<script language="javascript">
	
		
		function modificar_familia(cdgfam,dscfam) {
			
	     parent.document.form_familias.cdgfam.value=cdgfam;
		 parent.document.form_familias.dscfam.value=dscfam;
		 parent.document.form_familias.accion.value="modificar";
		 parent.cdgfam.disabled=true;

		}
		
	//////////////////////////////////////  funcion anular familia //////////////////////////////////////////////////////

		
		function CambiaColor(esto,fondo,texto)
		{
			colorfondo=esto.style.background;
			esto.style.background=fondo;
			esto.style.color=texto;
			return colorfondo;
		}
		
		function CambiaColor2(esto,texto)
		{
			esto.style.background=colorfondo;
			esto.style.color=texto;
		}
		
				function nobackbutton(){	
   window.location.hash="no-back-button";	
   window.location.hash="Again-No-back-button" //chrome	
   window.onhashchange=function(){window.location.hash="no-back-button";}	
}

		
		</script>
	</head>

	<body onload= nobackbutton(); >	
		<div id="">
			<div id="zonaContenidoPP">
			<div align="center">
			<table class="fuente9" width="100%" cellspacing=0 cellpadding=0 border=0 ID="Table1">
            	<form id="formulario" name="formulario" method="post" action="">
			<input type="hidden" name="cdgfam" id="cdgfam" value="<?php echo $cdgfam2?>">

				<?php 
					if ($filas > 0) { 
					
$contador=0;		 
$consulta = "SELECT cdgfam, dscfam FROM familias ORDER BY cdgfam DESC ";

if ($resultado = $mysqli->query($consulta)) 

 
    while ($fila = $resultado->fetch_assoc()) {
        $cdgfam = $fila["cdgfam"];
        $dscfam = $fila["dscfam"];
		
					

								if ($contador % 2) { $fondolinea="itemParTabla"; } else { $fondolinea="itemImparTabla"; }?>
								
								<tr class="<?php echo $fondolinea ?>" onMouseOver="CambiaColor(this,'#000000','#ffffff')" onMouseOut="CambiaColor2(this,'#000000')" >
                   
								<td width="8%"><?php $contador+1;?></td>
								<td width="16%"><div align="left"><?php echo $cdgfam ?></div></td>
								<td width="39%"><div align="left"><?php echo $dscfam ?></div></td>	
							
								<td width="13%">&nbsp;</td>
								<td width="10%"><div align="center"><a href="#"><img src="../img/Edit_Male_User_Icon_32.png" width="25" height="25" border="0" onClick="modificar_familia('<?php echo $cdgfam ?>', '<?php echo $dscfam ?>')" title="Modificar"></a></div></td>
								<td width="14%">&nbsp;</td>
						</tr>
						</tr>	
						<?php $contador++;
							}
						?>			
					</table>
					<?php } else { ?>
					<table class="fuente8" width="87%" cellspacing=0 cellpadding=3 border=0>
						<tr>
							<td width="100%" class="mensaje"><?php echo $fechainicio; echo $fechafin; echo "No hay ninguna Familia registrada ";?></td>
					    </tr>
					</table>		
                    	</form>			
					<?php } ?>					
				</div>
			</div>
		  </div>			
		</div>
	</body>
</html>

	