<?php
session_start();
require_once '../include/Constants.php';
require_once "../vendor/autoload.php";
require_once "../include/DatabaseConn.php";
require_once "../models/User.php";
require_once '../models/USettings.php';
require_once '../models/Settings.php';
require_once '../include/GoogleAuthenticator.php';
require_once '../models/Document.php';

$db = new DatabaseConn();
$link = $db->connect();

$user = User::getCurrentUser($link);
if (is_null($user)) {
    $url = $_SERVER['REQUEST_URI'];
    header("location: ../login?" . RETURN_URL . "=" . $url);
    die();
}

$settings = USettings::getSystemSettings($link, $user->getEmail());
if ($settings->getTwofa() && is_null($user->getFa())) {
    header("location: add-2fa");
    die();
}

if (isset($_POST[PACKAGE])) {
    $package = filter_input(INPUT_POST, PACKAGE, FILTER_SANITIZE_STRING);
    if (is_null($package) || is_bool($package) || empty($package) || strcasecmp($package, NONE) == 0) {
        $error = "Please select an option";
    } else {
        header("location: upl90iut?op=" . $package);
        die();
    }
}
$docs = Document::getDocuments($link, $user->getEmail());
?>
<!DOCTYPE html>
<html>
    <head>
        <?php require_once './partials/head.php'; ?>
    </head>
    <body id="dark">
        <?php require_once './partials/header.php'; ?>
        <div class="settings  settings-profile mtb15">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-md-12 col-lg-3">
                        <div class="nav flex-column nav-pills settings-nav" id="v-pills-tab" role="tablist"
                             aria-orientation="vertical">
                            <a class="nav-link active" href="verification"><i class="icon ion-md-settings"></i> Account Verification</a>
                        </div>
                    </div>
                    <div class="col-md-12 col-lg-9" id="settings" role="tabpanel" aria-labelledby="settings-tab">
                        <div class="card">
                            <div class="card-body">
                                <?php if (is_null($docs)) { ?>
                                    <h5 class="card-title">Select document to upload</h5>
                                    <div class="panel panel-primary ch">
                                        <form action="verification" method="POST">
                                            <div class="panel-body">
                                                <div class="row">
                                                    <div class="col-md-12">
                                                        <select class="form-control" id="package" name="package">
                                                            <option value="None" selected=selected>None</option>
                                                            <option value="vp" >Valid Passport </option>
                                                            <option value="ni">National ID</option>
                                                            <option value="dl">Driver Licence</option>
                                                            <option value="ul">Utility Bill</option>

                                                        </select>
                                                        <br>
                                                        <div class="col-lg-10 col-lg-offset-2">
                                                            <button class="btn btn-primary" type="submit" name="select_btn">SELECT</button>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </form>
                                    </div>
                                <?php } else { ?>
                                    <h5 class="card-title">Uploaded Document(s)</h5>
                                    <table  class="table">
                                        <thead>
                                            <tr>
                                                <th>Date</th>
                                                <th>Document Type</th>
                                                <th>Status</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php
                                            foreach ($docs as $doc) {
                                                ?>
                                                <tr>
                                                    <td><?php echo (strftime("%h %d %G", $doc->getTime())); ?></td>
                                                    <td><?php
                                                        if (strcmp($doc->getType(), "vp") == 0)
                                                            echo ("Valid Passport");
                                                        else if (strcmp($doc->getType(), "ni") == 0)
                                                            echo ("National ID");
                                                        else if (strcmp($doc->getType(), "dl") == 0)
                                                            echo ("Driver Licence");
                                                        else if (strcmp($doc->getType(), "ul") == 0)
                                                            echo ("Utility Bill");
                                                        ?></td>
                                                    <td>
                                                        <?php
                                                        if (strcmp($doc->getStatus(), APPROVED) == 0) {
                                                            ?>
                                                            <input type='button' value='Document Approved' class='btn btn-success'>
                                                        <?php } else if (strcmp($doc->getStatus(), REJECTED) == 0) { ?>
                                                            <input type='button'  value='Rejected' class='btn btn-danger '>
                                                        <?php } else {
                                                            ?>
                                                            <input type='button' value='Pending Approval' class='btn btn-danger '>
                                                            <?php
                                                        }
                                                        ?>
                                                    </td>
                                                </tr>
                                                <?php
                                            }
                                            ?>
                                        </tbody>
                                    </table>
                                <?php } ?>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <?php require_once './partials/scripts.php'; ?>
    </body>
</html>