<?php

include 'dbconnection.php';

$Email2 = $_GET["Email"]; 

$DB = mysqli_query($mysql,"SELECT dbname FROM tb_users WHERE email ='$Email2'");
while($row = mysqli_fetch_array($DB)){
$dbname = $row ['dbname'];}


$hostname2 = "localhost";
$username2 = "tempdpe5_nicolas";
$password2= "(Made7575)";
$conn = mysqli_connect($hostname2,$username2,$password2,$dbname);
if (mysqli_connect_errno()){
  printf("Connect failed: %s\n", mysqli_connect_error());
  exit();
} 
mysqli_set_charset($conn ,"utf8");


// Perform queries

mysqli_query($conn,"UPDATE calendar,residentes SET calendar.Nombres = residentes.Nombres, calendar.Apellidos = residentes.Apellidos,calendar.email = residentes.Email  WHERE calendar.Condominio = residentes.Condominio AND calendar.Residencia = residentes.Residencia AND DATEDIFF(STR_TO_DATE(calendar.fechadeinicio, '%Y-%m-%d %h:%i %p'), STR_TO_DATE(calendar.fechafinal, '%Y-%m-%d %h:%i %p'))<=0 AND TIMESTAMPDIFF(MINUTE,STR_TO_DATE(calendar.fechadeinicio, '%Y-%m-%d %h:%i %p'), STR_TO_DATE(calendar.fechafinal, '%Y-%m-%d %h:%i %p'))>0 ");

mysqli_query($conn,"UPDATE calendar,tb_users SET calendar.entry_by = tb_users.id  WHERE tb_users.email = calendar.email");


mysqli_query($conn,"UPDATE calendar SET tipomodulo = 'Reservacion' WHERE Status = 'Reservar'");  

mysqli_query($conn,"UPDATE calendar SET start = STR_TO_DATE(fechadeinicio, '%Y-%m-%d %h:%i %p') , end = STR_TO_DATE(fechafinal, '%Y-%m-%d %h:%i %p') WHERE Status = 'Reservar'AND DATEDIFF(STR_TO_DATE(fechadeinicio, '%Y-%m-%d %h:%i %p'), STR_TO_DATE(fechafinal, '%Y-%m-%d %h:%i %p'))<=0 AND TIMESTAMPDIFF(MINUTE,STR_TO_DATE(fechadeinicio, '%Y-%m-%d %h:%i %p'), STR_TO_DATE(fechafinal, '%Y-%m-%d %h:%i %p'))>0 ");

mysqli_query($conn,"UPDATE calendar SET Status = 'Aprobada' WHERE Status = 'Reservar'AND DATEDIFF(STR_TO_DATE(fechadeinicio, '%Y-%m-%d %h:%i %p'), STR_TO_DATE(fechafinal, '%Y-%m-%d %h:%i %p'))<=0 AND TIMESTAMPDIFF(MINUTE,STR_TO_DATE(fechadeinicio, '%Y-%m-%d %h:%i %p'), STR_TO_DATE(fechafinal, '%Y-%m-%d %h:%i %p'))>0 ");


mysqli_close($conn);
mysqli_close($mysql);

echo "<script>window.location.href='reservacionesadmin';</script>";

?>