<?php

/*
  Created on : Mayo 26, 2019, 01:14:34 PM
  Author     : Gabriel Hernandez Gonzalez
 */
 
	require_once "core_classes.php";					
	require_once "PHPMailer.php";
	require_once "SMTP.php";	
	require_once "Exception.php";
	require_once "OAuth.php";


	switch (filter_input(INPUT_POST, 'tipoOperacion')) {

		case 'enviarNotificacion':
		
			$pBodyMail = filter_input(INPUT_POST, 'pBody');
			$pTipoNotificacion = filter_input(INPUT_POST, 'pTipoNotificacion');
			$pBuque = filter_input(INPUT_POST, 'pBuque');
			$pListaCorreos = filter_input(INPUT_POST, 'pListaCorreos');
			$pNidServicio = filter_input(INPUT_POST, 'pNidServicio');
										 
			$mail = new PHPMailer\PHPMailer\PHPMailer();
			 
			$response = new root_response;
			try {
				/*
				$mail->SMTPDebug = 0;                                       // Enable verbose debug output
				$mail->isSMTP();                                            // Set mailer to use SMTP
				$mail->Host       = 'mail.setiveracruz.com.mx';  // Specify main and backup SMTP servers
				$mail->Port       = 26;                                    // TCP port to connect to
				$mail->SMTPSecure = 'tls';                                  // Enable TLS encryption, `ssl` also accepted
				$mail->SMTPAuth   = true;                                   // Enable SMTP authentication
				$mail->Username   = 'notificacionessios@setiveracruz.com.mx';                     // SMTP username
				$mail->Password   = 'vg223*r^!pvA.';                           */    // SMTP password
				
				//$mail->setFrom('notificacionessios@setiveracruz.com.mx', $pBuque);
				
				//
				$mail->SMTPDebug = 0;                                       // Enable verbose debug output
				$mail->isSMTP();                                            // Set mailer to use SMTP
				$mail->Host       = 'mail.moversa.mx';  // Specify main and backup SMTP servers
				$mail->Port       = 587;                                    // TCP port to connect to
				$mail->SMTPSecure = 'tls';                                  // Enable TLS encryption, `ssl` also accepted
				$mail->SMTPAuth   = true;                                   // Enable SMTP authentication
				$mail->Username   = 'amarres@moversa.mx';                     // SMTP username
				$mail->Password   = '!%-i8n_{;y?A';                               // SMTP password
				//
				//Recipients
				$mail->setFrom('amarres@moversa.mx', $pBuque);
				
				$vArrayCorreos = explode(',', $pListaCorreos);
				foreach($vArrayCorreos as $correo){					
					$mail->addAddress($correo);// Name is optional
				}
				
				//$mail->addAddress('isc.ghernandez@gmail.com', 'Pruebas');     // Add a recipient
				//$mail->addAddress('ellen@example.com');               // Name is optional
				//$mail->addReplyTo('info@example.com', 'Information');
				//$mail->addCC('cc@example.com');
				//$mail->addBCC('bcc@example.com');

				// Attachmentskkkkb
				//$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
				//$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

                // Attachments
			    $mail->addAttachment('../../dataAccess/primerCabo/'.$pNidServicio.'.pdf');         // Add attachments
				// Content
				$mail->isHTML(true);                                  // Set email format to HTML
				$mail->Subject = $pTipoNotificacion;
				$mail->Body    = $pBodyMail;
				$mail->send();
				
				$response->transaccion = "1";
				$response->mensaje = "Correo enviado correctamente...!!!";
							
			} catch (Exception $e) {
				$response->transaccion = "0";
				$response->mensaje = "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";				
			}	
			
			echo json_encode($response);
								
			break;
			
		case 'enviarNotificacionUltimoCabo':
		
			$pBodyMail = filter_input(INPUT_POST, 'pBody');
			$pTipoNotificacion = filter_input(INPUT_POST, 'pTipoNotificacion');
			$pBuque = filter_input(INPUT_POST, 'pBuque');
			$pListaCorreos = filter_input(INPUT_POST, 'pListaCorreos');
			$pNidServicio = filter_input(INPUT_POST, 'pNidServicio');
										 
			$mail = new PHPMailer\PHPMailer\PHPMailer();
			 
			$response = new root_response;
			try {
				
				$mail->SMTPDebug = 0;                                       // Enable verbose debug output
				$mail->isSMTP();                                            // Set mailer to use SMTP
				$mail->Host       = 'mail.moversa.mx';  // Specify main and backup SMTP servers
				$mail->Port       = 587;                                    // TCP port to connect to
				$mail->SMTPSecure = 'tls';                                  // Enable TLS encryption, `ssl` also accepted
				$mail->SMTPAuth   = true;                                   // Enable SMTP authentication
				$mail->Username   = 'amarres@moversa.mx';                     // SMTP username
				$mail->Password   = '!%-i8n_{;y?A';                               // SMTP password
				//
				//Recipients
				$mail->setFrom('amarres@moversa.mx', $pBuque);
				
				$vArrayCorreos = explode(',', $pListaCorreos);
				foreach($vArrayCorreos as $correo){					
					$mail->addAddress($correo);// Name is optional
				}
				
				//$mail->addAddress('isc.ghernandez@gmail.com', 'Cliente');     // Add a recipient
	            //$mail->addAddress('isc.ghernandez@gmail.com', 'Pruebas');
				// Attachments
				$mail->addAttachment('../../dataAccess/ultimoCabo/'.$pNidServicio.'.pdf');         // Add attachments
				// Content
				$mail->isHTML(true);                                  // Set email format to HTML
				$mail->Subject = $pTipoNotificacion;
				$mail->Body    = $pBodyMail;
				$mail->send();
				
				$response->transaccion = "1";
				$response->mensaje = "Correo enviado correctamente...!!!";
							
			} catch (Exception $e) {
				$response->transaccion = "0";
				$response->mensaje = "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";				
			}	
			
			echo json_encode($response);
								
			break;				
			
		case 'enviarProforma':
		
			$pNidServicio = filter_input(INPUT_POST, 'pNidServicio');
			$pBuque = filter_input(INPUT_POST, 'pBuque');
			$pListaCorreos = filter_input(INPUT_POST, 'pListaCorreos');
			
			$mail = new PHPMailer\PHPMailer\PHPMailer();
			 
			$response = new root_response;
			try {
				
				$mail->SMTPDebug = 0;                                       // Enable verbose debug output
				$mail->isSMTP();                                            // Set mailer to use SMTP
				$mail->Host       = 'mail.moversa.mx';  // Specify main and backup SMTP servers
				$mail->Port       = 587;                                    // TCP port to connect to
				$mail->SMTPSecure = 'tls';                                  // Enable TLS encryption, `ssl` also accepted
				$mail->SMTPAuth   = true;                                   // Enable SMTP authentication
				$mail->Username   = 'amarres@moversa.mx';                     // SMTP username
				$mail->Password   = '!%-i8n_{;y?A';                               // SMTP password
				//
				//Recipients
				$mail->setFrom('amarres@moversa.mx', $pBuque);
				
				$vArrayCorreos = explode(',', $pListaCorreos);
				foreach($vArrayCorreos as $correo){					
					$mail->addAddress($correo);// Name is optional
				}
				//$mail->addAddress('isc.ghernandez@gmail.com', 'Pruebas');
				//$mail->addAddress('isc.ghernandez@gmail.com', 'Cliente');     // Add a recipient
				//$mail->addAddress('ellen@example.com');               // Name is optional
				//$mail->addReplyTo('info@example.com', 'Information');
				//$mail->addCC('cc@example.com');
				//$mail->addBCC('bcc@example.com');

				// Attachments
				$mail->addAttachment('../../dataAccess/proformas/'.$pNidServicio.'.pdf');         // Add attachments										
				//$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

				// Content
				$mail->isHTML(true);                                  // Set email format to HTML
				$mail->Subject = "Proforma de servicio";
				$mail->Body    = "Se adjunta proforma del servicio prestado al buque:".$pBuque;

				$mail->send();
				
				$response->transaccion = "1";
				$response->mensaje = "Correo enviado correctamente...!!!";
							
			} catch (Exception $e) {
				$response->transaccion = "0";
				$response->mensaje = "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";				
			}	
			
			echo json_encode($response);
								
			break;
		//
		case 'enviarProformaContador':
		
			$pNidServicio = filter_input(INPUT_POST, 'pNidServicio');
			$pBuque = filter_input(INPUT_POST, 'pBuque');
			$pListaCorreos = filter_input(INPUT_POST, 'pListaCorreos');
			
			$mail = new PHPMailer\PHPMailer\PHPMailer();
			 
			$response = new root_response;
			try {
				
				$mail->SMTPDebug = 0;                                       // Enable verbose debug output
				$mail->isSMTP();                                            // Set mailer to use SMTP
				$mail->Host       = 'mail.moversa.mx';  // Specify main and backup SMTP servers
				$mail->Port       = 587;                                    // TCP port to connect to
				$mail->SMTPSecure = 'tls';                                  // Enable TLS encryption, `ssl` also accepted
				$mail->SMTPAuth   = true;                                   // Enable SMTP authentication
				$mail->Username   = 'amarres@moversa.mx';                     // SMTP username
				$mail->Password   = '!%-i8n_{;y?A';                               // SMTP password
				//
				//Recipients
				$mail->setFrom('amarres@moversa.mx', $pBuque);
				
				$vArrayCorreos = explode(',', $pListaCorreos);
				foreach($vArrayCorreos as $correo){					
					$mail->addAddress($correo);// Name is optional
				}
				//$mail->addAddress('isc.ghernandez@gmail.com', 'Pruebas');
				//$mail->addAddress('whernandez@setiveracruz.com.mx', 'Pruebas');
				//$mail->addAddress('isc.ghernandez@gmail.com', 'Cliente');     // Add a recipient
				//$mail->addAddress('ellen@example.com');               // Name is optional
				//$mail->addReplyTo('info@example.com', 'Information');
				//$mail->addCC('cc@example.com');
				//$mail->addBCC('bcc@example.com');

				// Attachments
				$mail->addAttachment('../../dataAccess/proformas/'.$pNidServicio.'.pdf');         // Add attachments										
				//$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

				// Content
				$mail->isHTML(true);                                  // Set email format to HTML
				$mail->Subject = "Solicitud de Factura de Servicio";
				$mail->Body    = "Buen día, favor de emitir la factura correspondiente al Servicio realizado al Buque:".$pBuque;

				$mail->send();
				
				$response->transaccion = "1";
				$response->mensaje = "Correo enviado correctamente...!!!";
							
			} catch (Exception $e) {
				$response->transaccion = "0";
				$response->mensaje = "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";				
			}	
			
			echo json_encode($response);
								
			break;
		//	
		default:
			echo json_encode(system_get_message(SISTEMA_PARAMETRO_NO_RECONOCIDO));
			break;
	}

        
?>
