<?php
session_start();
date_default_timezone_set("Africa/Lagos");
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 '../include/GoogleAuthenticator.php';
require_once '../models/Settings.php';
require_once '../models/Investment.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["plan"]) && !isset($_POST["package_type"])) {
    $package = filter_input(INPUT_GET, "plan", FILTER_SANITIZE_STRING);
    if (is_null($package) || is_bool($package) || empty($package) || strtolower($package) === NONE) {
        header("location: new-investment");
        die();
    }
} else if (isset($_POST["package_type"])) {
    $package = filter_input(INPUT_GET, "plan", FILTER_SANITIZE_STRING);
    if (is_null($package) || is_bool($package) || empty($package) || strtolower($package) === NONE) {
        header("location: new-investment");
        die();
    }
    $package_type = filter_input(INPUT_POST, "package_type", FILTER_SANITIZE_STRING);
    if (is_null($package_type) || is_bool($package_type) || empty($package_type) || strtolower($package_type) === NONE) {
        $error = "Please select a plan";
    } else {
        header("location: payment-options?gae=" . $package_type);
        die();
    }
} else {
    header("location: new-investment");
    die();
}
$plans = Investment::getInvesmentByName($link, $package);
?>

<!DOCTYPE html>
<html>
    <head>
        <?php
        require_once './partials/head.php';
        ?>
        <style>
            span.card-price {
                color: white;
            }
        </style>
    </head>
    <body id="dark">
        <?php require_once './partials/header.php'; ?>
        <div class="settings 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="new-investment"><i class="icon ion-md-person"></i> New Investments</a>
                            <a class="nav-link " href="current-investment"><i class="icon ion-md-wallet"></i> Current Investments</a>
                            <a class="nav-link" href="investment-history"><i class="icon ion-md-settings"></i> Investment History</a>
                        </div>
                    </div>
                    <div class="tab-pane col-md-12 col-lg-9" id="settings-wallet" role="tabpanel" aria-labelledby="settings-wallet-tab">
                        <div class="wallet">
                            <div class="">
                                <div class="tab-content">
                                    <div class="tab-pane fade show active" id="coinBTC" role="tabpanel">
                                                <div class="card">
                                                    <div class="card-body">
                                                        <div class="row wallet-address">
                                                            <div class="col-md-12 settings-profile">
                                                                <div class="row" id="pricingWrapper">
                                                                    <?php if (is_null($plans)) { ?>
                                                                        <p>No investment plan under this group</p>
                                                                        <?php
                                                                    } else {
                                                                        foreach ($plans as $plan) {
                                                                            ?>
                                                                            <div class="col-md-6 col-lg-4">
                                                                                <div class="card stacked mt-5">
                                                                                    <div class="card-header pt-0">
                                                                                        <span style="font-size: 18px" class="card-price">$<?php echo $plan->getMin(); ?></span>
                                                                                        <h3 class="card-title mt-3 mb-1"><?php echo $plan->getName(); ?></h3>
                                                                                    </div>
                                                                                    <div class="card-body">
                                                                                        <ul class="list-group list-group-minimal mb-3">
                                                                                            <li class="list-group-item d-flex  align-items-center"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="#0069d9" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-right"><line x1="5" y1="12" x2="19" y2="12"></line><polyline points="12 5 19 12 12 19"></polyline></svg>
                                                                                                Minimum amount: USD<?php echo $plan->getMin(); ?>
                                                                                            </li>
                                                                                            <li class="list-group-item d-flex align-items-center"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="#0069d9" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-right"><line x1="5" y1="12" x2="19" y2="12"></line><polyline points="12 5 19 12 12 19"></polyline></svg>
                                                                                                Maximum amount: <?php
                                                                                                if (strcmp($plan->getMax(), UNLIMITED) == 0) {
                                                                                                    echo $plan->getMax();
                                                                                                } else {
                                                                                                    echo "USD" . $plan->getMax();
                                                                                                }
                                                                                                ?>
                                                                                            </li>
                                                                                            <li class="list-group-item d-flex align-items-center"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="#0069d9" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-right"><line x1="5" y1="12" x2="19" y2="12"></line><polyline points="12 5 19 12 12 19"></polyline></svg>
                                                                                                Return on investment: <?php echo $plan->getPercentage(); ?>%
                                                                                            </li>
                                                                                            <li class="list-group-item d-flex align-items-center"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="#0069d9" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-right"><line x1="5" y1="12" x2="19" y2="12"></line><polyline points="12 5 19 12 12 19"></polyline></svg>
                                                                                                Return frequency: <?php echo $plan->getType(); ?>
                                                                                            </li>
                                                                                            <li class="list-group-item d-flex align-items-center"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="#0069d9" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-right"><line x1="5" y1="12" x2="19" y2="12"></line><polyline points="12 5 19 12 12 19"></polyline></svg>
                                                                                                Trade Duration: <?php echo $plan->getDays(); ?>days
                                                                                            </li>
                                                                                            <li class="list-group-item d-flex align-items-center"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="#0069d9" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-right"><line x1="5" y1="12" x2="19" y2="12"></line><polyline points="12 5 19 12 12 19"></polyline></svg>
                                                                                                Active Support
                                                                                            </li>
                                                                                        </ul>
                                                                                        <a style="cursor: pointer; color: white" data-id="<?php echo "{$plan->getName()}"; ?>" class="basic btn btn-block btn-primary">Invest Now</a>
                                                                                    </div>
                                                                                </div>
                                                                            </div>
                                                                        <?php }
                                                                        ?>

                                                                    </div>
                                                                <?php }
                                                                ?>

                                                            </div>

                                                        </div>

                                                    </div>
                                                </div>
                                            </div>
                                            <form id="fm" action="investment-plans?plan=<?php
                                            if (isset($_GET['plan'])) {
                                                echo ($_GET['plan']);
                                            }
                                            ?>" method="POST">
                                                <input type="hidden" name="package_type" id="pt" value="" />

                                            </form>
                                    <footer style="position: fixed;bottom: 0;right: 0; margin-right: 10px">
                                        <p style="text-align: right; color: white; font-size: 12px;"><?php echo APP_NAME; ?> Copyright 2020. All right reserved.</p>
                                    </footer>
                                </div>
                            </div>
                        </div>
                    </div>
                    <?php require_once './partials/scripts.php'; ?>
                </div>
            </div>
        </div>
        <?php require_once './partials/scripts.php'; ?>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.4/clipboard.min.js"></script>
        <script>
            var clipboard = new ClipboardJS('.copy');

            clipboard.on('success', function (e) {
                Snackbar.show({
                    text: 'Copied!',
                    pos: 'top-right'
                });
            });
        $('.basic').click(function (e) {
            e.preventDefault();
            var input = $("#pt");
            input.val($(this).attr("data-id"));
            $('#fm').trigger('submit');
        });
        </script>
    </body>
</html>
