<?php

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


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

$idj=$_GET['idj'];
$ids=$_GET['ids'];
$ida=$_GET['ida'];
$idga=$_GET['idga'];
$ugid=$_GET['ugid'];
$turno=$_GET['turno'];
$idtp=$_GET['idtp'];
$dia=$_GET['dia'];
$mm=$_GET['mes'];
$anno=$_GET['anno'];
$sqlhorario ="";
$texto="";

if(isset($_GET['idj']) and isset($_GET['ids']) and isset($_GET['ugid']) and isset($_GET['idtp']) and isset($_GET['idga']) and isset($_GET['ida']) and isset($_GET['dia']) and isset($_GET['mes']) and isset($_GET['anno']) and isset($_GET['codprog']) and isset($_GET['idh']) and isset($_GET['ugid']) and isset($_GET['thr'])){
$idj=$_GET['idj'];
$ids=$_GET['ids'];
$ugid=$_GET['ugid'];
$idtp=$_GET['idtp'];
$idga=$_GET['idga'];
$ida=$_GET['ida'];
$dia=$_GET['dia'];
$mm=$_GET['mes'];
$anno=$_GET['anno'];
$codprog=$_GET['codprog'];
$idh=$_GET['idh'];

$ugid = $_GET['ugid'];
$thr = $_GET['thr'];
$idsa = $_GET['idsa'];

$turno = $_GET['turno'];

$valor = validareprog($anno, $mm, $dia, $idj, $ids, $idtp, $codprog, $idga, $ida, $idh);
//1 cruce de horario en el serv
//6 cruce de hoarario con otro serv
//2 post guardia en el serv
//7 post guardia entre serv
//3 cumpleanos
//4 vacaciones
//5 descanso medico
//8 mas de 10 guardias
if($valor=='1'){
$texto = "CRUCE CON OTRO TURNO EN EL SERVICIO";
}
if($valor=='2'){
$texto = "SALIO DE GUARDIA NOCTURNA EN EL SERVICIO";
}
if($valor=='3'){
$texto = "ONOMASTICO DEL PERSONAL";
}
if($valor=='4'){
$texto = "ESTA DE VACACIONES";
}
if($valor=='5'){
$texto = "PERSONAL CON DESCANSO MEDICO";
}
if($valor=='6'){
$texto = "CRUCE CON OTRO TURNO EN OTRO SERVICIO";
}
if($valor=='7'){
$texto = "SALIO DE GUARDIA NOCTURNA EN OTRO SERVICIO";
}
if($valor=='8'){
$texto = "YA TIENE 10 GUARDIAS";
}

if($valor=='0'){
$guardar = guardarturno($anno, $mm, $dia, $idj, $ids, $idtp, $codprog, $idga, $ida, $idh, $ugid, $thr);
if($guardar==10){
$texto = "SE REGISTRO EL TURNO CORRECTAMENTE";
echo "<script type='text/javascript'>window.opener.location.reload();</script>"; 
}
}
}

//combobox /busca primer horario de area
if($turno==1){
$sqlh = "select a.h_id as idh, concat(date_format(b.h_horaini,'%k:%i %p'), ' - ',date_format(b.h_horafin,'%k:%i %p')) as hora, h_horaturn as turno, h_horanum as numhora from areahora a inner join tb_horario b on a.h_id=b.h_id where a.s_id='$ids' and a.j_id='$idj' and a.ga_id='$idga' and a.a_id='$ida' and a.ah_estado=1 and b.h_horaturn in ('D','M')";
}
if($turno==3){
$sqlh = "select a.h_id as idh, concat(date_format(b.h_horaini,'%k:%i %p'), ' - ',date_format(b.h_horafin,'%k:%i %p')) as hora, h_horaturn as turno, h_horanum as numhora from areahora a inner join tb_horario b on a.h_id=b.h_id where a.s_id='$ids' and a.j_id='$idj' and a.ga_id='$idga' and a.a_id='$ida' and a.ah_estado=1 and b.h_horaturn in ('T')";
}
if($turno==4){
$sqlh = "select a.h_id as idh, concat(date_format(b.h_horaini,'%k:%i %p'), ' - ',date_format(b.h_horafin,'%k:%i %p')) as hora, h_horaturn as turno, h_horanum as numhora from areahora a inner join tb_horario b on a.h_id=b.h_id where a.s_id='$ids' and a.j_id='$idj' and a.ga_id='$idga' and a.a_id='$ida' and a.ah_estado=1 and b.h_horaturn in ('N')";
}
$resulth = mysql_query($sqlh);
if ($resulth == false){
echo 'PROVOCO UN ERROR EN LA CONSULTA 1';
}else{
$horario = "<select name='horario' id='horario'>";
while ($rowh= mysql_fetch_array($resulth)){
$horario = $horario."<option value='".$rowh['idh']."'>".$rowh['hora']."</option>";
}
$horario=$horario."</select>";
}
?>
<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'>
///revisar funcion
function validaturnopersonal(){
var nombre = document.getElementById('nombre').value;
if( nombre == 1){
var idj = document.getElementById('idj').value;
var ids = document.getElementById('ids').value;
var ugid = document.getElementById('ugid').value;
var idtp = document.getElementById('idtp').value;
var idga = document.getElementById('idga').value;
var ida = document.getElementById('ida').value;
var dia = document.getElementById('dia').value;
var mes = document.getElementById('mes').value;
var anno = document.getElementById('anno').value;
var codprog = document.getElementById('codprog').value;
var idh = document.getElementById('horario').value;
var turno = document.getElementById('turno').value;
//alert(idh);
var thr = new Array();
		 thr = document.getElementsByName('rdth');
		for(var j=0; j<thr.length; j++) {
			if(thr[j].checked==true){
			var idsj = thr[j].value;
			var thr =0;
			 //alert(0);
			}else{
			var idsj = thr[j].value;
			var thr =1;			 
			 //alert(1);
			}
			}

var cadena = "programanuevoper.php?idj="+idj+"&ids="+ids+"&ugid="+ugid+"&idtp="+idtp+"&idga="+idga+"&ida="+ida+"&dia="+dia+"&mes="+mes+"&anno="+anno+"&codprog="+codprog+"&idh="+idh+"&thr="+thr+"&turno="+turno;
document.location= cadena;
}else
alert("BUSQUE AL PERSONAL");
}</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='ugid' name='ugid' value='".$ugid."'>";
echo "<input type='hidden' id='idtp' name='idtp' value='".$idtp."'>";
echo "<input type='hidden' id='idga' name='idga' value='".$idga."'>";
echo "<input type='hidden' id='ida' name='idga' value='".$ida."'>";
echo "<input type='hidden' id='dia' name='dia' value='".$dia."'>";
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='turno' name='turno' value='".$turno."'>";
?>
<fieldset>
	<legend align='left'><font color='blue' size='15'>Registre turno a programar </font></legend>
	<br>
		<table align='center' class='FacetFormTABLE' width='450'>
		<tr><td class='FacetFieldCaptionTDT' colspan='3'><center>PROGRAMAR TURNO DEL PERSONAL</center></td></tr>
		<tr><td colspan='3'><center><font color='red'><?php if($texto!=""){ echo $texto;}?></font></center></td></tr>
		<tr><td colspan='3'><center><?php if($error=='0'){ echo "REVISE LOS DATOS, PERSONAL NO REGISTRADO";}elseif($error=='1'){ echo "PERSONAL REGISTRADO";}?></center></td></tr>
		<tr><td align='right' class='FacetFieldCaptionTD' style='width:100px'>PERSONAL</td><td><input type='text' id='codprog' name='codprog' size='4' onKeyPress='buscarpersonal()' ></td><td align='rigth'><div id='divper'>NOMBRE DEL PERSONAL A PROGRAMAR<input type='hidden' id='nombre' name='nombre' value='0'></div></td></tr>
		<tr><td align='right' class='FacetFieldCaptionTD'>HORARIO</td><td colspan='2'><?php echo $horario; ?></td></tr>
		<tr><td align='right' class='FacetFieldCaptionTD'>TIPO DE HORA</td><td><input type='radio' id='rdth' name='rdth' value='ho' checked>H.O.<input type='radio' id='rdth' name='rdth' value='he'>H.E.</td></tr>
		<tr><td align='center' colspan='3'><input type='button' id='pprog' style='width:100px' class='FacetButton'  value='Grabar' onClick='validaturnopersonal()'>
		<button type='button' id='pprog' style='width:100px' class='FacetButton' onClick='ventanaClose();' >Cancelar</button>
		</td></tr></table>
	  <br>
	</fieldset>
</td><tr>
</table>
</body>
</html>