<?php
session_start();

include "../conn.php";
include "../functions.php";

checkAdmin(session_id());

$title = "View Job Application Report";
include "header.php";

$page = $_GET["page"];
$key = $_GET["key"];
$jobid = $_GET["jobid"];
?>


<center>
<?php
$query = "select jobtitle from job where jobid='$jobid'";
$result = mysql_query($query) or die (mysql_error());
while ($row = mysql_fetch_array($result))
{
	$jobtitle = $row[0];
}
?>

<table width=80% border=0 cellspacing=1 cellpadding=5>
  <tr>
    <td align="center">
      Job Applications for <b><?php echo $jobtitle; ?></b>
    </td>
  </tr>
</table>

<table width=80%><tr><td align="right">
<table border=0 cellspacing=1 cellpadding=5>

<tr>
<td>
<input type="text" name="key" value="<?php echo $key; ?>">
</td>
<td>
<input type="submit" value="search">
</td>
</tr>
</table>
</td>
</form>
</tr>
</table>

<table width=80% border=0 cellspacing=1 cellpadding=5>
<?php

if (! isset($page))
{
	$page = 1;
}

$step = 30;

$start = ($page - 1) * $step;

echo "<tr bgcolor=\"a0a0a0\"><th>Application Date</th><th>Name</th><th>Email</th><th colspan=2>Options</th></tr>";

$query = "select application.appid, application.appdate, application.name, application.email, application.cvpath from application, job_application where job_application.jobid='$jobid' and application.appid=job_application.appid";
	
if (isset($key))
{
	$query .= " and (application.name like '%$key%' or application.email like '%$key%')";
}

$query .= " order by appdate desc limit $start, $step";

$result = mysql_query ($query) or die (mysql_error());

$trigger = 1;

while ($row = mysql_fetch_array($result))
{
	$appid = $row[0];
	$date = $row[1];
	$name = $row[2];
	$email = $row[3];
	$cvpath = $row[4];
	
	$trigger = ($trigger + 1) % 2;
	
	if ($trigger == 1)
	{
		echo "<tr bgcolor=\"#c9c9c9\">";
	}
	else
	{
		echo "<tr bgcolor=\"#e9e9e9\">";
	}
	
	echo "<td>$date</td><td>$name</td>";
	echo "<td>$email</td>";
	echo "<td width=1 align=\"center\"><form action=\"viewcv.php\" method=\"post\"><input type=\"hidden\" name=\"file\" value=\"$cvpath\"><input type=\"submit\" value=\"View CV\"></td></form>";
	echo "<td width=1 align=\"left\"><form action=\"delapplication.php\" method=\"post\"><input type=\"hidden\" name=\"appid\" value=\"$appid\"><input type=\"submit\" value=\"Delete\"></td></form>";
	echo "</tr>";
}
?>
</table>
</center>


<?php
echo "<center><table border=0 width=80%><tr><td align=right width=100% nowrap>";
if ($jobcatid > 0)
{
	$query = "select count(*) from job, job_cat where job_cat.jobcatid='$jobcatid' and job.jobid=job_cat.jobid";
	
	if (isset($key))
	{
		$query .= " and (job.jobtitle like '%$key%' or job.jobdescrip like '%$key%')";
	}
}
else
{
	$query = "select count(*) from job"; 

	if (isset($key))
	{
		$query .= " where (jobtitle like '%$key%' or jobdescrip like '%$key%')";
	}
}

$result = mysql_query($query) or die (mysql_error());
$row = mysql_fetch_array($result);
$total = $row[0];

if ($total % $step == 0)
{
	$pages = $total / $step;
}
else
{
	$pages = ceil($total / $step);
}

if ($pages > 1)
{
	echo "<table width=1 border=0 height=1><tr><td valign=bottom nowrap>Page:</td>";
	
	if ($pages <= 7)
	{
		for ($i = 1; $i <= $pages; $i++)
		{
			echo "<td align=left valign=bottom><a href=$PHP_SELF?page=$i&key=$key&groupID=$groupID>$i</a></td>";
		}
	}
	
	if ($pages > 7)
	{
		for ($i = 1; $i <= 7; $i++)
		{
			echo "<td align=left valign=bottom><a href=$PHP_SELF?page=$i&key=$key&groupID=$groupID>$i</a></td>";
		}
		echo "<td align=left valign=bottom>...<a href=$PHP_SELF?page=$pages&key=$key&groupID=$groupID>$pages</a></td>";
	}
	
	echo "<td align=left valign=bottom><form action=$PHP_SELF method=get><input type=text size=3 name=page><input type=submit value=\"Go\"></td></form>";
	echo "</tr></table>";
}
echo "</td></tr></table><center>";
include "footer.php";
?>