<?php
session_start();
//RESTRICCIÓN DE ACCESO A TRABAJADOR (ACCESO INDIVIDUAL) 
if (isset($_SESSION['tipousuario'])) {
	if ($_SESSION['tipousuario'] == '1')
		header('location:menu_personal.php');
}
if (trim($_SESSION['nombre_validar']) == '') header('location:index.php');
require('fphp.php');

$accion = $_REQUEST["accion"];
$codigo = $_REQUEST["codigo"];
$cedula = $_REQUEST["cedula"];
$enlace = cargar_conexion();
$consulta = mysqli_query($enlace, "select * from periodo where id='" . $_SESSION['id_validar'] . "' order by nomina,periodo desc limit 1");
if (mysqli_num_rows($consulta) != 0) {
	$row = mysqli_fetch_array($consulta);
	$_SESSION['per_validar'] = mostrar_fecha($row['desde']) . ' al ' . mostrar_fecha($row['hasta']) . " (Nom:" . $row['nomina'] . ")";
} else $_SESSION['per_validar'] = '';

///////////////////////////////////////////////// inicio del programa ////////////////////////////////////////////////////////////////
menu();
$entra = 0;

///////////////////////////////////////////////// bajar foto ////////////////////////////////////////////////////////////////
if ($_POST['bandera'] == 33) {
	$tamano = $_FILES["archivo"]['size'];
	$tipo = $_FILES["archivo"]['type'];
	$archivo = $_FILES["archivo"]['name'];
	$prefijo = substr(md5(uniqid(rand())), 0, 6);
	if ($archivo != "") {
		$destino =  "fotos/" . $_POST['cedulafoto'] . ".jpg";
		if (copy($_FILES['archivo']['tmp_name'], $destino)) {
			$status = "Archivo subido: <b>" . $archivo . "</b>";
		} else {
			$status = "Error al subir el archivo";
		}
	} else {
		$status = "Error al subir archivo";
	}
}

///////////////////////////////////////////////// cargar fotos ////////////////////////////////////////////////////////////////
if ($accion == 33) {
	echo "<b>Opciones:</b> &nbsp;&nbsp;<img border = 0 src='images/b_regresa.png' alt='Regresar'><a href='" . $_SERVER['PHP_SELF'] . "'>Regresar</a>";
	echo "<hr><h2>Cargar Foto de la C&eacute;dula Nro: " . $cedula . "</h2>";
	echo "<form name = 'entrada4' action = '" . $_SERVER['PHP_SELF'] . "' method = 'POST'  enctype='multipart/form-data'>";
	echo "<input type = 'hidden' name = 'bandera' value = '33'>";
	echo "<input type = 'hidden' name = 'cedulafoto' value = '" . $cedula . "'>";
	echo "<table cellpadding=2 cellspacing=0 >";
	echo "<tr>";
	echo "<td align=right> Archivo de Foto? (Imagen formato: JPG):</td>";
	echo "<td> <input name='archivo' type='file' size='35' /></td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td colspan=2><input name='contrato' type = 'submit' value = 'Bajar Foto a SAIREH? / Regresar'></td>";
	echo "</tr>";
	echo "</table>";
	echo "</form>";
} else {

	///////////////////////////////////////////////// ACTUALIZAR TRABAJADOR ////////////////////////////////////////////////////////////////
	echo "<b>Opciones:</b> &nbsp;&nbsp;<img border = 0 src='img2/icono_07.png' alt='Elimina'>&nbsp;Eliminar	
&nbsp;&nbsp;<img border = 0 src='img2/icono_01.png' alt='Modificar'>&nbsp;Modificar";
	if (substr($_SESSION['acceso_validar'], 16, 1) == '1')
		echo "&nbsp;&nbsp;<a href='" . $_SERVER['PHP_SELF'] . "?accion=11'><img border = 0 src='img2/icono_04.png' alt='Nuevo Trabajador'>&nbsp;Nuevo Trabajador</a>";
	if (substr($_SESSION['acceso_validar'], 6, 1) == '1')
		echo "&nbsp;&nbsp;<a href='archivo_trabajador_estructura.php'><img border = 0 src='img2/icono_03.png' alt='Estructuras'>&nbsp;Estructuras</a>";

	echo "<hr><h2>Actualizaci&oacute;n de Trabajadores</h2>";

	///////////////////////////////////////////////// graba registros  ////////////////////////////////////////////////////////////////
	if ($_POST['graba'] == 1) {

		$consulta3 = mysqli_query($enlace, "select * from periodo where nomina='" . substr($_POST['nomina'], 0, 3) . "' and id='" . $_SESSION['id_validar'] . "' order by periodo DESC limit 1");
		if (mysqli_num_rows($consulta3) != 0) {
			$row3 = mysqli_fetch_array($consulta3);
			if ($row3['pre'] == 1) $nograbar = 1;
			else $nograbar = 0;
		}
		if ($nograbar == 1)
			echo "<center><font color=red>" . ($nograbar == 1 ? "No se puede procesar al trabajador porque la Nómina esta en Pre-Cierre" : "") . "</font></center>";
		else {

			$query = "select * from personal where codigo = '" . $_POST['cedula'] . "' and id='" . $_SESSION['id_validar'] . "' ";
			$consulta = mysqli_query($enlace, $query) or die("no se pudo realizar la consulta...");
			$resultado = mysqli_num_rows($consulta);
			if ($resultado == 0) {
				$fnac = crear_fecha($_POST['fecnac']);
				$fing = crear_fecha($_POST['fecing']);
				$fegr = crear_fecha($_POST['fecegr']);
				$flic = crear_fecha($_POST['fechalic']);
				$fcar = crear_fecha($_POST['fechacar']);
				$fsal = crear_fecha($_POST['fechasal']);
				$fman = crear_fecha($_POST['fechaman']);

				$query = "insert into personal  ( id, nrocontrato, contrato, clave, fechalic, fechacar, fechasal, fechaman, apellidos1, nombres1,  mensaje,codigo,nombres,apellidos,dir,telefono,sexo,hijo,gerencia,departamento,cargo,unidad,";
				$query .= "nomina, turno, banco, fechan, fechai, fechae, cuenta, smes, division, sector, estado, pago, parroquia, nacionalidad, municipio, ";
				$query .= "area, edocivil, tipo, profesion, direccion, sso, status) values ( '" . $_SESSION['id_validar'] . "', ";
				$query .= "ucase(trim('" . $_POST['nrocontrato'] . "')), ";
				$query .= "ucase(trim('" . $_POST['contrato'] . "')), ";
				$query .= "ucase(trim('" . $_POST['clave'] . "')), ";
				$query .= "ucase(trim('" . $flic . "')), ";
				$query .= "ucase(trim('" . $fcar . "')), ";
				$query .= "ucase(trim('" . $fsal . "')), ";
				$query .= "ucase(trim('" . $fman . "')), ";
				$query .= "ucase(trim('" . $_POST['apellidos1'] . "')), ";
				$query .= "ucase(trim('" . $_POST['nombres1'] . "')), ";
				$query .= "ucase(trim('" . $_POST['email'] . "')), ";
				$query .= "ucase(trim('" . $_POST['cedula'] . "')), ";
				$query .= "ucase(trim('" . $_POST['nombres'] . "')), ";
				$query .= "ucase(trim('" . $_POST['apellidos'] . "')), ";
				$query .= "ucase(trim('" . $_POST['dir'] . "')), ";
				$query .= "ucase(trim('" . $_POST['telefono'] . "')), ";
				$query .= "ucase(trim('" . $_POST['sexo'] . "')), ";
				if (strlen($_POST['hijo']) == 0) $query .= "'0', ";
				else $query .= trim($_POST['hijo']) . ", ";
				$query .= "ucase(trim('" . $_POST['gerencia'] . "')), ";
				$query .= "ucase(trim('" . $_POST['departamento'] . "')), ";
				$query .= "ucase(trim('" . $_POST['cargo'] . "')), ";
				$query .= "ucase(trim('" . $_POST['unidad'] . "')), ";
				$query .= "ucase(trim('" . $_POST['nomina'] . "')), ";
				$query .= "ucase(trim('" . $_POST['turno'] . "')), ";
				$query .= "ucase(trim('" . $_POST['banco'] . "')), ";
				$query .= "ucase(trim('" . $fnac . "')), ";
				$query .= "ucase(trim('" . $fing . "')), ";
				$query .= "ucase(trim('" . $fegr . "')), ";
				$query .= "ucase(trim('" . $_POST['cuenta'] . "')), ";
				$query .= "'" . $_POST['smes'] . "', ";
				$query .= "ucase(trim('" . $_POST['division'] . "')), ";
				$query .= "ucase(trim('" . $_POST['sector'] . "')), ";
				$query .= "ucase(trim('" . $_POST['estado'] . "')), ";
				$query .= "ucase(trim('" . $_POST['pago'] . "')), ";
				$query .= "ucase(trim('" . $_POST['parroquia'] . "')), ";
				$query .= "ucase(trim('" . $_POST['nacionalidad'] . "')), ";
				$query .= "ucase(trim('" . $_POST['municipio'] . "')), ";
				$query .= "ucase(trim('" . $_POST['area'] . "')), ";
				$query .= "ucase(trim('" . $_POST['edocivil'] . "')), ";
				$query .= "ucase(trim('" . $_POST['tipo'] . "')), ";
				$query .= "ucase(trim('" . $_POST['profesion'] . "')), ";
				$query .= "ucase(trim('" . $_POST['direccion'] . "')), ";
				$query .= "ucase(trim('" . $_POST['sso'] . "')), ";
				$query .= "ucase(trim('" . $_POST['status'] . "')))";
				$consulta = mysqli_query($enlace, $query) or die("no se pudo ingresar los datos..." . mysqli_error($enlace) . $query);

				$consulta = "select * from personal_sueldo where codigo='" . $_POST['cedula'] . "' and fec='" . $fing . "' and id='" . $_SESSION['id_validar'] . "' ";
				$resultado = mysqli_query($enlace, $consulta) or die($consulta . mysqli_error($enlace));
				if (mysqli_num_rows($resultado) == 0) {
					$query = "insert into personal_sueldo  ( id, codigo, fec, sueldo) values('" . $_SESSION['id_validar'] . "', ucase(trim('" . $_POST['cedula'] . "')), '" . $fing . "', '" . $_POST['smes'] . "') ";
					$consulta = mysqli_query($enlace, $query) or die("no se pudo ingresar los datos..." . mysqli_error($enlace) . $query);
				} else {
					$query = "update personal_sueldo  set sueldo='" . $_POST['smes'] . "' where codigo='" . $_POST['cedula'] . "' and fec='" . $fing . "' and id='" . $_SESSION['id_validar'] . "' ";
					$consulta = mysqli_query($enlace, $query) or die("no se pudo ingresar los datos..." . mysqli_error($enlace) . $query);
				}
			}
		}
	}

	///////////////////////////////////////////////// actualizar registros  ////////////////////////////////////////////////////////////////
	if ($_POST['modifica'] == 1) {
		$consulta3 = mysqli_query($enlace, "select * from periodo where nomina='" . substr($_POST['nomina'], 0, 3) . "' and id='" . $_SESSION['id_validar'] . "' order by periodo DESC limit 1");
		if (mysqli_num_rows($consulta3) != 0) {
			$row3 = mysqli_fetch_array($consulta3);
			if ($row3['pre'] == 1) $nograbar = 1;
			else $nograbar = 0;
		}
		if ($nograbar == 1)
			echo "<center><font color=red>" . ($nograbar == 1 ? "No se puede procesar al trabajador porque la Nómina esta en Pre-Cierre" : "") . "</font></center>";
		else {

			$fnac = crear_fecha($_POST['fecnac']);
			$fing = crear_fecha($_POST['fecing']);
			$fegr = crear_fecha($_POST['fecegr']);
			$flic = crear_fecha($_POST['fechalic']);
			$fcar = crear_fecha($_POST['fechacar']);
			$fsal = crear_fecha($_POST['fechasal']);
			$fman = crear_fecha($_POST['fechaman']);

			$query = "update personal set ";
			$query .= "clave = ucase(trim('" . $_POST['clave'] . "')), ";
			$query .= "contrato = ucase(trim('" . $_POST['contrato'] . "')), ";
			$query .= "nrocontrato = ucase(trim('" . $_POST['nrocontrato'] . "')), ";
			$query .= "mensaje = ucase(trim('" . $_POST['email'] . "')), ";
			$query .= "nombres = ucase(trim('" . $_POST['nombres'] . "')), ";
			$query .= "apellidos = ucase(trim('" . $_POST['apellidos'] . "')), ";
			$query .= "nombres1 = ucase(trim('" . $_POST['nombres1'] . "')), ";
			$query .= "apellidos1 = ucase(trim('" . $_POST['apellidos1'] . "')), ";
			$query .= "dir = ucase(trim('" . $_POST['dir'] . "')), ";
			$query .= "telefono = ucase(trim('" . $_POST['telefono'] . "')), ";
			$query .= "sexo = ucase(trim('" . $_POST['sexo'] . "')), ";
			if (strlen($_POST['hijo']) == 0) $query .= "hijo = '0', ";
			else $query .= "hijo = trim('" . $_POST['hijo'] . "'), ";
			$query .= "gerencia = ucase(trim('" . $_POST['gerencia'] . "')), ";
			$query .= "departamento = ucase(trim('" . $_POST['departamento'] . "')), ";
			$query .= "cargo = ucase(trim('" . $_POST['cargo'] . "')), ";
			$query .= "unidad = ucase(trim('" . $_POST['unidad'] . "')), ";
			$query .= "nomina = ucase(trim('" . $_POST['nomina'] . "')), ";
			$query .= "turno = ucase(trim('" . $_POST['turno'] . "')), ";
			$query .= "banco = ucase(trim('" . $_POST['banco'] . "')), ";
			$query .= "fechalic = ucase(trim('" . $flic . "')), ";
			$query .= "fechacar = ucase(trim('" . $fcar . "')), ";
			$query .= "fechasal = ucase(trim('" . $fsal . "')), ";
			$query .= "fechaman = ucase(trim('" . $fman . "')), ";
			$query .= "fechai = ucase(trim('" . $fing . "')), ";
			$query .= "fechan = ucase(trim('" . $fnac . "')), ";
			$query .= "fechae = ucase(trim('" . $fegr . "')), ";
			$query .= "cuenta = ucase(trim('" . $_POST['cuenta'] . "')), ";
			$query .= "smes = '" . $_POST['smes'] . "', ";
			$query .= "division = ucase(trim('" . $_POST['division'] . "')), ";
			$query .= "sector = ucase(trim('" . $_POST['sector'] . "')), ";
			$query .= "estado = ucase(trim('" . $_POST['estado'] . "')), ";
			$query .= "pago = ucase(trim('" . $_POST['pago'] . "')), ";
			$query .= "parroquia = ucase(trim('" . $_POST['parroquia'] . "')), ";
			$query .= "nacionalidad = ucase(trim('" . $_POST['nacionalidad'] . "')), ";
			$query .= "municipio = ucase(trim('" . $_POST['municipio'] . "')), ";
			$query .= "area = ucase(trim('" . $_POST['area'] . "')), ";
			$query .= "edocivil = ucase(trim('" . $_POST['edocivil'] . "')), ";
			$query .= "tipo = ucase(trim('" . $_POST['tipo'] . "')), ";
			$query .= "profesion = ucase(trim('" . $_POST['profesion'] . "')), ";
			$query .= "direccion = ucase(trim('" . $_POST['direccion'] . "')), ";
			$query .= "sso = ucase(trim('" . $_POST['sso'] . "')), ";
			$query .= "status = ucase(trim('" . $_POST['status'] . "')) ";
			$query .= " where codigo = '" . $_POST['cedula'] . "' and id='" . $_SESSION['id_validar'] . "' ";
			mysqli_query($enlace, $query) or die(mysqli_error($enlace) . $query);
		}
	}

	///////////////////////////////////////////////// eliminar registros ////////////////////////////////////////////////////////////////
	if ($accion == 2) {
		$consulta = "select cedula from historico where cedula='" . $cedula . "' and id='" . $_SESSION['id_validar'] . "' ";
		$resultado = mysqli_query($enlace, $consulta) or die('verificaci&oacute; del c&oacute;digo fall&oacute;: ' . mysqli_error($enlace));
		if (mysqli_num_rows($resultado) != 0) $mensaje = 'Trabajador NO puede eliminarse por tener informaci&oacute;n en Historicos...';
		else {
			$consulta = "delete from personal where codigo='" . $cedula . "' and id='" . $_SESSION['id_validar'] . "' ";
			$resultado = mysqli_query($enlace, $consulta) or die('verificaci&oacute; del c&oacute;digo fall&oacute;: ' . mysqli_error($enlace));
		}
	}

	///////////////////////////////////////////////// mostrar entrada de datos  ////////////////////////////////////////////////////////////////
	if ((!$_POST || $accion == 2 || $_POST['graba'] == 1 || $_POST['modifica'] == 1 || $_POST['bandera'] == 33) && $accion != 3 && $accion != 11) {
		echo "<form name = 'entrada' action = '" . $_SERVER['PHP_SELF'] . "' method = 'POST'  enctype='multipart/form-data'>";
		echo "<input type = 'hidden' name = 'busca' value = '1'>";
		echo "<table cellpadding=2 cellspacing=0 width='500' align='center'>";
		echo "<tr>";
		echo "<td align=right> C&eacute;dula/Nombre/Apellido:</td>";
		echo "<td> <input name='cedula' size = '30' maxlength = '8'></td>";
		echo "</tr>";
		echo "<tr>";
		echo "<td align=right> Ordenado por:</td>";
		echo "<td>";
		echo "	<input type = 'radio' name = 'orden' value = 'ced' checked> cedula";
		echo "	<input type = 'radio' name = 'orden' value = 'nom'> nombre";
		echo "	<input type = 'radio' name = 'orden' value = 'ape'> apellido";
		echo "	<input type = 'radio' name = 'orden' value = 'sso'> Ficha";
		echo "</tr>";
		echo "<tr>";
		echo "<td> </td>";
		echo "<td colspan=3><input type='image' src='img2/boton_buscar.jpg' value='Entrar' ></td>";
		echo "</tr>";
		echo "</table></form>";
		echo "<script language='javascript' type='text/javascript'>document.entrada.cedula.focus();</script>";
		if ($mensaje != '') echo "<br><br><center><font style='color:black; font-size:1em;' face='Verdana' ><b>" . $mensaje . "</b></font></center>";
	}

	///////////////////////////////////////////////// agregar registros ////////////////////////////////////////////////////////////////
	if ($_POST['agrega'] == 1 || $accion == 11) {
		if ($_POST['cedula'] == '') {
			echo "<form name = 'entrada3' action = '" . $_SERVER['PHP_SELF'] . "' method = 'POST'  enctype='multipart/form-data' onsubmit='return validar_cedula(this)'>";
			echo "<input type = 'hidden' name = 'agrega' value = '1'>";
			echo "<table align ='center'>";
			echo "<tr>";
			echo "<td align = 'right'> C&eacute;dula a ingresar: </td>";
			echo "<td align = 'left'> <input type = 'text' name = 'cedula' size = '30' maxlength = '8' onkeyup='verificar_cedula(this.form)'> * </td>";
			echo "<td ><input name='procesar' type = 'submit' value = 'Procesar Registro Nuevo' disabled></td></form>";
			echo "<script language='javascript' type='text/javascript'>document.entrada3.cedula.focus();</script>";
			echo "<form name = 'entrada2' method = 'POST' action = '" . $_SERVER['PHP_SELF'] . "'>";
			echo "<td ><input type = 'submit' value = 'Regresar al Inicio'></td>";
			echo "</tr>";
			echo "</table></form>";
		} else {
			$consulta = "select * from personal where codigo='" . $_POST['cedula'] . "' and id='" . $_SESSION['id_validar'] . "' ";
			$resultado = mysqli_query($enlace, $consulta) or die('verificaci&oacute; del c&oacute;digo fall&oacute;: ' . mysqli_error($enlace));
			$num_rows = mysqli_num_rows($resultado);
			if ($num_rows != 0) {
				echo "<form name = 'entrada' action = '" . $_SERVER['PHP_SELF'] . "' method = 'POST'  enctype='multipart/form-data' onsubmit='return validar_cedula_nombre_apellido(this)'>";
				echo "<input type = 'hidden' name = 'agrega' value = '1'>";
				echo "<table width = '100%' align ='center'>";
				echo "<tr>";
				echo "<td width = '20%' align = 'right'> C&eacute;dula a ingresar: </td>";
				echo "<td width = '36%' align = 'left'> <input type = 'text' name = 'cedula' size = '30' maxlength = '8' > * </td>";
				echo "</tr>";
				echo "</table>";
				echo "<table width = '20%' align ='center'>";
				echo "<tr><td><input type = 'submit' value = 'Procesar'></td></form>";
				echo "<form action = '" . $_SERVER['PHP_SELF'] . "'>";
				echo "<td><input type = 'submit' value = 'Regresar'></td></tr></table></form>";
				echo "<center>registro existente.....</center>";
				echo "	<script language='javascript' type='text/javascript'> document.entrada.cedula.focus(); </script>";
			} else {
				include("calendario/calendario.php");
				echo "<form name = 'entrada' action = '" . $_SERVER['PHP_SELF'] . "' method = 'POST'  enctype='multipart/form-data' onsubmit='return validar_cedula_nombre_apellido(this)'>";
				echo "<input type = 'hidden' name = 'graba' value = '1'>";

				echo "<table rules=none border=0 cellpadding=2 cellspacing=0 align='center' style='border-width: 1px; border-color: 75263C;border-style: solid;'>";

				//=================================================================================================///
				echo "<tr bgcolor='#43bced' height='25'>";
				echo "<td colspan=4 align = center><font color='white'> Datos Personales </font></td>";
				echo "</tr>";

				echo "<tr bgcolor='#F5F6CE'>";
				echo "<td  align = 'right'> C&eacute;dula: </td>";
				echo "<td  align = 'left'> <input type = 'text' name = 'cedula' size = '30' maxlength = '8' readonly value = '" . $_POST['cedula'] . "' readonly> * </td>";
				echo "</tr>";
				echo "<tr bgcolor='#F5F6CE'>";
				echo "<td  align = 'right'> Nombre del Personal: </td>";
				echo "<td  align = 'left'> <input type = 'text' name = 'nombres' size = '30' maxlength = '50' value = '" . $row['nombres'] . "'> * </td>";
				echo "</tr>";
				echo "<tr bgcolor='#F5F6CE'>";
				echo "<td align = 'right'> Apellido del Personal: </td>";
				echo "<td align = 'left'> <input type = 'text' name = 'apellidos' size = '30' maxlength = '50' value = '" . $row['apellidos'] . "'> * </td>";
				echo "</tr>";
				echo "<tr bgcolor='#F5F6CE'>";
				echo "<td align = 'right' > Nro.Ficha de N&oacute;mina: </td>";
				echo "<td align = 'left' > <input type = 'text' name = 'sso' size = '30' maxlength = '30' value = '" . $row['sso'] . "'></td>";
				echo "</tr>";
				echo "<tr bgcolor='#F5F6CE'>";
				echo "<td align = 'right' > Cargo del Personal: </td>";
				echo "<td align = 'left'> <input type = 'text' name = 'pasaporte' size = '30' maxlength = '30' value = '" . $_POST['pasaporte'] . "' > </td>";
				echo "</tr>";
				echo "<tr bgcolor='#F5F6CE'>";
				echo "<td align = 'right' > Email del Personal: </td>";
				echo "<td align = 'left' ><input type = 'text' name = 'email' size = '30' maxlength = '50' value = '" . $row['mensaje'] . "'> </td>";
				echo "</tr>";
				echo "<tr bgcolor='#F5F6CE'>";
				echo "<td align = 'right' > Clave Acceso WEB: </td>";
				echo "<td align = 'left' ><input type = 'text' name = 'clave' size = '10' maxlength = '10' value = '" . $row['clave'] . "'></td>";
				echo "</tr>";
				echo "<tr bgcolor='#F5F6CE'>";
				echo "<td align = 'right' > Fecha de Ingreso (ddmmaaaa): </td>";
				echo "<td align = 'left'>";
				escribe_formulario_fecha_vacio("fecing", "entrada");
				echo "</td>";
				echo "</tr>";
				echo "<tr bgcolor='#F5F6CE'>";
				echo "<td align = 'right'> Nro. de Horas Trabajados: </td>";
				echo "<td align = 'left'> <input type = 'text' name = 'hijo' size = '30' maxlength = '30' value = '" . $row['hijo'] . "'></td>";
				echo "</tr>";
				echo "<tr bgcolor='#F5F6CE'>";
				echo "<td align = 'right' > Fecha de Egreso: </td>";
				echo "<td align = 'left'>";
				escribe_formulario_fecha_vacio("fecegr", "entrada");
				echo "</td>";
				echo "</tr>";

				echo "</table>";
				echo "<br><br>";
				echo "<table width = '20%' align ='center'>";
				echo "<tr><td><input type = 'submit' value = 'Grabar Nuevo Registro?' " . (substr($_SESSION['acceso_validar'], 16, 1) == '1' ? '' : 'disabled') . "></td></form>";
				echo "<form name = 'regresar' method = 'POST' action = '" . $_SERVER['PHP_SELF'] . "'>";
				echo "<td><input type = 'submit' value = 'Regresar al Inicio'></td></tr></form>";
				echo "	<script language='javascript' type='text/javascript'>";
				echo "		document.entrada.nombres.focus()";
				echo "	</script>";
			}
		}
	}

	///////////////////////////////////////////////// modificar registros ////////////////////////////////////////////////////////////////
	if ($accion == 3) {
		$consulta = "select * from personal where codigo='" . $cedula . "' and id='" . $_SESSION['id_validar'] . "' ";
		$resultado = mysqli_query($enlace, $consulta) or die('verificaci&oacute; del c&oacute;digo fall&oacute;: ' . mysqli_error($enlace));
		$num_rows = mysqli_num_rows($resultado);
		if ($num_rows != 0) {
			include("calendario/calendario.php");
			$row = mysqli_fetch_array($resultado);
			echo "<center><img src='img2/administrator.png'><font color=black size=5>&nbsp;" . trim($row['nombres']) . " " . trim($row['nombres1']) . ' ' . trim($row['apellidos']) . " " . trim($row['apellidos1']) . "</font></center><br>";
			echo "<form name = 'entrada' action = '" . $_SERVER['PHP_SELF'] . "' method = 'POST'  enctype='multipart/form-data' onsubmit='return validar_cedula_nombre_apellido(this)'>";
			echo "<input type = 'hidden' name = 'modifica' value = '1'>";

			echo "<table rules=none border=0 cellpadding=2 cellspacing=0 align='center' style='border-width: 1px; border-color: 75263C;border-style: solid;'>";
			//=================================================================================================///
			echo "<tr bgcolor='#43bced' height='25'>";
			echo "<td colspan=4 align = center><font color='white'> Datos Personales </font></td>";
			echo "</tr>";

			echo "<tr bgcolor='#F5F6CE'>";
			echo "<td  align = 'right'> C&eacute;dula: </td>";
			echo "<td  align = 'left'> <input type = 'text' name = 'cedula' size = '30' maxlength = '8' readonly value = '" . $row['codigo'] . "' readonly> * </td>";
			echo "</tr>";
			echo "<tr bgcolor='#F5F6CE'>";
			echo "<td  align = 'right'> Nombre del Personal: </td>";
			echo "<td  align = 'left'> <input type = 'text' name = 'nombres' size = '30' maxlength = '50' value = '" . $row['nombres'] . "'> * </td>";
			echo "</tr>";
			echo "<tr bgcolor='#F5F6CE'>";
			echo "<td align = 'right'> Apellido del Personal: </td>";
			echo "<td align = 'left'> <input type = 'text' name = 'apellidos' size = '30' maxlength = '50' value = '" . $row['apellidos'] . "'> * </td>";
			echo "</tr>";
			echo "<tr bgcolor='#F5F6CE'>";
			echo "<td align = 'right' > Fecha de Ingreso (ddmmaaaa): </td>";
			echo "<td align = 'left'>";
			$valor = (unir_fecha($row['fechai']) == '00000000' ? '' : unir_fecha($row['fechai']));
			escribe_formulario_fecha_modifica("fecing", "entrada", $valor);
			echo "</td>";
			echo "</tr>";
			echo "<tr bgcolor='#F5F6CE'>";
			echo "<td align = 'right'> Nro. de Horas Trabajadas: </td>";
			echo "<td align = 'left'> <input type = 'text' name = 'hijo' size = '30' maxlength = '30' value = '" . $row['hijo'] . "'></td>";
			echo "</tr>";
			echo "<tr bgcolor='#F5F6CE'>";
			echo "<td align = 'right' > Nro.Ficha de N&oacute;mina: </td>";
			echo "<td align = 'left' > <input type = 'text' name = 'sso' size = '30' maxlength = '30' value = '" . $row['sso'] . "'></td>";
			echo "</tr>";
			echo "<tr bgcolor='#F5F6CE'>";
			echo "<td align = 'right' > Cargo del Personal: </td>";
			echo "<td align = 'left'> <input type = 'text' name = 'pasaporte' size = '30' maxlength = '30' value = '" . $row['pasaporte'] . "' > </td>";
			echo "</tr>";
			echo "<tr bgcolor='#F5F6CE'>";
			echo "<td align = 'right' > Email del Personal: </td>";
			echo "<td align = 'left' ><input type = 'text' name = 'email' size = '30' maxlength = '50' value = '" . $row['mensaje'] . "'> </td>";
			echo "</tr>";
			echo "<tr bgcolor='#F5F6CE'>";
			echo "<td align = 'right' > Clave Acceso WEB:</td>";
			echo "<td align = 'left' ><input type = 'text' name = 'clave' size = '10' maxlength = '10' value = '" . $row['clave'] . "'></td>";
			echo "</tr>";
			echo "<tr bgcolor='#F5F6CE'>";
			echo "<td align = 'right' > Fecha de Egreso: </td>";
			echo "<td align = 'left'>";
			$valor = (unir_fecha($row['fechae']) == '00000000' ? '' : unir_fecha($row['fechae']));
			escribe_formulario_fecha_modifica("fecegr", "entrada", $valor);
			echo "</td>";
			echo "</tr>";


			echo "</table><br><br>";

			echo "<table width = '20%' align ='center'>";
			echo "<tr><td><input type = 'submit' value = 'Grabar Cambios a Modificar?' " . (substr($_SESSION['acceso_validar'], 17, 1) == '1' ? '' : 'disabled') . "></td></form>";
			echo "<form name = 'regresar' method = 'POST' action = '" . $_SERVER['PHP_SELF'] . "'>";
			echo "<td><input type = 'submit' value = 'Regresar al Inicio'></td></tr></form>";
			echo "	<script language='javascript' type='text/javascript'>";
			echo "		document.entrada.nombres.focus()";
			echo "	</script>";
		}
	}

	////////////////////////////////////////// busca por filtros en tabla de registros ////////////////////////////////////////////////////////////////
	if ((!$_POST || $_POST['busca'] == 1 || $_POST['graba'] == 1 || $_POST['modifica'] == 1 || $_POST['bandera'] == 3 || $_POST['bandera'] == 33) && $accion != 3 && $accion != 11) {

		if ($_POST['modifica'] == 1) $_POST['cedula'] == '';

		if ($_POST['busca'] == 1) {
			echo "<form name = 'entrada' action = '" . $_SERVER['PHP_SELF'] . "' method = 'POST'  enctype='multipart/form-data'>";
			echo "<input type = 'hidden' name = 'busca' value = '1'>";
			echo "<table cellpadding=2 cellspacing=0 width='500' align='center'>";
			echo "<tr>";
			echo "<td align=right> C&eacute;dula/Nombre/Apellido:</td>";
			echo "<td> <input name='cedula' size = '30' maxlength = '8'></td>";
			echo "</tr>";
			echo "<tr>";
			echo "<td align=right> Ordenado por:</td>";
			echo "<td>";
			echo "	<input type = 'radio' name = 'orden' value = 'ced' checked> cedula";
			echo "	<input type = 'radio' name = 'orden' value = 'nom'> nombre";
			echo "	<input type = 'radio' name = 'orden' value = 'ape'> apellido";
			echo "	<input type = 'radio' name = 'orden' value = 'sso'> Ficha";
			echo "</tr>";
			echo "</table>";
			echo "	<script language='javascript' type='text/javascript'>";
			echo "		document.entrada.cedula.focus()";
			echo "	</script>";
			echo "<table border=0  align = 'center'><tr>";
			echo "<td colspan=3><input type='image' src='img2/boton_buscar.jpg' value='Entrar' ></td>";
			echo "</form></table>";
		}

		$query1 = "";
		if ($_POST['cedula'] != "") {
			$query1 = " and (codigo like '%" . $_POST['cedula'] . "%' or nombres like '%" . $_POST['cedula'] . "%' or apellidos like '%" . $_POST['cedula'] . "%') ";
		}
		if ($_POST['orden'] == "ced") $query2 = " order by codigo";
		else if ($_POST['orden'] == "nom") $query2 = " order by nombres";
		else if ($_POST['orden'] == "ape") $query2 = " order by apellidos";
		else if ($_POST['orden'] == "sso") $query2 = " order by sso";
		else $query2 = " order by nombres";

		$query3 = '';
		if ($_SESSION['nom01_validar'] != '') $query3 = " and nomina!='" . $_SESSION['nom01_validar'] . "' ";

		$consulta = "select * from personal where status<>'I' and id='" . $_SESSION['id_validar'] . "' " . $query1 . $query3 . $query2;
		$resultado = mysqli_query($enlace, $consulta) or die('verificaci&oacute; del c&oacute;digo fall&oacute;: ' . mysqli_error($enlace));
		$num_rows = mysqli_num_rows($resultado);
		if ($num_rows != 0) {
			echo "<table rules=none border=0 cellpadding=2 cellspacing=0 align='center'>";
			echo "<tr bgcolor='#43bced' height='35'>";
			echo "<th align = center><font color='white'> # </font></th>";
			echo "<th align = center><font color='white'> Foto </font></th>";
			echo "<th align = left><font color='white'> C&eacute;dula </font></th>";
			echo "<th align = left><font color='white'> Nombres </font></th>";
			echo "<th align = left><font color='white'> Apellidos </font></th>";
			echo "<th align = left><font color='white'> Ficha </font></th>";
			echo "<th align = center><font color='white'> Cargo </font></th>";
			echo "<th align = center><font color='white'> Ingreso </font></th>";
			echo "<th colspan = 2 align = center><font color='white'> Acci&oacute;n </font></th>";
			for ($cont = 0; $cont < $num_rows; $cont++) {
				$row = mysqli_fetch_array($resultado);
				$cedula = $row['codigo'];
				if ($color) {
					$tcolor = "#dfdfd0";
					if (file_exists('fotos/' . $cedula . '.jpg')) $imagen = 'fotos/' . $cedula . '.jpg';
					else $imagen = 'img2/nohay2.jpg';
				} else {
					$tcolor = "white";
					if (file_exists('fotos/' . $cedula . '.jpg')) $imagen = 'fotos/' . $cedula . '.jpg';
					else $imagen = 'img2/nohay.jpg';
				}
				echo '<tr bgcolor=' . $tcolor . ' >';
				echo "<td width='20'><font color=red>" . ($cont + 1) . "</font></td>";
				echo "<td align'center'><a href='" . $_SERVER['PHP_SELF'] . "?accion=33&cedula=" . $cedula . "'><img src='" . $imagen . "' height='64' width='64'></a></td>";
				echo "<td width='100'><a href='" . $_SERVER['PHP_SELF'] . "?accion=3&cedula=" . $cedula . "'>" . $row['codigo'] . "</a></td>";
				echo "<td width='250'>" . trim($row['nombres']) . " " . $row['nombres1'] . "</td>";
				echo "<td width='250'>" . trim($row['apellidos']) . " " . $row['apellidos1'] . "</td>";
				echo "<td width='100'>" . $row['sso'] . "</td>";
				echo "<td width='100'>" . $row['pasaporte'] . "</td>";
				echo "<td width='120' align=center>" . mostrar_fecha($row['fechai']) . "</td>";

				if (substr($_SESSION['acceso_validar'], 18, 1) == '1')
					echo "<td width='30' align='center'><a onclick='return validar_elimina()' href='" . $_SERVER['PHP_SELF'] . "?accion=2&cedula=" . $cedula . "'><img  border = 0  src='img2/icono_07.png'alt='eliminar éste registro' ></a></td>";
				else echo "<td width='30' align='center'>--</td>";

				if (substr($_SESSION['acceso_validar'], 17, 1) == '1')
					echo "<td width='30' align='center'><a href='" . $_SERVER['PHP_SELF'] . "?accion=3&cedula=" . $cedula . "'><img border = 0 src='img2/icono_01.png' alt='modificar éste registro'></a></td>";
				else echo "<td width='30' align='center'>--</td>";

				echo '</tr>';
				$color = !$color;
			}
			echo '</table>';
		} else echo "<hr>";
		mysqli_free_result($resultado);
	}
} //=========================== CARGAR FOTOS=================================	
mysqli_close($enlace);
echo "</font>";
echo "</body>";
echo "</html>";
