<?php
session_start();
use Codenixsv\CoinGeckoApi\CoinGeckoClient;

require_once '../models/User.php';
require_once '../include/Helper.php';
require_once '../models/Document.php';
require_once '../models/Withdrawal.php';
require_once '../models/Invoice.php';
require_once '../include/DatabaseConn.php';
require_once '../include/Constants.php';
require_once '../models/Admin.php';
require_once '../models/Wallet.php';
require_once '../models/Referral.php';
require_once "../vendor/autoload.php";
require_once "../vendor1/autoload.php";
require_once "../models/PaymentWallet.php";

$admin = Admin::getCurrentAdmin();

if (is_null($admin)) {
    $url = $_SERVER['REQUEST_URI'];
    header("location: ./login?" . RETURN_URL . "=" . $url);
    die();
}

$db = new DatabaseConn();
$link = $db->connect();

if(isset($_POST['amounte'])) {
    $amount = filter_input(INPUT_POST, "amounte",FILTER_VALIDATE_FLOAT);
    $email = filter_input(INPUT_POST, "idUser",FILTER_VALIDATE_EMAIL);
    $id = filter_input(INPUT_POST, "idInc");
    $token = filter_input(INPUT_POST, "token");
    
    
    if(is_null($amount) || is_bool($amount)){
        $error = "Enter a valid amount";
    }
    else if(is_null($email) || is_bool($email)){
        $error = "Token missing in request";
    }
     else if(is_null($id) || is_bool($id)){
        $error = "Token missing in request";
    }
     else if(is_null($token) || is_bool($token)){
        $error = "Token missing in request";
    }
    else {
         $client = new CoinGeckoClient();
         $token = PaymentWallet::getTokenPrice($token,$client);
          $roi = $amount / $token;
         mysqli_query($link, "update `invoice` set `amount` = $amount, `roi` = $roi where email = '$email' and id = $id");
        if(mysqli_affected_rows($link) == 1) {
            $success = "Changed";
        }
        else {
            $error = "There was an error updating";
        }
    }
}

$invoices = Invoice::getInvoices($link);

?>
<!DOCTYPE html>
<html lang="en">
    <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" />
    </head>
    <body>
        <div id="app">
            <?php require_once './partials/top-header.php'; ?>
            <!--  BEGIN MAIN CONTAINER  -->
            <div class="main-container" id="container">
                <div class="overlay"></div>
                <div class="search-overlay"></div>
                <?php require_once './partials/side-bar.php'; ?>
                <!--  BEGIN CONTENT AREA  -->
                <div id="content" class="main-content">
                    <div class="layout-px-spacing">
                        <div class="account-settings-container layout-top-spacing">
                            <div class="account-content">
                                <div class="scrollspy-example" data-spy="scroll" data-target="#account-settings-scroll" data-offset="-100">
                                    <div class="row">
                                        <div class="col-xl-12 col-lg-12 col-md-12 layout-spacing">
                                            <form id="about" class="section about">
                                                <div class="info inv-section">
                                                    <h5 class="">Wallet Log</h5>
                                                    <div class="row">
                                                        <div class="col-md-11 mx-auto">
                                                            <div class="table-responsive mb-4 style-1">
                                                                <table id='style-7' class='table style-1 table-hover non-hover'>
                                                                    <thead>
                                                                        <tr>
                                                                            <th>Email</th>
                                                                            <th>Amount</th>
                                                                            <th>Payment Wallet</th>
                                                                            <th>Date</th>
                                                                            <th class=''>Status</th>
                                                                            <th class='text-center'>Action</th>
                                                                        </tr>
                                                                    </thead>
                                                                    <tbody>
                                                                        <?php
                                                                        if (!is_null($invoices)) {
                                                                            foreach ($invoices as $invoice) {
                                                                                $user = User::getCurrentUser($link, $invoice->getUser());
                                                                                ?>
                                                                                <tr class="inv">
                                                                                    <td><?php echo "{$invoice->getUser()}"; ?></td>
                                                                                    <td><?php echo "{$invoice->getAmount()}"; ?></td>
                                                                                    <td><?php echo "{$invoice->getPackage()}"; ?></td>
                                                                                    <td><?php echo Helper::readableTime($invoice->getDate()); ?></td>
                                                                                    <td style="color: #3c8dbc;"><?php echo "{$invoice->getStatus()}"; ?></td>
                                                                                    <td class='text-center'>
                                                                                        <div class='dropdown custom-dropdown'>
                                                                                            <a class='dropdown-toggle' href='#' role='button' id='dropdownMenuLink' data-toggle='dropdown' aria-haspopup='true' aria-expanded='true'>
                                                                                                <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-more-horizontal'> <circle cx='12' cy='12' r='1'></circle> <circle cx='19' cy='12' r='1'></circle> <circle cx='5' cy='12' r='1'></circle> </svg>
                                                                                            </a>
                                                                                            <div class='dropdown-menu' aria-labelledby='dropdownMenuLink'>
                                                                                                
                                                                                                <?php if (strcmp($invoice->getStatus(), CONFIRMED) != 0 && (strcmp($invoice->getStatus(), GROWING) != 0 && strcmp($invoice->getStatus(), Paused) != 0)) {
                                                                                                    ?>
                                                                                                    <a class='dropdown-item cursor confirminv' data-id="<?php echo "{$invoice->getId()}"; ?>"  data-email="<?php echo "{$invoice->getUser()}"; ?>" data-amount="<?php echo "{$invoice->getAmount()}"; ?>">Confirm</a>
                                                                                                <?php } ?>
                                                                                                <a class='dropdown-item cursor deleteIn' data-id="<?php echo "{$invoice->getId()}"; ?>">Delete</a>
                                                                                                <a class='dropdown-item cursor editInc' data-id="<?php echo "{$invoice->getId()}"; ?>" data-token="<?php echo "{$invoice->getIType()}"; ?>"  data-email="<?php echo "{$invoice->getUser()}"; ?>" data-amount="<?php echo "{$invoice->getAmount()}"; ?>">Edit Amount</a>
                                                                                            </div>
                                                                                        </div>
                                                                                    </td>
                                                                                </tr>
                                                                                <?php
                                                                            }
                                                                        } else {
                                                                            ?>
                                                                            <tr><td colspan="7"> <p style="text-align: center">No investment history </p></td></tr>
                                                                        <?php }
                                                                        ?>
                                                                    </tbody>
                                                                </table>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </form>
                                        </div>
                                        
                                    </div>
                                </div>
                            </div>

                        </div>
                    </div>
                    <?php require_once './partials/footer.php'; ?>
                </div>
                <!--  END CONTENT AREA  -->
            </div>
        </div>
        <div class="modal fade login-modal" id="loginModal" tabindex="-1" role="dialog" aria-labelledby="loginModalLabel"  aria-hidden="true">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header" id="loginModalLabel">
                        <h4 class="modal-title">Edit</h4>
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                            <svg
                                aria-hidden="true"
                                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-x"
                                >
                            <line x1="18" y1="6" x2="6" y2="18"></line>
                            <line x1="6" y1="6" x2="18" y2="18"></line>
                            </svg>
                        </button>
                    </div>
                    <div class="modal-body">
                        <form method="POST" action="history" id="kfj">
                            <div class="form-row">
                                <div class="col-md-12">
                                    <input type="hidden" name="idInc"  />
                                    <input type="hidden" name="idUser"  />
                                    <input type="hidden" name="token"  />
                                    <label for="generateKey">Edit Amount(USD)</label>
                                    <input id="amounte" name="amounte" type="text" value="" class="form-control" placeholder="">
                                </div>
                               
                                <div class="col-md-12 settings-profile" style="margin-top: 20px">
                                    <input type="button"  id="saveInc" value="Save">
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
        <!-- END MAIN CONTAINER -->
        <?php require_once './partials/scripts.php'; ?>
        <script src="../js/datatables.js" type="text/javascript"></script>
        <script src="../js/snackbar.min.js" type="text/javascript"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/lightbox2/2.11.1/js/lightbox.min.js"></script>
        <script src="../js/jqueryblockui.js" type="text/javascript"></script>
        <script src="../js/customblockui.js" type="text/javascript"></script>
        <script>
                                                                            let query = "select[name='type_'] option[value='" + '<?php echo $user->getWSettings(); ?>' + "']";
                                                                            $(query).prop('selected', true);


                                                                            // var e;
                                                                            c1 = $("#style-1").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: 100,
                                                                            });

                                                                            multiCheck(c1);
                                                                            c2 = $("#style-2").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: 100
                                                                            });

                                                                            multiCheck(c2);
                                                                            c3 = $("#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: 100,
                                                                            });

                                                                            multiCheck(c3);

                                                                            c4 = $("#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: 100,
                                                                            });

                                                                            multiCheck(c4);
                                                                            c7 = $("#style-7").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: 100,
                                                                            });

                                                                            multiCheck(c7);

                                                                            c90 = $("#style-90").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: 100,
                                                                            });

                                                                            multiCheck(c90);




        </script>
        <script>
            $('.dodo').click(function () {
                alert("The user has not uploaded payment proof");
            });
            function loader(blockContent) {
                var block = $(blockContent);
                $(block).block({
                    message: "<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-loader spin'><line x1='12' y1='2' x2='12' y2='6'></line><line x1='12' y1='18' x2='12' y2='22'></line><line x1='4.93' y1='4.93' x2='7.76' y2='7.76'></line><line x1='16.24' y1='16.24' x2='19.07' y2='19.07'></line><line x1='2' y1='12' x2='6' y2='12'></line><line x1='18' y1='12' x2='22' y2='12'></line><line x1='4.93' y1='19.07' x2='7.76' y2='16.24'></line><line x1='16.24' y1='7.76' x2='19.07' y2='4.93'></line></svg>",
                    overlayCSS: {
                        backgroundColor: '#000',
                        opacity: 0.8,
                        zIndex: 99999,
                        cursor: 'wait'
                    },
                    css: {
                        border: 0,
                        color: '#fff',
                        padding: 0,
                        backgroundColor: 'transparent'
                    }
                });
            }

            function removeLoader(element) {
                $(element).unblock();
            }

            $('#list').change(function () {
                loader('div.settings');
                axios.get('v1/withdrawal-settings', {
                    params: {
                        user: '<?php echo $user->getEmail(); ?>',
                        option: $("option:selected", this).val()
                    }
                }).then((response) => {
                    showToast("Settings Changed.");
                }).catch((error) => {
                    showToast("Error occured");
                }).finally(() => {
                    removeLoader('div.settings');
                });
            });
            $('.confirm').click(function () {
                var user = $(this).attr('data-date');
                var date = $(this).attr('data-user');
                loader('div.with-section');
                axios.get('v1/confirm-withdrawal', {
                    params: {
                        id: date,
                        user: user
                    }
                }).then((response) => {
                    var data = response.data;
                    if (!data.error) {
                        $(this).closest('tr.with').find('td.status').text(data.msg).css("color", "#009688");
                        $(this).hide().next().hide();
                    } else {
                        showToast("Error occured");
                    }
                }).catch((error) => {
                    showToast("Error occured");
                }).finally(() => {
                    removeLoader('div.with-section');
                });
            });
            $('.editInc').click(function () {
               $("input[name='amounte']").val($(this).attr("data-amount"));
                $("input[name='idInc']").val($(this).attr("data-id"));
                $("input[name='idUser']").val($(this).attr("data-email"));
                $("input[name='token']").val($(this).attr("data-token"));
                $('#loginModal').modal();
            });

            $('#saveInc').click(function () {
                $('#kfj').trigger('submit');
                return;
                loader('body');
                $('#loginModal').modal('hide');
                var email = $("input[name='idUser']").val();
                axios.get('v1/edit-invoice', {
                    params: {
                        id: $("input[name='idInc']").val(),
                        capital: $("input[name='capital']").val(),
                        profit: $("input[name='profit']").val(),
                        email: $("input[name='idUser']").val()
                    }
                }).then((response) => {
                    var data = response.data;
                    if (!data.error) {
                        showToast("Operation successful");
                        window.location.href = "view-user?email=" + email;
                    } else {
                        showToast("Error occured");
                    }
                }).catch((error) => {
                    showToast("Error occured");
                }).finally(() => {
                    removeLoader('body');
                });
            });

            $('.confirminv').click(function () {
                var user = $(this).attr('data-id');
                var email = $(this).attr('data-email');
                var amount = $(this).attr('data-amount');
                loader('div.inv-section');
                axios.get('v1/confirm-invoice', {
                    params: {
                        id: user,
                        email: email,
                        amount: amount
                    }
                }).then((response) => {
                    var data = response.data;
                    if (!data.error) {
                        showToast("Confirmed successful");
                        window.location.href = "history";
                    } else {
                        showToast("Error occured");
                    }
                }).catch((error) => {
                    showToast("Error occured");
                }).finally(() => {
                    removeLoader('div.inv-section');
                });
            });

            $('.approve').click(function () {
                var user = $(this).attr('data-user');
                loader('div.docs-section');
                axios.get('v1/approve-document', {
                    params: {
                        id: user
                    }
                }).then((response) => {
                    var data = response.data;
                    if (!data.error) {
                        $(this).closest('tr.docs').find('td.status').text(data.msg).css("color", "#009688");
                        if ($(this).next().length == 1) {
                            $(this).hide().next().show();
                        } else if ($(this).next().length == 0) {
                            $(this).hide().prev().show();
                        }
                    } else {
                        showToast("Error occured");
                    }
                }).catch((error) => {
                    showToast("Error occured");
                }).finally(() => {
                    removeLoader('div.docs-section');
                });

            });

            $('.reject').click(function () {
                var user = $(this).attr('data-user');
                loader('div.docs-section');
                axios.get('v1/reject-document', {
                    params: {
                        id: user
                    }
                }).then((response) => {
                    var data = response.data;
                    if (!data.error) {
                        if ($(this).next().length == 1) {
                            $(this).hide().next().show();
                        } else if ($(this).next().length == 0) {
                            $(this).hide().prev().show();
                        }
                        $(this).closest('tr.docs').find('td.status').text(data.msg).css("color", "#e95f2b");

                    } else {
                        showToast("Error occured");
                    }
                }).catch((error) => {
                    showToast("Error occured");
                }).finally(() => {
                    removeLoader('div.docs-section');
                });

            });

            $('.delete').click(function () {
                var user = $(this).attr('data-user');
                loader('div.with-section');
                axios.get('v1/delete-withdrawal', {
                    params: {
                        id: user
                    }
                }).then((response) => {
                    var data = response.data;
                    if (!data.error) {
                        showToast("Deleted Successful");
                        $("#style-2").DataTable().row($(this).closest('tr.with'))
                                .remove()
                                .draw();
                    } else {
                        showToast("Error occured");
                    }
                }).catch((error) => {
                    showToast("Error occured");
                }).finally(() => {
                    removeLoader('div.with-section');
                });

            });
            $('.deleteIn').click(function () {
                var user = $(this).attr('data-id');
                loader('div.inv-section');
                axios.get('v1/delete-invoice', {
                    params: {
                        id: user
                    }
                }).then((response) => {
                    var data = response.data;
                    if (!data.error) {
                        showToast("Deleted Successful");
                        $("#style-7").DataTable().row($(this).closest('tr.inv'))
                                .remove()
                                .draw();
                    } else {
                        showToast("Error occured");
                    }
                }).catch((error) => {
                    showToast("Error occured");
                }).finally(() => {
                    removeLoader('div.inv-section');
                });

            });
            $('.pauseIn').click(function () {
                var user = $(this).attr('data-id');
                var email = $(this).attr('data-email');
                loader('div.cinv-section');
                axios.get('v1/pause-invoice', {
                    params: {
                        id: user,
                        email: email
                    }
                }).then((response) => {
                    var data = response.data;
                    if (!data.error) {
                        showToast("Operation Successful");
                        window.location.href = "view-user?email=" + email;
                    } else {
                        showToast("Error occured");
                    }
                }).catch((error) => {
                    showToast("Error occured");
                }).finally(() => {
                    removeLoader('div.cinv-section');
                });

            });
            $('.resumeIn').click(function () {
                var user = $(this).attr('data-id');
                var email = $(this).attr('data-email');
                loader('div.cinv-section');
                axios.get('v1/resume-invoice', {
                    params: {
                        id: user,
                        email: email
                    }
                }).then((response) => {
                    var data = response.data;
                    if (!data.error) {
                        showToast("Operation Successful");
                        window.location.href = "view-user?email=" + email;
                    } else {
                        showToast("Error occured");
                    }
                }).catch((error) => {
                    showToast("Error occured");
                }).finally(() => {
                    removeLoader('div.cinv-section');
                });

            });
            $('.rejectWalletDeposit').click(function () {
                var user = $(this).attr('data-user');
                loader('div.wallet-section');
                axios.get('v1/delete-wallet-deposit', {
                    params: {
                        id: user
                    }
                }).then((response) => {
                    var data = response.data;
                    if (!data.error) {
                        showToast("Deleted Successful");
                        $("#style-4").DataTable().row($(this).closest('tr.with'))
                                .remove()
                                .draw();
                    } else {
                        showToast("Error occured");
                    }
                }).catch((error) => {
                    showToast("Error occured");
                }).finally(() => {
                    removeLoader('div.wallet-section');
                });

            });

            $('.approveWalletDeposit').click(function () {
                let c = confirm("Are you sure");
                if (!c) {
                    return;
                }
                var user = $(this).attr('data-user');
                loader('div.wallet-section');
                axios.get('v1/approve-wallet-deposit', {
                    params: {
                        id: user
                    }
                }).then((response) => {
                    var data = response.data;
                    if (!data.error) {
                        showToast("Deposit Confirmed Successful");
                        $(this).hide().next().hide();
                        $(this).closest('tr.with').find('td.status').text(data.msg).css("color", "#e95f2b");

                    } else {
                        showToast("Error occured");
                    }
                }).catch((error) => {
                    showToast("Error occured");
                }).finally(() => {
                    removeLoader('div.wallet-section');
                });

            });
            $('.reset').click(function () {
                var r = confirm("Are you sure");
                if (r) {
                    var user = $(this).attr('data-user');
                    loader('div.settings');
                    axios.get('v1/reset-2fa', {
                        params: {
                            id: user
                        }
                    }).then((response) => {
                        var data = response.data;
                        if (!data.error) {
                            showToast(data.msg);

                        } else {
                            showToast(data.error);
                        }
                    }).catch((error) => {
                        showToast("Error occured");
                    }).finally(() => {
                        removeLoader('div.settings');
                    });

                }
            });
            $('.fund').click(function () {
                var amount = prompt("Enter amount");
                if (amount === null || amount.trim().length < 0) {
                    return;
                }
                if (amount <= 0) {
                    alert("Enter a valid amount");
                    return;
                }
                var user = $(this).attr('data-user');
                loader('div.wallet-section');
                axios.get('v1/fund-wallet', {
                    params: {
                        user: user,
                        amount: amount
                    }
                }).then((response) => {
                    var data = response.data;
                    if (!data.error) {
                        showToast("Wallet Funded");
                        window.location.href = "view-user?email=" + user;
                    } else {
                        showToast("Error occured");
                    }
                }).catch((error) => {
                    showToast("Error occured");
                }).finally(() => {
                    removeLoader('div.wallet-section');
                });
            });
            function showToast(msg) {
                Snackbar.show({
                    text: msg,
                    pos: 'top-right'
                });
            }
            <?php if(isset($error)) {?>
                Snackbar.show({
                    text: '<?php echo $error; ?>',
                    pos: 'top-right'
                });
           <?php }
                ?>
                 <?php if(isset($success)) {?>
                Snackbar.show({
                    text: '<?php echo $success; ?>',
                    pos: 'top-right'
                });
           <?php }
                ?>
        </script>
    </body>
</html>