<?php

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

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

//$idj = $_GET['idj'];
//$ids = $_GET['ids'];
//codigo del usuario
$codprog=$_GET['codprog'];
//$idj=$_GET['idj'];
$codugid=$_GET['codugid'];
//
//$codigo=$_GET['codigo'];
$codigo = strstr($codugid, '-');
$numPos = strpos($codugid,'-');
$ugid= substr($codugid,0,$numPos);
$idfecha= substr($codugid,$numPos+1,strlen($ugid));
//echo $idfecha."<br>";
//echo $ugid;

//$ugid=$_GET['ugid'];
//$idfecha=$_GET['idfecha'];

if(isset($_GET['fechaini']) && isset($_GET['fechafin']) && $_GET['idt']==0){

$fechainia=$_GET['fechaini'];
$fechafina=$_GET['fechafin'];
$ugid = $_GET['ugid'];
$numid = $_GET['numid'];

echo $fechainia;
$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=$_POST['obs'];
if(($diaini<=$diafin && $mesini==$mesfin && $annoini==$annofin) || ($mesini<$mesfin && $annoini==$annofin) || ($annoini<$annofin)){
$sqlconsulta1="select * from tb_descmed where ug_id='$ugid' and '$fechaini' between dm_fechaini and dm_fechafin and dm_id not in ('$numid')";
$sqlconsulta2="select * from tb_descmed where ug_id='$ugid' and '$fechafin' between dm_fechaini and dm_fechafin and dm_id not in ('$numid')";
echo $sqlconsulta1;
$resultsql1 = mysqli_query($con,$sqlconsulta1);
$resultsql2 = mysqli_query($con,$sqlconsulta2);
	if((mysqli_num_rows($resultsql1)==0) && (mysqli_num_rows($resultsql2)==0)){
	$sqlinsertdm="update tb_descmed set dm_fechaini='$fechaini', dm_fechafin='$fechafin', dm_numdias='$numdias' where ug_id='$ugid' and dm_id='$numid'";
	//echo "se modifico";
	//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;
}}

if(isset($_GET['idt']) && $_GET['idt']==1){
$ugid = $_GET['ugid'];
$idfecha = $_GET['idfecha'];
$sqlupdate = "delete from tb_descmed where dm_id='$idfecha' and ug_id='$ugid'";
//echo $sqlupdate;
$resultdm = mysqli_query($con,$sqlupdate);
echo "<script type='text/javascript'>window.opener.location.reload(); window.close();</script>";
}

if(isset($_GET['idt']) && $_GET['idt']==2){
$ugid = $_GET['ugid'];
$idfecha = $_GET['idfecha'];
$codprog = $_GET['codprog'];
$sqlupdate = "update tb_descmed set dm_estado='1', dm_usuval='$codprog', dm_fechaval=now() where dm_id='$idfecha' and ug_id='$ugid'";
//echo $sqlupdate;
$resultdm = mysqli_query($con,$sqlupdate);
echo "<script type='text/javascript'>window.opener.location.reload(); window.close();</script>";
}

//restando fechas
function restaFechas($dFecIni, $dFecFin)
{
    $dFecIni = str_replace("-","",$dFecIni);
    $dFecIni = str_replace("/","",$dFecIni);
    $dFecFin = str_replace("-","",$dFecFin);
    $dFecFin = str_replace("/","",$dFecFin);

    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,$aFecIni[2], $aFecIni[1], $aFecIni[3]);
    $date2 = mktime(0,0,0,$aFecFin[2], $aFecFin[1], $aFecFin[3]);

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


//lectura de las fechas
$sqlconsulta1="select date_format(dm_fechaini,'%d/%m/%Y') as fechaini, date_format(dm_fechafin,'%d/%m/%Y') as fechafin from tb_descmed where ug_id='$ugid' and dm_id='$idfecha'";
//echo $sqlconsulta1;
$resultf = mysqli_query($con,$sqlconsulta1);
while ($rowf= mysqli_fetch_array($resultf)){
$fechaini = $rowf['fechaini'];
$fechafin = $rowf['fechafin'];
}


//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_array($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(a.ua_apepat,' ',a.ua_apemat,' ',a.ua_nom) as nombre, date_format(b.ud_fechaing,'%d/%m/%Y') as fechaing from tb_usuasist a inner join tb_usudep b on a.ug_id=b.ug_id where a.ug_id='$ugid'" ;
//echo $sqltc;
$resultp = mysqli_query($con,$sqlp);
while ($rowp= mysqli_fetch_array($resultp)){
$nombre = $rowp['nombre'];
//$fechaing = $rowp['fechaing'];
}
?>
<html>
<head>
<title>EDITAR 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 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 suspenderdm(){ 
   //alert("hola mundo");
	var ugid = document.getElementById('ugid').value;
	var numid = document.getElementById('numid').value;
	//alert(ugid);
    window.location ="editardesmed.php?ugid="+ugid+"&idfecha="+numid+"&idt=1";
	//window.opener.location.reload(); window.close();
} 

function validardm(){ 
   //alert("hola mundo");
    var codprog = document.getElementById('ugidprog').value;
	var ugid = document.getElementById('ugid').value;
	var numid = document.getElementById('numid').value;
	//alert(ugid);
    window.location ="editardesmed.php?ugid="+ugid+"&idfecha="+numid+"&codprog="+codprog+"&idt=2";
	//window.opener.location.reload(); window.close();
} 

function modificardm(){ 
   //alert("hola mundo");
    var fechaini = document.getElementById('fechaini').value;
	var fechafin = document.getElementById('fechafin').value;
	var ugid = document.getElementById('ugid').value;
	var numid = document.getElementById('numid').value;
	//alert(ugid);
	cadena = "editardesmed.php?ugid="+ugid+"&fechaini="+fechaini+"&fechafin="+fechafin+"&numid="+numid+"&idt=0";
	window.location = cadena;
	
	//window.opener.location.reload(); window.close();
} 
</script> 
</head>
<body>
<br><br>
<table align='center'>
<tr><td>
<div id='transfpersonal'>
<?php
echo "<input type='hidden' id='ugidprog' name='ugidprog' value='".$codprog."'>";

echo "<input type='hidden' id='ugid' name='ugid' value='".$ugid."'>";
echo "<input type='hidden' id='numid' name='numid' value='".$idfecha."'>";
?>
<fieldset>
	<legend align='left'><font color='blue' size='15'>Modifique la Fecha del Descanso Medico </font></legend>
	<br>
		<table align='center' class='FacetFormTABLE' width='450'>
		<tr><td class='FacetFieldCaptionTDT' colspan='4'><center>EDITAR 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' value='<?php if(isset($idfecha)){ echo $fechaini;}?>'>
		<button id="f_rangeStart_trigger1"><img src="../../img/b_calendar.png" ></button>
                <button id="f_clearRangeStart1" onclick="clearRangeStart1()">clear</button>
                <script type="text/javascript">
				   Calendar.setup({
        trigger    : "f_rangeStart_trigger1",
        inputField : "fechaini",
		dateFormat : "%d/%m/%Y",
		onSelect   : function() { this.hide() }
    });
	 function clearRangeStart() {
                          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($idfecha)){ echo $fechafin;}?>'>
		<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 clearRangeStart() {
                          document.getElementById("fechafin").value = "";                        
                  };
                </script>
		</td>
		</tr>
		
		<tr><td align='center' colspan='4'><input type='button'  style='width:150px' class='FacetButton'  value='Modificar' onClick='modificardm()'>
		<button type='button' id='pprog' style='width:150px' class='FacetButton' onClick='suspenderdm();' >Suspender</button>
		<button type='button' id='pprog' style='width:150px' class='FacetButton' onClick='validardm();' >Validar DM</button>
		</td></tr>
		<tr><td align='center' colspan='4'>
		<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>