session_start();
require_once 'DB.php';
require_once 'seekerTable.php';
require_once 'employerTable.php';
//Set and sanitize the user inputs
$email = filter_var($_POST['email'], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$pass = filter_var($_POST['password'], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
//if data has been set
if ($email && $pass) {
    try {
        ini_set("display_errors", 1);
        ob_start();
        $connection = DB::getConnection(DB::host, DB::database, DB::user, DB::password);
        $seekerTable = new seekerTable($connection);
        $employerTable = new employerTable($connection);
        $seekers = $seekerTable->showAll();
        $employers = $employerTable->showAll();
        foreach ($seekers as $seeker) {
            $dbSeekerEmail = $seeker->getEmail();
            $dbPassWord = $seeker->getPassword();
            $dbSeekerName = $seeker->getFName();
            $dbTheme = $seeker->getTheme();
            $dbId = $seeker->getId();
            // if match has been found, session will be created with the user's username (first name) ELSE return to login page
            if (strpos(strtoupper($email), strtoupper($dbSeekerEmail)) !== false && $pass == $dbPassWord) {
                $_SESSION['seeker'] = $dbSeekerName;
                $_SESSION['id'] = $dbId;
                $_SESSION['theme'] = $dbTheme;
                header("Location: sControlPanel.php");
                die;
}
//variables used to store the POST values from the employer
$fName = $_POST['fName'];
$lName = $_POST['surname'];
$company = $_POST['company'];
$email = $_POST['email'];
$bio = $_POST['bio'];
$password = $_POST['password'];
//location can be updated in "Edit Profile"
$location = "Update";
//default image used
$pic = "noImage.jpeg";
try {
    ini_set("display_errors", 1);
    //establish a connection
    $connection = DB::getConnection(DB::host, DB::database, DB::user, DB::password);
    //instantiate a new employer table, passing the connection details as a parameter
    $table = new employerTable($connection);
    //instantiate a new employer object
    $employer = new employer(-1, $fName, $lName, $company, $email, $location, $bio, $pic, $password);
    //addEmployer will add the newly created employer object into the database and return the id which will be stored.
    $id = $table->addEmployer($employer);
    //the employer object's id will be set.
    $employer->setId($id);
    //redirect to login page
    header("Location: login.php");
} catch (PDOException $e) {
    //if a problem occurs, close the connection
    $connection = null;
    exit("Connection failed: " . $e->getMessage());
}
        if (file_exists($file)) {
            $result = $pic2["name"];
        } else {
            if (move_uploaded_file($pic2["tmp_name"], $file)) {
                $result = $pic2["name"];
            }
        }
    }
    return $result;
}
try {
    ini_set("display_errors", 1);
    $connection = DB::getConnection(DB::host, DB::database, DB::user, DB::password);
    //gets the user id and searches the member table for matching id
    $id = $_SESSION["id"];
    $table = new employerTable($connection);
    $employer = $table->findById($id);
    if (isset($_POST['submit'])) {
        $company = filter_var($_POST['company'], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
        $fName = filter_var($_POST['fName'], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
        $lName = filter_var($_POST['lName'], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
        $email = filter_var($_POST['email'], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
        $location = filter_var($_POST['location'], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
        $bio = filter_var($_POST['bio'], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
        $password = filter_var($_POST['password'], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
        $picture = $_FILES['picture'];
        if ($picture === null) {
            $picture = $employer->getPicture();
        } else {
            $picture = uploadPicture($picture);
        }