<? include("../settings.php"); ?>
<? include("session.php"); ?>

<!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>Abstract Submission Portal</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Include all CSS/JS -->
        <!-- Bootstrap -->
    <link href="css/bootstrap.css" rel="stylesheet">
    <link href="css/jquery.dataTables.css" rel="stylesheet">
    <link href="https://cdn.datatables.net/responsive/1.0.1/css/dataTables.responsive.css" rel="stylesheet">
    <style>
    #tooltip
    {
        text-align: center;
        color: #fff;
        background: #111;
        position: absolute;
        z-index: 100;
        padding: 15px;
    }
 
    #tooltip:after /* triangle decoration */
    {
        width: 0;
        height: 0;
        border-left: 10px solid transparent;
        border-right: 10px solid transparent;
        border-top: 10px solid #111;
        content: '';
        position: absolute;
        left: 50%;
        bottom: -10px;
        margin-left: -10px;
    }
 
        #tooltip.top:after
        {
            border-top-color: transparent;
            border-bottom: 10px solid #111;
            top: -20px;
            bottom: auto;
        }
 
        #tooltip.left:after
        {
            left: 10px;
            margin: 0;
        }
 
        #tooltip.right:after
        {
            right: 10px;
            left: auto;
            margin: 0;
        }
    </style>

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="js/jquery-2.1.1.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
    <script src="js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/responsive/1.0.1/js/dataTables.responsive.js"></script>

    <!-- End of all CSS/JS -->

	<script>
		
		function selectTopic(){
			$('#reviewer').submit() ;
		}
    </script>
   </head>
<body>


<? include("nav_menu.php"); ?>
      <div class="well">
<div class="content" bgcolor="#FFFFD0">
<div class="col-12 feature">
<div class="panelCheck">
    <div class="panel-heading">
        <h3 class "panel-title"><strong>Topical Summary</strong></h3>
    </div>

    <form name="reviewer" method='POST' action="" enctype="multipart/form-data" class="form-horizontal" role="form" id="reviewer">
   
	<?
		$output = "<select name='topic_id' onchange='selectTopic();' class='form-control'><option value='0'>Please select</option>";
		if ($_SESSION['user_admin']=='1'){
			$results = $db->select("tbl_topic");
			foreach($results as $result){
				$output = $output."<option value='".$result['topic_id']."'>".$result['topic']."</option>"; 
			}
		}else{
			$bind = array(
				":search1"=>$_SESSION['user_id']
			);
			$results = $db->select("tbl_topic as a, tbl_topic_user as b","b.topic_id = a.topic_id AND b.user_id = :search1",$bind);
			foreach ($results as $result){
				$output = $output."<option value='".$result['topic_id']."'>".$result['topic']."</option>"; 
			}
		}
		$output = $output."<select name='topic_id'>";
	?>
    Topic : <?=$output;?><br/>
	<?
		if (isset($_REQUEST['topic_id']) || $_REQUEST['topic_id']!=0){
			$papers = $db->run("SELECT COUNT(*) as total FROM tbl_abstract WHERE abstract_deleted = 0 AND topic_id = '".$_REQUEST['topic_id']."'");
			$papertotal = $papers[0]['total'];
		}else{
			$papertotal = 0;
		}
	?>
	Total number of papers : <?=$papertotal;?></br>
	<?
		if (isset($_REQUEST['topic_id']) || $_REQUEST['topic_id']!=0){
			$reviews = $db->run("SELECT COUNT(*) as total FROM tbl_topic_user as a, tbl_abstract_user as b WHERE a.topic_id = '".$_REQUEST['topic_id']."' AND a.user_id = b.user_id AND b.user_principle = 0");
			$reviewertotal = $reviews[0]['total'];
		}else{
			$reviewertotal = 0;
		}
	?>
	Total number of reviewers : <?=$reviewertotal;?></br>
	<?
		$totalcompletereviews = $db->run("SELECT count(*) as total FROM tbl_abstract as a, tbl_review as b where b.abstract_id = a.abstract_id AND a.topic_id = '".$_REQUEST['topic_id']."'");
		$completedreviews=$totalcompletereviews[0]['total'];
	?>
    Total number of successfully reviewed papers : <?=$completedreviews;?>/<?=$papertotal*4?></br></br>  
    <?
		if (isset($_REQUEST['topic_id']) || $_REQUEST['topic_id']!=0){
			echo "<table id='myTable' class='table table-striped table-hover' cellspacing='0' width='100%'><thead><tr><th>#</th><th>Abstract Number</th><th>Abstract Title</th><th>Reviewer 1</th><th>Reviewer 2</th><th>Reviewer 3</th><th>Reviewer 4</th><th>Completed</th></tr></thead>";
			echo "<tbody>";
			$bind = array(
				":search1" => $_REQUEST['topic_id']
			);
			$topics = $db->select("tbl_topic","topic_id = :search1",$bind);
			$topic = $topics[0]['topic_name'];
			echo "<b>TOPIC : $topic</b><br/><br/>";
			
			$results = $db->select("tbl_abstract","abstract_deleted = 0 AND topic_id=:search1",$bind);
			$i=1;
			foreach ($results as $result){
				$detailbind = array(
					":search1"=>$result['abstract_id']
				);
				$assigned = $db->run("SELECT count(*) as total FROM tbl_abstract_review WHERE abstract_id = '".$result['abstract_id']."'");
				$assignedcount = $assigned[0]['total'];
				$review = $db->run("SELECT count(*) as total FROM tbl_review WHERE abstract_id = '".$result['abstract_id']."'");
				$reviewcount = $review[0]['total'];
				$completed = "No";
				if ($reviewcount > 3){
					$class = "success";
					$completed = "Yes";
				}else if ($assignedcount < 4){
					$class = "danger";
				}else
					$class = "info";
				$reviewers = $db->select("tbl_abstract_review as a, tbl_abstract_user as b","a.abstract_id = :search1 AND a.user_id=b.user_id",$detailbind);
				// Reviewer 1
				$reviewstatus1 = $db->run("SELECT count(*) as total FROM tbl_review WHERE abstract_id='".$result['abstract_id']."' AND abstract_user_id = '".$reviewers[0]['user_id']."'");
				if (isset($reviewers[0])){
					if ($reviewstatus1[0]['total']==1)
						$status1 = $reviewers[0]['user_name']." <span class='glyphicon glyphicon-ok' aria-hidden=true></span>";
					else
						$status1 = $reviewers[0]['user_name']." <span class='glyphicon glyphicon-remove' aria-hidden=true></span>";
				}else{
					$status1 = "N.A. <span class='glyphicon glyphicon-remove' aria-hidden=true></span>";
				}
				// Reviewer 2
				$reviewstatus2 = $db->run("SELECT count(*) as total FROM tbl_review WHERE abstract_id='".$result['abstract_id']."' AND abstract_user_id = '".$reviewers[1]['user_id']."'");
				if (isset($reviewers[1])){
					if ($reviewstatus2[0]['total']==1)
						$status2 = $reviewers[1]['user_name']." <span class='glyphicon glyphicon-ok' aria-hidden=true></span>";
					else
						$status2 = $reviewers[1]['user_name']." <span class='glyphicon glyphicon-remove' aria-hidden=true></span>";
				}else{
					$status2 = "N.A. <span class='glyphicon glyphicon-remove' aria-hidden=true></span>";
				}
				// Reviewer 3
				$reviewstatus3 = $db->run("SELECT count(*) as total FROM tbl_review WHERE abstract_id='".$result['abstract_id']."' AND abstract_user_id = '".$reviewers[2]['user_id']."'");
				if (isset($reviewers[2])){
					if ($reviewstatus3[0]['total']==1)
						$status3 = $reviewers[2]['user_name']." <span class='glyphicon glyphicon-ok' aria-hidden=true></span>";
					else
						$status3 = $reviewers[2]['user_name']." <span class='glyphicon glyphicon-remove' aria-hidden=true></span>";
				}else{
					$status3 = "N.A. <span class='glyphicon glyphicon-remove' aria-hidden=true></span>";
				}
				// Reviewer 4
				$reviewstatus4 = $db->run("SELECT count(*) as total FROM tbl_review WHERE abstract_id='".$result['abstract_id']."' AND abstract_user_id = '".$reviewers[3]['user_id']."'");
				if (isset($reviewers[3])){
					if ($reviewstatus4[0]['total']==1)
						$status4 = $reviewers[3]['user_name']." <span class='glyphicon glyphicon-ok' aria-hidden=true></span>";
					else
						$status4 = $reviewers[3]['user_name']." <span class='glyphicon glyphicon-remove' aria-hidden=true></span>";
				}else{
					$status4 = "N.A. <span class='glyphicon glyphicon-remove' aria-hidden=true></span>";
				}
				echo "<tr class='$class'><td>$i</td><td>".$result['abstract_number']."</td><td>".$result['abstract_title']."</td><td>".$status1."</td><td>".$status2."</td><td>".$status3."</td><td>".$status4."</td><td>".$completed."</td></tr>";
				$i++;
			}
			echo "</tbody></table>";
		}
	?>
    </form>

</div>
</div>
</div>
</div>
</body>
</html>