#!/usr/bin/perl

require "config.idx";

$long = $ENV{'QUERY_STRING'};
if ($long) {
	@pairs=split(/&/,$long);
	foreach $item(@pairs) {
		($name,$content)=split (/=/,$item,2);
		$content=~tr/+/ /;
		$content=~ s/%(..)/pack("c",hex($1))/ge;
		if ($INPUT{$name}) { $INPUT{$name} = $INPUT{$name}.",".$content; }
		else { $INPUT{$name} = $content; }
	}
}
else {
	read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
	@pairs = split(/&/, $buffer);
	foreach $pair (@pairs) {
		($name, $value) = split(/=/, $pair);
		$value =~ tr/+/ /;
		$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
		if ($INPUT{$name}) { $INPUT{$name} = $INPUT{$name}.",".$value; }
		else { $INPUT{$name} = $value; }
	}
}

use DBI;
my $dsn = "DBI:mysql:database=$d_name;host=localhost";
my $dbh = DBI->connect($dsn, $d_username, $d_pass, {RaiseError => 1}) || die $DBI::errstr;

$Cook{agentid} = "600";

my $aagsth = $dbh->prepare("select firstname,lastname,email from agents where mlsid = '$Cook{agentid}' order by agentid DESC LIMIT 0,5");
$aagsth->execute;
while (($otagent_firstname,$otagent_lastname,$otagent_email) = $aagsth->fetchrow_array()) {

	if ($otagent_firstname eq 'Aaron') {
	$byname = substr(lc($otagent_firstname),0,2);
	}
	else {
	$byname = substr(lc($otagent_firstname),0,1);
	}

my $agsth = $dbh->prepare("select DATE_FORMAT(now(), '%b %D, %Y')");
$agsth->execute;
while (($datenow2) = $agsth->fetchrow_array()) {

#CALLS OR LEADS

$getcalldata_na .=qq~
<center>
<table border="0" cellpadding="0" cellspacing="0" width="581">
~;

	my $asqlcall = $dbh->prepare("select id,DATE_FORMAT(datetime, '%b %D, %Y at %h\:%i%p'),from_msg,subject,body,accepted,accepted_by,accepted_type from trans_callleadtracker $sql ORDER BY id DESC LIMIT 0,30");
	$asqlcall->execute;
	while (($id,$datetime,$from_msg,$subject,$body,$accepted,$accepted_by,$accepted_type) = $asqlcall->fetchrow_array()) {
	$sendemail = "yes";

if ($accepted eq "y") { $setdarkcolor = "#009900"; $setlightcolor = "#ccffcc"; } elsif ($accepted_type eq "Attempted") { $setdarkcolor = "#0066ff"; $setlightcolor = "#99ccff"; } else { $setdarkcolor = "#ff0000"; $setlightcolor = "#ffcccc"; } 
if ($accepted eq "n" && $accepted_type eq "Attempted") { 
$acceptcall =qq~
<table border="0" align="right" cellpadding="1" cellspacing="1" width="260" bgcolor="white">
	<tr>
		<td bgcolor="#0066ff" align="center" nowrap><font color="white">
			<b>Accept Message As:</b><br> <a class="topmenu" href="http://www.munciels.com/accept_lead.php?id=$id&by=$byname&as=call"><img src='http://www.munciemls.com/images/callsystem/callnf.gif' width='225' height='35' alt='Call / No Followup' border='0'></a>
			<a class="topmenu" href="http://www.munciels.com/accept_lead.php?id=$id&by=$byname&as=lead"><img src='http://www.munciemls.com/images/callsystem/lead.gif' width='75' height='35' alt='Lead' border='0'></a></font></td></tr></table>

~;
}
elsif ($accepted eq "n") { 
$acceptcall =qq~
<table border="0" align="right" cellpadding="1" cellspacing="1" width="260" bgcolor="white">
	<tr>
		<td bgcolor="red" align="center" nowrap><font color="white">
			<b>Accept Message As:</b><br> <a class="topmenu" href="http://www.munciels.com/accept_lead.php?id=$id&by=$byname&as=call"><img src='http://www.munciemls.com/images/callsystem/callnf.gif' width='225' height='35' alt='Call / No Followup' border='0'></a>
			<a class="topmenu" href="http://www.munciels.com/accept_lead.php?id=$id&by=$byname&as=lead"><img src='http://www.munciemls.com/images/callsystem/lead.gif' width='75' height='35' alt='Lead' border='0'></a>
			<a class="topmenu" href="http://www.munciels.com/accept_lead.php?id=$id&by=$byname&as=attempted"><img src='http://www.munciemls.com/images/callsystem/attempted.gif' width='135' height='35' alt='Attempted' border='0'></a></font></td></tr></table>
~;
}
else {
$acceptcall = "";
}
#$body = substr $body, 38;
#$body =~ s/-//g;
#$body =~ s/=//g;


$getcalldata_na .=qq~
		<tr>
			<td valign="middle" width="30" align="center" bgcolor="black">
				<font color="white" size="2" face="Tahoma"><b>#$id</b><font size="1">$accepted_type</font></font><br>
			<td valign="middle" align="center" width="273" bgcolor="$setdarkcolor">
				 <font color="white"><b>$datetime $from_msg</b></font><br>
			</td>
			<td valign="middle" align="right" width="275" bgcolor="$setdarkcolor">
				<img src="http://www.munciels.com/lead_status.php?id=$id" width="275" height="16"><br>
			</td>
		</tr>
		<tr>
			<td colspan="3" valign="top" width="100%" bgcolor="$setlightcolor">$acceptcall<font size="2">$subject $body</font><br></td>
		</tr>
		<tr>
			<td colspan="3" valign="top" width="100%" bgcolor="white"><br></td>
		</tr>
		~;
	}
$getcalldata_na .=qq~
</table>
</center>
~;



	if ($sendemail eq "yes") {
					open(MAIL,"|/usr/sbin/sendmail -t");
					print MAIL "To: $otagent_email\n";
					print MAIL "From: Info\@ORRHomes.com\n";
					print MAIL "Subject: ORRTEAM Daily Call and Lead Summary for $datenow2\n";
					print MAIL "Content-type: text/html\n\n";
					print MAIL qq~
					<html>
					<head>
					<title>Task Report for $otagent_firstname $otagent_lastname - Connect.EagleSold.com</title>
					<link rel="stylesheet" TYPE="text/css" href="includes/style.css">
					</head>
					<body bgcolor="white" leftMargin="0" topMargin="0" marginwidth="0" marginheight="0">
					<center>
					<table border="0" cellspacing="0" cellpadding="0" width="600">
					<tr>
					<td valign="top" width="600">
					<img src="http://www.munciemls.com/images/emailheader.gif"><br>
					<center><font size="5" face="Arial Narrow"><b>ORRTEAM DAILY Call and Lead Summary LAST 30</b><br></font></center>
					</td>
					</tr>
					</table>
					<table border="0" cellpadding="3" cellspacing="1" width="100%">
					<tr>
					<td valign="top" width="100%">
					$getcalldata_na
					</td>
					</tr>
					</table>
<img src="http://www.munciemls.com/images/emailfooter.gif"><br><br><br>
</center>
</body>
</html>
					~;
					close(MAIL);

$getcalldata_na = "";
		}
}
}