<?php
require_once("../class/class.conectar.php");
require_once("../class/class.descrad.php");
require_once("../class/class.personal.php");
require_once("../class/class.jefaturas.php");
require_once("../funcionesphp/func.calculo.php");


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

$ugid=$_GET['ugid'];

date_default_timezone_set("America/Lima"); setlocale(LC_TIME,"es_PE.UTF-8");

if(isset($_POST['fechaini']) && isset($_POST['fechafin'])){
$fechainia=$_POST['fechaini'];
$fechafina=$_POST['fechafin'];
$periodo=$_POST['anno'];
$ugid = $_POST['ugid'];
$diaini=substr($fechainia, 0, 2);
$diafin=substr($fechafina, 0, 2);
$mesini=substr($fechainia, 3, 2);
$mesfin=substr($fechafina, 3, 2);
$annoini=substr($fechainia, 6, 4);
$annofin=substr($fechafina, 6, 4);
$fechaini =$annoini.'/'.$mesini.'/'.$diaini;
$fechafin =$annofin.'/'.$mesfin.'/'.$diafin;
$numdias=restaFechas($fechainia,$fechafina);
$numdias=$numdias+1;

	if(($diaini<=$diafin && $mesini==$mesfin && $annoini<=$annofin) || ($mesini<$mesfin && $annoini<=$annofin) || ($annoini<$annofin)){

	$Objdr = new DescansoRadiacion();
	$res_a = array(); $res_p = array();

	$res_a = $Objdr->consultar_descrad_anterior($conex,$ugid, $fechaini);
	$res_p = $Objdr->consultar_descrad_posterior($conex,$ugid, $fechafin);

		if((count($res_a)==0) && (count($res_p)==0)){
		
		$iddr = $Objdr->buscar_id_descrad($conex,$ugid);
		//$sqlconsulta="select v_id from tb_vacaciones where ug_id='$ugid' order by v_id desc limit 1";
		//$resultsql = mysql_query($sqlconsulta);
		echo $iddr."<br>salida";
			if($iddr==0){
				$iddr = 1;
			}else{
				$iddr = $iddr + 1;
			}
		//echo $iddr."<br>";
		$Objdr->insert_dr_prog($conex,$iddr,$ugid,$periodo,$fechaini,$fechafin,$numdias);
		
		//$sqlinsertdm="insert into tb_vacaciones values('$numid','$ugid','$periodo','$fechaini','$fechafin','$numdias',0)";
		$error=0;
		echo "<script type='text/javascript'>window.opener.location.reload(); window.close();</script>";
		}else{
		$error = 1;
		}
	}else{
	$error=1;
	}
}

//seleccionando al personal
$Objper = new Personal();
$datospersona = array();
$datospersona = $Objper->buscar_datos_per_campo( $conex,$ugid);
$nombre = $datospersona[0]['nombre'];
$fechaing = $datospersona[0]['fechaing'];

?>
<html>
<head>
<title>DESCANSO POR RADIACION 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='descradiacion.php' method='POST'>
<?php
echo "<input type='hidden' id='ugid' name='ugid' value='".$ugid."'>";
?>
<fieldset>
	<legend align='left'><font color='blue' size='15'>Escriba el Periodo de las Fechas </font></legend>
	<br>
		<table align='center' class='FacetFormTABLE' width='450'>
		<tr><td class='FacetFieldCaptionTDT' colspan='4'><center>DESCANSO POR RADIACION DEL PERSONAL ASISTENCIAL</center></td></tr>
		<tr><td colspan='4'><center><?php if(isset($error)){ if($error=='1'){ echo "REVISE LOS DATOS, INFORMACION NO REGISTRADA";}elseif($error=='0'){ echo "INFORMACION REGISTRADA";}}?></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' style='width:300px'>FECHA INGRESO </td><td colspan='3'><?php echo $fechaing; ?></td></tr>
		<tr><td align='right' class='FacetFieldCaptionTD' style='width:300px'>PERIODO </td><td colspan='3'>
		<?php 
		echo "<select name='anno' id='anno'>";
		for ($i=2000;$i<2100;$i++){
		echo "<option value='".$i."'";
		if(date("Y",time())==$i){?> selected
		<?php 
		$anno=$i;
		} echo ">".$i."</option>";
		}
		echo "</select>"; 
		?></td></tr>
		<tr><td align='right' class='FacetFieldCaptionTD'>FECHA INICIAL</td>
		<td>
		<input type='text' id='fechaini' name='fechaini' size='15' value='<?php //if(isset($_GET['ugid'])){ echo $fechaing;}?>'>
		<button id="f_rangeStart_trigger1">...</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: "fechaini",
                          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("fechaini").value = "";
                          LEFT_CAL.args.min = null;
                          LEFT_CAL.redraw();
                  };
                </script>
		</td>
		<td align='right' class='FacetFieldCaptionTD'>FECHA FINAL</td>
		<td>
		<input type='text' id='fechafin' name='fechafin' size='15' value='<?php //if(isset($_GET['ugid'])){ echo $fechaing;}?>'>
		<button id="f_rangeStart_trigger2">...</button>
                <button id="f_clearRangeStart2" onclick="clearRangeStart2()">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: "fechafin",
                          dateFormat: "%d/%m/%Y",
                          trigger: "f_rangeStart_trigger2",
                          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 clearRangeStart2() {
                          document.getElementById("fechafin").value = "";
                          LEFT_CAL.args.min = null;
                          LEFT_CAL.redraw();
                  };
                </script>
		</td>
		</tr>
		
		<tr><td align='center' colspan='4'><input type='submit' id='pprog' style='width:150px' class='FacetButton'  value='Grabar'>
		<button type='button' id='pprog' style='width:150px' class='FacetButton' onClick='ventanaClose();' >Cancelar</button>
		</td></tr></table>
	  <br>
	</fieldset>
</div>
</td><tr>
</table>
</body>
</html>