<?php 
    // continue the session
    session_start();
    
    include_once dirname(__FILE__) . '/../php_only_scripts/global_functions.php';
    
//    // grab recaptcha library
//    require_once '../php_only_scripts/recaptchalib.php';
//
//    // your secret key
//    $secret = "6Ld1HOIcAAAAAL1N3QPUIoFR--s866q4xVFM1CT1";

    // empty response
    $response = null;

//    // check secret key
//    $reCaptcha = new ReCaptcha($secret);
//
//    // if submitted check response
//    if ($_POST["g-recaptcha-response"]) {
//        $response = $reCaptcha->verifyResponse(
//            $_SERVER["REMOTE_ADDR"],
//            $_POST["g-recaptcha-response"]
//        );
//
//        // Determine if we have a good check
//        if ($response != null && $response->success) {
            // continue the session
            session_start();

            // Add includes to PHP files that I will need to access
            include_once dirname(__FILE__) . '/../php_only_scripts/database.php';

            // Set the login failed to false at first
            $_SESSION['failedLogin']='FALSE';

            // Init some variables
            $currentUser=null;

            // Create and Setup the database
            $db = new Database();
            $db->setupTmsDB();

            // Determine if BOTH the user name AND password has been set
            if (isset($_POST['login_id']) and isset($_POST['password'])){
                // Attempt to get the username and password
                $userName = htmlspecialchars($_POST['login_id'], ENT_QUOTES, 'UTF-8');
                $password = htmlspecialchars($_POST['password'], ENT_QUOTES, 'UTF-8');

                // Attempt to log in with this user
                $currentUser = $db->loginUser($userName, $password, session_id());
            }
            else {
                // Attempt to log in with this user
                $currentUser = $db->getLoggedInSessionUser(session_id());
            }

            // If there is only one result for a username/password combo, then they are good to go.
            if ($currentUser != null and mysqli_num_rows($currentUser)==1){
                // Attempt to get the user row
                $userRow = $currentUser->fetch_assoc();
                
                // Determine if they are the SU company
                if ($userRow["SUEmployee"]==1 &&
                    $userRow["CompanyID"]==1){
                    // Determine if they are NOT an SU
                    if ($userRow["UserLevel"]!='SU' && $userRow["FirstPayDate"]!="0000-00-00 00:00:00"){
                        // Create a query to determine if they are currently clocked in
                        $sql = 'SELECT * FROM laborclock WHERE UserID=' . $userRow["UserID"] . 
                               ' AND EndTime IS NULL;';
                        
                        // Update the rows that have just been sent a reorder notification
                        $rows = $db->queryDB($sql, $userRow, "log_check occurance 6");
                        
                        // If there is not one, then they should probably login.
                        if ($rows != null and mysqli_num_rows($rows)==0){
                            // Set the redirect to go to the login page
                            $_SESSION['pageToGoTo']='../updateTimecard.php';
                            
                            // Bypass the protector
                            $_SESSION['bypassProtector']="True";

                            // Inform the user
                            echo '<script>alert("You are not currently clocked in. Redirecting to the clock-in page.");</script>';
                        }
                        else {
                            // Determine if the are clockin only user
                            if ($userRow["UserLevel"]=='TIMECARD ONLY'){
                                // Set the redirect to go to the login page
                                $_SESSION['pageToGoTo']='../clockedStatus.php';
                            }
                            
                            // Do not bypass the protector
                            $_SESSION['bypassProtector']="False";
                        }
                    }
                }
                
                // Determine if they had a certain place in mind to go to
                if ($_SESSION['pageToGoTo']!=''){
                    // redirect to the tasks page
                    echo ('<script>window.location = "' . $_SESSION['pageToGoTo'] . '";</script>');

                    // Set the page to go to session variable to nothing
                    $_SESSION['pageToGoTo']='';
                }
                else {
                    // Determine what the user preference is for tasks.  Set to machining as default
                    $ts=1;
                    switch ($userRow["PrimaryTaskType"]) {
                        case 'DESIGNING':
                            // Set to desiging tasks
                            $ts=2;

                            // Leve this case statement
                            break;
                        case 'PROGRAMMING':
                            // Set to programming tasks
                            $ts=3;

                            // Leve this case statement
                            break;
                        case 'IN-HOUSE':
                            // Set to in-house tasks
                            $ts=4;

                            // Leve this case statement
                            break;
                        default:
                            break;
                    }

                    // redirect to the tasks page
                    echo ('<script>window.location = "../tasks.php?ts=' . $ts . '";</script>');
                }

                // Exit this code
                exit;
            }
            else {
                // Set the login failed to true
                $_SESSION['failedLogin']='TRUE';

                // redirect to the login page
                echo ('<script>window.location = "../index.php?errorCode=1";</script>');
            }
//        }
//    }
//    else {
//        // Set the login failed to true
//        $_SESSION['failedLogin']='TRUE';
//
//        // redirect to the login page
//        echo ('<script>window.location = "../index.php?errorCode=2";</script>');
//    }
?>