<?php
require_once ('promo/PHPMailer/class.phpmailer.php');
require_once('promo/PHPMailer/class.smtp.php');
  switch($Etape)
  {
    case 0:
	  $resultat = ClientWebTMP::getByValidationMethod();
	  echo "<table><tr><th>&nbsp;</th><th>ID</th><th>Nom</th><th>Prénom</th><th>Email</th><th>Natel</th><th>Date</th></tr>\n";
	  foreach($resultat as $ligne)
	  {
	    echo "<tr><td><a href=\"default.php?Type=$Type&Sous_Type=$Sous_Type&Etape=1&IdRequette=" . $ligne["IdRequette"] . "\">edit</a></td><td>" . $ligne["IdRequette"]. "</td><td>" . $ligne["Nom"] . "</td><td>" . $ligne["Prenom"] . "</td><td>" . $ligne["Email"] . "</td><td>" . $ligne["Natel"]. "</td><td>" . $ligne["Date"]. "</td></tr> \n";
	  }
	  echo "</table>\n";
    break;
    case 1:
	  $ligne = ClientWebTMP::getByIdRequette($IdRequette);
	  echo "<table><tr><th>Nom</th><td>".$ligne["Nom"]."</td></tr>\n";
	  echo "<tr><th>Prénom</th><td>".$ligne["Prenom"]."</td></tr>\n";
	  echo "<tr><th>Email</th><td>".$ligne["Email"]."</td></tr>\n";
	  echo "<tr><th>Natel</th><td>".Format_Telephone($ligne["Natel"])."</td></tr>\n";
	  echo "<tr><th>Date</th><td>".$ligne["Date"]."</td></tr></table>\n";
	  $resultat2 = Client::getClientByNom($ligne["Nom"]);
	  if (count($resultat2) != 0)
	  {
	    echo "<table><tr><th>&nbsp;</th><th>No client</th><th>Nom</th><th>Prénom</th><th>Adresse</th></tr>\n";
	    foreach($resultat2 as $ligne2)
	    {
	      echo "<tr><td><a href=\"default.php?Type=$Type&Sous_Type=$Sous_Type&Etape=2&IdRequette=$IdRequette&NoClient=" . $ligne2["NoClient"] . "\">Associer</a></td><td><a href=\"default.php?NoClient=" . $ligne2["NoClient"] . "&Type=3&Sous_Type=1&Etape=2&NoVisu=0\" target=\"_blank\" rel='opener'>" . $ligne2["NoClient"] . "</a></td><td>" . $ligne2["Nom"] . "</td><td>" . $ligne2["Prenom"] . "</td><td>" . $ligne2["Adresse"] . "</td></tr>\n";
	    }
	    echo "</table>\n";
	  }
	  else
	  {
	    echo "Aucun client existant trouvé<br/>\n";
	  }
	  echo "<a href=\"default.php?Type=$Type&Sous_Type=$Sous_Type&Etape=3&IdRequette=$IdRequette\">Créer nouveau client</a><br/>\n";
	  echo "<a href=\"default.php?Type=$Type&Sous_Type=$Sous_Type&Etape=4&IdRequette=$IdRequette\">Rejeter demande</a><br/>\n";
    break;
    case 2:
	$mail = new PHPMailer();
  	$mail->isSMTP();
  	$mail->setLanguage('fr', '../phpmailer/language/');
	  $ligne = ClientWebTMP::getByIdRequette($IdRequette);
      $requette2 = "UPDATE clients SET Password = '".$ligne["Password"]."', Salt = '".$ligne["Salt"]."', Email= '".$ligne["Email"]."', Natel='".$ligne['Natel']."' WHERE NoClient = $NoClient";
	  Connection::executeRawQuery($requette2);
	  $requette3 = "DELETE FROM clientswebtmp WHERE IdRequette = $IdRequette";
	  Connection::executeRawQuery($requette3);	  
	  $mail->Subject = "Souscription au Service Honda Geneve";
	  $mail->Body = "
		  
			Merci de votre Demande!
			
			Votre compte a été activé.
			   
		  ";
	  $mail->From= 'service@hondageneve.com';
	  $mail->addAddress($ligne['Email']);
	  $mail->send();
    break;
    case 3:
	$mail = new PHPMailer();
        $mail->isSMTP();
        $mail->setLanguage('fr', '../phpmailer/language/');
	
	  $ligne = ClientWebTMP::getByIdRequette($IdRequette);
	  $requette2 = "INSERT INTO clients (Nom, Prenom, TelPriv, TelProf, Natel, Email, Password, Salt) VALUES ('".$ligne['Nom']."', '".$ligne['Prenom']."', '', '', '".$ligne['Natel']."', '".$ligne['Email']."', '".$ligne['Password']."', '".$ligne['Salt']."')";
	  Connection::executeRawQuery($requette2);
	  $requette3 = "DELETE FROM clientswebtmp WHERE IdRequette = $IdRequette";
	  echo "$requette2<br/>$requette3</br>";
	  Connection::executeRawQuery($requette3);	  

	  $mail->Subject = "Souscription au Service Honda Geneve";
          $mail->Body = " 
			Merci de votre Demande!
			
			Votre compte a été activé.
			   
		  ";
	  $mail->From= 'service@hondageneve.com';
          $mail->addAddress($ligne['Email']);
          $mail->send();
    break;
    case 4:
	  $requette = "DELETE FROM clientswebtmp WHERE IdRequette = $IdRequette";
	  Connection::executeRawQuery($requette);	  
    break;
  }
?>
