<?php
require_once 'MU_database2024.php';
$dbObject = new MySQLDB;
// what's happening here.
// we're going to get a GUID, a method, an answer, and one or two parameters.
/*
		 case "Phone":= $("#PhoneNumber").val();
			break;
		 case "Text":= $("#TextNumber").val();
			break;
		 case "Email":= $("#EmailAddress").val();
			break;
		 case "Fax":_ContactInfo = $("#FaxNumber").val();
*/
try
{
	//var params = { "ContactType": ContactType, "ContactInfo":_ContactInfo, "PropertyManagerID":_PropertyManagerID };
	$dbObject->logger->logDebug("landlord_contact Info: ".print_r($_REQUEST,true));
    $MessageID = $_POST['MessageID'];				
    $ContactType = $_POST['ContactType'];
	$ContactInfo = $_POST['ContactInfo'];
	
	
	switch ($ContactType)
	{
		case "Phone":
			$retval =  SaveLLPhone($MessageID, $ContactInfo, $dbObject);
			break;
		case "Text":
			$retval = SaveLLText($MessageID, $ContactInfo, $dbObject);
			break;
		case "Email":
			$retval = SaveLLEmail($MessageID, $ContactInfo, $dbObject);
			break;
		case "Fax":
			$retval = SaveLLFax($MessageID,$ContactInfo,  $dbObject);
			break;
		default:
			$retval = "We had a problem saving your preferred contact methods.  Please contact Rental Kharma at support@rentalkharma.com and we'll figure this out \n";
	}
	
}
   
catch (Exception $e)
{
	$myMessage = $e->getMessage();
	$dbObject->logger->logError("Error updating contact info: ".$myMessage);
	
}
echo json_encode($retval);


function SaveLLPhone($GUID, $PhoneNumber, $dbObj)
{
	// this one's pretty easy.  We're just going to save an event to both the PM and RR that we got this notification.
	try
	{
		$UpdateMessage = "Set preferred contact to 'PHONE'.";
		
		
		//$q0 = "select distinct PM.PropertyManagerID , PM.Phone, formatPhone(?) from email_reminder ER inner join propertymanager PM on ER.PropertyManagerID = PM.PropertyManagerID where ER.MessageGUID = ?";
		$q0 = "select distinct PM.PropertyManagerID , PM.Phone, formatPhone(?) from lease_payment LP inner join lease L on LP.LeaseID = L.LeaseID ";
		$q0 = $q0." inner join propertymanager PM on L.PropertyManagerID = PM.PropertyManagerID where LP.ReminderGUID = ?";
		$LandlordQuery = $dbObj->connection->prepare($q0);
		$LandlordQuery->bindValue(1, $PhoneNumber);
		$LandlordQuery->bindValue(2, $GUID);
		$success = $LandlordQuery->execute();
		$dbObj->logger->logDebug("result of q0: ".$success.", guid: ".$GUID);
		$PropertyManagerInfo = $LandlordQuery->fetch();
		
		$dbObj->logger->logDebug("made it through first query in SaveNotPaidEvent");
		$dbObj->logger->logDebug("q0 results:".print_r($PropertyManagerInfo,true));
		if (isset($PropertyManagerInfo))
		{		
			$PropertyManagerID = $PropertyManagerInfo[0];
			$OldPhone = $PropertyManagerInfo[1];
			$NewPhone = $PropertyManagerInfo[2];
			$dbObj->logger->logDebug("PropertyManagerID results:".$PropertyManagerID);
			//if ($NewPhone != "")
			if ($NewPhone != $OldPhone)
			{
				$q = "update propertymanager set phone = formatPhone(?), preferredContact = 'PHONE' where propertymanagerID = ?";
				$PrefsQuery = $dbObj->connection->prepare($q);
				$PrefsQuery->bindValue(1, $NewPhone);
				$PrefsQuery->bindValue(2, $PropertyManagerID);
				$success = $PrefsQuery->execute();
				$UpdateMessage = $UpdateMessage." Old phone: ".$OldPhone.", New Phone: ".$NewPhone;
			} else			
			{
				$q = "update propertymanager set preferredContact = 'PHONE' where propertymanagerID = ?";
				$PrefsQuery = $dbObj->connection->prepare($q);
				$PrefsQuery->bindValue(1, $PropertyManagerID);
			}
			$success = $PrefsQuery->execute();
			if ($success == 1)
				$dbObj->logPropertymanagerEvent($UpdateMessage, $PropertyManagerID, "Update");
		}

	}
    catch (Exception $e)
	{
		$myMessage = $e->getMessage();
		$dbObj->logger->logError("Error updating contact info: ".$myMessage);
		
	}
	if ($success)
	{
		return "We have updated your contact preferences.  Thank you!";
	} else
		return "There was an error setting your contact preferences.  We're on it.";
}

function SaveLLText($GUID, $PhoneNumber, $dbObj)
{
	// this one's pretty easy.  We're just going to save an event to both the PM and RR that we got this notification.
	try
	{
		$UpdateMessage = "Set preferred contact to 'SMS'.";
		
		
		//$q0 = "select distinct PM.PropertyManagerID , PM.Phone, formatPhone(?) from email_reminder ER inner join propertymanager PM on ER.PropertyManagerID = PM.PropertyManagerID where ER.MessageGUID = ?";
		$q0 = "select distinct PM.PropertyManagerID , PM.Phone, formatPhone(?) from lease_payment LP inner join lease L on LP.LeaseID = L.LeaseID ";
		$q0 = $q0." inner join propertymanager PM on L.PropertyManagerID = PM.PropertyManagerID where LP.ReminderGUID = ?";
		$LandlordQuery = $dbObj->connection->prepare($q0);
		$LandlordQuery->bindValue(1, $PhoneNumber);
		$LandlordQuery->bindValue(2, $GUID);
		$success = $LandlordQuery->execute();
		$dbObj->logger->logDebug("result of q0: ".$success.", guid: ".$GUID);
		$PropertyManagerInfo = $LandlordQuery->fetch();
		
		$dbObj->logger->logDebug("made it through first query in SaveNotPaidEvent");
		$dbObj->logger->logDebug("q0 results:".print_r($PropertyManagerInfo,true));
		if (isset($PropertyManagerInfo))
		{		
			$PropertyManagerID = $PropertyManagerInfo[0];
			$OldPhone = $PropertyManagerInfo[1];
			$NewPhone = $PropertyManagerInfo[2];
			$dbObj->logger->logDebug("PropertyManagerID results:".$PropertyManagerID);
			//if ($NewPhone != "")
			if ($NewPhone != $OldPhone)
			{
				$q = "update propertymanager set phone = formatPhone(?), preferredContact = 'SMS' where propertymanagerID = ?";
				$PrefsQuery = $dbObj->connection->prepare($q);
				$PrefsQuery->bindValue(1, $NewPhone);
				$PrefsQuery->bindValue(2, $PropertyManagerID);
				$success = $PrefsQuery->execute();
				$UpdateMessage = $UpdateMessage." Old phone: ".$OldPhone.", New Phone: ".$NewPhone;
			} else			
			{
				$q = "update propertymanager set preferredContact = 'SMS' where propertymanagerID = ?";
				$PrefsQuery = $dbObj->connection->prepare($q);
				$PrefsQuery->bindValue(1, $PropertyManagerID);
			}
			$success = $PrefsQuery->execute();
			if ($success == 1)
				$dbObj->logPropertymanagerEvent($UpdateMessage, $PropertyManagerID, "Update");
		}

	}
    catch (Exception $e)
	{
		$myMessage = $e->getMessage();
		$dbObj->logger->logError("Error updating contact info: ".$myMessage);
		
	}
	if ($success)
	{
		return "We have updated your contact preferences.  Thank you!";
	} else
		return "There was an error setting your contact preferences.  We're on it.";
}


function SaveLLFax($GUID, $FaxNumber, $dbObj)
{
	// this one's pretty easy.  We're just going to save an event to both the PM and RR that we got this notification.
	try
	{
		$UpdateMessage = "Set preferred contact to 'FAX'.";
		
		
		//$q0 = "select distinct PM.PropertyManagerID , PM.FaxNumber, formatPhone(?) from email_reminder ER inner join propertymanager PM on ER.PropertyManagerID = PM.PropertyManagerID where ER.MessageGUID = ?";
		$q0 = "select distinct PM.PropertyManagerID , PM.Phone, formatPhone(?) NewNum from lease_payment LP inner join lease L on LP.LeaseID = L.LeaseID ";
		$q0 = $q0." inner join propertymanager PM on L.PropertyManagerID = PM.PropertyManagerID where LP.ReminderGUID = ?";
		$LandlordQuery = $dbObj->connection->prepare($q0);
		$LandlordQuery->bindValue(1, $FaxNumber);
		$LandlordQuery->bindValue(2, $GUID);
		$success = $LandlordQuery->execute();
		$dbObj->logger->logDebug("result of q0: ".$success.", guid: ".$GUID);
		$PropertyManagerInfo = $LandlordQuery->fetch();
		
		$dbObj->logger->logDebug("made it through first query in SaveLLFax");
		$dbObj->logger->logDebug("q0 results:".print_r($PropertyManagerInfo,true));
		if (isset($PropertyManagerInfo))
		{		
			$PropertyManagerID = $PropertyManagerInfo[0];
			$OldFax = $PropertyManagerInfo[1];
			$NewFax = $PropertyManagerInfo[2];
			$dbObj->logger->logDebug("PropertyManagerID results:".$PropertyManagerID);
			//if ($NewFax != "")
			if ($NewFax != $OldFax)	
			{
				$q = "update propertymanager set FaxNumber = formatPhone(?), preferredContact = 'FAX' where propertymanagerID = ?";
				$PrefsQuery = $dbObj->connection->prepare($q);
				$PrefsQuery->bindValue(1, $NewFax);
				$PrefsQuery->bindValue(2, $PropertyManagerID);
				$success = $PrefsQuery->execute();
				$UpdateMessage = $UpdateMessage." Old fax: ".$OldFax.", New Fax: ".$NewFax;
			} else			
			{
				$q = "update propertymanager set preferredContact = 'FAX' where propertymanagerID = ?";
				$PrefsQuery = $dbObj->connection->prepare($q);
				$PrefsQuery->bindValue(1, $PropertyManagerID);
			}
			$success = $PrefsQuery->execute();
			if ($success == 1)
				$dbObj->logPropertymanagerEvent($UpdateMessage, $PropertyManagerID, "Update");
		}

	}
    catch (Exception $e)
	{
		$myMessage = $e->getMessage();
		$dbObj->logger->logError("Error updating contact info: ".$myMessage);
		
	}
	if ($success)
	{
		return "We have updated your contact preferences.  Thank you!";
	} else
		return "There was an error setting your contact preferences.  We're on it.";
}


function SaveLLEmail($GUID, $EmailAddress, $dbObj)
{
	// this one's pretty easy.  We're just going to save an event to both the PM and RR that we got this notification.
	try
	{
		$UpdateMessage = "Set preferred contact to 'EMAIL'.";
		
		
		//$q0 = "select distinct PM.PropertyManagerID , PM.Email from email_reminder ER inner join propertymanager PM on ER.PropertyManagerID = PM.PropertyManagerID where ER.MessageGUID = ?";
		//$q0 = "select distinct PM.PropertyManagerID , PM.Phone, formatPhone(?) from lease_payment LP inner join lease L on LP.LeaseID = L.LeaseID ";
		//$q0 = $q0." inner join propertymanager PM on L.PropertyManagerID = PM.PropertyManagerID where LP.ReminderGUID = ?";
		$q0 = "select distinct PM.PropertyManagerID , PM.Email from lease_payment LP inner join lease L on LP.LeaseID = L.LeaseID ";
		$q0 = $q0." inner join propertymanager PM on L.PropertyManagerID = PM.PropertyManagerID where LP.ReminderGUID = ?";
		
		$LandlordQuery = $dbObj->connection->prepare($q0);
		$LandlordQuery->bindValue(1, $GUID);
		$success = $LandlordQuery->execute();
		$dbObj->logger->logDebug("result of q0: ".$success.", guid: ".$GUID);
		$PropertyManagerInfo = $LandlordQuery->fetch();
		
		$dbObj->logger->logDebug("made it through first query in SaveLLEmail");
		$dbObj->logger->logDebug("q0 results:".print_r($PropertyManagerInfo,true));
		if (isset($PropertyManagerInfo))
		{		
			$PropertyManagerID = $PropertyManagerInfo[0];
			$OldEmail = $PropertyManagerInfo[1];
			$dbObj->logger->logDebug("PropertyManagerID results:".$PropertyManagerID);
			//if ($EmailAddress != "")
			if ($EmailAddress != $OldEmail)
			{
				$q = "update propertymanager set Email = ? ,  preferredContact = 'EMAIL' where propertymanagerID = ?";
				$PrefsQuery = $dbObj->connection->prepare($q);
				$PrefsQuery->bindValue(1, $EmailAddress);
				$PrefsQuery->bindValue(2, $PropertyManagerID);
				$success = $PrefsQuery->execute();
				$UpdateMessage = $UpdateMessage." Old Email: ".$OldEmail.", New Email: ".$EmailAddress;
			} else			
			{
				$q = "update propertymanager set preferredContact = 'EMAIL' where propertymanagerID = ?";
				$PrefsQuery = $dbObj->connection->prepare($q);
				$PrefsQuery->bindValue(1, $PropertyManagerID);
			}
			$success = $PrefsQuery->execute();
			if ($success == 1)
				$dbObj->logPropertymanagerEvent($UpdateMessage, $PropertyManagerID, "Update");
		}

	}
    catch (Exception $e)
	{
		$myMessage = $e->getMessage();
		$dbObj->logger->logError("Error updating contact info: ".$myMessage);
		
	}
	if ($success)
	{
		return "We have updated your contact preferences.  Thank you!";
	} else
		return "There was an error setting your contact preferences.  We're on it.";
}	

?>
