<?php
require_once("config.php");
require("./contactdata.php");

header('Content-Type: application/json');
if (!$link = @mysqli_connect($database['MYSQL_HOST'], $database['MYSQL_USER'], $database['MYSQL_PASSWORD'])) {
	echo json_encode(array('error' => true));
	exit;
}

if (!@mysqli_select_db($link, $database['MYSQL_DB'])) {
	echo json_encode(array('error' => true));
	exit;
}


if ($_SERVER['REQUEST_METHOD'] == 'POST'){

	$postdata = file_get_contents("php://input");
	$request = json_decode($postdata);

	if ( $request->franklin_action == 'committeesubmitform' ){
		$lname = $request->lname;
		$fname = $request->fname;
		$area = $request->area;
		$hours = $request->hours;
		$college = $request->college;
		$study = $request->study;
		$email = $request->email;
		$address = $request->address;
		$city = $request->city;
		$state = $request->state;
		$zip = $request->zip;
		$phone = $request->phone;
		$received_loan_before = $request->received_loan_before;
		$committee_member = trim($request->committee_member);
		$comments = $request->comments;


		if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
			echo json_encode(array('error' => true,"message"=>"Invalid email format"));
			exit;
		}


		if ($area == "Abilene") {
			$preto = "NPITTARD56@GMAIL.COM";
		}
		if ($area == "Austin") {
			$preto = "blasher@austin.utexas.edu, Marsha.Kelman@UTexas.edu";
		}
		if ($area == "College Station") {
			$preto = "abormann39@gmail.com";
		}
		if ($area == "Corpus Christi") {
			$preto = "MARGARET.DECHANT@TAMUCC.EDU";
		}
		if ($area == "Dallas Ft Worth") {
			$preto = "cjwilson.franklin.lindsay@gmail.com";
		}
		if ($area == "El Paso") {
			$preto = "lindaagh469@gmail.com";
		}
		if ($area == "Harlingen") {
			$preto = "hansstormy@yahoo.com";
		}
		if ($area == "Houston") {
			$preto = "apoliticalguy@outlook.com, rapptexas@gmail.com";
		}
		if ($area == "Lubbock") {
			$preto = "Jimmyp@pphef.org";
		}
		if ($area == "San Antonio") {
			$preto = "alfred27327@gmail.com";
		}
		if ($area == "Waco") {
			$preto = "wrolfk@gmail.com";
		}

		$sql = "insert into submissions (email, location, requestedby, dt) values ('".$preto."', '".$area."', '".$email."', now())";
		mysqli_query($link, $sql) or die(mysqli_error($link).' IN ['.$sql.']');

		//Going live
		$to = $preto . ', Deborah.L.Ottinger@jpmorgan.com';
		//For testing
		//$to = 'deb@dartcc.org';

		$from = $email;
		$subject = 'E-mail from the Franklin Lindsay Committee Contact form';
		$committee_member_str = $committee_member ? 'Name of the committee member: ' . $committee_member . "\r\n\n\n" : '';
		$message =
			'Name: ' . $lname . ', ' . $fname . "\r\n\n\n" .
			'Home Address: ' . $address . ', ' . $city . ', ' . $state . ' ' . $zip . "\r\n\n\n" .
			'Email Address: ' . $email . "\r\n\n\n" .
			'Geographic Area: ' . $area . "\r\n\n\n" .
			'Home Phone Number: ' . $phone . "\r\n\n\n" .
			'Number of Hours Completed: ' . $hours . "\r\n\n\n" .
			'University or College: ' . $college . "\r\n\n\n" .
			'Major area of study: ' . $study . "\r\n\n\n" .
			'Received Franklin Lindsay Loan: ' . $received_loan_before . "\r\n\n\n" .$committee_member_str.
			'Comments: ' . $comments . "\r\n\n";
		$headers = 'From: ' .  $from . "\r\n" .
			'Reply-To: contact@franklinlindsay.org' . "\r\n" .
			'X-Mailer: PHP/' . phpversion() . "\r\n" .
			'BCC: deb@iensemble.com' . "\r\n";


		$data = null;
		$htmlData = null;

		if (array_key_exists($area,$contactData)) {
			$data = $contactData[$area];
			$htmlContacts = array_map(function($cur) {
				return array(
					"contact"=>nl2br($cur['contact']),
					"email"=>$cur['email']
				);
			},$data['contacts']); 
			$htmlData = $data;
			$htmlData['contacts'] = $htmlContacts;
		}


		$mail2to = $email;
		//$mail2to = $email . ', deb@dartcc.org';
		$mail2from = "contact@franklinlindsay.org";
		$mail2subject = 'Franklin Lindsay Committee Contact';

		$mail2message = "Your information has been passed along to the Committee member listed below. Please note their contact information. If you do not hear from us in the next few days, please contact this person directly.\n\n";
		if ($data) {
			$mail2message .= $data['location'] . "\n\n";

			foreach($data['contacts'] as $row) {
				$mail2message .= $row['contact'] . "\n";
				$mail2message .= $row['email'] . "\n\n";
			}
		} else {
			$mail2message .= "Could not locate proper committee member";
		}


		$mail2headers = 'From: ' .  $mail2from . "\r\n" .
			'Reply-To: contact@franklinlindsay.org' . "\r\n" .
			'X-Mailer: PHP/' . phpversion() . "\r\n" .
			'BCC: deb@iensemble.com' . "\r\n";

		try{
			mail($to, $subject, $message, $headers);
			mail($mail2to,$mail2subject,$mail2message,$mail2headers);
			echo json_encode(array('error' => false,'data'=>$htmlData));
		}catch(Exception $e){
			echo json_encode(array('error' => true));
		}
	}

	if ( $request->franklin_action == 'contactsubmitform' ){
		$body = "\nThe following website feeback was sent on ".date("M d Y").": \n\nFrom:      " . $request->firstname . " " . $request->lastname . " \nEmail:     " . $request->email . " \nAddress:   " . $request->address . " \nPhone:     " . $request->phone . "\n\nHow do you feel that we could make this site better? \n" . $request->sitebetter . "\n\nHow did you hear about us? \n" . $request->hearofus		. "\n\nOther Comments or Questions: \n" . $request->comments."\r\r";
		try{
			mail ("deborah.l.ottinger@jpmorgan.com", "FranklinLindsay.org Website Feedback Form", $body, "From: postmaster@iensemble.com");
			mail ("deb@iensemble.com", "FranklinLindsay.org Website Feedback Form", $body, "From: postmaster@iensemble.com");
			echo json_encode(array('error' => false));
		}catch(Exception $e){
			echo json_encode(array('error' => true));
		}
	}

	if ( $request->franklin_action == 'sharesubmitform' ){
		try{
			$body = $request->message."\n\n\nThe Franklin Lindsay Student Aid Fund - www.FranklinLindsay.org - has information you might find helpful.";
			mail ($request->to, "FranklinLindsay.org from ".$request->to, $body, "From: ".$request->from);
			echo json_encode(array('error' => false));
		}catch(Exception $e){
			echo json_encode(array('error' => true));
		}
	}
}
?>
