<?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/Wallet.php';
require_once '../include/Helper.php';
require_once '../models/Settings.php';
require_once '../models/USettings.php';
require_once '../include/GoogleAuthenticator.php';
require_once '../models/Referral.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();
}
$referrals = Referral::getAllReferral($user->getEmail(), $link);
?>
<!DOCTYPE html>
<html>
    <head>
        <?php
        require_once './partials/head.php';
        ?>
        <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;
            }
        </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="referrals"><i class="icon ion-md-settings"></i> Referrals</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">
                                                        <h5 class="card-title">Referrals</h5>
                                                        <h5 class="card-title">Referral Link: <a target="_blank" href="<?php echo DOMAIN_NAME . "/register?referral=" . $user->getEmail(); ?>"><?php echo DOMAIN_NAME . "/register?ref=" . $user->getEmail(); ?></a></h5>
                                                        <table class="table table-hover table-bordered">
                                                            <thead>
                                                                <tr>
                                                                    <th class="center">Name</th>
                                                                    <th class="center">Reg Date</th>
                                                                </tr>
                                                            </thead>
                                                            <tbody>
                                                                <?php
                                                                if (!is_null($referrals)) {
                                                                    foreach ($referrals as $ref) {
                                                                        ?>
                                                                        <tr>
                                                                            <td class="center"><?php echo($ref->getName()); ?></td>
                                                                            <td class="center"><?php echo (strftime("%h %d %G", $ref->getDate())); ?></td>
                                                                        </tr>
                                                                        <?php
                                                                    }
                                                                }
                                                                ?>
                                                            </tbody>
                                                        </table>
                                                    </div>
                                                </div>
                                            </div>
                                    <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 src="../js/datatables.js" type="text/javascript"></script>
    <script>

            var clipboard = new ClipboardJS('.copy');

            clipboard.on('success', function (e) {
                Snackbar.show({
                    text: 'Copied!',
                    pos: 'top-right'
                });
            });
            // var e;
            c1 = $("#style-9").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>
