<?php
session_start();

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require_once '../include/Constants.php';
require_once "../vendor/autoload.php";
require_once "../include/DatabaseConn.php";
require_once "../models/PaymentWallet.php";
require_once '../models/Withdrawal.php';
require_once "../models/User.php";
require_once '../models/USettings.php';
require_once '../models/Settings.php';
require_once '../models/Invoice.php';
require_once '../include/GoogleAuthenticator.php';
require_once '../models/News.php';
require_once '../include/Helper.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();
}



if (isset($_POST['message'])) {
    $name = $user->getName();
    $message = filter_input(INPUT_POST, "message", FILTER_SANITIZE_STRING);
    $subject = filter_input(INPUT_POST, "subject", FILTER_SANITIZE_STRING);
    $email = $user->getEmail();

    if (is_null($subject) || is_bool($subject)) {
        $error = "Please provide subject";
    }
    if (is_null($name) || is_bool($name)) {
        $error = "Please provide a name";
    }

    if (is_null($message) || is_bool($message)) {
        $error = "Message body is empty";
    }
    if (!isset($error)) {
        $mail = new PHPMailer(true);
        try {
            $text = "<p>Name: $name</p><br/><p>Email: $email</p><br/><p>Subject: $subject</p><br/><p>Message: $message</p><br/>";
            $mail->isSMTP();
            $mail->SMTPDebug = 0;
            $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
            $mail->Debugoutput = 'html';
            $mail->Host = EMAIL_HOST;

            $mail->Port = 587;
            $mail->SMTPSecure = 'tls';
            $mail->SMTPAuth = true;
            $mail->Username = EMAIL_USERNAME;
            $mail->Password = EMAIL_PASSWORD;
            $mail->setFrom(EMAIL_NO_REPLY, APP_NAME);
            $mail->addAddress(EMAIL_SUPPORT);
            $mail->Subject = $subject;
            $mail->isHTML(true);
            $mail->msgHTML($text);
            //This should be the same as the domain of your From address
            $mail->DKIM_domain = DKIM_DOMAIN;
            $mail->DKIM_private = 'dkim_private.pem';
            $mail->DKIM_selector = DKIM_SELECTOR;
            $mail->DKIM_identity = $mail->From;
            $mail->DKIM_copyHeaderFields = false;

            $mail->send();

            $success = 'Message has been sent We would contact you soon.';
            //    return true;
        } catch (Exception $e) {
            var_dump($e);
            die();
            $error = "Message could not be sent.";
        }
    }
}

$news = News::getNewsPage($link);
$cInvoices = Invoice::getCurrentInvoices($link, $user->getEmail());
$invoices = Invoice::getInvoices($link, $user->getEmail());
$invoices1 = Withdrawal::getWithdrawals($link, $user->getEmail());
?>

<!DOctype html>
<html class="gr__">
    <head>
        <?php require_once './partials/head.php'; ?>
        <link href="../css/account-setting.css" rel="stylesheet" type="text/css"/>
        <link href="../css/datatables.css" rel="stylesheet" type="text/css"/>
        <link href="../css/dt-global_style.css" rel="stylesheet" type="text/css"/>
        <link href="../css/custom_dt_custom.css" rel="stylesheet" type="text/css"/>
        <link href="../css/snackbar.min.css" rel="stylesheet" type="text/css"/>
        <style>
            table#style-1 td, td p {
                color: black;
            }
            table#style-2 th {
                color: #506690;
            }
            .userimg{height:90px;width:90px;line-height:1.5;color:#888ea8;font-size:.875rem;letter-spacing:.0312rem;font-family:Nunito,sans-serif;text-align:center!important;box-sizing:border-box;vertical-align:middle;border-style:none;border-radius:9px;box-shadow:0 6px 10px 0 rgba(0,0,0,.14),0 1px 18px 0 rgba(0,0,0,.12),0 3px 5px -1px rgba(0,0,0,.2)}
        </style>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/lightbox2/2.11.1/css/lightbox.min.css" />
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.1/animate.min.css" />
        <link href="../css/datatables.css" rel="stylesheet" type="text/css"/>
        <style>
            div.dataTables_wrapper div.dataTables_filter svg {
                position: absolute;
                top: -2px;
                right: 14px;
                width: 20px;
                color: #d3d3d3;
            }
            #style-4_paginate {
                display: none !important;
            }
             #style-3_paginate {
                display: none !important;
            }
        </style>
    </head>
    <body cz-shortcut-listen="true" data-gr-c-s-loaded="true">
        <div id="app">
            <div class="sc-eIHaNI jXnkjm" style="">
                <div class="sc-bsbRJL jBomjO">
                    <span></span>
                </div>
                <div></div>
                <?php require_once './partials/navbar.php'; ?>
                <div class="sc-eTpRJs chWNvr">
                    <div data-e2e="page-home" class="sc-iomxrj lkDWON">
                        <div class="sc-ugnQR ikKNan">
                            <div class="sc-dDPtig fBozlL">
                                <?php require_once './partials/sidebar.php'; ?>
                                <section class="sc-jAarDj jmkFbA">
                                    <div class="sc-fNbuCz sc-bqsUL dgojan">
                                        <div class="sc-dArhMf fOVmDi">
                                            <div class="sc-hEsumM BCdqX sc-jYIdPM gLdSNN" style="background-color: transparent;">
                                                <span width="33%" data-e2e="totalTab" class="sc-ktHwxA iBBsrM">
                                                    <span>Receive</span>
                                                </span>
                                            </div>
                                            <div class="sc-bHwgHz EpALc">
                                                <table id="style-4" class="table style-2 table-hover non-hover">
                                                    <thead>
                                                        <tr>
                                                            <th>DATE</th>
                                                            <th>AMOUNT(USD)</th>
                                                            <th>Crypto</th>
                                                            <th>STATUS</th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                        <?php
                                                        if (!is_null($invoices)) {
                                                            foreach ($invoices as $invoice) {
                                                                ?>
                                                                <tr>
                                                                    <td class='text-center'><?php echo Helper::readableTime($invoice->getDate()); ?></td>

                                                                    <td class='text-center'><?php echo number_format($invoice->getAmount()); ?></td>
                                                                    <td class='text-center'><?php echo "{$invoice->getPackage()}"; ?></td>
                                                                    <td class='text-center'>
                                                                        <?php if (strcmp($invoice->getPaymentMode(), WALLET_BALANCE) == 0) { ?>
                                                                            <a>Confirmed</a>
                                                                            <?php
                                                                        } else {
                                                                            if (is_null($invoice->getProof())) {
                                                                                if (strcmp($invoice->getStatus(), CONFIRMED) == 0 || (strcmp($invoice->getStatus(), GROWING) == 0 || strcmp($invoice->getStatus(), Paused) == 0)) {
                                                                                    ?>
                                                                                    <a>Confirmed</a>
                                                                                <?php } else { ?>
                                                                                    <a style="color: rgb(60, 141, 188)">Pending</a>
                                                                                <?php }
                                                                                ?>

                                                                                <?php
                                                                            } else {
                                                                                if (strcmp($invoice->getStatus(), CONFIRMED) == 0 || (strcmp($invoice->getStatus(), GROWING) == 0 || strcmp($invoice->getStatus(), Paused) == 0)) {
                                                                                    ?>
                                                                                    <a>Confirmed</a>
                                                                                <?php } else {
                                                                                    ?>
                                                                                    <a style="color: rgb(60, 141, 188)">Pending</a>
                                                                                    <?php
                                                                                }
                                                                            }
                                                                        }
                                                                        ?>

                                                                    </td>
                                                                </tr>
                                                                <?php
                                                            }
                                                        }
                                                        ?>
                                                    </tbody>
                                                </table>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="sc-fNbuCz sc-eRjRog jUkgna">
                                        <div class="sc-gEGaab dwfEmX dodo">
 <div class="sc-hEsumM BCdqX sc-jYIdPM gLdSNN" style="background-color: transparent;">
                                                <span width="33%" data-e2e="totalTab" class="sc-ktHwxA iBBsrM">
                                                    <span>Sent</span>
                                                </span>
                                            </div>
                                            <div class="sc-bHwgHz EpALc">
                                                <table id="style-3" class="table style-2 table-hover non-hover">
                                                    <thead>
                                                        <tr>
                                                            <th>DATE</th>
                                                            <th>AMOUNT(USD)</th>
                                                            <th>Crypto</th>
                                                            <th>STATUS</th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                        <?php
                                                        if (!is_null($invoices1)) {
                                                            foreach ($invoices1 as $invoice) {
                                                                ?>
                                                                <tr>
                                                                    <td class='text-center'><?php echo Helper::readableTime($invoice->getDate()); ?></td>

                                                                    <td class='text-center'><?php echo number_format($invoice->getAmount()); ?></td>
                                                                    <td class='text-center'><?php echo "{$invoice->getWallet()}"; ?></td>
                                                                    <td class='text-center'><?php if(strcmp($invoice->getStatus(),"Paid") == 0 ){ echo  "Sent";} else{ echo "{$invoice->getStatus()}"; }?></td>
                                                                </tr>
                                                                <?php
                                                            }
                                                        }
                                                        ?>
                                                    </tbody>
                                                </table>
                                            </div>
                                        </div>
                                    </div>
                                </section>
                            </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="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'
                });
            });
        </script>

        <script>
            var clipboard = new ClipboardJS('.copy');

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

    </body>
</html>