<?php
try {
  error_reporting(E_ALL); ini_set('display_errors', 1);
  require_once "./pineapple.php";
  require_once "./mysql/helper.php";
  pineapple_html_begin("Pineapple PNP", "Referrals");

  $db = db_connect("pontech_pineapplepnp");

  //print("<pre>\n");
  //print_r($_POST);
  //print_r($_SESSION);  // Warning, printing session vars could lead to security issues
  //print("</pre>\n");
	
	if(!isset($_SESSION['email']))
  {
		echo "<br>You must sign in to view this page<br>";
    goto exit_page;
  }
  
  $email = $_SESSION['email'];
  $referral_email = $_POST['referral_email'];
  
  // customer can't refer themselves
  if ($email == $referral_email){
    ?>
		<p>We appreciate your enthusiasm, but you can’t refer yourself. Please click <a href="pineapple_referral.php">here</a> to return to the referral page.</p>
		<?php
    goto exit_page;
  }
  
  // customer can't refer a currently registered customer
  $sql = "SELECT * FROM user WHERE email = '$referral_email'";
  $result = $db->query($sql);
  $num_rows = $result->rowCount();
  if ($num_rows > 0) {
    ?>
		<p>Your referral can't be processed because the provided e-mail is currenly registered. Please click <a href="pineapple_referral.php">here</a> to return to the referral page.</p>
		<?php
    goto exit_page;
  }
  
  // customer can't refer an already referred customer
  $sql = "SELECT * FROM referral WHERE referral_email = '$referral_email'";
  $result = $db->query($sql);
  $num_rows = $result->rowCount();
  if ($num_rows > 0) {
    ?>
		<p>Your referral can't be processed because the provided e-mail has already been referred by another customer. Please click <a href="pineapple_referral.php">here</a> to return to the referral page.</p>
		<?php
    goto exit_page;
  }
  
  // append to referral table
  $sql = "INSERT INTO referral SET email = ?, referral_email = ?";
  $sth = $db->prepare($sql); 
  $sth->execute(array($email,$referral_email));
  
  ?>
  <h2>Current Referrals</h2>
  <?php
  
  // All referrals and thier status
  $statement = "SELECT pontech_pineapplepnp.referral.referral_email, If(fullname Is Null,'','Yes') AS signed_up, If(phone_num_verified Is Null,''," . 
               "If(phone_num_verified=1,'Yes','')) AS verified_account FROM pontech_pineapplepnp.referral LEFT JOIN pontech_pineapplepnp.user ON " . 
               "referral.referral_email = user.email WHERE (((referral.email)='$email'))";
  db_table($db, $statement, "t01", NULL);
  
  // send email
  $sql = "SELECT user.fullname FROM user WHERE email='$email'";
  $sth = $db->query ($sql);
  while ($row = $sth->fetch (PDO::FETCH_NUM))
  {
    $customer_name = $row[0];
  }
  $message = "Hello,\n\n" . 
             "$customer_name has invited you to sign up for a Pineapple Pick & Place account. " . 
             "At Pineapple Pick & Place, our mission is to make surface mount pick and place of " . 
             "printed circuit boards assemblies an affordable turn-key process for the hobbyist " . 
             "and maker. By signing up for and validating an account you will automatically earn " . 
             "a $10 credit* on your first order. But wait, there's more. You can earn an additional " . 
             "$10 credit* for both yourself and each validated person that you refer.  Refer 100 " . 
             "people get $1000 in credit*..\n\n" . 
             "https://pineapplepnp.com/pineapple_signup.php \n\n" . 
             "Thank you for your time.\n\n" . 
             "-Pineapple Pick & Place\n\n" . 
             "*credits are applicable towards setup fees for orders placed and fulfilled.";
  
  mail($referral_email,"$customer_name has referred you to Pineapple PnP", $message, "From:Pineapple PnP Promotions<promotions@pontech.com>");
  
  ?>
  <h2>An invitation e-mail has been sent to <?php echo $referral_email; ?></h2>
  <p>Please note: in order to receive a referral credit, the referral must sign up and verify their account.</p>
  <?php
  
  $db = NULL;
  
exit_page:

	pineapple_script_end();
	pineapple_html_end();
}
catch (PDOException $e)
{
  print ("<br />$e");
}
?>