<?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';

$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($_GET["op"]) && !isset($_FILES['proof'])) {
    $package = filter_input(INPUT_GET, "op", FILTER_SANITIZE_STRING);
    if (is_null($package) || is_bool($package) || empty($package) || strcasecmp($package, NONE) == 0) {
        header("location: verification");
        die();
    }
    if (strcmp($package, "vp") == 0) {
        $doc = "Valid Passport";
    } else if (strcmp($package, "ni") == 0) {
        $doc = "National ID";
    } else if (strcmp($package, "dl") == 0) {
        $doc = "Driver Licence";
    } else if (strcmp($package, "ul") == 0) {
        $doc = "Utility Bill";
    }
} else if (isset($_FILES['proof'])) {
    $package = filter_input(INPUT_GET, "op", FILTER_SANITIZE_STRING);
    if (is_null($package) || is_bool($package) || empty($package) || strtolower($package) === NONE) {
        $package = filter_input(INPUT_POST, "type", FILTER_SANITIZE_STRING);
        if (is_null($package) || is_bool($package) || empty($package) || strtolower($package) === NONE) {
            header("location: verification");
            die();
        }
    }
    if (strcmp($package, "vp") == 0) {
        $doc = "Valid Passport";
    } else if (strcmp($package, "ni") == 0) {
        $doc = "National ID";
    } else if (strcmp($package, "dl") == 0) {
        $doc = "Driver Licence";
    } else if (strcmp($package, "ul") == 0) {
        $doc = "Utility Bill";
    }
    if ($_FILES['proof']['size'] > 0) {
        $target_dir = IMG_UPLOAD_DIR;
        $s = substr(str_shuffle(str_repeat("abcdefghijklmnopqrstuvwxyz", 5)), 0, 5);
        $target_file = $target_dir . $s . filter_var(basename($_FILES["proof"]["name"]), FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
        $uploadOk = 1;
        $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
        $check = getimagesize($_FILES["proof"]["tmp_name"]);
        if ($check !== false) {
            $uploadOk = 1;
        } else {
            $error = "File is not an image.";
            $uploadOk = 0;
        }
// Check if file already exists
        if (file_exists($target_file)) {
            $error = "Sorry, file already exists.";
            $uploadOk = 0;
        }
// Check file size
        if ($_FILES["proof"]["size"] > 50000000) {
            $error = "Sorry, your file is too large.";
            $uploadOk = 0;
        }
// Allow certain file formats
        if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") {
            $error = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
            $uploadOk = 0;
        }
// Check if $uploadOk is set to 0 by an error
        if ($uploadOk == 0) {
// $error = "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
        } else {
            if (move_uploaded_file($_FILES["proof"]["tmp_name"], $target_file)) {
                $success = "Document has been uploaded. Pending verification";
                $time = time();
                $status = PENDING;
                $rss = mysqli_query($link, "insert INTO documents (`user`,`path`,status,`type`,`time`) VALUES ('{$user->getEmail()}', '$target_file', '$status','$package','$time')");
                header("location: verification");
                die();
            } else {
                $error = "Sorry, there was an error uploading your file.";
            }
        }
    } else {
        $error = "Please upload document.";
    }
} else {
    header("location: verification");
    die();
}
?>
<!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">
                                <h5 class="card-title">Upload <?php echo "$doc"; ?></h5>
                                <div class="panel panel-primary ch">
                                    <div class="">
                                        <div class="row">
                                            <div class="col-md-12">
                                                <form action="upl90iut?op=<?php
                                                if (isset($_GET['op'])) {
                                                    echo ($_GET['op']);
                                                } else if
                                                ((isset($_POST['op']))) {
                                                    echo ($_GET['op']);
                                                }
                                                ?>" method="POST" enctype="multipart/form-data">
                                                    <input type="hidden" name="type" value="<?php
                                                    if (isset($_GET['op'])) {
                                                        echo ($_GET['op']);
                                                    } else if
                                                    ((isset($_POST['type']))) {
                                                        echo ($_POST['type']);
                                                    }
                                                    ?>" style="display: none;" />

                                                    <div class="card-body">
                                                        <div class="form-group">
                                                            <input type="file" name="proof" accept="image/*" class="form-control" required="">
                                                        </div>
                                                    </div>
                                                    <div class="card-footer">
                                                        <button class="btn btn-primary icon-btn"  type="submit" name="upload_btn"><i class="fa fa-fw fa-lg fa-check-circle"></i>UPLOAD</button>
                                                    </div>
                                                </form>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <?php require_once './partials/scripts.php'; ?>
    </body>

</html>