<?php
function out($m) {
  //echo "panda " . $m . PHP_EOL;
}

try {
  require '../vendor/autoload.php';

  $output = "";
  foreach($_POST as $key => $value) {
    $output .= "<h2>$key</h2><ul>";
    foreach($value as $formName => $formValue) {
      $fname = str_replace("_", " ", $formName);
      $output .= "<p><b>$fname</b> $formValue</p>";
    }
    $output .= "</ul>";
  }

  $sendgrid = new SendGrid("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");
  out("sendgrid");

  $email = new SendGrid\Email();
  $email
    -> setSubject('New Rental Application (' . date('M jS, Y') . ')')
    -> setFrom('no-reply@bdevelopments.ca')
    -> addTo('verwornevan@gmail.com')
    -> addTo('verworn@cogeco.ca')
    -> addTo('rentals@bdevelopments.ca')
    // -> addAttachment($target_file)
    -> setHtml($output);

  out("email");
  $res = $sendgrid->send($email);

} catch (Exception $e) {
  out($e->getMessage());
  out($e->getTraceAsString());
  echo "Error, please email rentals@bdevelopments.ca and notify them of this error and your rental submission.";
  return;
}
?>

<div style="margin: 3em auto auto 2em; max-width: 400px;">
<h1>Form Submission Successful</h1>
<p>Form has been submitted and will be reviewed. If you have any questions on the
  status of your application or of the rental property don't hesitate to contact
  rentals@bdevelopments.ca</p>
</div>
