<?php
include("../include/session.php");  
include("suggestions.class.php");
require_once("../include/accessCheck.php");   

if (isset($_GET['page'])){ //find out what the sub page is
	$page = $_GET['page'];
	}
else{$page = 'display';}

global $pageNum;
if(isset($_GET['pageNum']))
        $pageNum=$_GET['pageNum']; //Get the current page number
    else
        $pageNum=1;
        
/**
* displaySuggestions - Displays the suggestion database table in
* a nicely formatted html table.
*/
function displaySuggestions(){
    global $pageNum,$session;
    
//paging code below
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++      
$result=suggestions::showAdminSuggestions($session->userinfo['adminID']);
$nrResults = mysql_numrows($result);

$limit=20; // Limit of result per page 
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
$result = suggestions::showAdminSuggestions($session->userinfo['adminID'],$pageNum,$limit);
$num_rows = mysql_numrows($result);

if(!$result || ($num_rows < 0)){
echo "Error displaying info";
return;
}
if($num_rows == 0){
echo "No record.";
return;
}

/* Display table contents */
echo "<table  cellspacing=\"3\" cellpadding=\"2\">";
echo "<tr><th><b>Application</b></th><th><b>Suggestion Subject</b></th><th><b>Submission Date</b></th><th><b>Category</b></th><th><b>Status</b></th><th width='100px'><b>Action</b></th></tr>\n";
for($i=0; $i<$num_rows; $i++){
    
$suggestionID  = mysql_result($result,$i,"suggestionID");
$organizationName  = mysql_result($result,$i,"organizationName"); 
$appName  = mysql_result($result,$i,"appName");
$appID  = mysql_result($result,$i,"appID"); 
$suggestionSubject  = mysql_result($result,$i,"suggestionSubject");
$submissionDateTime= mysql_result($result,$i,"submissionDateTime");  
$submissionCategory= mysql_result($result,$i,"categoryDescription");
$status= mysql_result($result,$i,"statusDescription");

echo '<tr><td class="'.($i%2? "c1":"c2").'"><a href="suggestionList.php?appID='.$appID.'"  >'.$organizationName.': '.$appName.'</a></td>';
echo '<td class="'.($i%2? "c1":"c2").'">'.$suggestionSubject.'</td>'; 
echo '<td class="'.($i%2? "c1":"c2").'">'.date("m/d/Y g:i A", strtotime($submissionDateTime)).'</td>';
echo '<td class="'.($i%2? "c1":"c2").'">'.$submissionCategory.'</td>';
echo '<td class="'.($i%2? "c1":"c2").'">'.$status.'</td>';  
  
?>  
<script type="text/javascript">

function submitForm(form)
{
    document.getElementById(form).submit();
    return true;  
}
</script>                   
<td align="center" class="<?php echo ($i%2? "c1":"c2") ?>">
    <div style="float: left; width:30px;"> 
            <a href="<?php echo $_SERVER['PHP_SELF'] . '?page=edit&subeditsuggestionID=' . $suggestionID; ?>"><img SRC="../icons/Edit.png" title="Update Suggestion" ALT="Update Suggestion" border="0"/></a>
    </div>
    <!--    ||||||||||||||||||   --> 
    <div style="float: left; width:30px;">
            <a href="<?php echo $_SERVER['PHP_SELF'] . '?page=display&subdelsuggestionID=' . $suggestionID;?>" onClick="javascript:return confirm('Are you sure you want to delete this suggestion? This action cannot be undone.');"><img border="0" SRC="../icons/delete.gif" title="Delete Suggestion" ALT="Delete Suggestion"  ></a>
    </div>
</td>
<?php
echo "</tr>\n";
}
echo "</table><br>";
//Paging code below
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++    
suggestions::displayPageNav($nrResults,$limit,$pageNum,$_SERVER['PHP_SELF']);

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
}
/**
* displayComments - Displays the comments database table in
* a nicely formatted html table.
*/
function displayComments($suggestionID){
    global $session;
    if(trim($suggestionID)=='')          //Make sure suggestionID isn't blank
    {
        exit;
    }

$result = suggestions::showSuggestionComments($suggestionID);
$num_rows = mysql_numrows($result);

if(!$result || ($num_rows < 0) || $num_rows==0){
    echo "No History";  
    return;
}

/* Display table contents */
echo "<table  cellspacing=\"3\" cellpadding=\"2\">";
echo "<tr><th><b>Date</b></th><th><b>Comment</b></th><th><b>Comment Owner</b></th><th><b>Comment Owner's Email</b></th></tr>\n";
for($i=0; $i<$num_rows; $i++){
    
$suggestionID  = mysql_result($result,$i,"suggestionID");
$dateTime  = mysql_result($result,$i,"submissionDateTime");
$comment  = mysql_result($result,$i,"comment");
$ownerName  = mysql_result($result,$i,"firstName")." ".mysql_result($result,$i,"lastName");
$ownerEmail = mysql_result($result,$i,"email"); 

echo '<tr><td class="'.($i%2? "c1":"c2").'">'.date("m-d-Y g:iA", strtotime($dateTime)).'</td>';
echo '<td class="'.($i%2? "c1":"c2").'">'.$comment.'</td>'; 
echo '<td class="'.($i%2? "c1":"c2").'">'.$ownerName.'</td>';
echo '<td class="'.($i%2? "c1":"c2").'">'.$ownerEmail.'</td>';  
  
echo "</tr>\n";
}
echo "</table><br>";

}
 //----------------------------------------------------------------------------------
 if(isset($_GET['subdelsuggestionID']))
 {  
    if(suggestions::querySuggestion($_GET['subdelsuggestionID'])!=false)
    { 
        //check permission first          
        if($session->userlevel!=1 && !suggestions::hasSuggestionAccess(trim($_REQUEST['subdelsuggestionID']),$session->userinfo['adminID']))
        {
           echo "Access Denied.";
           exit;
        }
        $deleteResult=suggestions::deleteSuggestion($_GET['subdelsuggestionID']);
        
        if($deleteResult)
            $process_result="The selected suggestion has been removed successfully.";
        else
            $process_result="Failed to remove the suggestion from the database.";
    }

 }
  //----------------------------------------------------------------------------------
if(isset($_POST['subeditsuggestion']) && isset($_POST['subeditsuggestionID']))
 {  
    global $form,$session;
   
   $publicView=0 ;
    if(isset($_POST['publicView']))
    {
     $publicView=1;
    }
    $field = "status_code";  
    if(!isset($_POST['status_code']) || strlen(trim($_POST['status_code'])) == 0){
    $form->setError($field, "* Missing Status.");
    }
    
    /* Errors exist, have user correct them */
    if($form->num_errors > 0){
    $_SESSION['value_array'] = $_POST;
    $_SESSION['error_array'] = $form->getErrorArray();
    }
    /* No errors, update application information in the database*/
    else
    { 
       
           if(suggestions::updateSuggestion($session->userinfo['adminID'],$_POST['subeditsuggestionID'],$_POST['status_code'],$_POST['disposition'],$publicView,$_POST['comment']))
           {
                $process_result= "Suggestion updated successfully. <a href='suggestions.php?appID=".$_POST['appID']."'>Go Back</a>...";
                $page="edit";
           }    
            else
                $process_result="Failed to update.";
    }

 }

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="../style.css"/>       


</head>
<body >

<div id="layout">
	<?php require('../include/header.php');   ?>
        
<?php    switch ($page){ //what page to load
		case 'display':  //begin display page?>
			<div id="displaySuggestions" class="in"> 
            <P class=veryLargeBlue>Quick View</P>
				<?php if(isset($process_result)) {echo $process_result;}
				
                        displaySuggestions();
				?>
			</div>
		<?php
		break; //end display page
		
		case 'edit': //begin edit page?>
            <div id="panel-left" class="box panel">
            <P class=veryLargeBlue>Update Suggestion</P> 
                <?php
                if(isset($_REQUEST['subeditsuggestionID']))
                { //check permission first
                 
                    if($session->userlevel!=1 && !suggestions::hasSuggestionAccess(trim($_REQUEST['subeditsuggestionID']),$session->userinfo['adminID']))
                    {
                       echo "Access Denied.";
                       exit;
                    }
                  $suggestionInfo=suggestions::querySuggestion(trim($_REQUEST['subeditsuggestionID']));
                ?>
                <div id="editApp">
                    <?php if(isset($process_result)) {echo $process_result;}?>
                    <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
                        <table align="left" border="0" cellspacing="0" cellpadding="3">

                        <tr>
                            <td class="c1">Application Name:</td>
                            <td class="c1"><?php echo $suggestionInfo->appName; ?></td>
                           
                            <input type="hidden" name="subeditsuggestionID" value="<?php echo $_REQUEST['subeditsuggestionID']; ?>"> 
                            <input type="hidden" name="appID" value="<?php echo $suggestionInfo->appID; ?>" >   
                        </tr>
                         <tr>
                            <td class="c2">App Sub Location:</td>
                            <td class="c2"><?php echo $suggestionInfo->appSubLocationDescription; ?></td>
                        </tr>
                         <tr>
                            <td class="c1">App Sub Location Address:</td>
                            <td class="c1"><?php echo $suggestionInfo->appSubLocationHttp; ?></td>
                        </tr>
                           <tr>
                            <td class="c2">Category:</td>
                            <td class="c2"><?php echo $suggestionInfo->categoryDescription; ?></td>
                        </tr>
                           <tr>
                            <td class="c1">Submission Date:</td>
                            <td class="c1"><?php echo date("m-d-Y @ g:i A", strtotime($suggestionInfo->submissionDateTime)); ?></td>
                        </tr>
                           <tr>
                            <td class="c2">Submitter Name:</td>
                            <td class="c2"><?php echo $suggestionInfo->submitterName; ?></td>
                        </tr>
                         <tr>
                            <td class="c1">Submitter E-Mail:</td>
                            <td class="c1"><?php echo $suggestionInfo->submitterEmail; ?></td>
                        </tr>
                         <tr>
                            <td class="c2">Submitter Location:</td>
                            <td class="c2"><?php echo $suggestionInfo->submitterCity.", ".$suggestionInfo->submitterState.", ".$suggestionInfo->submitterZipCode; ?></td>
                        </tr>
                          <tr>
                            <td class="c1">Suggestion Subject:</td>
                            <td class="c1"><?php echo $suggestionInfo->suggestionSubject; ?></td>
                        </tr>
                          <tr>
                            <td class="c2">Suggestion Description:</td>
                            <td class="c2"><?php echo $suggestionInfo->suggestionDescription; ?></td>
                        </tr>
                        <tr>
                            <td class="c1">Status:</td>
                            <td class="c1">
                            <?php $statusList=suggestions::showStatusList(); ?>
                            <select class="style01"  name="status_code" size="1" >
                             
                                <?php 
                                     
                                    while ($row = mysql_fetch_object($statusList))
                                            {
                                                if($suggestionInfo->suggestionStatusCode==$row->statusCode)
                                                {
                                                    echo "<option selected value='$row->statusCode' >$row->statusDescription</option>";
                                                }
                                                else
                                                    echo "<option value='$row->statusCode' >$row->statusDescription</option>";
                                            }
                                ?>
                                </select>
                                <?php echo $form->error("status_code"); ?>
                            </td>
                        </tr>
                         <tr>
                            <td class="c2">Suggestion Disposition:</td>
                            <td class="c2"><textarea  name="disposition" cols="70" rows="6" ><?php if(isset($_POST['disposition'])) echo $_POST['disposition']; else echo $suggestionInfo->suggestionDisposition; ?></textarea>
                            <?php  echo $form->error("disposition"); ?></td>
                        </tr>
                        <tr >
                            <th class="c1"><b>Enable Public View:</b></th>
                            <td class="c1">
                            <?php 
                            $checked="";
                            if($suggestionInfo->enablePublicView==1)
                            {
                                $checked= "checked";
                            } 
                                
                                echo "<input name='publicView' type='checkbox' value='1' $checked >";
                             
                            ?>
                            </td>
                        </tr>
                        <tr>
                            <td class="c2">Comment:</td>
                            <td class="c2"><textarea  name="comment" cols="70" rows="6" ><?php if(isset($_POST['comment'])) echo $_POST['comment']; else {
                                
                                $result= suggestions::showAdminComment($session->userinfo['adminID'],$suggestionInfo->suggestionID);
                                 if($result!=false)
                                    {
                                    echo $result->comment;
                                    }
                                 }  ?></textarea>
                            <?php // echo $form->error("comment"); ?></td>
                        </tr>
                        <tr>
                            <td class="c1">Comment History:</td>
                            <td class="c1"> <?php displayComments($suggestionInfo->suggestionID);?></td>
                        </tr>
                        <input type="hidden" name="subeditsuggestion" value="1"> 

                        <tr>
                            <td></td>
                            <td>
                                <div style="float:left;"><button name="editsuggestion" type="submit" >Update Suggestion</button></div> 
                               
                                <div style="float:left;"><button type="button" onclick="window.location='suggestions.php?appID=<?php echo $suggestionInfo->appID; ?>'">Cancel</button>
                            </td>
                        </tr>
   
                        </table>
                        </form>
         
                </div>
            </div>
		<?php break; //end edit page
		 }
} //end switch page
 require('../include/footer.php'); ?>
</div>
<?php
 unset($_SESSION['error_array']); 
?>
</body>
</html>
