#!/usr/bin/perl -wT

use strict;
use DBI();
use CGI qw(:standard);
use CGI::Session;
use CGI::Session::Driver::mysql;

my $query = new CGI;
$query->import_names('R');
my $cmd = ($R::act);
my $qs = $ENV{QUERY_STRING};
my $server = "www.ictprovision.com";
my $dbh;
my $host = "localhost";
my $db = "supportdb";
my $dbu = "www-data";
my $dpw = "999or666";
my $table1 = "issues";
my $rows = ''; my $stit; my $stxt; my $href; my @ex;
my @pri = ('dummy', 'Critical', 'Important', 'Routine', 'Request');
my @status = ('dummy', 'Open', 'Closed', 'In Hand', 'Cancelled');
my $Pprofile; my $Sprofile;

$dbh = DBI->connect("DBI:mysql:database=$db;host=$host", "$dbu", "$dpw", {'RaiseError' => 1});
my $session = new CGI::Session("driver:mysql", undef, { Handle => $dbh, TableName => "sessions" } );


if ( $session->param("~logged-in") ) {
	$Pprofile = $session->param("~Pprofile");
	$Sprofile = $session->param("~Sprofile");
	print "Content-type: text/html\n\n";
	my $qrystr = liststr();
#	my $sth = $dbh->prepare("select id, date_format(repdate, '%d/%m/%Y'), itit, itext, status, pri from $table1 where sid=?");
	my $sth = $dbh->prepare($qrystr);
#	$sth->execute($Sprofile->{uid});
	$sth->execute(@ex);

print <<END;

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Issues List</title>
<script src="../SpryAssets/SpryMenuBar.js" type="text/javascript"></script>
<link href="../SpryAssets/SpryMenuBarHorizontal.css" rel="stylesheet" type="text/css" />
<link href="../ict.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="container">
	<div id="banner" title="This site is best viewed with Mozilla Firefox or equivalent.">
		<ul id="ict_menu" class="MenuBarHorizontal">
			<li><a href="../index.htm">Home</a></li>
			<li><a href="../services.htm">Services</a></li>
			<li><a href="dashboard.cgi?act=dash">Dashboard</a></li>
			<li><a href="issues.cgi?act=issuesl">&nbsp;&nbsp;&nbsp;&nbsp;Issues</a></li>
			<li><a href="../links.htm">&nbsp;&nbsp;&nbsp;Links</a></li>
			<li><a href="../contact.htm">Contact Us</a></li>
		</ul>
	</div>
    <div id="issues"><table width="100%" border="1" cellpadding="4">
  <tr>
    <th width="9%" scope="col">No.</th>
    <th width="10%" scope="col">Date</th>
    <th width="28%" scope="col">Title</th>
    <th width="35%" scope="col">Description</th>
    <th width="9%" scope="col">Status</th>
    <th width="9%" scope="col">Piority</th>
  </tr>
  
END

while(my @row = $sth->fetchrow_array()) {
	if ( length($row[2]) > 32 ) { $stit = substr($row[2], 0, 32) . ".."; } else { $stit = $row[2]; }
	if ( length($row[3]) > 38 ) { $stxt = substr($row[3], 0, 38) . ".."; } else { $stxt = $row[3]; }
	$href = qq(<a href="addissue.cgi?act=View&iss=$row[0]">$row[0]</a>);
	$rows = "$rows<tr>
			<td>$href</td>
			<td>$row[1]</td>
			<td>$stit</td>
			<td>$stxt</td>
			<td>$status[$row[4]]</td>
			<td>$pri[$row[5]]</td>
		</tr>";
} 

print <<END;

$rows
</table>
</div>
    <div id="footer"></div>
</div>
<script type="text/javascript">
<!--
var MenuBar1 = new Spry.Widget.MenuBar("ict_menu", {imgDown:"../SpryAssets/SpryMenuBarDownHover.gif", imgRight:"../SpryAssets/SpryMenuBarRightHover.gif"});
//-->
</script>
</body>
</html>

END

} else {
#	print $query->redirect( -url => "http://$server/login.cgi?act=$cmd" );
	print $query->redirect( -url => "http://$server/login.cgi?$qs" );
}

sub liststr {

	my $lqry;
	
	if ( defined ($R::stat) ) {
	# 	We're only looking for open issues
		if ( defined ($R::typ) ) {
	# 		We only want open, critical issues
			$lqry = "select id, date_format(repdate, '%d/%m/%Y'), itit, itext, status, pri from $table1 where sid=? and pri=? and status=?
					order by id desc";
			@ex = ($Sprofile->{uid}, $R::typ, $R::stat);
		
		} elsif ( defined ($R::mine) ) {
		# 	We want all open issues for the specific user
			$lqry = "select id, date_format(repdate, '%d/%m/%Y'), itit, itext, status, pri from $table1 where sid=? and uid=? and (status=1 or status=3)
					order by id desc";		
			@ex = ($Sprofile->{uid}, $Pprofile->{uid});
		} else {
		# 	We want all open issues
			$lqry = "select id, date_format(repdate, '%d/%m/%Y'), itit, itext, status, pri from $table1 where sid=? and (status=1 or status=3)
					order by id desc";		
			@ex = ($Sprofile->{uid});
		}
	
	} elsif ( defined ($R::tot) ) {
	# 	We want the last 10 issues
		$lqry = "select id, date_format(repdate, '%d/%m/%Y'), itit, itext, status, pri from $table1 where sid=?
				order by id desc limit 10";		
		@ex = ($Sprofile->{uid});
	} elsif ( defined ($R::mine) ) {
	# 	We want all issues for the specific user
		$lqry = "select id, date_format(repdate, '%d/%m/%Y'), itit, itext, status, pri from $table1 where sid=? and uid=?
				order by id desc";		
		@ex = ($Sprofile->{uid}, $Pprofile->{uid});
	} else  {
	#	List all issues
		$lqry = "select id, date_format(repdate, '%d/%m/%Y'), itit, itext, status, pri from $table1 where sid=?
				order by id desc";
		@ex = ($Sprofile->{uid});
	}
	
	return $lqry;
}

sub dummysub {
#	The following lines are solely to suppress 'only used once' warnings
	$R::act = $R::act; $R::stat = $R::stat;  $R::tot = $R::tot; $R::typ = $R::typ;
}
