<? 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');
include("fphp.php");
/*
$accion=$_REQUEST["accion"];
$cedula=$_REQUEST["cedula"];
$curso=$_REQUEST["curso"];
*/
$enlace = cargar_conexion();
	$accion = array_key_exists('accion', $_REQUEST) ? (int)$_REQUEST["accion"]  : "";
	$codigo = array_key_exists('codigo', $_REQUEST) ? $_REQUEST["codigo"] : "";
	$cedula = array_key_exists('cedula', $_REQUEST) ? $_REQUEST["cedula"] : "";
	$curso = array_key_exists('curso', $_REQUEST) ? $_REQUEST["curso"] : "";

$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);
	$per = mostrar_fecha($row['desde']) . ' al ' . mostrar_fecha($row['hasta']) . " (Nom:" . $row['nomina'] . ")";
} else $per = '';
menu();
set_time_limit(3000);

///////////////////////////////////////////////// grabar vacaciones ////////////////////////////////////////////////////////////////
if (array_key_exists('grabar', $_POST) && $_POST['grabar'] == 1) {
	$fecha1 = crear_fecha($_POST['fecha1']);
	$fecha2 = crear_fecha($_POST['fecha2']);
	if ($_POST['hora'] == '') $_POST['hora'] = 0;
				$consulta = "INSERT INTO ficha_academica
				(id, cedula, curso, titulo, instituto, hora, pais, fdesde, fhasta)
				VALUES
				('".$_SESSION['id_validar']."',
				'".$_POST['cedula']."',
				'".$_POST['curso']."',
				'".$_POST['titulo']."',
				'".$_POST['instituto']."',
				'".$_POST['hora']."',
				'".$_POST['pais']."',
				'".$fecha1."',
				'".$fecha2."')";

				$resultado = mysqli_query($enlace, $consulta)
				or die('verificación falló: ' . mysqli_error($enlace));

}
///////////////////////////////////////////////// grabar vacaciones ////////////////////////////////////////////////////////////////
if ($accion == 2) {
	$consulta = "delete from ficha_academica where id='" . $_SESSION['id_validar'] . "' and cedula='" . $cedula . "' and curso='" . $curso . "'";
	$resultado = mysqli_query($enlace, $consulta) or die('verificaci&oacute; del c&oacute;digo fall&oacute;: ' . mysqli_error($enlace));
}

/////////////////////////////////////////////////MOSTRAR SOLICITUD DE CEDULA ////////////////////////////////////////////////////////////////
if (!$_POST || (array_key_exists('busca', $_POST) && $_POST['busca'] == 1) && ($accion != 5) && ($accion != 2) && (array_key_exists('grabar', $_POST) && $_POST['grabar'] != 1)) {

	/* echo "<b>Opciones:</b> &nbsp;&nbsp;<img border = 0 src='img2/icono_01.png' alt='Seleccionar'>&nbsp;Seleccionar Trabajador"; */
	echo "<h2><center>Actualizaci&oacute;n de Ficha Acad&eacute;mica del Trabajador</center></h2>";

	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 colspan = 3>";
	echo "	<input type = 'radio' name = 'orden' value = 'ced' " . (obtener_valor_diccionario($_POST, 'orden') == 'ced' ? 'checked' : '') . "> cedula";
			echo "	<input type = 'radio' name = 'orden' value = 'nom' " . (obtener_valor_diccionario($_POST, 'orden') == 'nom' ? 'checked' : '') . "> nombre";
			echo "	<input type = 'radio' name = 'orden' value = 'ape' " . (obtener_valor_diccionario($_POST, 'orden') == 'ape' ? 'checked' : '') . "> apellido";
			echo "	<input type = 'radio' name = 'orden' value = 'fechai' " . (obtener_valor_diccionario($_POST, 'orden') == 'fechai' ? 'checked' : '') . "> Fecha Ingreso";
	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><input type = 'submit' class='boton' value = 'Buscar Registros'></td>";
	echo "</tr></table></form>";

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

	$consulta = "select * from personal where id='" . $_SESSION['id_validar'] . "' and status<>'I' " . $query1 . $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 class='encabezadoTabla'>";
		echo "<th>#</th>";
		//echo "<th> Foto </th>";
		echo "<th> C&eacute;dula </th>";
		echo "<th> Nombres </th>";
		echo "<th> Apellidos </th>";
		echo "<th> Ficha </th>";
		echo "<th> Status </th>";
		echo "<th> Fec. Ingreso </th>";
		echo "<th> Acci&oacute;n </th>";
		echo "</tr>";

		for ($cont = 0; $cont < $num_rows; $cont++) {
			$row = mysqli_fetch_array($resultado);
			$cedula = $row['codigo'];
			if ($color) {
				$tcolor = "#CFE7DE";
				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' class='numeroFila'>" . ($cont + 1) . "</td>";
			//echo "<td align'center'><img src='" . $imagen . "' height='64' width='64' style='border-radius:50%;'></td>";
			echo "<td width='100'><a href='" . $_SERVER['PHP_SELF'] . "?accion=5&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>";
			if ($row['status'] == 'A') {
						echo "<td width='50' align='center'><img src=img2/status_ver_doble_zaul.png></td>";
					} elseif ($row['status'] == 'C') {
						echo "<td width='50' align='center'><img src=img2/status_ver.png></td>";
					} elseif ($row['status'] == 'S') {
						echo "<td width='50' align='center'><img src=img2/status_ver_naranja.png></td>";
					}
			echo "<td width='120' align=center>" . $row['fechai'] . "</td>";

			echo "<td width='30' align='center'><a href='" . $_SERVER['PHP_SELF'] . "?accion=5&cedula=" . $cedula . "'><img border = 0 src='img2/modificar_editar.png' alt='Seleccionar éste registro'></a></td>";

			echo '</tr>';
			$color =! $color;
		}
		echo '</table>';
		/* echo "<form name='imprimir' method='post' target ='_blank' action='" . $_SERVER['PHP_SELF'] . "' enctype='multipart/form-data'>";
		echo "  <input type = 'hidden' name = 'imprime' value = '1'>";
		echo "<center><input type = 'submit' value = 'Imprimir Resultado'></center></form>"; */
	} else echo "<hr>";
	mysqli_free_result($resultado);
}

///////////////////////////////////////////////// MOSTRAR DATA A INGRESAR ////////////////////////////////////////////////////////////////
if ($accion == 2 || $accion == 5 || (array_key_exists('grabar', $_POST) && $_POST['grabar'] == 1)) {
	include("calendario/calendario.php");
	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>Actualizaci&oacute;n de Ficha Acad&eacute;mica del Trabajador</h2>";

	echo "<form name = 'fentrada' action = '" . $_SERVER['PHP_SELF'] . "' method = 'POST'  enctype='multipart/form-data'>";
	echo "<input type = 'hidden' name = 'grabar' value = '1'>";

	$consulta = "select * from personal where id='" . $_SESSION['id_validar'] . "' and codigo='" . $cedula . "' ";
	$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) {
		$row = mysqli_fetch_array($resultado);

		echo "<table width='70%' 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=2 align = center><font color='white'> Datos Personales </font></td>";
		echo "</tr>";


		echo "<tr><td width = '20%' align = 'right'> C&eacute;dula: </td>";
		echo "<td width = '36%' align = 'left'> <input type = 'text' name = 'cedula' size = '30' maxlength = '8' value = '" . $row['codigo'] . "' readonly> * </td>";
		echo "</tr>";

		echo "<tr><td width = '15%' align = 'right'> Nombres: </td>";
		echo "<td width = '29%' align = 'left'> <input type = 'text' name = 'nombres' size = '30' maxlength = '50' value = '" . trim($row['nombres']) . " " . $row['nombres1'] . "' readonly> * </td>";
		echo "</tr>";

		echo "<tr><td align = 'right'> Apellidos: </td>";
		echo "<td align = 'left'> <input type = 'text' name = 'apellidos' size = '30' maxlength = '50' value = '" . trim($row['apellidos']) . " " . $row['apellidos1'] . "' readonly> * </td>";
		echo "</tr>";

		echo "<tr><td align = 'right' > Fecha de Ingreso: </td>";
		echo "<td align = 'left' > <input type = 'text' name = 'fec' size = '10' maxlength = '50' value = '" . $row['fechai'] . "' readonly></td>";
		echo "</tr>";

		//=================================================================================================///
		echo "<tr bgcolor='#43bced' height='25'>";
		echo "<td colspan=2 align = center><font color='white'> Datos Acad&eacute;micos </font></td>";
		echo "</tr>";

		echo "<tr><td align = 'right' > Curso? </td>";
		echo "<td align = 'left' > <input type = 'text' name = 'curso' size = '100' maxlength = '100' ></td>";
		echo "</tr>";
		echo "<tr><td align = 'right' > Instituto? </td>";
		echo "<td align = 'left' > <input type = 'text' name = 'instituto' size = '100' maxlength = '100' ></td>";
		echo "</tr>";
		echo "<tr><td align = 'right' > Titulo/Detalle? </td>";
		echo "<td align = 'left' > <input type = 'text' name = 'titulo' size = '100' maxlength = '100' ></td>";
		echo "</tr>";
		echo "<tr><td align = 'right' > Pais/Ciudad? </td>";
		echo "<td align = 'left' > <input type = 'text' name = 'pais' size = '100' maxlength = '100' ></td>";
		echo "</tr>";
		echo "<tr><td align = 'right' > Horas Acad&eacute;micas? </td>";
		echo "<td align = 'left' > <input type = 'text' name = 'hora' size = '10' maxlength = '10' ></td>";
		echo "</tr>";

		echo "<tr><td align = 'right' > Fecha Desde (ddmmaaaa): </td>";
		echo "<td align = 'left'>";
		escribe_formulario_fecha_vacio("fecha1", "fentrada");
		echo "</td></tr>";

		echo "<tr><td align = 'right' > Fecha Hasta (ddmmaaaa): </td>";
		echo "<td align = 'left'>";
		escribe_formulario_fecha_vacio("fecha2", "fentrada");
		echo "</td></tr>";

		echo "<tr><td align = 'right' > </td>";
		echo "<td align = 'left'>";
		echo "<input type = 'submit' value = 'Grabar Registro al Trabajador'>";
		echo "</td></tr>";

		echo "</tr>";
		echo "</table>";
	}
	echo "	<script language='javascript' type='text/javascript'>";
	echo "		document.fentrada.curso.focus()";
	echo "	</script>";
	echo "</form>";

	if ($cedula != '') {
		$consulta = "select * from ficha_academica where id='" . $_SESSION['id_validar'] . "' and cedula= '" . $cedula . "' order by fhasta desc";
		$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 class='encabezadoTabla'>";
			echo "<th> Fecha </th>";
			echo "<th> Curso </th>";
			echo "<th> Titulo </th>";
			echo "<th> Instituto </th>";
			echo "<th> Pais/Ciudad </th>";
			echo "<th> Horas </th>";
			echo "<th> ? </th>";
			echo "</tr>";

			for ($cont = 0; $cont < $num_rows; $cont++) {
				$row = mysqli_fetch_array($resultado);
				$cedula = $row['cedula'];
				if ($color) {
					$tcolor = "#CFE7DE";
				} else {
					$tcolor = "white";
				}
				echo '<tr bgcolor=' . $tcolor . ' >';
				echo "<td width='200'>" . $row['fdesde'] . " al " . $row['fhasta'] . "</td>";
				echo "<td width='200'>" . $row['curso'] . "</td>";
				echo "<td width='200'>" . $row['titulo'] . "</td>";
				echo "<td width='200'>" . $row['instituto'] . "</td>";
				echo "<td width='200'>" . $row['pais'] . "</td>";
				echo "<td width='100' align='center'>" . number_format($row['hora'], 0) . "</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 . "&curso=" . $row['curso'] . "'><img  border = 0  src='img2/icono_07.png'alt='eliminar éste registro' ></a></td>";
				else echo "<td width='30' align='center'>&nbsp;</td>";

				echo '</tr>';
				$color = !$color;
			}
			echo '</table>';
		}
		mysqli_free_result($resultado);
	}
	echo "<br><br><br><br></body>";
	echo "</html>";
}

mysqli_close($enlace);
?>
	<style>
	.numeroFila {
		color: black;
		font-weight: bold;
	}

	.boton {
    border-radius: 8px;      /* Bordes redondeados */
    padding: 8px 16px;       /* Más espacio interno */
    border: none;            /* Quita el borde feo por defecto */
    cursor: pointer;         /* Mano al pasar */
    transition: 0.2s ease;   /* Animación suave */
	}

	.boton:hover {
	transform: scale(1.03);  /* Efecto sutil al pasar el mouse */
	}

/*Encabezado tablas */
	.encabezadoTabla {
    background: #43bced;
    height: 35px;
}

.encabezadoTabla th {
    padding: 8px 12px;
    text-align: left;
    color: white;
    font-weight: bold;
    font-size: 14px;
    letter-spacing: 0.5px;
    border-bottom: 2px solid #2fa3c9;
}

.encabezadoTabla th:nth-child(1),
.encabezadoTabla th:nth-child(2),
.encabezadoTabla th:nth-child(7),
.encabezadoTabla th:nth-child(8) {
    text-align: center;
}
/*hasta aquí Encabezado tablas */

</style>

