<?php

$loc       = $_SESSION['location'];
$firstname = mysql_real_escape_string($_GET['customer_firstname']);
$lastname  = mysql_real_escape_string($_GET['customer_lastname']);
$email 	   = mysql_real_escape_string($_GET['customer_email']);
$phone	   = mysql_real_escape_string($_GET['customer_phone']);
$apptDate  = mysql_real_escape_string($_GET['apptDate']);
$apptTime  = mysql_real_escape_string($_GET['apptTime']);
$service = mysql_real_escape_string($_GET['service']);
$staff   = mysql_real_escape_string($_GET['staff']);
$notes   = mysql_real_escape_string($_GET['notes']);
$promo   = (isset($_GET['promotion'])) ? mysql_real_escape_string($_GET['promotion']) : 1;

$q = "INSERT INTO `newcustomers` (
                  `account_location_id`,
                  `firstname`,
				  `lastname`,
				  `email`,
				  `phone1`,
				  `date`,
				  `time`,
				  `service`,
				  `stylist`,
				  `notes`,
				  `promotion`
				  )
	VALUES ($loc, '$firstname', '$lastname', '$email', '$phone', '$apptDate', '$apptTime', '$service', '$staff', '$notes', $promo);";

if (!$res = mysql_query($q, $link)) {
    //echo 'database error';
}
//now send the customer an email
//$e_location = 'defaultlocation';  // get this from the database;
//$service = getServiceName($serviceID);
//$stylist = getStylistName($staffID);
$promoArray = getPromotion($promo, $_SESSION['account']);
$promohref  = $promoArray['results']['href'];
$promoimage = $promoArray['results']['img'];

/* 
# to get the 'name' of the location and account
$locArray = getLocation($row['account_location_id']);
foreach ($locArray['results'] as $k=>$v) {
 $fields[]= '__LOCATION_' . $k . '__';
 $values['LOCATION_' . $k] = $v;
 }
$locID = $locArray['results']['id'];
$locName = $locArray['results']['name'];
$acctID = $locArray['results']['account_id'];
$acctArray = getAccount($acctID);
$acctName = $acctArray['results']['name'];
*/

$email_tpl = 'default';
$email_tpl_path = 'email_templates/' . $loc . '_' . $email_tpl;
if (!$msg = file_get_contents($email_tpl_path)) {
    mail('jrotering@gmail.com, henry@immarketinggroup.com', 'New Customer Database: Error', 'Could not find an email template at "' . $email_tpl_path . '"');
}
else {
    $timestamp = strtotime($apptDate);
    $day = date('l', $timestamp);
    $msg = str_replace(
        array(
            '__date__',
            '__time__',
            '__day__',
            '__firstname__',
            '__lastname__',
            '__email__',
            '__phone__',
            '__service__',
            '__stylist__',
            '__notes__',
            '__promohref__',
            '__promoimage__'
        ),
        array(
            $apptDate,
            $apptTime,
            $day,
            $firstname,
            $lastname,
            $email,
            $phone,
            $service,
            $staff,
            $notes,
            $promohref,
            $promoimage
        ),
        $msg);

	// multiple recipients
	$to  = $email;

	

	// message
    $message = '
<html>
<head>
  <title>Appointment Confirmation</title>
</head>
<body>' . $msg . '

</body>
</html>
';
	// get ACCOUNT from database;
	$q = "SELECT `account_id` from `locations` WHERE `id` = $loc;";
	if (!$res = mysql_query($q, $link)) {
        	mail('jrotering@gmail.com', 'New Customer Database: Error', 'Could not find the account based on location');
	}
	else { 
   		$row = mysql_fetch_assoc($res);
   		$acct = $row['account_id'];
   		$q = "SELECT * FROM `accounts` WHERE `id` = $acct;";
   		if (!$res = mysql_query($q, $link)) {
    			//echo 'database error';
            		mail('jrotering@gmail.com', 'New Customer Database: Error', 'Could not retrieve account info');
   		}
   		else {
   			$row = mysql_fetch_assoc($res);
   			$account = $row['id'];
   			$from_name = $row['displayname'];
   			$from_email     = $row['email'];
   			 // GET THE DISPLAY NAME AND EMAIL
    		}
	}
	// subject
    $subject = 'Confirm Your '.$from_name.' Appointment';
	
	if (isset($from_name) && isset($from_email)) {

    		// To send HTML mail, the Content-type header must be set
    		$headers  = 'MIME-Version: 1.0' . "\r\n";
    		$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    		//$from_name = "Test Salon";
    		//$from_email = "jrotering@gmail.com";
		// Additional headers
    		$headers .= 'To: ' . $firstname . ' ' . $lastname . ' <' . $email . '>' . "\r\n";
    		$headers .= 'From: ' . $from_name . ' <' . $from_email . '>' . "\r\n";

		// Mail it
    		if (mail($to, $subject, $message, $headers)) {
        		mail('jrotering@gmail.com', 'appt confirmation was sent', '...');
    		}
    		else {
        		mail('jrotering@gmail.com', 'appt confirmation failed', '...message was ' . $msg);
    		}
    		$qs   .= "location=$locID";
	}
	else {
    		mail('jrotering@gmail.com, henry@immarketinggroup.com', 'New Customer Database: Error', 
    			'could not find the account display name and email for account ' . $account);
	}
}