<?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;
$message = '';
$hasError = false;
//you cannot scroll through multiple results. All results are dumped in one page.

//do stuff only if create button has been hit.
if (array_key_exists('btnSearch', $_REQUEST))
{
    //Confirm that survey name is not empty
    $searchString = trim($_REQUEST['searchString']);
    
    if(strlen($searchString) === 0)
    {
        $errors['searchStringError'] = 'Please enter some string to search for.';
        $hasError = true;
    }
        
    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;
    }
    
    if(!$hasError)
    {
        //if there are no errors, then try putting data in to database.
        $matchingSurveys = $biz->SearchSurvey($searchString, $isActiveDb);
        if(count($matchingSurveys) == 0)
        {
            $message = '<span class=\'error\'>No surveys that matched the search criteria were found.</span>';
        }
    }
    else
    {
        $message = '<span class=\'error\'>Kindly resolve the shown errors before saving the survey.</span>';
    }
}

$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 Download Survey Data</title>
        <link rel="stylesheet" href="admin_main.css" type="text/css" />
    </head>
    <body>
        <?php PrintTopbar('Download Survey Data'); ?>
        <div id='content' class='mainContent'>
            <p class='helpText'>Please find a survey that you wish to edit.</p>
            <form action='admin_survey_search.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.</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='5'>
                            <span class='helpText'>Click the link in first column to go to the update page.</span>
                        </td>
                    </tr>
                    <tr>
                        <th>Survey Name</th>
                        <th>Number of Questions</th>
                        <th>Is Active</th>
                        <th>Last Modified By</th>
                        <th>Last Modified On</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'];
                            echo "
                        <tr valign='top' 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>
                        </tr>";
                        }
                        if(count($matchingSurveys) > 0)
                echo "
                    </tbody>
                </table>";
                        ?>
            </form>
        </div>
    </body>
</html>