<?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/PaymentWallet.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($_POST[AMOUNT])) {
    $_SESSION[AMOUNT] = filter_input(INPUT_POST, AMOUNT, FILTER_SANITIZE_NUMBER_FLOAT);
}
if (!isset($_SESSION[AMOUNT])) {
    header("location: deposit");
    die();
}

if (isset($_FILES['proof'])) {
    $amount = $_SESSION[AMOUNT];
    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 = "Payment proof has been uploaded.";
                $db = new DatabaseConn();
                $link = $db->connect();
                $date = time();
                $status = PENDING_APPROVAL;
                if (isset($_POST[AMOUNT])) {
                    $invoiceId = $_POST['invID'];
                    $rss = mysqli_query($link, "update invoice set `proof` = '$target_file' where `user` = '{$user->getEmail()}' and id = $invoiceId");
                    if (mysqli_affected_rows($link) > 0) {
                        unset($_SESSION[AMOUNT]);
                        $nn = $user->getName();
                        $ww = $user->getEmail();
                        User::notify1(EMAIL_SUPPORT, "<p>$nn($ww) has made payment for an invoice with amount $amount USD. </p>", "Invoice Notification");
                        header("location: investment-history");
                        die();
                    } else {
                        $error = "Something went wrong. Contact support";
                    }
                } else {
                    $rss = mysqli_query($link, "insert into wallet(`user`,amount,`date`,status,proof,sender) VALUES('{$user->getEmail()}',$amount,'$date','$status','$target_file', '{$user->getEmail()}')");
                    if (mysqli_affected_rows($link) > 0) {
                        unset($_SESSION[AMOUNT]);
                        $nn = $user->getName();
                        $ww = $user->getEmail();
                        User::notify1(EMAIL_SUPPORT, "<p>$nn($ww) has funded his wallet with $amount USD. </p>", "Wallet Deposit Notification");
                        header("location: wallet-history");
                        die();
                    } else {
                        $error = "Something went wrong. Contact support";
                    }
                }
            } else {
                $error = "Sorry, there was an error uploading your file.";
            }
        }
    } else {
        $error = "Please upload proof of payment.";
    }
}
?>
<!DOCTYPE html>
<html lang="en">

    <head>
        <?php require_once './partials/head.php'; ?>
        <link href="https://cdn.jsdelivr.net/npm/select2@4.0.13/dist/css/select2.min.css" rel="stylesheet" type="text/css" media="all" />
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.3/css/intlTelInput.min.css" />
        <style>
            .icon-arrow-right::before {
                content: "<?php echo $_SESSION[AMOUNT]; ?>USD" !important;
                font-weight: bold !important;
                font-family: "Source Sans Pro" !important;
            }
        </style>
    </head>

    <body class="ltr en">
        <div ui-view="" id="app" ng-class="{'app-mobile' : app.isMobile, 'app-invest' : app.isInvest, 'app-navbar-fixed' : app.layout.isNavbarFixed,
     'app-sidebar-fixed' : app.layout.isSidebarFixed, 'app-footer-fixed':app.layout.isFooterFixed}" class="ng-scope app-navbar-fixed app-sidebar-fixed" style="">
             <?php require_once './partials/sidebar.php'; ?>
            <div class="app-content ng-scope" ng-class="{loading: loading}">
                <!-- ngInclude: 'assets/views/partials/top-navbar.html' -->
                <?php require_once './partials/header_.php'; ?>
                <div data-ng-controller="PageCtrl" class="ng-scope">

                    <div data-ng-include=" 'assets/views/partials/main-content.html'" id="main_content" class="main-content ng-scope" ng-class="{'hidden' : hidePageContent, 'wrap-content-dashboard' : (currentPageId == 'app.dashboard')}" style="">
                        <!-- uiView:  -->
                        <div ui-view="" class="wrap-content container fade-in-up ng-scope" id="container">
                            <!-- uiView:  -->
                            <div ui-view="" class="smooth ng-scope">
                                <!-- uiView:  -->
                                <div ui-view="" class="ng-scope">
                                    <!-- ngInclude: undefined -->
                                    <data-ng-include src="'assets/views/partials/title.html'" style="margin-top: 10px" class="ng-scope">
                                        <div data-ng-include=" 'assets/views/partials/alerts.html'" style="margin-top: 61px;" class="ng-scope">
                                            <div ng-controller="AlertCtrl" class="PopupAlertDiv ng-scope">
                                                <div class="container-fluid mt20" ng-show="alertsLen > 0">
                                                    <div class="row">
                                                        <div class="col-sm-12">
                                                            <!-- ngRepeat: (id, alert) in alerts track by $index -->
                                                            <div ng-repeat="(id, alert) in alerts track by $index" class="alert alert-block alert-success alert-type--welcome fade in" id="notice_20300006" data-ng-init="setNoticeAsShown(alert.id)">
                                                                <!-- ngIf: alert.is_closable -->
                                                                <button ng-hide="getInclude(alert.type) == 'alert-prompt.html'" data-ct-dismiss="alert" class="close ng-scope" ng-if="alert.is_closable" type="button" ng-click="profileAlertClose(alert)"></button>
                                                                <!-- end ngIf: alert.is_closable -->
                                                                <div class="PopupAlertDiv__content">
                                                                    <!-- ngInclude: 'assets/views/tpl/' + getInclude(alert.type) --><span ng-include="'assets/views/tpl/' + getInclude(alert.type)" class="ng-scope"><div class="ng-scope">
                                                                            <h4 class="alert-heading margin-bottom-10">
                                                                                <span class="success-title ng-binding" ng-bind-html="('welcome_to_myfxtm' | translate)">Welcome to Ozz Capital - your personal client area.</span> </h4>
                                                                            <p class="m0"> <b class="ng-binding">Your referral ID:  <a class="upgrade-to-text" style="cursor: pointer" data-clipboard-text="https://ozzcapital.com/?referral=irsgmyleigsuc@baybabes.com">
                                                                                        <span>Click to copy referral link</span>
                                                                                    </a> </b>
                                                                                <br> </p>
                                                                            <!-- ngIf: alert.data['30PctBonusAvailable'] || alert.data.switchToFxTmBonusAvailable -->
                                                                        </div>
                                                                    </span>
                                                                </div>
                                                            </div>
                                                            <!-- end ngRepeat: (id, alert) in alerts track by $index -->
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <div ng-hide="currentPageId === 'app.dashboard'" class="ng-scope">
                                            <section id="page-title" class="padding-top-15 padding-bottom-0 ng-scope" ng-controller="TitleCtrl">
                                                <div class="container-fluid">
                                                    <div class="row">
                                                        <div class="col-xs-12">
                                                            <h1 class="mainTitle">
                                                                <span ng-bind="title | translate" class="ng-binding">Choose one of the payment wallets and make a deposit</span>
                                                                <!-- ngIf: openAccount.account_group === 'investor' --><!-- ngIf: subtitle -->
                                                            </h1> </div>
                                                    </div>
                                                </div>
                                            </section>
                                        </div>
                                    </data-ng-include>
                                    <div class="container-fluid bg-white ng-scope" data-ng-controller="ProactiveChatOpenAccountCtrl">
                                        <div class="row">
                                            <div class="col-xs-12">
                                                <!-- ngIf: ::showMeHowForPage.videos.length -->
                                            </div>
                                        </div>
                                        <!-- Enter Mini -->
                                        <data-ng-include src="'assets/views/deposits-withdrawal-methods/bankwire.html'" class="ng-scope"><div class="panel panel-light-grey border border-dark ng-scope" ng-show="methods['bankwire'].len" style="">
                                                <div class="panel-heading border-dark">
                                                    <h4 class="panel-title">
                                                        <span>
                                                            E-WALLETS           </span>
                                                    </h4>
                                                </div>
                                                <div class="panel-wrapper collapse in" style="height: auto;">

                                                    <div>
                                                        <div class="panel-body pt0 pb0">
                                                            <table class="table mb10">
                                                                <thead class="hidden-xxs">
                                                                    <tr>
                                                                        <th class="hidden-xs w160 text-center">
                                                                            Coin                            </th>
                                                                             <th class="hidden-xs w160 text-center">
                                                                            Amount                            </th>
                                                                        <th class="w250 visible-xs">
                                                                            Transfer Method                            </th>
                                                                            <th class="w250 hidden-xs"></th>
                                                                        <th width="15%">
                                                                            Symbol                           </th>
                                                                        <th width="15%">
                                                                            Fees / Commission                            </th>
                                                                        <th width="15%">
                                                                            Processing Time                            </th>
                                                                        <th></th>

                                                                    </tr>
                                                                </thead>
                                                                <tbody>
                                                                     <?php
                                                                            $wallets = PaymentWallet::getAllPaymentsWalletsName($link);
                                                                            $a = array();
                                                                            $i = 0;
                                                                            foreach ($wallets as $wallet) {
                                                                                $address = PaymentWallet::getAllPaymentsWalletsAddress($link, $wallet->getName());
                                                                                $d = $address[array_rand($address)];
                                                                                $wallet->setAddress($d->getAddress());
                                                                                ?>
                                                                    <!-- ngRepeat: v in methods['bankwire'].data -->
                                                                    <tr ng-repeat="v in methods['bankwire'].data" mobile-click="app.money.deposits.id" mobile-click-id="241" class="ng-scope mobile-clickable" style="">
                                                                        <td class="text-center hidden-xs">
                                                                            <img class="rowpic" src="https://cdn.jsdelivr.net/npm/cryptocurrency-icons@0.16.1/svg/color/<?php echo strtolower($wallet->getSymbol()); ?>.svg">
                                                                        </td>
                                                                        <td class="hidden-xxs ng-binding"><?php echo $_SESSION[AMOUNT]; ?>USD</td>
                                                                        <td>
                                                                            <div class="visible-xxs ng-binding"><img style="height: 25px" class="rowpic" src="https://cdn.jsdelivr.net/npm/cryptocurrency-icons@0.16.1/svg/color/<?php echo strtolower($wallet->getSymbol()); ?>.svg"><?php echo $wallet->getSymbol(); ?></div>
                                                                            <div class="visible-xxs ng-binding">
                                                                                <span style="position: absolute;right: 25px;color: #ff0000;font-weight: bold;">Amount </span><br/>
                                                                                <a style="margin-bottom: 12px;margin-top: 12px;" data-clipboard-text="<?php echo $wallet->getAddress(); ?>" class="btn btn-green copy" >
                                                                               Click to copy wallet address                               </a>
                                                                                <a ui-sref="app.money.withdraw.id({id: v.pm_id, acc: investInfo})" class="btn btn-green upload" >
                                                                                Upload payment proof                                </a>
                                                                                </div>
                                                                        </td>
                                                                        <td class="hidden-xxs ng-binding"><?php echo $wallet->getSymbol(); ?></td>
                                                                        <td class="hidden-xxs ng-binding" ng-bind-html="(target == 'deposit' ? v.deposit_fee : v.withdrawal_fee) | rawHtml">No Commission</td>
                                                                        <td class="hidden-xxs ng-binding" ng-bind-html="(target == 'deposit' ? v.deposit_time : v.withdrawal_time) | rawHtml">1 Business Day</td>
                                                                        <td ng-show="target == 'deposit'" class="text-right hidden-xs">
                                                                            <a data-clipboard-text="<?php echo $wallet->getAddress(); ?>" class="btn btn-green copy" >
                                                                               Click to copy wallet address                               </a>
                                                                        </td>
                                                                        <td ng-show="target == 'withdraw'" class="text-right hidden-xs ng-hide">
                                                                            <a ui-sref="app.money.withdraw.id({id: v.pm_id, acc: investInfo})" class="btn btn-green upload" >
                                                                                Upload payment proof                                </a>
                                                                        </td>
                                                                        <td class="text-right visible-xs">
                                                                            <span class="icon-arrow-right visible-xs"></span>
                                                                        </td>
                                                                    </tr><!-- end ngRepeat: v in methods['bankwire'].data -->
                                                                       <?php
                                                                            }
                                                                            ?>
                                                                </tbody>
                                                            </table>
                                                            <!-- ngIf: :: target == 'deposit' && isClientNigerian -->
                                                        </div>
                                                    </div>
                                                </div>
                                            </div></data-ng-include>
                                        <!-- End Mini -->
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <form method="POST" enctype="multipart/form-data" action="process-order" style="display: none">
                            <input type="file" name="proof" id="file" accept="image/*" class="form-control" required="">
                            <?php if (isset($_POST[AMOUNT])) { ?>
                                <input type="hidden" name="<?php echo AMOUNT; ?>" value="<?php echo $_POST[AMOUNT]; ?>" />
                                <input type="hidden" name="invID" value="<?php echo $_POST['invID']; ?>" />

                            <?php } ?>
                        </form>
            <!-- ngInclude: 'assets/views/partials/footer.html' -->
            <footer data-ng-include=" 'assets/views/partials/footer.html'" id="footer" class="ng-scope">
                <div class="footer-inner ng-scope">
                    <div class="pull-left">
                        <p class="m0"> ©
                            <?php echo APP_NAME; ?> 2011-2021
                            <br /> </p>
                        <!-- ngIf: footerAddText -->
                    </div>
                    <div class="pull-right"> <span class="go-top" ng-click="toTheTop()"><i style="font-size: 30px;" class="icon-angle-up"></i></span> </div>
                    <!-- ngIf: !isMobileApp -->
                    <!-- ngInclude: 'assets/views/partials/livechat.html' -->
                    <!-- end ngIf: !isMobileApp -->
                </div>
            </footer>
        </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://cdn.jsdelivr.net/npm/js-cookie@rc/dist/js.cookie.min.js"></script>

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

            clipboard.on('success', function (e) {
                Snackbar.show({
                    text: 'Copied!',
                    pos: 'top-right'
                });
            });
            $("[data-toggle='pill']").on('shown.bs.tab', function () {
                if (window.innerWidth < 770) {
                    let id = 'scroll' + $(this).attr('data-id');
                    let sc = document.getElementById(id);
                    sc.scrollIntoView(true);
                    console.log(sc);
                }
                Cookies.set('selection', $(this).attr('data-id'), {expires: 7});
            });
            $('.upload').click(function () {
                $('#file').trigger('click');
            });
            $('#file').change(function () {
                $(this).parent('form').trigger('submit');
            });
<?php if (isset($error)) { ?>
                if (Cookies.get('selection')) {
                    var q = "a[data-id='" + Cookies.get("selection") + "']";
                    $(q).trigger('click');
                }
<?php } ?>
        </script>

        <script>

            $('#k34').addClass('active');
        </script>
    </body>

</html>