<? 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>Reviewer Assignment</strong></h3>
    </div>
	<?
		if (isset($_REQUEST['confirm'])){
		    $db->run("DELETE FROM tbl_abstract_review WHERE abstract_id=".$_REQUEST['abstract_id']);
			// Check for reviewer 1 
			if ($_REQUEST['reviewer1']!=0){
				$bind = array(
					":search1" => $_REQUEST['reviewer1'],
					":search2" => $_REQUEST['abstract_id']
				);
				$check = $db->select("tbl_abstract_review","abstract_id = :search2 AND user_id = :search1",$bind);
				if (count($check)==0){
					$insert = array(
						"abstract_id" => $_REQUEST['abstract_id'],
						"user_id" => $_REQUEST['reviewer1']
					);
					$db->insert("tbl_abstract_review",$insert);
				}
			}
			// Check for reviewer 2
			if ($_REQUEST['reviewer2']!=0){
				$bind = array(
					":search1" => $_REQUEST['reviewer2'],
					":search2" => $_REQUEST['abstract_id']
				);
				$check = $db->select("tbl_abstract_review","abstract_id = :search2 AND user_id = :search1",$bind);
				if (count($check)==0){
					$insert = array(
						"abstract_id" => $_REQUEST['abstract_id'],
						"user_id" => $_REQUEST['reviewer2']
					);
					$db->insert("tbl_abstract_review",$insert);
				}
			}
			// Check for reviewer 3
			if ($_REQUEST['reviewer2']!=0){
				$bind = array(
					":search1" => $_REQUEST['reviewer3'],
					":search2" => $_REQUEST['abstract_id']
				);
				$check = $db->select("tbl_abstract_review","abstract_id = :search2 AND user_id = :search1",$bind);
				if (count($check)==0){
					$insert = array(
						"abstract_id" => $_REQUEST['abstract_id'],
						"user_id" => $_REQUEST['reviewer3']
					);
					$db->insert("tbl_abstract_review",$insert);
				}
			}
			// Check for reviewer 4
			if ($_REQUEST['reviewer4']!=0){
				$bind = array(
					":search1" => $_REQUEST['reviewer4'],
					":search2" => $_REQUEST['abstract_id']
				);
				$check = $db->select("tbl_abstract_review","abstract_id = :search2 AND user_id = :search1",$bind);
				if (count($check)==0){
					$insert = array(
						"abstract_id" => $_REQUEST['abstract_id'],
						"user_id" => $_REQUEST['reviewer4']
					);
					$db->insert("tbl_abstract_review",$insert);
				}
			}
			$SQL1 = "";
			$SQL2 = "";
			// Remove previous assignments
			//if (($_REQUEST['reviewer1']!=0 && $_REQUEST['reviewer2']!=0 && $_REQUEST['reviewer3']!=0 && $_REQUEST['reviewer3']!=0)){
			//	$SQL1 = "DELETE FROM tbl_review WHERE abstract_id =".$_REQUEST['abstract_id']." AND user_id NOT IN (".$_REQUEST['reviewer1'].",".$_REQUEST['reviewer2'].",".$_REQUEST['reviewer3'].",".$_REQUEST['reviewer4'].")";
			//	$SQL2 = "DELETE FROM tbl_abstract_review WHERE abstract_id =".$_REQUEST['abstract_id']." AND user_id NOT IN (".$_REQUEST['reviewer1'].",".$_REQUEST['reviewer2'].",".$_REQUEST['reviewer3'].",".$_REQUEST['reviewer4'].")";
			//}
			//if (($_REQUEST['reviewer1']!=0 && $_REQUEST['reviewer2']==0)){
			//	$SQL1 = "DELETE FROM tbl_review WHERE abstract_id =".$_REQUEST['abstract_id']." AND user_id NOT IN (".$_REQUEST['reviewer1'].")";
			//	$SQL2 = "DELETE FROM tbl_abstract_review WHERE abstract_id =".$_REQUEST['abstract_id']." AND user_id NOT IN (".$_REQUEST['reviewer1'].")";
			//}
			//if (($_REQUEST['reviewer1']==0 &&  $_REQUEST['reviewer2']!=0)){
			//	$SQL1 = "DELETE FROM tbl_review WHERE abstract_id =".$_REQUEST['abstract_id']." AND user_id NOT IN (".$_REQUEST['reviewer2'].")";
			//	$SQL2 = "DELETE FROM tbl_abstract_review WHERE abstract_id =".$_REQUEST['abstract_id']." AND user_id NOT IN (".$_REQUEST['reviewer2'].")";
			//}
			//$db->run($SQL1);
			//$db->run($SQL2);
			echo "<script>alert('Assignment Updated');window.location='assignreviewer.php?topic_id=".$_REQUEST['topic_id']."';</script>";
		}
	
	
	?>
    <form name="reviewer_detail" method='POST' action="" enctype="multipart/form-data" class="form-horizontal" role="form" id="reviewer">
	<?
		$bind = array(
			"search1" => $_REQUEST['abstract_id']
		);
		$abstracts = $db->select("tbl_abstract","abstract_id = :search1",$bind);
		$abstract = $abstracts[0];
		$reviewers = $db->select("tbl_abstract_review as a, tbl_abstract_user as b","a.abstract_id = :search1 AND a.user_id=b.user_id",$bind);
	?>
	<font size="+1"> Abstract Number/Title : <?=$abstract['abstract_number'];?> <?=$abstract['abstract_title'];?></font><br/>
	Author : <?=$abstract['registrant_salutation'];?> <?=$abstract['registrant_firstname'];?> <?=$abstract['registrant_lastname'];?> ( <?=$abstract['registrant_organisation'];?> )<br/>
	<? if ($abstract['registrant_firstname1']!="") echo "Co-Author : ".$abstract['registrant_salutation1']." ".$abstract['registrant_firstname1']." ".$abstract['registrant_lastname1']." ( ".$abstract['registrant_organisation1']." )<br/>"; ?>
	<? if ($abstract['registrant_firstname2']!="") echo "Co-Author : ".$abstract['registrant_salutation2']." ".$abstract['registrant_firstname2']." ".$abstract['registrant_lastname2']." ( ".$abstract['registrant_organisation2']." )<br/>"; ?>
	<? if ($abstract['registrant_firstname3']!="") echo "Co-Author : ".$abstract['registrant_salutation3']." ".$abstract['registrant_firstname3']." ".$abstract['registrant_lastname3']." ( ".$abstract['registrant_organisation3']." )<br/>"; ?>
	<? if ($abstract['registrant_firstname4']!="") echo "Co-Author : ".$abstract['registrant_salutation4']." ".$abstract['registrant_firstname4']." ".$abstract['registrant_lastname4']." ( ".$abstract['registrant_organisation4']." )<br/>"; ?>
	<? if ($abstract['registrant_firstname5']!="") echo "Co-Author : ".$abstract['registrant_salutation5']." ".$abstract['registrant_firstname5']." ".$abstract['registrant_lastname5']." ( ".$abstract['registrant_organisation5']." )<br/>"; ?>
	<? if ($abstract['registrant_firstname6']!="") echo "Co-Author : ".$abstract['registrant_salutation6']." ".$abstract['registrant_firstname6']." ".$abstract['registrant_lastname6']." ( ".$abstract['registrant_organisation6']." )<br/>"; ?>
	<? if ($abstract['registrant_firstname7']!="") echo "Co-Author : ".$abstract['registrant_salutation7']." ".$abstract['registrant_firstname7']." ".$abstract['registrant_lastname7']." ( ".$abstract['registrant_organisation7']." )<br/>"; ?>
	<? if ($abstract['registrant_firstname8']!="") echo "Co-Author : ".$abstract['registrant_salutation8']." ".$abstract['registrant_firstname8']." ".$abstract['registrant_lastname8']." ( ".$abstract['registrant_organisation8']." )<br/>"; ?>
	<? if ($abstract['registrant_firstname9']!="") echo "Co-Author : ".$abstract['registrant_salutation9']." ".$abstract['registrant_firstname9']." ".$abstract['registrant_lastname9']." ( ".$abstract['registrant_organisation9']." )<br/>"; ?>
	<? if ($abstract['registrant_firstname10']!="") echo "Co-Author : ".$abstract['registrant_salutation10']." ".$abstract['registrant_firstname10']." ".$abstract['registrant_lastname10']." ( ".$abstract['registrant_organisation10']." )<br/>"; ?>
	<br/><br/>
	<div class='form-group'>
		<label for='reviewer1' class='col-lg-4 control-label'>Reviewer 1</label>
		<div class='col-lg-4 text-left'>
			<?
				$output = "<select name='reviewer1' class='form-control'><option value='0'>Please select</option>";
				
				$bind = array(
					":search1"=>$abstract['topic_id']
				);
				
				$results = $db->select("tbl_topic_user as a, tbl_abstract_user as b","b.user_id = a.user_id AND a.topic_id = :search1 AND b.user_principle=0",$bind);
				foreach ($results as $result){
					if (isset($reviewers[0]) && $reviewers[0]['user_id']==$result['user_id'])
						$output = $output."<option value='".$result['user_id']."' selected>".$result['user_name']."</option>"; 
					else
						$output = $output."<option value='".$result['user_id']."'>".$result['user_name']."</option>"; 
				}
				
				$output = $output."</select>";
				echo $output;
			?>
		</div>
	</div>
	
	<div class='form-group'>
		<label for='reviewer2' class='col-lg-4 control-label'>Reviewer 2</label>
		<div class='col-lg-4 text-left'>
			<?
				$output = "<select name='reviewer2' class='form-control'><option value='0'>Please select</option>";
				
				$bind = array(
					":search1"=>$abstract['topic_id']
				);
				
				$results = $db->select("tbl_topic_user as a, tbl_abstract_user as b","b.user_id = a.user_id AND a.topic_id = :search1 AND b.user_principle=0",$bind);
				foreach ($results as $result){
					if (isset($reviewers[1]) && $reviewers[1]['user_id']==$result['user_id'])
						$output = $output."<option value='".$result['user_id']."' selected>".$result['user_name']."</option>"; 
					else
						$output = $output."<option value='".$result['user_id']."'>".$result['user_name']."</option>"; 
				}
				
				$output = $output."</select>";
				echo $output;
			?>
		</div>
	</div>
	
	<div class='form-group'>
		<label for='reviewer3' class='col-lg-4 control-label'>Reviewer 3</label>
		<div class='col-lg-4 text-left'>
			<?
				$output = "<select name='reviewer3' class='form-control'><option value='0'>Please select</option>";
				
				$bind = array(
					":search1"=>$abstract['topic_id']
				);
				
				$results = $db->select("tbl_topic_user as a, tbl_abstract_user as b","b.user_id = a.user_id AND a.topic_id = :search1 AND b.user_principle=0",$bind);
				foreach ($results as $result){
					if (isset($reviewers[1]) && $reviewers[2]['user_id']==$result['user_id'])
						$output = $output."<option value='".$result['user_id']."' selected>".$result['user_name']."</option>"; 
					else
						$output = $output."<option value='".$result['user_id']."'>".$result['user_name']."</option>"; 
				}
				
				$output = $output."</select>";
				echo $output;
			?>
		</div>
	</div>
	
	<div class='form-group'>
		<label for='reviewer4' class='col-lg-4 control-label'>Reviewer 4</label>
		<div class='col-lg-4 text-left'>
			<?
				$output = "<select name='reviewer4' class='form-control'><option value='0'>Please select</option>";
				
				$bind = array(
					":search1"=>$abstract['topic_id']
				);
				
				$results = $db->select("tbl_topic_user as a, tbl_abstract_user as b","b.user_id = a.user_id AND a.topic_id = :search1 AND b.user_principle=0",$bind);
				foreach ($results as $result){
					if (isset($reviewers[1]) && $reviewers[3]['user_id']==$result['user_id'])
						$output = $output."<option value='".$result['user_id']."' selected>".$result['user_name']."</option>"; 
					else
						$output = $output."<option value='".$result['user_id']."'>".$result['user_name']."</option>"; 
				}
				
				$output = $output."</select>";
				echo $output;
			?>
		</div>
	</div>
	<input type='hidden' name="abstract_id" value="<?=$_REQUEST['abstract_id'];?>">
	<input type='hidden' name="topic_id" value="<?=$abstract['topic_id'];?>">
	<input type="submit" name="confirm" value="Update" class="btn btn-default"><br/>
	<font color='red'><i>*Note : Removal or reassignment of a reviewer will delete a previously submitted review for this abstract.</i></font>
    </form>

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