<?php 
include_once '../conexion/conexion.php';
include '../validacion.php';
    date_default_timezone_set("America/Lima");
    $t = $_GET['t'];
	if($t == "m"){$turno = " - Mañana";} else if($t == "t"){$turno = " - Tarde";}
	$fecha = date("d-m-Y");;
	$nombre = "Lista de Postulantes ".date('d-m-Y', strtotime($fecha)).$turno.".xls";
	
	

			header('Expires: 0');
			header('Cache-control: private');
			header("Content-type: application/vnd.ms-excel;charset=iso-8859-15"); // Archivo de Excel
			header("Cache-Control: cache, must-revalidate"); 
			header('Content-Description: File Transfer');
			header('Last-Modified: '.date('D, d M Y H:i:s'));
			header("Pragma: public"); 
			header('Content-Disposition:attachment; filename="'.$nombre.'"');
			header("Content-Transfer-Encoding: binary");	




		$rol = $_GET['rol'];
		$solicitud = $_GET['solicitud'];
		

	echo utf8_decode("<table border='0'> 
						<tr > 
						<td style='font-weight:bold; border:1px solid #eee;background: #3b81ff;color:white;padding:10px; text-align: center;'>Nª</td>
						<td style='font-weight:bold; border:1px solid #eee;background: #3b81ff;color:white;padding:10px; text-align: center;'>POSTULANTE</td>
						<td style='font-weight:bold; border:1px solid #eee;background: #3b81ff;color:white;padding:10px; text-align: center;'>TIPO</td>
						<td style='font-weight:bold; border:1px solid #eee;background: #3b81ff;color:white;padding:10px; text-align: center;'>DOCUMENTO</td>
						<td style='font-weight:bold; border:1px solid #eee;background: #3b81ff;color:white;padding:10px; text-align: center;'>EDAD</td>
						<td style='font-weight:bold; border:1px solid #eee;background: #3b81ff;color:white;padding:10px; text-align: center;'>CELULAR</td>
						<td style='font-weight:bold; border:1px solid #eee;background: #3b81ff;color:white;padding:10px; text-align: center;'>DISTRITO</td>
						<td style='font-weight:bold; border:1px solid #eee;background: #3b81ff;color:white;padding:10px; text-align: center;'>FUENTE</td>
						<td style='font-weight:bold; border:1px solid #eee;background: #3b81ff;color:white;padding:10px; text-align: center;'>ESTADO</td>
						<td style='font-weight:bold; border:1px solid #eee;background: #3b81ff;color:white;padding:10px; text-align: center;'>ASISTENCIA</td>
						<td style='font-weight:bold; border:1px solid #eee;background: #3b81ff;color:white;padding:10px; text-align: center;'>CONTACTO</td>
						<td style='font-weight:bold; border:1px solid #eee;background: #3b81ff;color:white;padding:10px; text-align: center;'>ENTREVISTA</td>
                        <?php if($idusuario_sesion == 1) { ?>  <td style='font-weight:bold; border:1px solid #eee;background: #3b81ff;color:white;padding:10px; text-align: center;'>SEDE</td>  <?php } ?>
						</tr>");

                        date_default_timezone_set('America/Lima');;
                        $fecha = date("Y-m-d");
						$contador = 0;
                        $sql="SELECT * FROM agenda WHERE (fechaagenda BETWEEN '2023-05-01' AND '2023-05-31') or (fechareprogramacion BETWEEN '2023-05-01' AND '2023-05-31') ORDER BY sede";
						
						$consulta = $pdo->prepare($sql);
						$consulta->execute();
						$agendas = $consulta->fetchAll(PDO::FETCH_ASSOC);



				foreach ($agendas as $value) {

					$postulante = $value['postulante'];
					$tipodocumento = $value['tipodocumento'];
					$numerodocumento = $value['numerodocumento'];
					$edad = $value["edad"];
					$celular =  $value["celular"];
					$distrito = $value["distrito"];
					$fuente = $value["fuente"];
					$contactos = $value["idusuario"];
                    $sede = $value["sede"];
                    $turno = $value["turno"];
                    $agenda = $value['agenda'];
                    $vfechaagenda = $value['fechaagenda'];
                    $vfechareprogramacion = $value['fechareprogramacion'];
                    $estado = $value["estado"];
                    $asistencia = $value['asistencia'];
                    if($agenda == "AGENDADO"){
                        $fechaagenda = $vfechaagenda;
                    }
                    else if($agenda == "REPROGRAMADO"){
                        $fechaagenda = $vfechareprogramacion;
                    }
                    
					$contador = 1 + $contador;
                    					
					$sql = "SELECT * FROM usuario WHERE idusuario = '$contactos'";
						$consulta = $pdo->prepare($sql);
						$consulta->execute();
						$nusuario = $consulta->fetchAll(PDO::FETCH_ASSOC);

					foreach( $nusuario as $usuario){
						$contacto = $usuario["nombreusuario"];
					}
	
					 echo utf8_decode("<tr>
					 	<td style='border:1px solid #eee; text-align: start;'>".$contador."</td>	
						<td style='border:1px solid #eee; text-align: start;'>".$postulante."</td>
						<td style='border:1px solid #eee; text-align: center;'>".$tipodocumento."</td>
						<td style='border:1px solid #eee; text-align: center;'>".$numerodocumento."</td>
						<td style='border:1px solid #eee; text-align: center;'>".$edad."</td>
						<td style='border:1px solid #eee; text-align: center;'>".$celular."</td>
						<td style='border:1px solid #eee; text-align: start;'>".$distrito."</td>
						<td style='border:1px solid #eee; text-align: start;'>".$fuente."</td>
						<td style='border:1px solid #eee; text-align: start;'>".$agenda."</td>
						<td style='border:1px solid #eee; text-align: start;'>".$asistencia."</td>
						<td style='border:1px solid #eee; text-align: start;'>".$contacto."</td>
						<td style='border:1px solid #eee; text-align: start;'>".$fechaagenda."</td>
                        <?php if($idusuario_sesion == 1) { ?> <td style='border:1px solid #eee; text-align: center;'>".$sede."</td> <?php }?>
						</tr>");

				}


			echo "</table>";
		

			 ?>