<?php
	session_start();

	if (!(isset($_SESSION['VaildOkId']) && ($_SESSION['ICIC06_Grade']=="Administrator")))
	{	
		header("Location: ../submission.php");
   	exit;
	}
		
	require_once 'common.php';
	require_once 'connection.php';
	require_once 'Smarty.php';

	$smarty->assign('supportMail', $GLOBALS['supportMail']);
	$smarty->assign('contactMail', $GLOBALS['contactMail']);
	
	if (isset($_GET['message']))
	{
		if (isset($_GET['message']) > 1)
			$smarty->assign('message', $_GET['message'].'<br>There are '.$_GET['count'].' email of result notification have been successfully sent out.');
		else
			$smarty->assign('message', $_GET['message'].'<br>There is '.$_GET['count'].' email of result notification has been successfully sent out.');
	}
	
	// 接收上個頁面頁傳來的$page編號，以決定本頁顯示的資料
	$now_page = $_GET['page'];
	
	if($now_page == "")
		$now_page = 1;

	$order = $_GET['order'];
	if($order == "")
		$order = 1;
	
	$smarty->assign('order', $order);
	
	// 預設每頁顯示 30 筆
	if($_GET['num'] == "" && $_SESSION["show_num"] == "")
		$show_num = 30;
	else if($_GET['num'] != "")
	{
		$show_num = $_GET['num'];
		$_SESSION["show_num"] = $show_num;
	}
	else
		$show_num = $_SESSION["show_num"];
		
	$smarty->assign('num', $show_num);	
	
	// 本頁之起始指標
	$record_begin = ($now_page - 1) * $show_num;
	
	// 本頁實際顯示筆數
	$record_show = $show_num;

	// 計算資料總筆數
	$Q0 = "SELECT Paper_Sn FROM papers p,Author a Where p.Author_Sn=a.Author_Sn";
	$result0 = mysql_query($Q0, $conn) or die(mysql_error());
   $ntotal = mysql_num_rows($result0);

	$smarty->assign('totalRows', $ntotal);
	
	$Q02 = "SELECT count(Result) as AcceptCount FROM papers WHERE Result='Accept'";
	$result02 = mysql_query($Q02, $conn) or die(mysql_error());
	$row02 = mysql_fetch_assoc($result02);

	$smarty->assign('acceptCount', $row02["AcceptCount"]);
	
	$Q03 = "SELECT count(Result) as RejectCount FROM papers WHERE Result='Reject'";
	$result03 = mysql_query($Q03, $conn) or die(mysql_error());
	$row03 = mysql_fetch_assoc($result03);
	
	$smarty->assign('rejectCount', $row03["RejectCount"]);
	$smarty->assign('notYetCount', $ntotal-($row02["AcceptCount"]+$row03["RejectCount"]));
		
	// 本頁之實際顯示筆數不可大於總筆數
	if(($record_begin + $record_show) > $ntotal)
  		$record_show = $ntotal - $record_begin;
  	
  	// 計算總頁數
   $pagetotal = ceil($ntotal / $show_num);

	if($ntotal > 0) 
	{
		$nextpage = $now_page + 1;
		$prevpage = $now_page - 1;

		if($prevpage == 0)
		{
        	$smarty->assign('BTNFIRST', "First&nbsp;&nbsp;");
         $smarty->assign('BTNPREV', "Previous&nbsp;&nbsp;");
		}			
		else
		{
        	$smarty->assign('BTNFIRST', "<a href='javascript:showpage(1);'>First</a>&nbsp;&nbsp;");
         $smarty->assign('BTNPREV', "<a href='javascript:showpage($prevpage);'>Previous</a>&nbsp;&nbsp;");
		}

		if($nextpage > $pagetotal)
		{
         $smarty->assign('BTNNEXT', "Next&nbsp;&nbsp;");
         $smarty->assign('BTNLAST', "Last");
		}
		else
		{
         $smarty->assign('BTNNEXT', "<a href='javascript:showpage($nextpage);'>Next</a>&nbsp;&nbsp;");
         $smarty->assign('BTNLAST', "<a href='JavaScript:showpage($pagetotal);'>Last</a>");
		}

     	$smarty->assign('CURPAGE', $now_page);
     	$smarty->assign('TPAGE', $pagetotal);
     	$smarty->assign('TCOUNT', $ntotal);     	

		$Q1 = "SELECT p.*,Login_Id,First_Name,Last_Name FROM papers p,Author a WHERE p.Author_Sn=a.Author_Sn";

		switch ($order)
		{
		   case 1:
		      $smarty->assign('orderBy', "Paper ID");
		      $Q1.= " order by Paper_Sn ASC";
		      break;
		   case 2:
		      $smarty->assign('orderBy', "Paper Title");
		      $Q1.= " order by Paper_Title";
		      break;
		   case 3:
		      $smarty->assign('orderBy', "Author Name");
		      $Q1.= " order by First_Name,Last_Name,Paper_Sn";
		      break;
		   case 4:
		      $smarty->assign('orderBy', "Average Score");
		      $Q1.= " order by AvgScore DESC,Paper_Sn";
		      break;
		   case 5:
		      $smarty->assign('orderBy', "Result");
		      $Q1.= " order by Result,Paper_Sn";
		      break;
		   default:
		   	$smarty->assign('orderBy', "Paper ID");
		   	$Q1.= " order by Paper_Sn ASC";
		}
		$Q1.= " limit $record_begin, $record_show";

		$Recordset = mysql_query($Q1, $conn) or die(mysql_error());

		$papers = array();
		while($row_Recordset = mysql_fetch_assoc($Recordset))
		{
			array_push($papers, $row_Recordset);
		}
		
	   $smarty->assign('papers', $papers);
	}
	
	$options = array();
	for($b=1; $b <= $pagetotal; $b++)
	{  
	   if($b != $now_page)
		   array_push($options, "<option value='$b'>Page $b</option>");
		else
			array_push($options, "");
	}
	$smarty->assign('options', $options);
		
	$smarty->display('list_result.tpl.html');
	
	mysql_free_result($Recordset);
	mysql_free_result($Recordset1);
	mysql_free_result($Recordset2);
	mysql_free_result($Recordset3);
?>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       