<?php
require_once 'admin_topbar.php';
require_once 'admin_bizLayer.php';
$biz = new admin_bizLayer();
$searchString = '';
$matchingSurveys = array();
$errors = array();
$isActiveYes = '';
$isActiveNo = '';
$isActiveEither = 'checked';
$isActiveDb = -1;
$shouldPerformSearch = false;
$message = '';
$surveysToHide = array();
$surveysToUnhide = array();
//you cannot scroll through multiple results. All results are dumped in one page.

function PerformSearch()
{
    global $searchString, $isActiveYes, $isActiveNo, $isActiveEither, $isActiveDb,
            $matchingSurveys, $message, $biz;
    //Confirm that survey name is not empty
    $searchString = trim($_REQUEST['searchString']);
            
    switch($_REQUEST['isActive'])
    {
        case 'yes':
            $isActiveYes = 'checked';
            $isActiveNo = '';
            $isActiveEither = '';
            $isActiveDb = 1;
            break;
        
        case 'no':
            $isActiveYes = '';
            $isActiveNo = 'checked';
            $isActiveEither = '';
            $isActiveDb = 0;
            break;
        
        case 'either':
            $isActiveYes = '';
            $isActiveNo = '';
            $isActiveEither = 'checked';
            $isActiveDb = 2;
            break;
        
        default:
            $isActiveYes = '';
            $isActiveNo = '';
            $isActiveEither = 'checked';
            $isActiveDb = 2;
    }
    
    $matchingSurveys = $biz->SearchSurvey($searchString, $isActiveDb);
    if($matchingSurveys instanceof Admin_ErrorObject)
    {
        $message = '<span class=\'error\'>Following error occured while
            searching for surveys: '.$matchingSurveys->errorDescription.'</span>';
    }
    else if(count($matchingSurveys) == 0)
    {
        $message = '<span class=\'error\'>No surveys that matched the search criteria were found.</span>';
    }
}
//do stuff only if Search button has been hit.
if (array_key_exists('btnSearch', $_REQUEST))
{
    PerformSearch();
}

else if(array_key_exists('btnUpdate', $_REQUEST))
{
    $surveysToHide = explode(',', trim($_REQUEST['surveysToHide'], ', '));
    $surveysToUnhide = explode(',', trim($_REQUEST['surveysToUnhide'], ', '));
    
    $retval = $biz->SetSurveyAvailability($surveysToHide, $surveysToUnhide);
    if($retval instanceof Admin_ErrorObject)
    {
        $message = '<span class=\'error\'>Following error occured while
            saving to the database: '.$retval->errorDescription.'</span>';
    }
    else if($retval === true)
    {
        $message = '<span class=\'success\'>Changes were successfully saved to the database.</span>';
    }
    else
    {
        $message = '<span class=\'success\'><em>Nothing to update.</em></span>';
    }
    PerformSearch();
}



$biz->CloseDatabaseConnection();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>
        <title>Carview | VRoom 360 (Un)Hide Surveys</title>
        <link rel="stylesheet" href="admin_main.css" type="text/css" />
        <script type="text/javascript">
            function ToggleAvailability(surveyId, rowid, origStatus, button)
            {
                surveysToHide = document.getElementById('surveysToHide').value;
                surveysToHideArray = surveysToHide.split(',');
                surveysToUnhide = document.getElementById('surveysToUnhide').value;
                surveysToUnhideArray = surveysToUnhide.split(',');
                
                if(button.innerHTML != "Unhide" && origStatus == "Yes")
                {
                    //originally the survey is active and i want to hide it.
                    document.getElementById(rowid).className = 'inActive';
                    button.innerHTML = 'Unhide';
                    
                    surveyIndex = surveysToHideArray.indexOf(surveyId);
                    //do the hide action - add to surveysToHide
                    //first check if it has been already marked for hiding
                    if(surveyIndex == -1)
                    {
                        //questionId does not exist in the array. add it.
                        surveysToHide = surveysToHide + surveyId.toString() + ',';
                    }
                }
                else if(button.innerHTML == "Unhide" && origStatus == "Yes")
                {
                    //this means originally the survey was active and it is
                    //being reactivated. Just remove it from the surveysToHide.
                    document.getElementById(rowid).className = 'active';
                    button.innerHTML = '&nbsp;&nbsp;Hide&nbsp;&nbsp;';
                    
                    surveyIndex = surveysToHideArray.indexOf(surveyId);
                    if(surveyIndex > -1)
                    {
                        surveysToHideArray.splice(surveyIndex, 1);
                        surveysToHide = surveysToHideArray.toString();
                    }
                }
                else if(button.innerHTML != "Unhide" && origStatus == "No")
                {
                    //originally the survey is not active and i want to restore it to its original position.
                    //Just remove from the unhide list
                    document.getElementById(rowid).className = 'inActive';
                    button.innerHTML = 'Unhide';
                    
                    surveyIndex = surveysToUnhideArray.indexOf(surveyId);
                    if(surveyIndex > -1)
                    {
                        surveysToUnhideArray.splice(surveyIndex, 1);
                        surveysToUnhide = surveysToUnhideArray.toString();
                    }
                }
                else if(button.innerHTML == "Unhide" && origStatus == "No")
                {
                    //originally the survey is not active and i want make it available.
                    //Just add to the unhide list
                    document.getElementById(rowid).className = 'active';
                    button.innerHTML = 'Hide';
                    
                    surveyIndex = surveysToUnhideArray.indexOf(surveyId);
                    //do the hide action - add to surveysToHide
                    //first check if it has been already marked for hiding
                    if(surveyIndex == -1)
                    {
                        //questionId does not exist in the array. add it.
                        surveysToUnhide = surveysToUnhide + surveyId.toString() + ',';
                    }
                }
                document.getElementById('surveysToHide').value = surveysToHide;
                document.getElementById('surveysToUnhide').value = surveysToUnhide;
            }
        </script>
    </head>
    <body>
        <?php PrintTopbar('(Un)Hide Surveys'); ?>
        <div id='content' class='mainContent'>
            <p class='helpText'>Please find a survey that you wish to edit.</p>
            <form action='admin_survey_availability.php' method='post'>
                <table border='0' id="tblSearchForm">
                    <tbody>
                        <tr>
                            <td>Search Survey:</td>
                            <td><input type='text' name='searchString' id='searchString' value='<?php echo $searchString ?>' />&nbsp;&nbsp;<span id="searchStringError" class='error'><?php if(array_key_exists('searchStringError', $errors)) echo $errors['searchStringError']; ?></span>
                                <br/>
                                <span class='helpText'>Search for surveys that contain these characters. <em>Leave the field empty and hit Search to see all surveys.</em></span>
                            </td>
                        </tr>
                        <tr>
                            <td>Is Active:</td>
                            <td>
                                <input type='radio' name='isActive' value='yes' <?php echo $isActiveYes; ?> />&nbsp;Yes
                                <input type='radio' name='isActive' value='no' <?php echo $isActiveNo; ?>/>&nbsp;No
                                <input type='radio' name='isActive' value='either' <?php echo $isActiveEither; ?>/>&nbsp;Does not matter
                                <br/><span class='helpText'>Search surveys which only users can see, cannot see or may or may not see.</span></td>
                        </tr>
                        <tr>
                            <td colspan="2">
                                <input type='submit' name='btnSearch' id='btnSearch' value='Search' />
                            </td>
                        </tr>
                    </tbody>
                </table>
                <p style='clear: left;'><?php echo $message; ?></p>
                <?php
                if(count($matchingSurveys) > 0)
                echo "
                <table border='0px' id='tblQuestions' cellspacing='0px'>
                    <tr>
                        <td colspan='6'>
                            <span class='helpText'>Click the link in first column to go to the update page.</span>
                        </td>
                    </tr>
                    <tr>
                        <td colspan='6'><input type='submit' name='btnUpdate' id='btnUpdate' value='Update'/>&nbsp;&nbsp;<span class='helpText'>Hit this button to save the changes you made anywhere in this page.</span></td>
                        
                    </tr>
                    <tr>
                        <th>Survey Name</th>
                        <th>Number of Questions</th>
                        <th>Currently Active?</th>
                        <th>Last Modified By</th>
                        <th>Last Modified On</th>
                        <th>Action</th>
                    </tr>
                    <tbody>";
                        
                        for($i = 0; $i<  count($matchingSurveys); $i++)
                        {
                            $styleClass = $matchingSurveys[$i]['isActive'] === 0 ? 'inActive' : 'active';
                            $uiIndex = $i+1;
                            $isActiveString = $matchingSurveys[$i]['isActive'] === 0 ? 'No' : 'Yes';
                            
                            $surveyName = $matchingSurveys[$i]['name'];
                            $surveyDbId = $matchingSurveys[$i]['id'];
                            $btnText = $matchingSurveys[$i]['isActive'] === 0 ? 'Unhide' : '&nbsp;&nbsp;Hide&nbsp;&nbsp;';
                            echo "
                        <tr valign='top' id='s".$i."Row' class='$styleClass'>
                            <td>
                                <a href='admin_survey_update.php?id=".$matchingSurveys[$i]['id']."&name=".urlencode($surveyName)."&active=".$matchingSurveys[$i]['isActive']."'
                                    title='Click to update questions in $surveyName.'>$surveyName</a>
                            </td>
                            <td>
                                ".$matchingSurveys[$i]['quesCount']."
                            </td>
                            <td>
                               $isActiveString
                            </td>
                            <td>
                                ".$matchingSurveys[$i]['modifiedBy']."
                            </td>
                            <td>
                               ".date("F j, Y, g:i a", strtotime($matchingSurveys[$i]['modifiedOn']))."
                            </td>
                            <td>
                                <button name='btnToggleDelete' id='btnToggleDelete' type='button' value='$btnText' onclick='ToggleAvailability(\"$surveyDbId\", \"s".$i."Row\",\"$isActiveString\", this);'>$btnText</button>
                            </td>
                        </tr>";
                        }
                        if(count($matchingSurveys) > 0)
                echo "
                    <tr>
                        <td colspan='6'><input type='submit' name='btnUpdate' id='btnUpdate' value='Update'/>&nbsp;&nbsp;<span class='helpText'>Hit this button to save the changes you made anywhere in this page.</span></td>
                    </tr>
                    </tbody>
                </table>";
                        ?>
                <input value='' name='surveysToHide' id='surveysToHide' type='hidden' />
                <input value='' name='surveysToUnhide' id='surveysToUnhide' type='hidden' />
            </form>
        </div>
    </body>
</html>