<? 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");
$enlace = cargar_conexion();
$accion = $_REQUEST["accion"];
$cedula = $_REQUEST["cedula"];
$nombre = $_REQUEST["nombre"];
$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 ($_POST['grabar'] == 1) {
	if ($_POST['tiempo'] == '') $_POST['tiempo'] = 0;
	$consulta = "insert into ficha_referencia (id, cedula,nombre,parentesco,telefono,tiempo)  values ('" . $_SESSION['id_validar'] . "',";
	$consulta .= "'" . $_POST['cedula'] . "','" . $_POST['nombre'] . "','" . $_POST['parentesco'] . "','" . $_POST['telefono'] . "','" . $_POST['tiempo'] . "')";
	$resultado = mysqli_query($enlace, $consulta) or die('verificaci&oacute; del c&oacute;digo fall&oacute;: ' . mysqli_error($enlace));
}
///////////////////////////////////////////////// grabar vacaciones ////////////////////////////////////////////////////////////////
if ($accion == 2) {
	$consulta = "delete from ficha_referencia where id='" . $_SESSION['id_validar'] . "' and cedula='" . $cedula . "' and nombre='" . $nombre . "'";
	$resultado = mysqli_query($enlace, $consulta) or die('verificaci&oacute; del c&oacute;digo fall&oacute;: ' . mysqli_error($enlace));
}

/////////////////////////////////////////////////MOSTRAR SOLICITUD DE CEDULA ////////////////////////////////////////////////////////////////
if ((!$_POST || $_POST['busca'] == 1) && ($accion != 5) && ($accion != 2) && ($_POST['grabar'] != 1)) {
	echo "<b>Opciones:</b> &nbsp;&nbsp;<img border = 0 src='img2/icono_01.png' alt='Seleccionar'>&nbsp;Seleccionar Trabajador";
	echo "<hr><h2>Actualizaci&oacute;n de Ficha de Referencias del Trabajador</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' 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'> Operador";
	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' value = 'Buscar Registros'></td>";
	echo "</tr></table></form>";

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

	$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 bgcolor='#43bced' height='35'>";
		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'> Status </font></th>";
		echo "<th align = center><font color='white'> Ingreso </font></th>";
		echo "<th colspan = 4 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 align'center'><img src='" . $imagen . "' height='64' width='64'></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>";
			echo "<td width='50' align='center'>" . ($row['status'] == 'A' ? '<img src="img2/status_ver.png">' : '<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/icono_01.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 || $_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 de Referencias 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='60%' 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 de Referencias </font></td>";
		echo "</tr>";

		echo "<tr><td align = 'right' > Nombre y Apellido de la Referencia? </td>";
		echo "<td align = 'left' > <input type = 'text' name = 'nombre' size = '50' maxlength = '100' ></td>";
		echo "</tr>";
		echo "<tr><td align = 'right' > Parentesco? </td>";
		echo "<td align = 'left' > <input type = 'text' name = 'parentesco' size = '50' maxlength = '50' ></td>";
		echo "</tr>";
		echo "<tr><td align = 'right' > Telefonos? </td>";
		echo "<td align = 'left' > <input type = 'text' name = 'telefono' size = '50' maxlength = '50' ></td>";
		echo "</tr>";
		echo "<tr><td align = 'right' > Tiempo Conociendolo? </td>";
		echo "<td align = 'left' > <input type = 'text' name = 'tiempo' size = '10' maxlength = '10' ></td>";
		echo "</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.nombre.focus()";
	echo "	</script>";
	echo "</form>";

	if ($cedula != '') {
		$consulta = "select * from ficha_referencia where id='" . $_SESSION['id_validar'] . "' and cedula= '" . $cedula . "' order by tiempo 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 bgcolor='#43bced' height='35'>";
			echo "<th align = left><font color='white'> Tiempo Conociendolo </font></th>";
			echo "<th align = left><font color='white'> Nombre </font></th>";
			echo "<th align = left><font color='white'> Parentesco </font></th>";
			echo "<th align = left><font color='white'> Tel&eacute;fono </font></th>";
			echo "<th colspan =  align = center><font color='white'> ? </font></th>";
			for ($cont = 0; $cont < $num_rows; $cont++) {
				$row = mysqli_fetch_array($resultado);
				$cedula = $row['cedula'];
				if ($color) {
					$tcolor = "#dfdfd0";
				} else {
					$tcolor = "white";
				}
				echo '<tr bgcolor=' . $tcolor . ' >';
				echo "<td width='100' align='center'>" . number_format($row['tiempo'], 0) . "</td>";
				echo "<td width='200'>" . $row['nombre'] . "</td>";
				echo "<td width='200'>" . $row['parentesco'] . "</td>";
				echo "<td width='200'>" . $row['telefono'] . "</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 . "&nombre=" . $row['nombre'] . "'><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);
