<?php

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


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


if(isset($_GET['coduni']) && isset($_GET['idso']) && isset($_GET['codprog'])){
$coduni = $_GET['coduni'];
$codprog = $_GET['codprog'];
$idj=$_GET['idj'];
$ids=$_GET['ids'];
$idso=$_GET['idso'];
$idtp=$_GET['idtp'];
$mm=$_GET['mes'];
$anno=$_GET['anno'];

$sqlask2="select * from tb_usutrab where ug_id='$coduni' and tp_id='$idtp' and uap_anno='$anno' and uap_mes='$mm' and j_id='$idj' and ut_idserva='$ids' ";
$sqlask="select * from tb_usutrab where ut_codprog='$codprog' and tp_id='$idtp' and uap_anno='$anno' and uap_mes='$mm' and j_id='$idj' and ut_idserva='$ids' ";
//echo $sqlask."<br>";
$resultask = mysqli_query($con, $sqlask);
$resultask2 = mysqli_query($con,$sqlask2);
if(mysqli_num_rows($resultask2)==0){
if(mysqli_num_rows($resultask)==0){
$sqliinsert="insert into tb_usutrab values ('$codprog','$idtp','$coduni','$anno','$mm','$idso','$idj','','','$ids')";
//ECHO $sqliinsert."<BR>";
$resultask = mysqli_query($con,$sqliinsert);
echo "<script type='text/javascript'>window.opener.location.reload(); 
 document.location ='../buscarpersonalrepro.php?idtp=$idtp&anno=$anno&mes=$mm&idj=$idj&ids=$ids';
</script>";
}else{
$texto="CODIGO YA EXISTE";
}}else{
$texto="PERSONAL YA AGREGADO";
}
}

if(isset($_GET['coduni'])){
$coduni = $_GET['coduni'];
$idj=$_GET['idj'];
$ids=$_GET['ids'];
$idtp=$_GET['idtp'];
$mm=$_GET['mes'];
$anno=$_GET['anno'];
$sqllistado = "select a.ug_id as cod, concat(a.ua_apepat,' ',a.ua_apemat,' ',a.ua_nom) as nombre, b.tp_id as idtpa, b.s_id as idso, b.j_id as idja, (select j_desc from tb_jefatura where j_id=b.j_id) as jefatura, (select s_desc from tb_servicio where j_id=b.j_id and s_id=b.s_id) as servicio from tb_usuasist a inner join tb_usudep b on ";
$sqllistado = $sqllistado." a.ug_id = b.ug_id where (b.ua_estado= 1 or b.ua_estado= 2) and a.ug_id='$coduni'";
//echo $sqllistado;
$resultlistado = mysqli_query($con,$sqllistado);
while ($row1= mysqli_fetch_array($resultlistado)){
//echo $idtp;
$coduni=$row1['cod'];
$idso=$row1['idso'];
$idja=$row1['idja'];
$idtpa=$row1['idtpa'];
$jefatura=$row1['jefatura'];
$servicio=$row1['servicio'];
$nombre=$row1['nombre'];
$valor =1;
}
}
//echo "servicio ".$servicio;
?>
<html>
<head>
<title>PROGRAMACION DEL PERSONAL ASISTENCIAL</title>
<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 ventanaClose(){ 
   //alert("hola mundo");
    window.close();
}
</script> 

<script type='text/javascript'>
function grabarcodigoper(){
var codprog = document.getElementById('codprog').value;
if(codprog != ''){
var idj = document.getElementById('idj').value;
var ids = document.getElementById('ids').value;
var idtp = document.getElementById('idtp').value;
var mes = document.getElementById('mes').value;
var anno = document.getElementById('anno').value;
var coduni = document.getElementById('coduni').value;
var idso = document.getElementById('idso').value;
var cadena = "agregarnuevoper.php?idj="+idj+"&ids="+ids+"&idtp="+idtp+"&mes="+mes+"&anno="+anno+"&coduni="+coduni+"&idso="+idso+"&codprog="+codprog;
document.location= cadena;
}else{
alert('INGRESE CODIGO DE PROGRAMACION');
}
}

function buscarpersonalrepro(){
var idj = document.getElementById('idj').value;
var ids = document.getElementById('ids').value;
var idtp = document.getElementById('idtp').value;
var mes = document.getElementById('mes').value;
var anno = document.getElementById('anno').value;
var cadena = "../buscarpersonalrepro.php?idj="+idj+"&ids="+ids+"&idtp="+idtp+"&mes="+mes+"&anno="+anno;
document.location= cadena;
}
</script>

</head>
<body>
<table align='center'>
<tr><td>
<?php
echo "<input type='hidden' id='idj' name='idj' value='".$idj."'>";
echo "<input type='hidden' id='ids' name='ids' value='".$ids."'>";
echo "<input type='hidden' id='idtp' name='idtp' value='".$idtp."'>";
echo "<input type='hidden' id='mes' name='mes' value='".$mm."'>";
echo "<input type='hidden' id='anno' name='anno' value='".$anno."'>";

echo "<input type='hidden' id='coduni' name='coduni' value='".$coduni."'>";
echo "<input type='hidden' id='idso' name='idso' value='".$idso."'>";

?>
<fieldset>
	<legend align='left'><font color='blue' size='15'>Registre al personal </font></legend>
	<br>
		<table align='center' class='FacetFormTABLE' width='450'>
		<tr><td class='FacetFieldCaptionTDT' colspan='2'><center>DATOS DEL PERSONAL</center></td></tr>
		<tr><td colspan='2'><center><font color='red'><?php if($texto!=""){ echo $texto;}?></font></center></td></tr>
		<tr><td align='right' class='FacetFieldCaptionTD' style='width:100px'>PERSONAL</td><td align='rigth'><input type='text' id='nombre' name='nombre' size='45' disabled value='<?php if($valor==1){ echo $nombre;}?>'></td></tr>
		<tr><td align='right' class='FacetFieldCaptionTD'>COD PROG</td><td><input type='text' id='codprog' name='codprog' size='4'></td></tr>
		<tr><td align='right' class='FacetFieldCaptionTD' height='10'>AREA/DEPARTAMENTO</td><td><input type='text' id='idserv' name='idserv' size='45' disabled value='<?php if($valor==1){ echo $jefatura;}?>'></td></tr>
		<tr><td align='right' class='FacetFieldCaptionTD' height='10'>SERVICIO</td><td><input type='text' id='idserv' name='idserv' size='45' disabled value='<?php if($valor==1){ echo $servicio;}?>'></td></tr>
		<tr><td align='center' colspan='2'><input type='button' id='pprog' style='width:100px' class='FacetButton'  value='Grabar' onClick='grabarcodigoper()'>
		<button type='button' id='pprog' style='width:100px' class='FacetButton' onClick='ventanaClose();' >Cancelar</button>
		</td></tr></table>
	  <br>
	</fieldset><br>
	<table align='center'>
		<tr><td align='center'><button type='button' id='pprog' style='width:100px' class='FacetButton' onClick='buscarpersonalrepro()' >Buscar Personal</button></td></tr>
		</table>
</td><tr>
</table>
</body>
</html>