<?php
$title = 'User Wallet';
require_once('header-links.php');
// require_once('../lib/basic-function.php');
// require_once('../lib/user-functions.php');
// require_once('../lib/investment-plan-functions.php');
// require_once('../lib/package-functions.php');
// require_once('../lib/currency-functions.php');
// print_r($_GET['user']);die();
$user_balance = $wallet->get_balance($_GET['user']);

$total_withdrawals = $wallet->get_total_withdrawals($_GET['user']); //user_amt_in_investments
$total_investments = $wallet->user_amt_in_investments($_GET['user']);
$total_investments = $purchase->pending_purchase_amount($_GET['user']);

$all_wallet = $basicFunctn->selectWhereDlt($user->user_wallet_tb, 'user_id', $_GET['user']);
$all_txns = $basicFunctn->selectWhereDlt($basicFunctn->wallet_tb, 'user_id', $_GET['user']);

$user_detail = $basicFunctn->selectWhereDlt($user->user_tb, 'unique_id', $_GET['user'])[0];
$full_name = explode('++', $user_detail['full_name']);
$first_name = $full_name[0];
$last_name = $full_name[1];
// $date_joined = explode(',', $the_user['created_at']);
?>

<body>
    <div class="app">
        <div class="layout">
            <?php require_once('sidebar.php'); ?>

            <!-- Page Container START -->
            <div class="page-container">
                <?php require_once('header.php'); ?>



                <!-- Content Wrapper START -->
                <div class="main-content">
                    <div class="container-fluid">
                        <div class="page-title text-capitalize">
                            <h4><?php print $first_name . ' ' . $last_name; ?>'s Wallet</h4>
                        </div>
                        <div class="row">
                            <div class="col-lg-6">
                                <div class="card">
                                    <div class="card-block index-pg">
                                        <div class="inline-block">
                                            <p>Account Balance</p>
                                            <h1 class="no-mrg-vertical">$ <?php print number_format($user_balance); ?></h1>
                                        </div>
                                        <div class="pdd-top-25 inline-block pull-right">
                                            <i class="ei-users fa-5x"></i>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <!-- <div class="col-lg-4">
                                <div class="card">
                                    <div class="card-block index-pg">
                                        <div class="inline-block">
                                            <p>Total Withdrawals</p>
                                            <h1 class="no-mrg-vertical">$ <?php // print number_format($total_withdrawals); ?></h1>
                                        </div>
                                        <div class="pdd-top-25 inline-block pull-right">
                                            <i class="ei-users fa-5x"></i>
                                        </div>
                                    </div>
                                </div>
                            </div> -->
                            <div class="col-lg-6">
                                <div class="card">
                                    <div class="card-block index-pg">
                                        <div class="inline-block">
                                            <p>Total Purchased</p>
                                            <h1 class="no-mrg-vertical">$ <?php print number_format($total_investments); ?></h1>
                                        </div>
                                        <div class="pdd-top-25 inline-block pull-right">
                                            <i class="ei-users fa-5x"></i>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <!-- <div class="col-sm-12 col-lg-6" style="float:left;">
                                <div class="card">
                                    <div class="card-heading border bottom">
                                        <h4 class=""><strong>User Wallet Address</strong></h4>
                                    </div>
                                    <div class="card-block">
                                        <div class="row">

                                            <?php // foreach ($all_wallet as $e) {
                                                // $coin = $user->getDataDlt($e['coin_id'], $user->currencies_tb);  ?>
                                                <div class="col-md-12 col-sm-12">
                                                    <div class="col-md-12 mrg-top-10">
                                                        <p class=" "> <?php // print $coin['name']; ?> Wallet:</p>
                                                        <input type="text" disabled class="form-control" value="<?php // print $e['address']; ?>">
                                                    </div><br>
                                                </div>
                                            <?php //} ?>
                                        </div>
                                    </div>
                                </div>
                            </div> -->
                            <!-- <div class="col-sm-12 col-lg-6" style="float:left;">
                                <div class="card">
                                    <div class="card-block">
                                        <div class="row">
                                            <div class="col-md-6 col-sm-12 ">
                                                 <form class="top_up_form"> 
                                                 <div class="mrg-top-10 text-center col-md-12 text-left">
                                                 <button id="<?php // print $_GET['user']; ?>" class="btn btn-primary btn-lg col-md-12  top_up">Add to wallet</button>
                                                 </div>
                                                 </form>
                                            </div>
                                            <div class="col-md-6 col-sm-12">
                                                 <form class="top_up_form"> 
                                                 <div class="mrg-top-10 text-center col-md-12 text-left"> 
                                                <button id="<?php // print $_GET['user']; ?>" class="btn btn-primary btn-lg col-md-12  withdraw_balance">Withdraw from wallet</button>
                                                 </div> 
                                                 </form> 
                                            </div>

                                        </div>
                                    </div>
                                </div>
                            </div> -->
                        </div>

                        <div class="row">
                            <div class="col-md-12">
                                <div class="card">
                                    <div class="card-heading border bottom">
                                        <h4 class=""><strong>Wallet Transactions</strong></h4>
                                    </div>
                                    <div class="card-block">
                                        <div class="table-overflow table-responsive">
                                            <table id="dt-opt" class="table table-lg table-hover">
                                                <thead>
                                                    <tr>
                                                        <th>S/N</th>
                                                        <th>Transaction Id</th>
                                                        <th>Type</th>
                                                        <th>Description</th>
                                                        <th>Amount($)</th>
                                                        <th class="text-center">Status</th>
                                                        <th>Date Created</th>
                                                        <th></th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <?php
                                                    $i = 1;
                                                    foreach ($all_txns as $e) {
                                                        $coin = $basicFunctn->getDataDlt($e['coin_type_id'], $basicFunctn->currencies_tb);
                                                    ?>
                                                        <tr>
                                                            <td><?php print $i; ?></td>
                                                            <td>
                                                                <div class="relative mrg-top-15">
                                                                    <span class="text-info"><?php print $e['unique_id']; ?></span>
                                                                </div>
                                                            </td>
                                                            <td>
                                                                <div class="mrg-top-15">
                                                                    <span class="text-danger"><?php print $e['action_type']; ?></span>
                                                                </div>
                                                            </td>
                                                            <td>
                                                                <div class="mrg-top-15">
                                                                    <span class=""><?php print $e['description']; ?></span>
                                                                </div>
                                                            </td>
                                                            <td>
                                                                <div class="mrg-top-15">
                                                                    <span><?php print round($e['amt_in_usd']); ?></span>
                                                                </div>
                                                            </td>
                                                            <td>
                                                                <div class="relative mrg-top-15">

                                                                    <?php if ($e['status'] == 'pending') { ?>
                                                                        <span class="status warning"> </span>
                                                                    <?php } elseif ($e['status'] == 'confirmed') { ?>
                                                                        <span class="status success"> </span>
                                                                    <?php } ?>

                                                                    <span class="pdd-left-10 text-capitalize"><b><?php print $e['status']; ?></b></span>
                                                                </div>
                                                            </td>
                                                            <td>
                                                                <div class="mrg-top-15">
                                                                    <span><?php print $e['created_at']; ?></span>
                                                                </div>
                                                            </td>
                                                            <td>
                                                                <div class="mrg-top-10 text-center">
                                                                    <button class="btn btn-icon btn-flat btn-rounded dropdown-toggle" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                                                        <i class="ti-more-alt"></i>
                                                                    </button>
                                                                    <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                                                                        <?php if ($e['status'] == 'pending') { ?>
                                                                            <a id="<?php print $e['unique_id']; ?>" class="dropdown-item cursor-pointer confirm_modal">Confirm Deposit</a>
                                                                        <?php } elseif ($e['status'] == 'confirmed') { ?>
                                                                            <a id="<?php print $e['unique_id']; ?>" class="dropdown-item cursor-pointer unconfirm_modal">Unconfirm Deposit</a>
                                                                        <?php } ?>
                                                                        <a id="<?php print $e['unique_id']; ?>" class="dropdown-item cursor-pointer delete_txn_modal">Delete Txn</a>
                                                                    </div>
                                                                </div>
                                                            </td>
                                                        </tr>
                                                    <?php $i++;
                                                    } ?>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <!-- Content Wrapper END -->
                    <?php require_once('footer.php'); ?>
                    <!-- Footer END -->

                </div>
                <!-- Page Container END -->

            </div>
        </div>

        <div class="modal fade zoomInUp" id="top_up_modal">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h4>Top up user balance?</h4>
                    </div>
                    <form class="top_up_form">
                        <div class="modal-body">
                            <input type="number" class="form-control" name="amount" placeholder="Enter amount">
                        </div>
                    </form>
                    <div class="modal-footer no-border">
                        <div class="text-right">
                            <button class="btn btn-default btn-sm" data-dismiss="modal">Cancel</button>
                            <button class="btn btn-primary btn-sm top_up_btn" data-dismiss="modal">Continue</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="modal fade zoomInUp" id="withdraw_balance_modal">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h4>Withdraw from user balance?</h4>
                    </div>
                    <form class="withdraw_balance_form">
                        <div class="modal-body">
                            <input type="number" class="form-control" name="amount" placeholder="Enter amount">
                        </div>
                    </form>
                    <div class="modal-footer no-border">
                        <div class="text-right">
                            <button class="btn btn-default btn-sm" data-dismiss="modal">Cancel</button>
                            <button class="btn btn-primary btn-sm withdraw_balance_btn" data-dismiss="modal">Continue</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Modal START-->
        <div class="modal fade zoomInUp" id="confirm-modal">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h4>Confirm Deposit?</h4>
                    </div>
                    <form class="confirm_txn_form">
                    <input type="hidden" name="user_id" value="<?php print $_GET['user'] ?>">
                        <div class="modal-body">
                            <p class="">By confirming this, you acknowledge to have recieved payment for this deposit. <br> User can now use the fund for purchases.</p>
                        </div>
                    </form>
                    <div class="modal-footer no-border">
                        <div class="text-right">
                            <button class="btn btn-default btn-sm" data-dismiss="modal">Cancel</button>
                            <button class="btn btn-primary btn-sm confirm_txn_btn" data-dismiss="modal">Continue</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="modal fade zoomInUp" id="unconfirm_modal">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h4>Unconfirm Deposit?</h4>
                    </div>
                    <form class="unconfirm_form">
                    <input type="hidden" name="user_id" value="<?php print $_GET['user'] ?>">
                        <div class="modal-body">
                            <p class="">By clicking continue, this deposit will be made pending.</p>
                        </div>
                    </form>
                    <div class="modal-footer no-border">
                        <div class="text-right">
                            <button class="btn btn-default btn-sm" data-dismiss="modal">Cancel</button>
                            <button class="btn btn-primary btn-sm unconfirm_btn" data-dismiss="modal">Continue</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="modal fade zoomInUp" id="delete_txn_modal">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h4>Delete Deposit?</h4>
                    </div>
                    <form class="delete_txn_form">
                    <input type="hidden" name="user_id" value="<?php print $_GET['user'] ?>">
                        <div class="modal-body">
                            <p class="text-danger">By clicking continue, this deposit will be deleted permanently. <br> Users won't be able to view this transaction.</p>
                        </div>
                    </form>
                    <div class="modal-footer no-border">
                        <div class="text-right">
                            <button class="btn btn-default btn-sm" data-dismiss="modal">Cancel</button>
                            <button class="btn btn-primary btn-sm delete_txn_btn" data-dismiss="modal">Continue</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <?php require_once('footer-links.php'); ?>
        <script>
            $('.top_up').click(function(e) {
                e.preventDefault();
                append_id('top_up_user_id', '.top_up_form', '#top_up_modal', this)
                $('#top_up_modal').modal('toggle');
            });
            $('.withdraw_balance').click(function(e) {
                e.preventDefault();
                append_id('withdraw_balance_user_id', '.withdraw_balance_form', '#withdraw_balance_modal', this)
                $('#withdraw_balance_modal').modal('toggle');
            });
            $('.confirm_modal').click(function(e) {
                e.preventDefault();
                append_id('wallet_confirm_deposit_id', '.confirm_txn_form', '#confirm-modal', this)
                $('#confirm-modal').modal('toggle');
            });
            $('.unconfirm_modal').click(function(e) {
                e.preventDefault();
                append_id('wallet_unconfirm_txn_id', '.unconfirm_form', '#unconfirm_modal', this)
                $('#unconfirm_modal').modal('toggle');
            });
            $('.delete_txn_modal').click(function(e) {
                e.preventDefault();
                append_id('wallet_delete_txn_id', '.delete_txn_form', '#delete_txn_modal', this)
                $('#delete_txn_modal').modal('toggle');
            });

            $('.top_up_btn').click(async function(e) {
                e.preventDefault();
                let data = $('.top_up_form').serializeArray();
                let form_data = set_form_data(data);
                // console.log(data);return;
                let returned = await ajaxRequest('ajax.php', form_data);
                throwSweetalert(returned, 'user-wallet?user=' + data[1].value, 1);
            });
            $('.withdraw_balance_btn').click(async function(e) {
                e.preventDefault();
                let data = $('.withdraw_balance_form').serializeArray();
                let form_data = set_form_data(data);
                // console.log(data);return;
                let returned = await ajaxRequest('ajax.php', form_data);
                throwSweetalert(returned, 'user-wallet?user=' + data[1].value, 1);
            });
            $('.withdraw_deposit_btn').click(async function(e) {
                e.preventDefault();
                let data = $('.withdraw_deposit_form').serializeArray();
                let form_data = set_form_data(data);
                // console.log(data);return;
                let returned = await ajaxRequest('ajax.php', form_data);
                throwSweetalert(returned, 'user-wallet?user=' + data[1].value, 1);
            });
        $('.confirm_txn_btn').click(async function(e) {
            e.preventDefault();
            let data = $('.confirm_txn_form').serializeArray();
            let form_data = set_form_data(data);
            let returned = await ajaxRequest('ajax.php', form_data);
                throwSweetalert(returned, 'user-wallet?user=' + data[0].value, 1);
        });
        $('.unconfirm_btn').click(async function(e) {
            e.preventDefault();
            let data = $('.unconfirm_form').serializeArray();
            let form_data = set_form_data(data);
            // console.log(unconfirm_form);return;
            let returned = await ajaxRequest('ajax.php', form_data);
            // console.log(returned);return;
                throwSweetalert(returned, 'user-wallet?user=' + data[0].value, 1);
        });
        $('.delete_txn_btn').click(async function(e) {
            e.preventDefault();
            let data = $('.delete_txn_form').serializeArray();
            let form_data = set_form_data(data);
            // console.log(delete_txn_form);return;
            let returned = await ajaxRequest('ajax.php', form_data);
            // console.log(returned);return;
                throwSweetalert(returned, 'user-wallet?user=' + data[0].value, 1);
        });
        </script>