 <?php
// Require the bundled autoload file - the path may need to change
// based on where you downloaded and unzipped the SDK

require_once '../vendor/autoload.php';

include 'dbconnection.php';

$ID2 = $_GET["ID"]; 
$Email2 = $_GET["Email"]; 

$DB = mysqli_query($mysql,"SELECT dbname FROM tb_users WHERE email ='$Email2'");
while($row5 = mysqli_fetch_array($DB)){
$dbname = $row5 ['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");




$ID2 = $_GET["ID"]; 

$sql3 = mysqli_query($conn,"SELECT * FROM calendar WHERE id ='$ID2' AND tipomodulo = 'Reservacion' AND Status IN ('Aprobada', 'No Aprobada')");
$count=mysqli_num_rows($sql3);
while($row3 = mysqli_fetch_array($sql3)){
   $Email[] = $row3['email'];
   $Fechainicio[] = $row3['fechadeinicio'];
   $Fechafinal[] = $row3['fechafinal'];
   $Localizacion[] = $row3['title'];
   $Descripcion[] = $row3['description'];
   $Status[]= $row3['status'];
   }
   

$sql= mysqli_query($conn,"SELECT * FROM mailconfig");
$row = mysqli_fetch_assoc($sql);

 $Server= $row ['mailserver'];
 $Username = $row ['usuarioserver'];
 $Password = $row ['password']; 
 $UsuarioID = $row ['UsuarioID']; 
 

try {
    // Create the SMTP Transport
    // Create the SMTP Transport
    $transport = (new Swift_SmtpTransport($Server,465,'ssl'))
        ->setUsername($Username)
        ->setPassword($Password);
 
    // Create the Mailer using your created Transport
    $mailer = new Swift_Mailer($transport);
 
 for ($i=0; $i < $count; $i++){
   
    // Create a message
    $message = new Swift_Message();
 
    if($Status[$i]=='Aprobada'){
    // Set a "subject"
    $message->setSubject('Reservación Aprobada');
    
    }else{
    // Set a "subject"
    $message->setSubject('Reservación NO APROBADA');    
        
    }
 
    // Set the "From address"
    $message->setFrom([$Username => $UsuarioID]);
 
    // Set the "To address" [Use setTo method for multiple recipients, argument should be array]

    $message->setTo($Email[$i]);
    
    
    // Add "BCC" address [Use setBcc method for multiple recipients, argument should be array]
    //$message->addBcc('recipient@gmail.com', 'recipient name');
 
    //Add an "Attachment" (Also, the dynamic data can be attached)
    
      if($Status[$i]=='Aprobada'){
     $message->setBody(
         
     '<html>' .
      ' <body>' .
      '<p> Saludos: </p>'.
      '<p> Su reservación ha sido aprobada. A continuación las informaciones en detalle : </p>'.
      '<table style="width:100%">'.
      '<tr>'.
      '<th>Localizacion</th>'. 
      '<th>Fecha de Inicio</th>'. 
      '<th>Fecha</th>'.
      '<th>Descripcion</th>'.
      '</tr>'.
      '<tr>'.
      '<td>'.$Localizacion[$i].'</td>'.
      '<td>'.$Fechainicio[$i].'</td>'.
      '<td>'.$Fechafinal[$i].'</td>'.
      '<td style="text-align: left;">'.$Descripcion[$i].'</td>'.
      '</tr>'.
      '</table>'.
      '<style>'.
      'td, th {border: 1px solid #adadad;padding: 8px;text-align: center;}'.
      'th {padding-top: 5px;padding-bottom: 5px;background-color: #2c5285;color: white;font-weight:900;}'.

      '</style>'.
      ' </body>' .
      '</html>',
      'text/html'    
         
         
        );
          
      }else{
            
    $message->setBody(
         
     '<html>' .
      ' <body>' .
      '<p> Saludos: </p>'.
      '<p> Su reservación No ha sido aprobada. El Area requerida a reservar, en la fecha indicada por usted, no tiene disponibilidad. A continuacion el detalle de su Solicitud de Reservacion :</p>'.
      '<table style="width:100%">'.
      '<tr>'.
      '<th>Localizacion</th>'. 
      '<th>Fecha de Inicio</th>'. 
      '<th>Fecha</th>'.
      '<th>Descripcion</th>'.
      '</tr>'.
      '<tr>'.
      '<td>'.$Localizacion[$i].'</td>'.
      '<td>'.$Fechainicio[$i].'</td>'.
      '<td>'.$Fechafinal[$i].'</td>'.
      '<td style="text-align: left;">'.$Descripcion[$i].'</td>'.
      '</tr>'.
      '</table>'.
      '<style>'.
      'td, th {border: 1px solid #adadad;padding: 8px;text-align: center;}'.
      'th {padding-top: 5px;padding-bottom: 5px;background-color: #2c5285;color: white;font-weight:900;}'.

      '</style>'.
      
      
      ' </body>' .
      '</html>',
      'text/html'    
         
         
        );         
            
            
            
        }
 
    $result = $mailer->send($message);
   
  } 

}

catch (Exception $e) {
  echo $e->getMessage();
}

mysqli_close($conn);
mysqli_close($mysql);
?> 
