<?php

//session_start();
/*/->

if(!isset($_SESSION['username'])) 

{

  header('Location: ../../principal.html'); 

  exit();

} else { 

    $fechaGuardada = $_SESSION["ultimoAcceso"]; 

    $ahora = date("Y-n-j H:i:s"); 

    $tiempo_transcurrido = (strtotime($ahora)-strtotime($fechaGuardada)); 	
	
     if($tiempo_transcurrido >= 900) { 
     //1140
      session_destroy();

     header('Location: ../../principal.html');
    
    }else { 

    $_SESSION["ultimoAcceso"] = $ahora; 

   } 

}
*/


if(isset($_GET['save'])){
  require_once("class/class.conectar.php");
  require_once("class/class.regproduccion.php");
  require_once("class/class.paciente.php");
  require_once("class/class.atencionpac.php");
  
  $Objcon = new Conectar();
  $con = $Objcon->con();

  $Objrp = new RegProduccion();
  
   
  if($_GET['ida'] == 15){
  
  $dni=$_GET['dni'];  
  $fechai=$_GET['fecha'];
  $idh=$_GET['idh'];  
  $ida=$_GET['ida'];  
  $idact=$_GET['idact'];  
  $idth=$_GET['idth'];  
  $numpaq=$_GET['numpac']; 
    
  $dia=substr($fechai, 0, 2);
   $mes=substr($fechai, 3, 2);
   $anno=substr($fechai, 6, 4);
   $fecha =$anno.'/'.$mes.'/'.$dia;
    
  $Objrp = new RegProduccion();
  $res_lis_regp = array();
  $res_lis_regp = $Objrp->listar_regproduccion_reg($con,$dni,$fecha,$idh);
  
  if(count($res_lis_regp)>0){
    //para sacar del insert
    echo "<br><br><center><font color='red'><b>FECHA ".$fechai." Y TURNO ". $res_lis_regp[0]['horario']." YA REGISTRADO</b></font></center>";
  }else{
  $Objrp->registrar_regproduccion($con, $dni, $fecha, $idh,$ida,$idact, $idth, $numpaq);
  echo "<script type='text/javascript'>window.opener.location.reload(); window.close();</script>";
  }
    
  
  //si no es de central de esterilizacion
  }else{
  
  $dni=$_GET['dni'];  
  $fechai=$_GET['fecha'];
  $idh=$_GET['idh'];  
  $ida=$_GET['ida'];  
  $idact=$_GET['idact'];  
  $idth=$_GET['idth']; 
  
   
   $dia=substr($fechai, 0, 2);
   $mes=substr($fechai, 3, 2);
   $anno=substr($fechai, 6, 4);
   $fecha =$anno.'/'.$mes.'/'.$dia;
  
   $Objrp = new RegProduccion();
  $res_lis_regp = array();
  $res_lis_regp = $Objrp->listar_regproduccion_reg($con,$dni,$fecha,$idh);
    
  if(count($res_lis_regp)>0){
    //para sacar del insert
    echo "<br><br><center><font color='red'><b>FECHA ".$fechai." Y TURNO ". $res_lis_regp[0]['horario']." YA REGISTRADO</b></font></center>";
  
  }else{
   
  //print_r($_GET['noms']) ; 
  //$arr_dni = unserialize(stripslashes($_GET['dnis'])); 
  //$arr_nom = unserialize(stripslashes($_GET['noms'])); 
	 $tuVariableDNI = $_GET['dnis'];
     $tuVariableNom = $_GET['noms'];
	 $arr_dni = explode('|',$tuVariableDNI);
     $arr_nom = explode('|',$tuVariableNom);
     //print_r($arr_nom);
     //for ($i=0; $i<=count($arr_nom);$i++)
     //echo "\n<br>(".$i.")".$arr_nom[$i]."\n<br>";
  
  //print_r($arr_nom);
  //echo "lucas";
  $nump=0;
  //print_r( $arr_dni);
  //echo "<br>";
  //print_r( $arr_nom);

  for ($j=0;$j<count($arr_dni);$j++){
    //consulta si existe paciente
    $Objp = new Paciente();
    $res_pac = array();
    $res_pac = $Objp->buscar_paciente($con,$arr_dni[$j]);
    if(count($res_pac)==0){
      $Objp->registrar_paciente($con,$arr_dni[$j],$arr_nom[$j]);
	  
	  $Objrap = new AtencionPaciente();
	  $res_rap = array();
      $res_rap = $Objrap->registrar_atencion($con,$dni,$fecha,$idh,$arr_dni[$j]);	
	  $nump = $nump + 1;
    }else{
	 $Objrap = new AtencionPaciente();
     $res_rap = array();
     $res_rap = $Objrap->registrar_atencion($con,$dni,$fecha,$idh,$arr_dni[$j]);	
	 $nump = $nump + 1;	
    }
    
  
  }
  
  $Objrp->registrar_regproduccion($con, $dni, $fecha, $idh,$ida,$idact, $idth, $nump);
  echo "<script type='text/javascript'>window.opener.location.reload(); window.close();</script>";
  }
}
}

if(isset($_GET['dni'])){
require_once("class/class.conectar.php");
require_once("class/class.horario.php");
require_once("class/class.area.php");
require_once("class/class.actividad.php");
require_once("class/class.tipohora.php");

$dni =   $_GET['dni'];
  
$Objcon = new Conectar();
$con = $Objcon->con();

$Objh = new Horario();
$res_lis_hora = array();
$res_lis_hora = $Objh->listar_horario($con);

if (count($res_lis_hora)>0){
$horario = "<select name='cbhora' id='cbhora'>";
$horario = $horario."<option value='0'>Seleccione</option>";
for($ih=0;$ih<sizeof($res_lis_hora);$ih++){
$horario = $horario."<option value='".$res_lis_hora[$ih]['id']."'>".$res_lis_hora[$ih]['hora']."</option>";
}
$horario=$horario."</select>";
}

$Obja = new Area();
$res_lis_area = array();
$res_lis_area = $Obja->listar_area($con);

if (count($res_lis_area)>0){
$area_uo = "<select name='cbarea' id='cbarea' onChange='mostrarProd()'>";
$area_uo = $area_uo."<option value='0'>Seleccione</option>";
for($ia=0;$ia<sizeof($res_lis_area);$ia++){
$area_uo = $area_uo."<option value='".$res_lis_area[$ia]['id']."'>".$res_lis_area[$ia]['nom']."</option>";
}
$area_uo=$area_uo."</select>";
}else{
$area_uo ="NO EXISTE REGISTRO";
}

$Objact = new Actividad();
$res_lis_actividad = array();
$res_lis_actividad = $Objact->listar_actividad($con);

if (count($res_lis_actividad)>0){
$actividad = "<select name='cbactividad' id='cbactividad' >";
$actividad = $actividad."<option value='0'>Seleccione</option>";
for($iact=0;$iact<sizeof($res_lis_actividad);$iact++){
$actividad = $actividad."<option value='".$res_lis_actividad[$iact]['id']."'>".$res_lis_actividad[$iact]['nom']."</option>";
}
$actividad=$actividad."</select>";
}else{
$actividad ="NO EXISTE REGISTRO";
}

$Objth = new TipoHora();
$res_lis_tipohora = array();
$res_lis_tipohora = $Objth->listar_tipohora($con);

if (count($res_lis_tipohora)>0){
$tipohora = "<select name='cbtipohora' id='cbtipohora'>";
$tipohora = $tipohora."<option value='0'>Seleccione</option>";
for($ith=0;$ith<sizeof($res_lis_tipohora);$ith++){
$tipohora = $tipohora."<option value='".$res_lis_tipohora[$ith]['id']."'>".$res_lis_tipohora[$ith]['nom']."</option>";
}
$tipohora=$tipohora."</select>";
}else{
$tipohora ="NO EXISTE REGISTRO";
}

?>
<!DOCTYPE html>
<html lang="es">
<head>
  <meta charset="UTF-8">
   <link rel="stylesheet" type="text/css" href="styles/Style.css">
   <script language="JavaScript" type="text/javascript" src="js/ajax.js"></script>
   <script>setTimeout('document.location.reload()',900000); </script> 
   <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 CerrarVentana(){ 
   //alert("hola mundo");
    window.close();
   } 
     
    function mostrarProd(){
      var idact = document.getElementById("cbarea").value;
      
      
      if (idact == 15){
         document.getElementById('texttitulopac').style.display='none'; 
       document.getElementById('texttitulopaq').style.display='block';            
        document.getElementById('prodcentral').style.display = 'block';
      document.getElementById('prodpacientes').style.display = 'none';
      }else{
        document.getElementById('texttitulopac').style.display='block'; 
       document.getElementById('texttitulopaq').style.display='none';      
        document.getElementById('prodcentral').style.display = 'none';
      document.getElementById('prodpacientes').style.display = 'block';
      }
      
      
    }
     
    function cargarprodact(){
      document.getElementById('texttitulopac').style.display='block'; 
       document.getElementById('texttitulopaq').style.display='none'; 
        document.getElementById('prodcentral').style.display = 'none';
      document.getElementById('prodpacientes').style.display = 'block';
    }
     
    
     
     /*function GrabarReg(){
       var dni = document.getElementById("dni").value;
       var fecha = document.getElementById("fechaini").value; 
       var idh = document.getElementById("cbhora").value; 
       var ida = document.getElementById("cbarea").value; 
       var idact = document.getElementById("cbactividad").value; 
       var idth = document.getElementById("cbtipohora").value; 
      
       var i;
       var subcadena
      /*
       for(i=1;i<=10;i++){
       if(document.getElementById("pdni"+i).value != "" && document.getElementById("pnom"+i).value != "" ){
           var dnip = document.getElementById("pdni"+i).value;
           var nomp = document.getElementById("pnom"+i).value;
           subcadena = "&dnip"+i+"="+dnip+"&nomp"+i+"="+nomp;
        }
        }*//*
        var numpac = 3;
        var aleatorio=Math.random();
        var cadena = "regnuevo.php?dni="+dni+"&fecha="+fecha+"&idh="+idh+"&ida="+ida+"&idact="+idact+"&idth="+idth+"&numpac="+numpac+"&save=1";
        document.location=cadena;
     }*/
  </script>
  <title>NUEVO REGISTRO</title>
</head>
<body onload="cargarprodact()">
  
   <?php  echo "<input type='hidden' id='dni' name='dni' value='".$dni."'>"; ?>
  <br><br>
  <table align="center" width="410">
    <tr>
      <td colspan="2" class="tablacabecera">&nbsp;&nbsp;&nbsp;&nbsp;REGISTRO DE PRODUCCION</td>     
    </tr>
    <tr>
      <td class="tabladetalle3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FECHA</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()"><img src="img/del.png" ></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>
    </tr>
    <tr>
      <td class="tabladetalle3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TURNO</td>
      <td><?php  echo $horario; ?></td>
    </tr>
    <tr>
      <td class="tabladetalle3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UNIDAD ORGANICA</td>
      <td><?php  echo $area_uo; ?></td>
    </tr>
    <tr>
      <td class="tabladetalle3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ACTIVIDAD</td>
      <td><?php  echo $actividad; ?></td>
    </tr>
    <tr>
      <td class="tabladetalle3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TIPO HORA</td>
      <td><?php  echo $tipohora; ?></td>
    </tr>
      <tr><td colspan="2">  
      
      <div id="texttitulopac">
      <table align="center" width="410">
      <tr><td class="tablacabecera" colspan="2">&nbsp;&nbsp;&nbsp;&nbsp;PACIENTES</PA></td></tr>
      </table>
      </div>
      
      <div id="texttitulopaq">
      <table align="center" width="410">
      <tr><td class="tablacabecera" colspan="2">&nbsp;&nbsp;&nbsp;&nbsp;PROD. LITROS</td></tr>
      </table>
      </div>
      </td>
      </tr>
            
      <tr><td colspan="2">
      
       <div id='prodcentral'>
        <table align="center" ><tr>
          <td class="tabladetalle1" colspan='2'>NRO LITROS</td>                
        </tr>
         <tr>
          <td  colspan='2'><input type="text" size="10" id="pdnrop" name="pdnrop" onkeypress="return event.charCode >=48 && event.charCode <=57"></td>          
        </tr>         
           </table>
         </div>
         
         <div id='prodpacientes'>
        <table align="center" width="410">
      
         
          <tr>
          <td class="tabladetalle1">DNI</td>
          <td class="tabladetalle1">NOMBRE</td>
          <td class="tabladetalle1">BUSCAR</td>
          </tr>
         <tr>
          <td><input type="text" size="10" id="pdni1" name="pdni1" onkeypress="return event.charCode >=48 && event.charCode <=57"></td>
          <td><div id="divp1"><input type="text" size="30" id="pnom1" name="pnom1" disabled="disabled"></div></td>
          <td align="center"><input type="button" value="+" onClick="bus_pac(1)"></td>
        </tr>
        <tr>
           <td><input type="text" size="10" id="pdni2" name="pdni2" onkeypress="return event.charCode >=48 && event.charCode <=57"></td>
          <td><div id="divp2"><input type="text" size="30" id="pnom2" name="pnom2" disabled="disabled"></div></td>
          <td align="center"><input type="button" value="+" onClick="bus_pac(2)"></td>
        </tr>
        <tr>
           <td><input type="text" size="10" id="pdni3" name="pdni3" onkeypress="return event.charCode >=48 && event.charCode <=57"></td>
          <td><div id="divp3"><input type="text" size="30" id="pnom3" name="pnom3" disabled="disabled"></div></td>
          <td align="center"><input type="button" value="+" onClick="bus_pac(3)"></td>
        </tr>
        <tr>
         <td><input type="text" size="10" id="pdni4" name="pdni4" onkeypress="return event.charCode >=48 && event.charCode <=57"></td>
          <td><div id="divp4"><input type="text" size="30" id="pnom4" name="pnom4" disabled="disabled"></div></td>
          <td align="center"><input type="button" value="+" onClick="bus_pac(4)"></td>
        </tr>
        <tr>
          <td><input type="text" size="10" id="pdni5" name="pdni5" onkeypress="return event.charCode >=48 && event.charCode <=57"></td>
          <td><div id="divp5"><input type="text" size="30" id="pnom5" name="pnom5" disabled="disabled"></div></td>
          <td align="center"><input type="button" value="+" onClick="bus_pac(5)"></td>
        </tr>
        <tr>
          <td><input type="text" size="10" id="pdni6" name="pdni6" onkeypress="return event.charCode >=48 && event.charCode <=57"></td>
          <td><div id="divp6"><input type="text" size="30" id="pnom6" name="pnom6" disabled="disabled"></div></td>
          <td align="center"><input type="button" value="+" onClick="bus_pac(6)"></td>
        </tr>
        <tr>
          <td><input type="text" size="10" id="pdni7" name="pdni7" onkeypress="return event.charCode >=48 && event.charCode <=57"></td>
          <td><div id="divp7"><input type="text" size="30" id="pnom7" name="pnom7" disabled="disabled"></div></td>
          <td align="center"><input type="button" value="+" onClick="bus_pac(7)"></td>
        </tr>
        <tr>
          <td><input type="text" size="10" id="pdni8" name="pdni8" onkeypress="return event.charCode >=48 && event.charCode <=57"></td>
          <td><div id="divp8"><input type="text" size="30" id="pnom8" name="pnom8" disabled="disabled"></div></td>
          <td align="center"><input type="button" value="+" onClick="bus_pac(8)"></td>
        </tr>
        <tr>
         <td><input type="text" size="10" id="pdni9" name="pdni9" onkeypress="return event.charCode >=48 && event.charCode <=57"></td>
          <td><div id="divp9"><input type="text" size="30" id="pnom9" name="pnom9" disabled="disabled"></div></td>
          <td align="center"><input type="button" value="+" onClick="bus_pac(9)"></td>
        </tr>
        <tr>
         <td><input type="text" size="10" id="pdni10" name="pdni10" onkeypress="return event.charCode >=48 && event.charCode <=57"></td>
          <td><div id="divp10"><input type="text" size="30" id="pnom10" name="pnom10" disabled="disabled"></div></td>
          <td align="center"><input type="button" value="+" onClick="bus_pac(10)"></td>
        </tr>
         <tr>
         <td><input type="text" size="10" id="pdni11" name="pdni11" onkeypress="return event.charCode >=48 && event.charCode <=57"></td>
          <td><div id="divp11"><input type="text" size="30" id="pnom11" name="pnom11" disabled="disabled"></div></td>
          <td align="center"><input type="button" value="+" onClick="bus_pac(11)"></td>
        </tr>
         <tr>
         <td><input type="text" size="10" id="pdni12" name="pdni12" onkeypress="return event.charCode >=48 && event.charCode <=57"></td>
          <td><div id="divp12"><input type="text" size="30" id="pnom12" name="pnom12" disabled="disabled"></div></td>
          <td align="center"><input type="button" value="+" onClick="bus_pac(12)"></td>
        </tr>
         <tr>
         <td><input type="text" size="10" id="pdni13" name="pdni13" onkeypress="return event.charCode >=48 && event.charCode <=57"></td>
          <td><div id="divp13"><input type="text" size="30" id="pnom13" name="pnom13" disabled="disabled"></div></td>
          <td align="center"><input type="button" value="+" onClick="bus_pac(13)"></td>
        </tr>
         <tr>
         <td><input type="text" size="10" id="pdni14" name="pdni14" onkeypress="return event.charCode >=48 && event.charCode <=57"></td>
          <td><div id="divp14"><input type="text" size="30" id="pnom14" name="pnom14" disabled="disabled"></div></td>
          <td align="center"><input type="button" value="+" onClick="bus_pac(14)"></td>
        </tr>
         <tr>
         <td><input type="text" size="10" id="pdni15" name="pdni15" onkeypress="return event.charCode >=48 && event.charCode <=57"></td>
          <td><div id="divp15"><input type="text" size="30" id="pnom15" name="pnom15" disabled="disabled"></div></td>
          <td align="center"><input type="button" value="+" onClick="bus_pac(15)"></td>
        </tr>
        
      </table>
      </div>
    </td>
    </tr>
    <tr>
      <td colspan="2" align="center"><br><input type="button" value="GRABAR" class="boton1" onClick="GrabarReg()"><input type="button" value="CANCELAR" class="boton1" onClick="CerrarVentana()"></td>      
    </tr>
  </table>
  
  
</body>
</html>
<?php
}/*else{
  echo "USUARIO NO REGISTRADO";
}*/
?>