<?php

require 'mods.php';
require 'election.php';
$mods = new Mods();
$mods->startConnection();
$action = $mods->escapeString($_POST['action']);
if ($action == $mods->getAction(1)) {
    $studentNumber = $mods->escapeString($_POST['studentNumber']);
    $mods->setQuery("SELECT * FROM voters WHERE Voter_ID='{$studentNumber}'");
    if ($mods->getCount() == 1) {
        $row = $mods->getResults('array');
        if ($row['Status'] == 1) {
            $mods->login($studentNumber, 'Voter');
            $mods->sendStatus($mods->getError(0), '../index.php');
        } else {
            if ($row['Status'] == 0) {
                $mods->sendStatus($mods->getError(3), '../index.php');
            } else {
                if ($row['Status'] == 2) {
                    $mods->sendStatus($mods->getError(4), '../index.php');
                }
            }
        }
    } else {
        $mods->sendStatus($mods->getError(2), '../index.php');
    }
} else {
    if ($action == $mods->getAction(2)) {
        $username = $mods->escapeString($_POST['username']);
        $password = $mods->escapeString($_POST['password']);
<?php

require 'requests/mods.php';
require 'requests/chat.php';
$mods = new Mods();
if (!(isset($_SESSION['user_id']) && isset($_SESSION['user_type']))) {
    $mods->sendStatus($mods->getError(1), 'admin.php');
} else {
    if ($_SESSION['user_type'] == 'Voter') {
        $mods->sendStatus($mods->getError(7), 'index.php');
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>UE College of Law Election System</title>
    <link rel="stylesheet" href="assets/css/font-awesome.min.css">
    <link rel="stylesheet" href="assets/css/stylesheet.css">
    <script src="assets/js/jquery.min.js"></script>
    <script src="assets/js/dashboard.js"></script>
</head>
<body id="main-body">
    <div id="main-block" class="shadow">
        <div id="header">UE College of Law Election <?php 
echo date('Y');
?>
</div>
        <div id="content">
            echo 'Voter not registered.';
        } else {
            if ($row['Status'] == 1) {
                $ctr = 0;
                foreach ($_POST['votes'] as $vote) {
                    if ($vote != '') {
                        $mods->setQuery("INSERT INTO votes (Voter_ID, Candidate_ID) VALUES ('{$sessionID}', '{$vote}')");
                        if ($mods->getCount() > 0) {
                            $ctr++;
                        }
                    }
                }
                if ($ctr > 0) {
                    $mods->setQuery("UPDATE voters SET Status=2 WHERE Voter_ID='{$sessionID}'");
                    if ($mods->getCount() == 1) {
                        $mods->sendStatus('20c1e9da750353cf0cba49283c17f8a7', '../index.php', 'output');
                    }
                } else {
                    $mods->sendStatus($mods->getError(5), '../index.php');
                }
            } else {
                if ($row['Status'] == 2) {
                    echo 'Voter already voted.';
                } else {
                    echo 'Oops! There\'s something wrong with the voter\'s information.';
                }
            }
        }
    }
}
$mods->stopConnection();