<?php
require_once("class/class.conectar.php");
require_once("class/class.jefaturas.php");
require_once("class/class.tipopersonal.php");
require_once("class/class.area.php");
require_once("class/class.horario.php");
require_once("class/class.horaprog.php");
require_once("class/class.horareprog.php");
require_once("class/class.diaferiado.php");
date_default_timezone_set('UTC');

$Objcon = new Conectar();
$conex = $Objcon->con();

$anno = $_GET['anno'];
$mm = $_GET['mes'];
$idh =$_GET['idh'];
$ida =$_GET['ida'];
$idga =$_GET['idga'];
$idj = $_GET['idj'];
$ids = $_GET['ids'];
$idtp = $_GET['idtp'];
$ugid = $_GET['ugid'];
$val = $_GET['valor'];

$mes = mktime( 0, 0, 0, $mm, 1, $anno ); 
$numeroDeDias = intval(date("t",$mes)); 

$Objj = new Jefaturas();
$res_j = array(); $res_s = array();
$res_j = $Objj->buscar_jefatura($conex,$idj);
$nomjefatura = $res_j[0]['nom'];
if($ids!=0){
$res_s = $Objj->buscar_jefatura_servicio($conex,$idj,$ids);
$nomservicio = $res_s[0]['nom'];
}else{
$nomservicio="";	
}
//nombre de tipo de personal
$Objtp = new TipoPersonal();
$res_tp=array();
$res_tp=$Objtp->buscar_tipoper($conex,$idtp);
$tipoper=$res_tp[0]['nom'];


echo "<input type='hidden' id='ndias' value='".$numeroDeDias."'>";
echo "<input type='hidden' id='idga' value='".$idga."'>";
echo "<input type='hidden' id='anno' value='".$anno."'>";
echo "<input type='hidden' id='mes' value='".$mm."'>";
echo "<input type='hidden' id='area' value='".$ida."'>";
echo "<input type='hidden' id='hora' value='".$idh."'>";
echo "<input type='hidden' id='depa' value='".$idj."'>";
echo "<input type='hidden' id='serv' value='".$ids."'>";
echo "<input type='hidden' id='idtp' value='".$idtp."'>";
echo "<input type='hidden' id='ugid' value='".$ugid."'>";
echo "<input type='hidden' id='valor' value='".$val."'>";


$arraymes = array();
$arraymes[] = array('idmes'=>'1','mes'=>'ENERO');
$arraymes[] = array('idmes'=>'2','mes'=>'FEBRERO');
$arraymes[] = array('idmes'=>'3','mes'=>'MARZO');
$arraymes[] = array('idmes'=>'4','mes'=>'ABRIL');
$arraymes[] = array('idmes'=>'5','mes'=>'MAYO');
$arraymes[] = array('idmes'=>'6','mes'=>'JUNIO');
$arraymes[] = array('idmes'=>'7','mes'=>'JULIO');
$arraymes[] = array('idmes'=>'8','mes'=>'AGOSTO');
$arraymes[] = array('idmes'=>'9','mes'=>'SETIEMBRE');
$arraymes[] = array('idmes'=>'10','mes'=>'OCTUBRE');
$arraymes[] = array('idmes'=>'11','mes'=>'NOVIEMBRE');
$arraymes[] = array('idmes'=>'12','mes'=>'DICIEMBRE');

?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="styles/Style.css">
<script language="JavaScript" type="text/javascript" src="ajax.js"></script>
</head><body>
<form action="guardarturno.php?anno=<?php echo $anno;?>&mes=<?php echo $mm;?>&iddepa=<?php echo $idj;?>&idserv=<?php echo $ids;?>&idhora=<?php echo $idh;?>&idarea=<?php echo $ida;?>&nivel=<?php echo $idtp;?>" method="POST">
<br><br>
<hr color='blue' size='10'>
<table align='center'>
<tr><td align='center'><font color='red'><b><?php echo $nomjefatura;?></b></font></td></tr>
<tr><td align='center'><font color='blue'><b><?php echo $nomservicio;?></b></font></td></tr>
<tr><td align='center'><font color='blue' size='5'><b>PROGRAMACION DEL MES DE <?php echo $arraymes[$mm-1]['mes'];?> DEL <?php echo $anno; ?></b></font></td></tr>
<tr><td align='center'><font color='blue'><b><?php echo $tipoper;?></b></font></td></tr>
</table>
<hr color='blue' size='10'>
<br>
<table>
<tr><td colspan="36" align="center"> </td>
<?php
echo "<tr><td rowspan='2'>AREA</td><td rowspan='2'>HORARIO</td><td rowspan='2'>TURNO</td><td rowspan='2'>NH</td>";

for ($i=1;$i<=$numeroDeDias;$i++){

$dia= date("w",mktime( 0, 0, 0, $mm, $i, $anno )); 
$fechacal = $anno.'-'.$mes.'-'.$dia;
//dia feriado
$diaft=0;
$Objdf = new DiaFeriado();
$res_df=array();
$res_df=$Objdf->buscar_dia_feriado($conex,$fechacal);
if(count($res_df)>0){
$diaft=$res_df[0]['tipo'];
}

switch ($dia) {
     case 0:
        echo "<td align='center' class='facetdataTDR'><b>DOM</b></td>";
        break;
    case 1:
        echo "<td align='center'";if($diaft==1){ echo " class='facetdataTDR'><b>LUN</b></td>";}elseif($diaft==2){echo " class='facetdataTDNL'><b>LUN</b></td>";}else{ echo " class='facetdataTD'>LUN</td>"; }
		break;
    case 2:
        echo "<td align='center'";if($diaft==1){ echo " class='facetdataTDR'><b>MAR</b></td>";}elseif($diaft==2){echo " class='facetdataTDNL'><b>MAR</b></td>";}else{ echo " class='facetdataTD'>MAR</td>"; }
        break;
	case 3:
        echo "<td align='center'";if($diaft==1){ echo " class='facetdataTDR'><b>MIE</b></td>";}elseif($diaft==2){echo " class='facetdataTDNL'><b>MIE</b></td>";}else{ echo " class='facetdataTD'>MIE</td>"; }
        break;
	case 4:
        echo "<td align='center'";if($diaft==1){ echo " class='facetdataTDR'><b>JUE</b></td>";}elseif($diaft==2){echo " class='facetdataTDNL'><b>JUE</b></td>";}else{ echo " class='facetdataTD'>JUE</td>"; }
        break;
	case 5:
        echo "<td align='center'";if($diaft==1){ echo " class='facetdataTDR'><b>VIE</b></td>";}elseif($diaft==2){echo " class='facetdataTDNL'><b>VIE</b></td>";}else{ echo " class='facetdataTD'>VIE</td>"; }
        break;
	case 6:
        echo "<td align='center'";if($diaft==1){ echo " class='facetdataTDR'><b>SAB</b></td>";}elseif($diaft==2){echo " class='facetdataTDNL'><b>SAB</b></td>";}else{ echo " class='facetdataTD'>SAB</td>"; }
        break;
}
}
echo "</tr><tr>";

for ($i=1;$i<=$numeroDeDias;$i++){

$dia= date("w",mktime( 0, 0, 0, $mm, $i, $anno ));
$fechacal = $anno.'-'.$mes.'-'.$dia;
//dia feriado
$diaft=0;
$Objdf = new DiaFeriado();
$res_df=array();
$res_df=$Objdf->buscar_dia_feriado($conex,$fechacal);

if(count($res_df)>0){
$diaft=$res_df[0]['tipo'];
}
 
switch ($dia) {
    case 0:
        echo "<td align='center' class='facetfilaTD'><b>$i</b></td>";
        break;
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
        echo "<td align='center'";if($diaft==1){ echo " class='facetfilaTD'><b>$i</b></td>";}elseif($diaft==2){echo " class='facetfilaTD'><b>$i</b></td>";}else{ echo " class='facetfilaTD'>$i</td>"; }
        break;
}
}
echo"</tr>";

$Obja = new Area();
$res_a = array();
$res_a = $Obja->ask_area_id($conex,$idga,$ida);

echo "<tr><td rowspan='2'>".$res_a[0]['nom']."</td>";

$Objh = new Horario();
$res_hr = array();
$res_hr = $Objh->buscar_horario($conex,$idh);

echo "<td rowspan='2'>".$res_hr[0]['hora']."</td><td rowspan='2'>".$res_hr[0]['turno']."</td><td rowspan='2'>".$res_hr[0]['numhora']."</td>";

$valor="";
for ($i=1;$i<=$numeroDeDias;$i++){
//pregunta por horario
if($val==1){
$Objt = new HoraProgramacion();
$res_hp = array();
$res_hp= $Objt->consulta_hrprog_dia_sin_cod($conex,$anno,$mm,$idj,$ids,$idtp,$idh,$i,$idga,$ida);
}
if($val==2){
$Objt = new HoraReprogramacion();
$res_hp = array();
$res_hp= $Objt->consulta_hrreprog_dia_sin_cod($conex, $anno,$mm,$idj,$ids,$idtp,$idh,$i,$idga,$ida);
}

if(count($res_hp)==0){
$valor ="";
}else{
$valor = $res_hp[0]['codprog'];
}

$dia= date("w",mktime( 0, 0, 0, $mm, $i, $anno )); 
$j = $i;

switch ($dia) {
    case 0: echo "<td align='center'><input class='FacetInput' type='text' size='3' id='dia[".$j."]' name='dia[".$j."]' value='$valor'></td>";break;
    case 1: 
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
        echo "<td align='center'><input class='FacetInput' type='text' size='3' id='dia[".$j."]' name='dia[".$j."]' value='$valor'></td>";break;
}}
echo "</tr>";

$ext="";
for ($i=1;$i<=$numeroDeDias;$i++){

if($val==1){
$Objt = new HoraProgramacion();
$res_hp = array();
$res_hp= $Objt->consulta_hrprog_dia_sin_cod($conex,$anno,$mm,$idj,$ids,$idtp,$idh,$i,$idga,$ida);
}
if($val==2){
$Objt = new HoraReprogramacion();
$res_hp = array();
$res_hp= $Objt->consulta_hrreprog_dia_sin_cod($conex,$anno,$mm,$idj,$ids,$idtp,$idh,$i,$idga,$ida);
}

if(count($res_hp)==0){
$ext =" ";
}else{
$ext = $res_hp[0]['thora'];
}

$dia= date("w",mktime( 0, 0, 0, $mm, $i, $anno )); 
$j = $i;
switch ($dia) {
    case 0: echo "<td align='center'><input type='checkbox' id='extra[".$j."]' name='extra[".$j."]'"; if($ext==1){echo "CHECKED";}; echo "></td>";break;
    case 1: 
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
        echo "<td align='center'><input type='checkbox' id='extra[".$j."]' name='extra[".$j."]'"; if($ext==1){echo "CHECKED";}; echo "></td>";break;
}
} echo "</tr>";

?>
<table>

<table align='center'><tr><td><input class='FacetButton' type="button" value="Grabar" onCLick="grabarTurnosub(<?php echo $idga; ?>);"></td><td><input class='FacetButton' type="button" value="Cancelar" onclick="llenarareasub()"></td></tr></table>
</form>
</body>
</html>