<?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 '../include/Helper.php';
require_once '../models/Settings.php';
require_once '../models/Investment.php';
require_once '../models/Invoice.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();
}
$invoices = Invoice::getInvoices($link, $user->getEmail());
$cInvoices = Invoice::getCurrentInvoices($link, $user->getEmail());
?>

<!DOCTYPE html>
<html lang="en">
    <head>

        <?php require_once './partials/head_.php'; ?>
        <link rel="stylesheet" href="assets/css/style.css">
        <link href="../css/datatables.css" rel="stylesheet" type="text/css"/>
        <style>
            table#style-4 thead tr th {
                color: rgb(245, 222, 179) !important;
            }
            #style-4_info {
                color: white !important;
            }
           #style-4_previous a.page-link, #style-4_next a.page-link {
                padding: 5px;
            }
            img[src='https://hynportal.io/img/favicon.png'] {
                position: relative !important;
                left: 15px !important;
            }
        </style>
    </head>
    <body id="dark" style="background: #1F2225;">
        <div id="root">
            <div class="main-panel container-fluid">
                <div id="top-banner" style="min-height:unset" class="row"></div>
                <div class="flex-wrap flex-md-nowrap no-gutters row">
                    <div class="d-block menu-panel navigation__tab-container navigation__tab-container_hidden col-12 col-md-auto">
                        <?php require_once './partials/navbar.php'; ?>

                    </div>
                    <div class="col-12 col-md">
                        <div class="profile-item container-fluid">
                            <div class="settings  settings-profile mtb15" style="margin-left: 25px;margin-right: 25px;">
                                <div class="container-fluid">
                                    <div class="row">

                                        <div class="col-md-12"  id="settings" role="tabpanel" aria-labelledby="settings-tab">
                                            <div class="card">
                                                <div class="card-body" style='padding: 30px;font-size: 15px;'>
                                                    <h5 class="card-title" style="font-size: 18px; font-family: 'Segoe UI'">Current Trade</h5>
                                                    <div class="col-lg-12 settings-profile">
                                                           <div class="wallet-history table-responsive mb-4 style-1">
                                                            <table class="table style-1 table-hover non-hover" id="style-4">
                                                                 <thead>
                                                                        <tr>
                                                                            <th>User</th>
                                                                            <th>Amount</th>
                                                                            <th>Investment Plan</th>
                                                                            <th>Confirmed Date</th>
                                                                            <th>Day's Count</th>
                                                                            <th>Total Profit</th>
                                                                            <th>Amount Withdrawable</th>
                                                                            <th class=''>Status</th>
                                                                        </tr>
                                                                    </thead>
                                                                 <tbody>
                                                                        <?php
                                                                        if (!is_null($cInvoices)) {
                                                                            foreach ($cInvoices as $invoice) {
                                                                                ?>
                                                                                <tr class="inv">
                                                                                    <td><?php echo "{$user->getName()}"; ?></td>
                                                                                    <td><?php echo "{$invoice->getAmount()}"; ?></td>
                                                                                    <td><?php echo "{$invoice->getPackage()}"; ?></td>
                                                                                    <td><?php echo Helper::readableTime($invoice->getConfirmedDate()); ?></td>
                                                                                    <td style="color: #3c8dbc;"><?php echo $invoice->getCount() . "/" . $invoice->getDays(); ?></td>
                                                                                    <td style="color: #3c8dbc;"><?php echo $invoice->getCReturns(); ?></td>
                                                                                    <td style="color: #3c8dbc;"><?php echo $invoice->getWBalance(); ?></td>
                                                                                    <td style="color: #3c8dbc;"><?php echo "{$invoice->getStatus()}"; ?></td>
                                                                                  
                                                                                </tr>
                                                                                <?php
                                                                            }
                                                                        } else {
                                                                            ?>
                                                                            <tr><td colspan="7"> <p style="text-align: center">No current investment </p></td></tr>
                                                                        <?php }
                                                                        ?>
                                                                    </tbody>
                                                            </table>
                                                            
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>

                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </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 src="../js/datatables.js" type="text/javascript"></script>
        <script>
        $('.process').click(function () {
            var amount = $(this).attr('data-amount');
            var id = $(this).attr('data-id')
            $('#am').val(amount);
            $('#invID').val(id);
            $('#fam').trigger('submit');
        });
        $('a[href="current-investment"]').addClass('active');
        var clipboard = new ClipboardJS('.copy');

        clipboard.on('success', function (e) {
            Snackbar.show({
                text: 'Copied!',
                pos: 'top-right'
            });
        });
        // var e;
        c1 = $("#style-4").DataTable({
            oLanguage: {
                oPaginate: {
                    sPrevious:
                            '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-left"><line x1="19" y1="12" x2="5" y2="12"></line><polyline points="12 19 5 12 12 5"></polyline></svg>',
                    sNext:
                            '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" 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>',
                },
                sInfo: "Showing page _PAGE_ of _PAGES_",
                sSearch:
                        '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-search"><circle cx="11" cy="11" r="8"></circle><line x1="21" y1="21" x2="16.65" y2="16.65"></line></svg>',
                sSearchPlaceholder: "Search...",
                sLengthMenu: "Results :  _MENU_",
            },
            lengthMenu: [10, 20, 30, 50],
            pageLength: 10,
        });
        function multiCheck(tb_var) {
            tb_var.on("change", ".chk-parent", function () {
                var e = $(this).closest("table").find("td:first-child .child-chk"), a = $(this).is(":checked");
                $(e).each(function () {
                    a ? ($(this).prop("checked", !0), $(this).closest("tr").addClass("active")) : ($(this).prop("checked", !1), $(this).closest("tr").removeClass("active"))
                })
            }),
                    tb_var.on("change", "tbody tr .new-control", function () {
                        $(this).parents("tr").toggleClass("active")
                    })
        }
        multiCheck(c1);

    </script>
    </body>
</html>