<?php
$title = 'User Accounts';
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');
$all_users = $basicFunctn->selectAllDlt($basicFunctn->user_tb);
?>

<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">
                            <h4><?php print $title; ?></h4>
                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <div class="card">
                                    <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>User</th>
                                                        <th>Country</th>
                                                        <!-- <th>Referrer</th> -->
                                                        <th>Status</th>
                                                        <th>Joined On</th>
                                                        <th></th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <?php
                                                    $i = 1;
                                                    foreach ($all_users as $e) {
                                                        $user_detail = $basicFunctn->selectWhereDlt($basicFunctn->user_details, 'user_unique_id', $e['unique_id'])[0];
                                                        $referrer = $basicFunctn->getDataDlt($user_detail['referrer'],$basicFunctn->user_tb);
                                                        $full_name = explode('++', $e['full_name']);
                                                        $first_name = $full_name[0];
                                                        $last_name = $full_name[1];
                                                    ?>
                                                        <tr>
                                                            <td><?php print $i; ?></td>
                                                            <td>
                                                                <div class="list-info mrg-top-10">
                                                                    <img class="thumb-img" src="../../user/assets/uploads/profile/<?php print $user_detail['passport']; ?>" alt="">
                                                                    <div class="info">
                                                                        <span class="title text-capitalize"><?php print $first_name . ' ' . $last_name; ?></span>
                                                                        <span class="sub-title"><?php print $e['email']; ?></span>
                                                                    </div>
                                                                </div>
                                                            </td>
                                                            <td>
                                                                <div class="mrg-top-15">
                                                                    <span class="text-danger"><?php print $user_detail['country']; ?></span>
                                                                </div>
                                                            </td>
                                                            <!-- <td>
                                                                <div class="mrg-top-15">
                                                                    <span><?php // print $referrer['username']; ?></span>
                                                                </div>
                                                            </td> -->
                                                            <td>
                                                                <div class="relative mrg-top-15">

                                                                    <?php if ($e['is_blocked'] == '0') { ?>
                                                                    <span class="pdd-left-10 text-capitalize">
                                                                        <b>Not Blocked</b></span>
                                                                        <span class="status success"> </span>
                                                                    <?php } elseif ($e['is_blocked'] == '1') { ?>
                                                                    <span class="pdd-left-10 text-capitalize"><b>
                                                                        Blocked</b></span>
                                                                        <span class="status danger"> </span>
                                                                    <?php } ?>
                                                                </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">
                                                                        <a href="user-wallet?user=<?php print $e['unique_id']; ?>" class="dropdown-item cursor-pointer ">User wallet </a>
                                                                    <?php if ($e['is_blocked'] == '0') { ?>
                                                                        <a id="<?php print $e['unique_id']; ?>" class="dropdown-item cursor-pointer block_user_modal">Block </a>
                                                                    <?php } elseif ($e['is_blocked'] == '1') { ?>
                                                                        <a id="<?php print $e['unique_id']; ?>" class="dropdown-item cursor-pointer unblock_user_modal">Unblock </a>
                                                                    <?php } ?>
                                                                        <a class="dropdown-item" href="user-profile?user=<?php print $e['unique_id']; ?>">Edit</a>
                                                                        <a id="<?php print $e['unique_id']; ?>" class="dropdown-item cursor-pointer delete_user_modal">Delete</a>
                                                                    </div>
                                                                </div>
                                                            </td>
                                                        </tr>
                                                    <?php $i++;
                                                    } ?>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- Modal START-->
                <div class="modal fade zoomInUp" id="block_user_modal">
                    <div class="modal-dialog" role="document">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h4>Block Account?</h4>
                            </div>
                            <form class="block_user_form">
                                <div class="modal-body">
                                    <p class="">By confirming this, you acknowledge to the blocking of this account. <br> User won't have access to this account.</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 block_user_btn" data-dismiss="modal">Continue</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- Modal START-->
                <div class="modal fade zoomInUp" id="unblock_user_modal">
                    <div class="modal-dialog" role="document">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h4>Unblock Account?</h4>
                            </div>
                            <form class="unblock_user_form">
                                <div class="modal-body">
                                    <p class="">By clicking continue, this account will no longer be blocked. <br> User will now have access to this account.</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 unblock_user_btn" data-dismiss="modal">Continue</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- Modal START-->
                <div class="modal fade zoomInUp" id="delete_user_modal">
                    <div class="modal-dialog" role="document">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h4>Delete Withdrawal?</h4>
                            </div>
                            <form class="delete_user_form">
                                <div class="modal-body">
                                    <p class="text-danger">By clicking continue, this account will be deleted permanently. <br> It can't be recovered after this.</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_user_btn" data-dismiss="modal">Continue</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- Content Wrapper END -->
                <?php require_once('footer.php'); ?>
                <!-- Footer END -->

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

        </div>
    </div>

    <?php require_once('footer-links.php'); ?>
    <script>
        $('.block_user_modal').click(function(e) {
            e.preventDefault();
            append_id('block_user_id', '.block_user_form', '#block_user_modal', this)
            $('#block_user_modal').modal('toggle');
        });
        $('.unblock_user_modal').click(function(e) {
            e.preventDefault();
            append_id('unblock_user_id', '.unblock_user_form', '#unblock_user_modal', this)
            $('#unblock_user_modal').modal('toggle');
        });
        $('.delete_user_modal').click(function(e) {
            e.preventDefault();
            append_id('delete_user_id', '.delete_user_form', '#delete_user_modal', this)
            $('#delete_user_modal').modal('toggle');
        });


        $('.block_user_btn').click(async function(e) {
            e.preventDefault();
            let block_user_form = $('.block_user_form').serializeArray();
            let form_data = set_form_data(block_user_form);
            // console.log(block_user_form);return;
            let returned = await ajaxRequest('ajax.php', form_data);
            throwSweetalert(returned, 'users', 1);
        });
        $('.unblock_user_btn').click(async function(e) {
            e.preventDefault();
            let unblock_user_form = $('.unblock_user_form').serializeArray();
            let form_data = set_form_data(unblock_user_form);
            // console.log(unblock_user_form);return;
            let returned = await ajaxRequest('ajax.php', form_data);
            // console.log(returned);return;
            throwSweetalert(returned, 'users', 1);
        });
        $('.delete_user_btn').click(async function(e) {
            e.preventDefault();
            let delete_user_form = $('.delete_user_form').serializeArray();
            let form_data = set_form_data(delete_user_form);
            // console.log(delete_user_form);return;
            let returned = await ajaxRequest('ajax.php', form_data);
            // console.log(returned);return;
            throwSweetalert(returned, 'users', 1);
        });
    </script>