<?php

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

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

//$idj = $_GET['idj'];
//$ids = $_GET['ids'];

//$idj=$_GET['idj'];
//$ids=$_GET['ids'];
$ugid=$_GET['ugid'];
$ugidprog=$_GET['ugidprog'];

if(isset($_GET['fechaini']) && isset($_GET['fechafin'])){
$fechainia=$_GET['fechaini'];
$fechafina=$_GET['fechafin'];
$ugid = $_GET['ugid'];
$ugidprog = $_GET['ugidprog'];
$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;
$obs=$_GET['obs'];
if(($diaini<=$diafin && $mesini==$mesfin && $annoini==$annofin) || ($mesini<$mesfin && $annoini==$annofin) || ($annoini<$annofin)){
$sqlconsulta1="select * from tb_descmed where ug_id='$ugid' and date('$fechaini') between date(dm_fechaini) and date(dm_fechafin)";
$sqlconsulta2="select * from tb_descmed where ug_id='$ugid' and date('$fechafin') between date(dm_fechaini) and date(dm_fechafin)";
//echo $sqlconsulta1;
$resultsql1 = mysqli_query($con,$sqlconsulta1);
$resultsql2 = mysqli_query($con,$sqlconsulta2);
	if((mysqli_num_rows($resultsql1)==0) && (mysqli_num_rows($resultsql2)==0)){
	$sqlconsulta="select dm_id from tb_descmed where ug_id='$ugid' order by dm_id desc limit 1";
	$resultsql = mysqli_query($con, $sqlconsulta);
	if(mysqli_num_rows($resultsql)==0){
	$numid = 1;
	}else{
	while ($row= mysqli_fetch_assoc($resultsql)){
	$numid = $row['dm_id']+1;
	}
	}
	$sqlinsertdm="insert into tb_descmed values('$numid','$ugid','$fechaini','$fechafin','$numdias',upper('$obs'),0,'$ugidprog','','')";
	//echo $sqlinsertdm;
	$resultdm = mysqli_query($con, $sqlinsertdm);
	$error=0;
	echo "<script type='text/javascript'>window.opener.location.reload(); window.close();</script>";
	
	}else{
	$error = 1;
	}
}else{
$error=1;
}}

//restando fechas
function restaFechas($dFecIni, $dFecFin)
{
    $dFecIni = str_replace("-","",$dFecIni);
    $dFecIni = str_replace("/","",$dFecIni);
    $dFecFin = str_replace("-","",$dFecFin);
    $dFecFin = str_replace("/","",$dFecFin);
	
	$diai=substr($dFecIni, 0, 2);
	$mesi=substr($dFecIni, 2, 2);
	$annoi=substr($dFecIni, 4, 4);
	
	$diaf=substr($dFecFin, 0, 2);
	$mesf=substr($dFecFin, 2, 2);
	$annof=substr($dFecFin, 4, 4);

    //preg_match( "/([0-9]{1,2})([0-9]{1,2})([0-9]{2,4})/", $dFecIni, $aFecIni);
    //preg_match( "/([0-9]{1,2})([0-9]{1,2})([0-9]{2,4})/", $dFecFin, $aFecFin);

    $date1 = mktime(0,0,0,$mesi, $diai, $annoi);
    $date2 = mktime(0,0,0,$mesf, $diaf, $annof);

    return round(($date2 - $date1) / (60 * 60 * 24)) ;
}


/*
//seleccionando al combo jefatura
$sqljefatura = "select j_id as cod, j_desc as descripcion from tb_jefatura";
//echo $sqltpersonal;
$resultjefatura = mysqli_query($con, $sqljefatura);
$cadenaj="<select id='jefatura' name='jefatura' onChange='actualizaservicio()'>";
while ($rowj= mysqli_fetch_assoc($resultjefatura)){
$cadenaj=$cadenaj."<option value='".$rowj['cod']."'";
if ($rowj['cod']==$idj){
$cadenaj=$cadenaj." selected ";
}
$cadenaj=$cadenaj.">".$rowj['descripcion']."</option>";
}
$cadenaj=$cadenaj."</select>";

//seleccionando al comnbo servicio
$sqlservicio = "select s_id as cod, s_desc as descripcion from tb_servicio where j_id='$idj'";
//echo $sqlespecialidad;
$resultservicio = mysqli_query($con,$sqlservicio);
$cadenaserv="<select id='servicio' name='servicio'>";
while ($rows= mysqli_fetch_array($resultservicio)){
$cadenaserv=$cadenaserv."<option value='".$rows['cod']."'";
if ($rows['cod']==$ids){
$cadenaserv=$cadenaserv." selected ";
}
$cadenaserv=$cadenaserv.">".$rows['descripcion']."</option>";
}
$cadenaserv=$cadenaserv."</select>";
*/
//seleccionando al personal
$sqlp = "select concat(ua_apepat,' ',ua_apemat,' ',ua_nom) as nombre from tb_usuasist where ug_id='$ugid'";
//echo $sqltc;
$resultp = mysqli_query($con,$sqlp);
while ($rowp= mysqli_fetch_assoc($resultp)){
$nombre = $rowp['nombre'];
}

?>
<html>
<head>
<title>DESCANSO MEDICO 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 id="skin-gold" title="Gold" type="text/css" href="src/css/gold/gold.css" />
 <script src="src/js/jscal2.js"></script>
 <script src="src/js/lang/es.js"></script>
   

<script type="text/javascript"> 

function ventanaClose(){ 
   //alert("hola mundo");
    window.close();
} 
function guardarDM(){ 
   var fechaini = document.getElementById("fechaini").value;
   var fechafin = document.getElementById("fechafin").value;
   var ugid = document.getElementById("ugid").value;
   var ugidprog = document.getElementById("ugidprog").value;
   var obs = document.getElementById("obs").value;
   document.location= "descansomedico.php?fechaini="+fechaini+"&fechafin="+fechafin+"&ugid="+ugid+"&obs="+obs+"&ugidprog="+ugidprog;
}
</script>

</head>
<body>
<table align='center'>
<tr><td>
<div id='transfpersonal'>
<?php
echo "<input type='hidden' id='ugid' name='ugid' value='".$ugid."'>";
echo "<input type='hidden' id='ugidprog' name='ugidprog' value='".$ugidprog."'>";
?>
<fieldset>
	<legend align='left'><font color='blue' size='15'>Escriba la Fecha del Descanso Medico </font></legend>
	<br>
		<table align='center' class='FacetFormTABLE' width='450'>
		<tr><td class='FacetFieldCaptionTDT' colspan='4'><center>DESCANSO MEDICO 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 INICIAL</td>
		<td>
		<input type='text' id='fechaini' name='fechaini' size='15'>
		<button id="f_rangeStart_trigger1"><img src="../img/b_calendar.png" ></button>
                <button id="f_clearRangeStart1" onclick="clearRangeStart1()">clear</button>
				<script> 
				Calendar.setup({
					trigger    : "f_rangeStart_trigger1",
					inputField : "fechaini",
					dateFormat: "%d/%m/%Y",
					onSelect   : function() { this.hide() }
				});
				 function clearRangeStart1() {
									  document.getElementById("fechaini").value = "";                        
				 };
			</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"><img src="../img/b_calendar.png" ></button>
                <button id="f_clearRangeStart2" onclick="clearRangeStart2()">clear</button>
                <script type="text/javascript">
				  Calendar.setup({
					trigger    : "f_rangeStart_trigger2",
					inputField : "fechafin",
					dateFormat: "%d/%m/%Y",
					onSelect   : function() { this.hide() }
				});
				 function clearRangeStart2() {
									  document.getElementById("fechafin").value = "";                        
				 };
                </script>
		</td>
		</tr>
		<tr><td align='right' class='FacetFieldCaptionTD' style='width:300px'>OBSERVACIONES </td><td colspan='3'><input type='text' id='obs' name='obs' size='70'></td></tr>
		<tr><td align='center' colspan='4'><input type='button' id='pprog' style='width:150px' class='FacetButton'  value='Grabar' onClick='guardarDM()'>
		<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>