<?php
// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);

require '../../inc/PHPMailerAutoload.php';
include ('../../inc/confNetisse.php');


if(WTS_ENV == 'demo'){
	require './ses.php';
}

$sendmail = isset($PILE_CONF["SENDEMAIL"])?$PILE_CONF["SENDEMAIL"]:false;
if(isset($_GET['IDA']) && $sendmail) {

		$start = date('H:i:s');
		$IDA = $_GET['IDA'];
		$nom_agence = $_REQUEST['agence'];

		$partpro = 0;
		$conseiller=$_GET['conseiller'];
		$civ = "";
		if (isset($_GET['civ'])) $civ = $_GET['civ'];
		$nom = "";
		if (isset($_GET['nom'])) $nom = $_GET['nom'];
		$prenom = "";
		if (isset($_GET['prenom'])) $prenom = $_GET['prenom'];
		$company = "";
		if (isset($_GET['company'])) $company = $_GET['company'];
		$myst = ($conseiller == 0) ? 1 : 0;
		$phone = "";
		if (isset($_GET['phone'])) $phone = $_GET['phone'];
		$ticket = "";
		if (isset($_GET['ticket'])) $ticket = $_GET['ticket'];

		$ADRESSES = array();
		if($myst == 0){
			$mailCol = ($GLOBALS["BO_CONF"]["CONSEILLER"]["receive_email"]) ? ", receive_email" : "";
			$mailWhere = ($GLOBALS["BO_CONF"]["CONSEILLER"]["receive_email"]) ? " AND receive_email = 1" : "";
			$q = "SELECT email, nom, prenom, civ".$mailCol." FROM ca_conseillers WHERE id_conseiller='".$conseiller."'".$mailWhere;
			$r = excuteQuery($q);
                        if (!$r['status']) 
                           wts_die (var_dump($r['stmt']->errorInfo()));
			
                        while($o =$r['stmt']->fetch(PDO::FETCH_OBJ))
			{
				$to=$o->email;
				$ADRESSES[0] = $to;
				$consnom=$o->nom;
				$consprenom=$o->prenom;
				$consciv=$o->civ;
				$receivemail=$o->receive_email;
			}
		}
		else{
			$mailWhere = ($GLOBALS["BO_CONF"]["CONSEILLER"]["receive_email"]) ? " AND receive_email = 1" : "";
			$q = "SELECT email, nom, prenom, civ FROM ca_conseillers WHERE id_agence=".$agence." AND inactif=0 AND supp=0 AND fonction=4".$mailWhere;
			$r = excuteQuery($q);
                        if (!$r['status']) 
                           wts_die (var_dump($r['stmt']->errorInfo()));
			$i = 0;
			while($o = $r['stmt']->fetch(PDO::FETCH_OBJ))
			{
				$check = explode(";", $o->email);
				if (count($check) == 1) {
					if (!in_array($o->email, $ADRESSES)) {
						$ADRESSES[$i] = $o->email;
						$i++;
					}
				} else {
					for($j=0;$j<(count($check));$j++) {
						if (!in_array($check[$j], $ADRESSES)) {
							$ADRESSES[$i] = $check[$j];
							$i++;
						}
					}
				}
			}
			$consprenom="Je ne connais pas";
			$consnom="mon conseiller";
		}

		if(count($ADRESSES) > 0){

    		$headers ='From: "Borne d\'orientation"<'.$MAIL_FROM.'>'."\n";
    		$headers  = 'MIME-Version: 1.0' . "\r\n";
    		$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
            
    		$body=  '
    			<html xmlns="http://www.w3.org/1999/xhtml">
    			<head>
    			<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    			</head>
    			<body>
    			<table width="600" border="0" cellspacing="0" cellpadding="0">
    			  <tr>
    				<td align="center"><span style="font-family:Verdana, Geneva, sans-serif; font-size:16px; font-weight:bold;text-transform: uppercase;">'.$PROJECT_TITLE.'</span></td>
    			  </tr>
				  <tr>
					<td align="center">&nbsp;</td>
				  </tr>
				  <tr>
					<td align="center"><span style="font-family:Verdana, Geneva, sans-serif; font-size:14px; font-weight:bold;text-transform: uppercase;">'.$nom_agence.'</span></td>
				  </tr>
				  <tr>
					<td align="center">&nbsp;</td>
				  </tr>
    			  <tr>
    				<td align="center" bgcolor="#fafafa">&nbsp;</td>
    			  </tr>
    			  <tr>
    				<td align="center" bgcolor="#fafafa">
    					<span style="font-family:Verdana, Geneva, sans-serif; font-size:16px; font-weight:bold;">Un client s\'est enregistr&#233; sur la borne d\'orientation.</span></td>
    			  </tr>
    			  <tr>
    				<td align="center" bgcolor="#fafafa">&nbsp;</td>
    			  </tr>
    			  <tr>
    				<td align="center" bgcolor="#fafafa">
    				<table width="600" border="0" cellspacing="0" cellpadding="10">
    				  <tr>
    					<td align="right"><span style="font-family:Verdana, Geneva, sans-serif; font-size:12px;">Prise de rendez-vous préalable : </span></td>
    					<td align="left"><span style="font-family:Verdana, Geneva, sans-serif; font-size:12px; font-weight:bold;">Oui</span></td>
    				  </tr>
    				  <tr>
    				<td align="right"><span style="font-family:Verdana, Geneva, sans-serif; font-size:12px;">Nom du conseiller : </span></td>
    				<td align="left"><span style="font-family:Verdana, Geneva, sans-serif; font-size:12px; font-weight:bold;">'.$consprenom.' '.$consnom.'</span></td>
    			  </tr>
    			  <tr>
    				<td align="right"><span style="font-family:Verdana, Geneva, sans-serif; font-size:12px;">Heure d\'arriv&eacute;e : </span></td>
    				<td align="left"><span style="font-family:Verdana, Geneva, sans-serif; font-size:12px; font-weight:bold;">'.$start.'</span></td>
    			  </tr>';
    
    			  if ($nom != "") {
    			  	$body .=  '<tr>
    				<td align="right"><span style="font-family:Verdana, Geneva, sans-serif; font-size:12px;">Nom du client : </span></td>
    				<td align="left"><span style="font-family:Verdana, Geneva, sans-serif; font-size:12px; font-weight:bold;">'.$civ.' '.$prenom.' '.$nom.'</span></td>
    			  </tr>';
    			  }
    
    			  if ($phone != "") {
    			  	$body .=  '<tr>
    				<td align="right"><span style="font-family:Verdana, Geneva, sans-serif; font-size:12px;">Numéro de Téléphone : </span></td>
    				<td align="left"><span style="font-family:Verdana, Geneva, sans-serif; font-size:12px; font-weight:bold;">'.$phone.'</span></td>
    			  </tr>';
    			  }
    
    			  if ($ticket != "") {
    			  	$body .=  '<tr>
    				<td align="right"><span style="font-family:Verdana, Geneva, sans-serif; font-size:12px;">Numéro de Ticket : </span></td>
    				<td align="left"><span style="font-family:Verdana, Geneva, sans-serif; font-size:12px; font-weight:bold;">'.$ticket.'</span></td>
    			  </tr>';
    			  }
    
    
    			$body .= '</table>
    				</td>
    			  </tr>
    			  <tr>
    				<td align="center" bgcolor="#fafafa">&nbsp;</td>
    			  </tr>
    			  <tr>
    				<td align="center" bgcolor="#fafafa"><a href="'.$MAILRDVSRDVPATH.'/?IDA='.$IDA.'" style="font-family:Verdana, Geneva, sans-serif; font-size:14px; font-weight:bold;color:#c70317;">Acc&eacute;der &agrave; l\'interface de gestion client</a></td>
    			  </tr>
    			  <tr>
    				<td align="center" bgcolor="#fafafa">&nbsp;</td>
    			  </tr>
    			</table>
    
    			</body>
    			</html>';
    
    
    		if(WTS_ENV == 'demo'){
    	
    			$SENDERNAME = $PROJECT_ABBREVIATION;
    			$SENDEREMAIL = $MAIL_FROM;
    			if ($prenom != "") {
				$SUBJECT = "Borne d'accueil : RDV avec ".$civ." ".$prenom." ".$nom;
    			} else {
    				$SUBJECT = "Borne d'accueil : RDV avec ".$civ." ".$nom;
    			}
    
    			$ses = new SimpleEmailService('AKIAJ4AK54QVXKXT2VXA', 'OyNcXwcmTGhvxEqrgD8TjCwYeg68fPFXXBGVU2WZ');
    			$m = new SimpleEmailServiceMessage();
    			// ENCODAGE :
    			$m->setFrom($SENDERNAME.' <'.$SENDEREMAIL.'>');
    			$m->addReplyTo($SENDEREMAIL);
    			$m->setSubject($SUBJECT);
    			$m->setMessageFromString('',$body);
    			// DESTINATAIRES
    			$m->addTo($ADRESSES);
    			try {
    				$ses->sendEmail($m);
    				$return = array("status"=>true);
    			} 
    			catch (AwsException $e) {
    				$return = array("status"=>false, "message" => ("Mailer Error: " . $e->getMessage()));
    			}
    			echo json_encode($return);
    		}
    		else{
    			//Create a new PHPMailer instance
    			$mail = new PHPMailer();
    			//Tell PHPMailer to use SMTP
    			$mail->isSMTP();
    			//Enable SMTP debugging
    			// 0 = off (for production use)
    			// 1 = client messages
    			// 2 = client and server messages
    			$mail->SMTPDebug = 0;
    			//Ask for HTML-friendly debug output
    			$mail->Debugoutput = 'html';
    			//Set the hostname of the mail server
    			$mail->Host = SMTP_HOST;
    			//"10.154.187.197";
    			//Set the SMTP port number - likely to be 25, 465 or 587
    			$mail->Port = SMTP_PORT;
    			//Whether to use SMTP authentication
    			$mail->SMTPAuth = false;
    			//Username to use for SMTP authentication
    			$mail->Username = SMTP_USERNAME;
    			//Password to use for SMTP authentication
    			$mail->Password = SMTP_PASSWORD;
    			//Set who the message is to be sent from
    			$mail->setFrom(MAIL_FROM, $PROJECT_ABBREVIATION.' : Borne d\'orientation');
    			$mail->CharSet = 'UTF-8';
    			//Set who the message is to be sent to
    			for($i=0;$i<count($ADRESSES);$i++){
    				$mail->addAddress($ADRESSES[$i], $PROJECT_ABBREVIATION.' : Borne d\'orientation');
    			}
    			//Set the subject line
    			if ($prenom != "") {
    				$mail->Subject = "Borne d'accueil : RDV avec ".$civ." ".$prenom." ".$nom;
    			} else {
    				$mail->Subject = "Borne d'accueil : RDV avec ".$civ." ".$nom;
    			}
    			$mail->msgHTML($body);
    			
    			//send the message, check for errors
    			if($mail->send()) 
    			{
    				$return = array("status"=>true);
    			} 
    			else 
    			{
    				$return = array("status"=>false, "message" => ("Mailer Error: " . $mail->ErrorInfo));
    			}
    			echo json_encode($return);
    		}
    		/*	 POPUP NETSEND :
    		$myFile = "liste_Postes.txt";
    		$fh = fopen($myFile, 'a') or die("can't open file");
    		$stringData = $poste;
    		fwrite($fh, $stringData);
    		fclose($fh);
    		exec("Diffusion_popup.bat",$output);
    		*/
	    }

	} else {
		echo json_encode(array("status"=>false));
	}
?>