<?php
require_once("../class/class.personal.php");
require_once("../class/class.hrpermiso.php");
require_once("../funcionesphp/func.calculo.php");

$ugid=$_GET['ugid'];
$idfecha=$_GET['idfecha'];
$idtipohper=$_GET['idthper'];

if(isset($_POST['horaini']) && isset($_POST['horafin']) && isset($_POST['fecha'])){

$hinia=$_POST['horaini'];
$hfina=$_POST['horafin'];
$ugid = $_POST['ugid'];
$fecha = $_POST['fecha'];

$dia=substr($fecha, 0, 2);
$mes=substr($fecha, 3, 2);
$anno=substr($fecha, 6, 4);
$fechamod =$anno.'/'.$mes.'/'.$dia;
$difhrs = $hfina - $hinia;

$idtphrp=$_POST['tphper'];
$numid=$_POST['numid'];


if($difhrs>0){
//if(($diaini<=$diafin && $mesini==$mesfin && $annoini==$annofin) || ($mesini<$mesfin && $annoini==$annofin) || ($annoini<$annofin)){
	$Objhper = new HoraPermiso();
	$res_a = array(); $res_p = array();
		$res_a = $Objhper->consultar_hpermiso_anterior_id($ugid, $anno,$mes,$dia,$numid,$hinia);
		$res_p = $Objhper->consultar_hpermiso_posterior_id($ugid, $anno,$mes,$dia,$numid,$hfina);
		
			if((count($res_a)==0) && (count($res_p)==0)){
			$Objhper = new HoraPermiso();
			$Objhper->modificar_horas_permiso($hinia, $hfina, $difhrs, $numid, $ugid, $idtphrp,$fechamod);
			$error=0;
			echo "<script type='text/javascript'>window.opener.location.reload(); window.close();</script>";
			}else{
			$error = 1;
			}	
	}else{
	$error=1;
	}
	/////////////////////////////
	}

	//lectura de fechas
	$Objhper = new HoraPermiso();
	$datosf = array();
	$datosf = $Objhper->consultar_horas_permiso_personal($ugid,$idfecha);
		for($i=0;count($datosf)>$i;$i++){
			$fecha = $datosf[$i]['fecha'];
			$horai = $datosf[$i]['horai'];
			$horaini = $datosf[$i]['horaini'];
			$horaf = $datosf[$i]['horaf'];
			$horafin = $datosf[$i]['horafin'];
			//$annoprog = $datosf[$i]['periodo'];	
		}
		
	
	
	//seleccionando al personal
	$Objper = new Personal();
	$datosper = array();
	$datosper = $Objper->buscar_datos_per_campo($ugid);
		for($i=0;count($datosper)>$i;$i++){
			$nombre = $datosper[$i]['nombre'];
			$fechaing = $datosper[$i]['fechaing'];
		}

//tipo de licencias
$Objhper = new HoraPermiso();
$listhrper = array();
$listhrper = $Objhper->listar_tipo_hpermiso();
$salidatipohper="<select id='tphper' name='tphper'>";
for($i=0;count($listhrper)>$i;$i++){
$salidatipohper=$salidatipohper."<option value='".$listhrper[$i]['thp_id']."'";
if ($listhrper[$i]['thp_id']==$idtipohper){
$salidatipohper=$salidatipohper." selected ";
}
$salidatipohper=$salidatipohper.">".$listhrper[$i]['thp_desc']."</option>";
}

$salidatipohper=$salidatipohper."</select>";

//hora inicio
	$horainicio = "<select id='horaini' name='horaini' style='width:12em;'>";
	for($ai=0;$ai<=24;$ai++){ 
	$horainicio = $horainicio."<option value='".$ai.":00'";
	$horai = substr($horai, 0, 2)*1;
	if($horai==$ai){
	$horainicio = $horainicio." selected ";
	}
	$horainicio = $horainicio.">".$ai.":00</option>";
	}
	$horainicio = $horainicio."</select>";
		
//hora final
	$horafinal = "<select id='horafin' name='horafin' style='width:12em;'>";
	for($ai=0;$ai<=24;$ai++){ 
	$horafinal = $horafinal."<option value='".$ai.":00'";
	$horaf = substr($horaf, 0, 2)*1;
	if($horaf==$ai){
	$horafinal = $horafinal." selected ";
	}
	$horafinal = $horafinal.">".$ai.":00</option>";

	}
	$horafinal = $horafinal."</select>";


?>
<html>
<head>
<title>HORA PERMISO DEL PERSONAL ASISTENCIAL</title>
<link rel="stylesheet" type="text/css" href="styles/Style.css">

<script language="JavaScript" type="text/javascript" src="ajax.js"></script>
<link rel="stylesheet" type="text/css" href="styles/Style.css">
<link type="text/css" rel="stylesheet" href="src/css/jscal2.css" />
    <link type="text/css" rel="stylesheet" href="src/css/border-radius.css" />
    <!-- <link type="text/css" rel="stylesheet" href="src/css/reduce-spacing.css" /> -->

    <link id="skin-win2k" title="Win 2K" type="text/css" rel="alternate stylesheet" href="src/css/win2k/win2k.css" />
    <link id="skin-steel" title="Steel" type="text/css" rel="alternate stylesheet" href="src/css/steel/steel.css" />
    <link id="skin-gold" title="Gold" type="text/css" rel="alternate stylesheet" href="src/css/gold/gold.css" />
    <link id="skin-matrix" title="Matrix" type="text/css" rel="alternate stylesheet" href="src/css/matrix/matrix.css" />

    <link id="skinhelper-compact" type="text/css" rel="alternate stylesheet" href="src/css/reduce-spacing.css" />

    <script src="src/js/jscal2.js"></script>
	<script src="src/js/lang/en.js"></script>
    <!-- you actually only need to load one of these; we put them all here for demo purposes -->
     <script src="src/js/lang/es.js"></script>
    
    <link type="text/css" rel="stylesheet" href="demopage.css" />

<script type="text/javascript"> 

function ventanaClose(){ 
   //alert("hola mundo");
    window.close();
} 
</script> 
</head>
<body>
<table align='center'>
<tr><td>
<div id='transfpersonal'>
<form action='editarhrpermiso.php' method='POST'>
<?php
echo "<input type='hidden' id='ugid' name='ugid' value='".$ugid."'>";
echo "<input type='hidden' id='numid' name='numid' value='".$idfecha."'>";
echo "<input type='hidden' id='idtlic' name='idtlic' value='".$idtipohper."'>";

?>
<fieldset>
	<legend align='left'><font color='blue' size='15'>Escriba la Fecha y Hora del Permiso </font></legend>
	<br>
		<table align='center' class='FacetFormTABLE' width='400'>
		<tr><td class='FacetFieldCaptionTDT' colspan='4'><center>HORA PERMISO DEL PERSONAL ASISTENCIAL</center></td></tr>
		<tr><td colspan='4'><center><?php if($error=='1'){ echo "REVISE LOS DATOS, PERSONAL NO REGISTRADO";}elseif($error=='0'){ echo "PERSONAL REGISTRADO";}?></center></td></tr>
		<tr><td align='right' class='FacetFieldCaptionTD' style='width:300px'>PERSONAL </td><td colspan='3'><?php echo $nombre; ?></td></tr>
		<tr><td align='right' class='FacetFieldCaptionTD'>FECHA</td>
		<td colspan='3'>
		<input type='text' id='fecha' name='fecha' size='15' value='<?php if(isset($_GET['ugid'])){ echo $fecha;}?>'>
		<button id="f_rangeStart_trigger1"><img src="../img/b_calendar.png" ></button>
                <button id="f_clearRangeStart1" onclick="clearRangeStart1()">clear</button>
                <script type="text/javascript">
				  var LEFT_CAL = Calendar.setup({
                    //cont: "cont",
                    //weekNumbers: true,
                    //selectionType: Calendar.SEL_MULTIPLE,
                    //showTime: 12
                    // titleFormat: "%B %Y"
					})
                  new Calendar({
                          inputField: "fecha",
                          dateFormat: "%d/%m/%Y",
                          trigger: "f_rangeStart_trigger1",
                          bottomBar: false,
                          onSelect: function() {
                                  var date = Calendar.intToDate(this.selection.get());
								  LEFT_CAL.setLanguage('es');
                                  LEFT_CAL.args.min = date;
                                  LEFT_CAL.redraw();
                                  this.hide();
                          }
                  });
                  function clearRangeStart1() {
                          document.getElementById("fecha").value = "";
                          LEFT_CAL.args.min = null;
                          LEFT_CAL.redraw();
                  };
                </script>
		</td></tr>
		<tr>
		<td align='right' class='FacetFieldCaptionTD'>HORA INICIAL</td><td><?php echo $horainicio; ?></td>
		<td align='right' class='FacetFieldCaptionTD'>HORA FINAL</td><td><?php echo $horafinal; ?></td>
		</tr>
		<tr><td align='right' class='FacetFieldCaptionTD' style='width:300px'>TIPO DE PERMISO </td><td colspan='3'><?php echo $salidatipohper; ?></td></tr>
		<tr><td align='center' colspan='4'><input type='submit' id='pprog' style='width:150px' class='SalidaButton'  value='Grabar'>
		<button type='button' id='pprog' style='width:150px' class='SalidaButton' onClick='ventanaClose();' >Cancelar</button>
		</td></tr></table>
	  <br>
	</fieldset>
</div>
</td><tr>
</table>
</body>
</html>