#!/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; }
	}
}

&checklogin;

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;

if ($INPUT{action} eq "activity_log") { &activity_log; }
elsif ($INPUT{action} eq "delete") { &delete; }
else { &main; }

sub delete {


	if ($INPUT{delete} eq "yes") {
		#DELETE CODE
		@variables = ($INPUT{id});
		$sql_query = "DELETE FROM trans_callleadtracker WHERE id = ?";
		my $aasth = $dbh->do($sql_query, undef, @variables);
		print qq~
		<html>
		<head>
		<title>Making Changes to Datebase</title>
		<meta http-equiv="Refresh" content="1; URL=callsleads.idx">
		</head>
		<body bgcolor="white">
		<br><br><br><br>
		<center><font size="3" face="Arial" color="Red">Removing Call / Lead</font><br>You will be redirected to when finished.  If not <a href="callsleads.idx">Click Here</a>.</center>
		</body>
		</html>
		~;
	}
	else {
		print qq~
		<html>
		<head>
		<title>Delete</title>
		</head>
		<body bgcolor="white">
		<form method="get" action="callsleads.idx">
		<input type="hidden" name="action" value="delete">
		<input type="hidden" name="id" value="$INPUT{id}">
		<input type="hidden" name="delete" value="yes">
		<center><br><br>
		<font size="3" color="Red"><b>Do You Really Want to Delete <u>$INPUT{id}</u> Call/LEAD?</b><br>
		<input type="submit" value="Yes"> or <a href="callsleads.idx">Go Back</a><br>
		</center>
		</form>
		</body>
		</html>
		~;
	}


}


sub main {

unless ($INPUT{view}) { $INPUT{view} = "na"; }

print qq~
<html>
<head>
<title>Connect.EagleSold.com - ORRTEAM CALL LEAD CENTER</title>
~;

&header;

print qq~
<td valign="top" width="830">
<img src="images/headers/transaction_manager.gif" height="33" width="830" alt="Transaction Manager"><br><br>
~;
print qq~
<br>
<table border="0" cellpadding="3" cellspacing="1" width="100%">
<tr>
<td valign="top" width="100%">
Call OR Lead Logs   <b>View:</b> <a href="callsleads.idx?view=na">Not Accepted</a> | <a href="callsleads.idx?view=attempted">Attempted</a> | <a href="callsleads.idx?view=accepted">Accepted</a> | <a href="callsleads.idx?view=all">ALL</a><br><br>
~;
&getcalldata;

print qq~
</td>
</tr>
</table>
~;
&footer;
}



sub getcalldata {

unless ($INPUT{view}) { $INPUT{view} = "all"; }

	my $usrdasth = $dbh->prepare("select firstname from users where userid = '$Cook{userid}'");
	$usrdasth->execute;
	while (($afirstname) = $usrdasth->fetchrow_array()) {
		if ($afirstname eq 'Aaron') {
			$byname = substr(lc($afirstname),0,2);
		}
		else {
			$byname = substr(lc($afirstname),0,1);
		}
	}


print qq~
<table border="0" cellpadding="3" cellspacing="0" width="100%">
~;

if ($INPUT{view} eq "na") { $sql = "WHERE accepted = 'n'"; } elsif ($INPUT{view} eq "accepted") { $sql = "WHERE accepted = 'y'"; } elsif ($INPUT{view} eq "attempted") { $sql = "WHERE accepted = 'n' AND accepted_type = 'Attempted'"; } else { $sql = ""; }

	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,100");
	$asqlcall->execute;
	while (($id,$datetime,$from_msg,$subject,$body,$accepted,$accepted_by,$accepted_type) = $asqlcall->fetchrow_array()) {

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="1" align="right" cellpadding="4" cellspacing="3" width="260">
	<tr>
		<td align="center" nowrap>
			<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><br></td></tr></table>
~;
}
elsif ($accepted eq "n") { 
$acceptcall =qq~
<table border="1" align="right" cellpadding="4" cellspacing="3" width="260">
	<tr>
		<td align="center" nowrap>
			<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><br></td></tr></table>
~;
}
else {
$acceptcall = "";
}

#$body = substr $body, 38;
#$body =~ s/-//g;
#$body =~ s/=//g;
$body =~ s/www.ORRHomes.com\/includes\/style.css/\/\/connect.eaglesold.com\/includes\/style.css/g;


		print qq~
		<tr>
			<td valign="middle" width="15" align="center" bgcolor="black">
				<font color="white" face="Tahoma"><b>#$id</b></font><br>
			<td valign="middle" align="center" width="465" bgcolor="$setdarkcolor">
				 <font color="white"><b>$datetime $from_msg</b></font><br>
			</td>
			<td valign="middle" align="right" width="320" bgcolor="$setdarkcolor">
				<img src="http://www.munciels.com/lead_status.php?id=$id" width="320" height="25"><br></td>
			<td valign="middle" align="center" width="30" bgcolor="$setdarkcolor"><a href='callsleads.idx?action=delete&id=$id'>Delete</a></td>
		</tr>
		<tr>
			<td colspan="4" valign="top" width="100%" bgcolor="$setlightcolor">$acceptcall $accepted_type $subject $body<br></td>
		</tr>
		<tr>
			<td colspan="4" valign="top" width="100%" bgcolor="white"><br></td>
		</tr>
		~;
	}
print qq~
</table>
~;
}