<?php

include("../bd/sql_variables.php");

$con= mysqli_connect("$sql_host", "$sql_usuario", "$sql_pass") or die(header ("No Disponible Servidor de Base de Datos"));
mysqli_select_db($con,"$sql_db");

$idj = $_GET['idj'];
$ugid = $_GET['ugid'];
$ids=0;
//$idj='1';
//$ids='2';
//nombre de la jefatura
$comboservicio ="<select id='serv1' name='serv1' onChange='listarpersonalterm1()'>";
$comboservicio =$comboservicio."<option value='-1' selected >TODOS</option>";
$sqljefatura = "select j_desc as nom from tb_jefatura where j_id='$idj'";
$resultjefatura = mysqli_query($con, $sqljefatura);
while ($rowj= mysqli_fetch_array($resultjefatura)){
$nomjefatura = $rowj['nom']; 
$comboservicio =$comboservicio."<option value='0'";
//if($_GET['ids']==0){ $comboservicio = $comboservicio." selected ";}
$comboservicio =$comboservicio.">".$nomjefatura."</option>";
}
//nombre de servicio
$sqlserv = "select s_id, s_desc as nom from tb_servicio where j_id='$idj'";
$resultserv = mysqli_query($con, $sqlserv);
while ($rows= mysqli_fetch_array($resultserv)){
$nomservicio = $rows['nom']; 
$idso = $rows['s_id']; 
$comboservicio =$comboservicio."<option value='".$idso."'";
//if($_GET['ids']==$idso){ $comboservicio = $comboservicio." selected ";}
$comboservicio =$comboservicio.">".$nomservicio."</option>";
}
$comboservicio =$comboservicio."</select>";

//seleccionando al combo tipo de personal
$sqltpersonal = "SELECT tp_id as cod, (select tp_desc from tb_tipoperson where tp_id=b.tp_id) as descripcion  FROM tb_usuasist a inner join tb_usudep b on a.ug_id=b.ug_id where b.j_id='$idj' group by b.tp_id";
$resulttpersonal = mysqli_query($con, $sqltpersonal);
$cadenatp="<select id='tipopersonal' name='tipopersonal' onChange='listarpersonalterm1();'><option value='0' selected>TODOS</option>";
while ($rowtp= mysqli_fetch_array($resulttpersonal)){
$cadenatp=$cadenatp."<option value='".$rowtp['cod']."'>".$rowtp['descripcion']."</option>";
}
$cadenatp=$cadenatp."</select>";

//seleccionando al comnbo especialidad
$sqlespecialidad = "SELECT b.ua_idespec as cod, (select esp_desc from tb_especialidad where ua_idespec=b.ua_idespec) as descripcion  FROM  tb_usudep b  where b.j_id='$idj' group by b.ua_idespec";
$resultespecialidad = mysqli_query($con, $sqlespecialidad);
$cadenaesp="<select id='especialidad' onChange='listarpersonalterm1()'><option value='0' selected>TODOS</option>";
while ($rowe= mysqli_fetch_array($resultespecialidad)){
$cadenaesp=$cadenaesp."<option value='".$rowe['cod']."'>".$rowe['descripcion']."</option>";
}
$cadenaesp=$cadenaesp."</select>";

//seleccionando al comnbo tipo de contrato
$sqlespecialidad = "SELECT b.tc_idcont as cod, (select tc_abrev from tb_tipocont where tc_idcont=b.tc_idcont) as tipocont  FROM tb_usudep b  where b.j_id='$idj' group by b.tc_idcont";
$resultespecialidad = mysqli_query($con, $sqlespecialidad);
$cadenatc="<select id='tcontrato' onChange='listarpersonalterm1()'><option value='0' selected>TODOS</option>";
while ($rowe= mysqli_fetch_array($resultespecialidad)){
$cadenatc=$cadenatc."<option value='".$rowe['cod']."'>".$rowe['tipocont']."</option>";
}
$cadenatc=$cadenatc."</select>";

//construytento listado
$sqllistado = "select a.ug_id as cod, concat(a.ua_apepat,' ',a.ua_apemat,' ',a.ua_nom) as nombre, (select s_abrev from tb_servicio where s_id=b.s_id and j_id=b.j_id ) as servicio,(select esp_desc from tb_especialidad ";
$sqllistado = $sqllistado." where ua_idespec=b.ua_idespec) as especialidad, date_format(b.ud_fechaing,'%d/%m/%Y') as fingreso, (datediff(now(), b.ud_fechaing)) div 365 as tiempo, (select tc_abrev from tb_tipocont ";
$sqllistado = $sqllistado." where tc_idcont=b.tc_idcont) as tipocontrato, (select tp_desc from tb_tipoperson where tp_id=b.tp_id) as tpersona from tb_usuasist a inner join tb_usudep b on ";
$sqllistado = $sqllistado." a.ug_id = b.ug_id where b.j_id='$idj' and b.s_id='$ids' and b.ua_estado in (1,2,4) order by nombre, especialidad";
//$sqllistado = $sqllistado." a.ug_id = b.ug_id where b.j_id='$idj' and b.s_id='$ids' and b.tp_id=1 and a.ua_idespec='2'";
//echo $sqllistado;
$resultlistado = mysqli_query($con, $sqllistado);
$sumap=0;
$a=1;
$muestra="";
$cadenalis="";
while ($rowl= mysqli_fetch_array($resultlistado)){
$codigo=$rowl['cod'];
//echo $codigo."<br>";
$muestra = $muestra."<input type='hidden' id='codper".$a."'  name='codper".$a."' value='".$codigo."'>";
$cadenalis=$cadenalis."<tr><td align='left'>".$rowl['nombre']."</td><td align='center'>".$rowl['servicio']."</td><td align='center'>".$rowl['tpersona']."</td><td align='center'>".$rowl['especialidad']."</td><td align='center'>".$rowl['tipocontrato']."</td><td align='center'>".$rowl['fingreso']."</td><td align='center'>".$rowl['tiempo']."</td><td align='center'><a href='#' onClick='verdescansomedico($a)'><img src='img/heart_red.png' width='17' height='17' border='0' ></a></td><td align='center'><a href='#' onClick=verdetallevacaprog($a);><img src='img/eye.png' border='0'></a></td><td align='center'><a href='#' onClick=datospersonal($a);><img src='img/book.png' width='17' height='17' id='person' border='0' ><a/></td><td align='center'><a href='#' onClick=askprogramacion($a);><img src='img/calendar.png' width='17' height='17' id='person' border='0' ></a></td></tr>";
$sumap=$sumap+1;
$a=$a+1;
}
$cadenalis=$cadenalis."<tr><td colspan='12' align='center'>El numero de Personal es ".$sumap."</td></tr>";
?>

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

<link rel="stylesheet" type="text/css" href="styles/Style.css">
<script language="JavaScript" type="text/javascript" src="ajax.js"></script>

<script type="text/javascript"> 
function agregarpersonal(){
	var idj = document.getElementById("depa").value;
	var ids = document.getElementById("serv").value;
    var ugid = document.getElementById("ugid").value;
   window.open("adminpersonal.php?idj="+idj+"&ids="+ids+"&ugid="+ugid,"","height=400,width=600,scrollbars=NO");
}
function verdetallevacaprog(a){
	var codigo = document.getElementById("codper"+a).value;
	var idj = document.getElementById("depa").value;
	var ids = document.getElementById("serv").value;
    var ugid = document.getElementById("ugid").value;
   window.open("verdetvacacionesprog.php?ugid="+ugid+"&idj="+idj+"&ids="+ids+"&codigo="+codigo,"","height=400,width=600,scrollbars=NO");
}

function datospersonal(a){
	var codigo = document.getElementById("codper"+a).value;
	var idj = document.getElementById("depa").value;
	var ids = document.getElementById("serv").value;
	window.open("vistapersonal.php?ugid="+codigo+"&idj="+idj+"&ids="+ids,"","height=410,width=390,scrollbars=NO");
} 
function askprogramacion(a){
	var codigo = document.getElementById("codper"+a).value;
	var idj = document.getElementById("depa").value;
	var ids = document.getElementById("serv").value;
	window.open("consultaprog.php?ugid="+codigo+"&idj="+idj+"&ids="+ids,"","height=310,width=390,scrollbars=NO");
}
function verdescansomedico(a){
    //alert(a);
	var codigo = document.getElementById("codper"+a).value;
	
	window.open("verdmedicoprog.php?ugid="+codigo,"","height=200,width=200,scrollbars=NO");
} 

//function verdescansomedico(codigo){
    //var codigox = codigo;
    //alert(codigox);
	//var codigo = document.getElementById("codper"+a).value;
	//window.open("verdmedicoprog.php?ugid="+codigo,"","height=200,width=200,scrollbars=NO");
//} 
function editarpersonal(codigo){
		var idj = document.getElementById("depa").value;
		var ids = document.getElementById("serv").value;
		window.open("modificarpersonal.php?ugid="+codigo+"&idj="+idj+"&ids="+ids,"","height=400,width=600,scrollbars=NO");
} 

function retornarmenu(){
		var idj = document.getElementById("depa").value;
		var ids = document.getElementById("serv").value;
        var ugid = document.getElementById("ugid").value;		
		document.location= "adminterminal.php?idj="+idj+"&ugid="+ugid;
} 
</script> 
</head>
<body id="lista" name="lista">
<?php
echo "<input type='hidden' id='depa' name='depa' value='".$idj."'>";
echo "<input type='hidden' id='serv'  name='serv' value='".$ids."'>";
echo "<input type='hidden' id='ugid'  name='ugid' value='".$ugid."'>";
?>
<table align='center'>
<tr><td align='left'><font color='blue'>Seleccione Servicio:</font></td></tr>
<tr><td align='center'><?php echo $comboservicio;?></td></tr>
<tr><td>

<fieldset>
	<legend align='left'><font color='blue'>Listado del Personal Asistencial</font></legend>
	<br>
	<table align='center' ><tr><td align='left'>Tipo de Personal</td>
	<td><div id='divtp'><?php echo $cadenatp;?></div></td></tr>
	<tr><td align='left'>Especialidad</td>
	<td><div id='divesp'><?php echo $cadenaesp;?></div></td>
	<td align='center'></td><td width='50'></td><td><img src='../img/home.png' width='70' height='60' onClick='retornarmenu()' alt='Regresar al Menu Principal'></td>
	</tr>
	<tr><td align='left'>Tipo de Contrato</td>
	<td><?php echo $cadenatc;?></td><td><img src='img/icoexcel.png' width='27' height='27' onClick='listarpersonalxls();' alt='Exportar al Excel'></td></tr>
	<tr><td align='left'>Busqueda por Apellido </td><td><input type='text' size='40' id='nombre' onKeyPress='buscarpersonal()'></td></tr>
	</table>
	<br>
	<div id='admlista'>
	<table align='center' class='FacetFormTABLE'>
	<tr><td colspan='12'class='FacetFieldCaptionTDT' align='center'> PERSONAL ASISTENCIAL </td></tr>
	<tr><td class='FacetFieldCaptionTD' align='center' width='250'>APEDLLIDOS Y NOMBRES</td><td class='FacetFieldCaptionTD' align='center' width='100'>SERVICIO</td><td class='FacetFieldCaptionTD' align='center' width='130'>TIPO PERSONAL</td><td class='FacetFieldCaptionTD' align='center' width='130'>ESPECIALIDAD</td><td class='FacetFieldCaptionTD' align='center' width='130'>TIPO DE CONTRATO</td><td class='FacetFieldCaptionTD' align='center' width='100'>FECHA DE INGRESO</td><td class='FacetFieldCaptionTD' align='center' width='70'>PERIOD. LAB.</td><td class='FacetFieldCaptionTD' align='center'>DesMed</td><td class='FacetFieldCaptionTD' align='center'>VACAC</td><td class='FacetFieldCaptionTD' align='center'>DATOS</td><td class='FacetFieldCaptionTD' align='center'>PROG</td></tr>
	
	<?php echo $cadenalis; echo $muestra;?>
	
	</table>
	</div>
	<br>
</fieldset>
</td></tr>
</table>
</body>
</html>