<?php
include('includes/header.php');
include('includes/navbar.php');
?>

<div class="container-fluid">

    <div class="card shadow mb-4">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-primary" style="margin-bottom:5px">User List</h6>
            <button type="button" class="btn btn-danger"  style="margin-bottom:10px" data-toggle="modal" data-target="#addadminprofile"> <i class="fa-solid fa-user-slash"></i> Suspend User</button>
        </div>
        <?php
        // DELETE USER DATA
        if(isset($_POST['userdelete_btn']))
        {
            $d_userid = $_POST['delete_user_id'];
            $query_delete = "DELETE FROM users WHERE user_id = '$d_userid'";
            if ($connection->query($query_delete)===TRUE) {
                $error = "delete_success";
            }else {
                $error = "error";
              }
        }
         ?>
        <div class="card-body">
            <div class="table-responsive">
            <?php
                $i=1;
                $query = "SELECT * FROM users WHERE id != 1 ORDER BY id DESC";
                $query_run = mysqli_query($connection, $query);
            ?>
                <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
                    <thead>
                        <tr>
                            <th> S/N </th>
                            <th> Fullname </th>
                            <th> Email </th>
                            <th> Bonus </th>
                            <th> Profit </th>
                            <th> BTC </th>
                            <th> ETH </th>
                            <th> LTC </th>
                            <th> USDT </th>
                            <th> KYC </th>
                            <th> Password</th>
                            <th> Status</th>
                            <th> Date </th>
                            <th>ACTION</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        if(mysqli_num_rows($query_run) > 0)
                        {
                            while($row = mysqli_fetch_assoc($query_run))
                            {
                        ?>
                            <tr>
                                <td><?php  echo $i++; ?></td>
                                <td><?php  echo $row['full_name']; ?></td>
                                <td><?php  echo $row['email']; ?></td>
                                <td>
                                  <?php
                                    $bonus = $row['bonus'];
                                    echo $row['currency'];
                                    echo number_format("$bonus",2);
                                  ?>
                                </td>
                                <td>
                                  <?php
                                    $profit = $row['profit'];
                                    echo $row['currency'];
                                    echo number_format("$profit",2);
                                  ?>
                                </td>
                                <td>
                                  <?php
                                    $btc_bal = $row['btc'];
                                    echo $row['currency'];
                                    echo number_format("$btc_bal",2);
                                  ?>
                                </td>
                                <td>
                                  <?php
                                    $eth_bal = $row['eth'];
                                    echo $row['currency'];
                                    echo number_format("$eth_bal",2);
                                  ?>
                                </td>
                                <td>
                                  <?php
                                    $ltc_bal = $row['ltc'];
                                    echo $row['currency'];
                                    echo number_format("$ltc_bal",2);
                                  ?>
                                </td>
                                <td>
                                  <?php
                                    $usdt_bal = $row['usdt'];
                                    echo $row['currency'];
                                    echo number_format("$usdt_bal",2);
                                  ?>
                                </td>
                                <td>
                                  <?php
                                    $kyc = $row['kyc_status'];
                                    if ($kyc == 0):
                                   ?>
                                   <span class="text-danger">Unverified</span>
                                   <?php
                                   elseif ($kyc == 1):
                                  ?>
                                  <a href="kyc.php" class="text-warning">Pending Verification</a>
                                  <?php
                                  elseif ($kyc == 2):
                                 ?>
                                 <span class="text-success">Verified</span>
                               <?php endif; ?>
                                </td>
                                <td><?php  echo $row['password']; ?></td>
                                <td><?php  echo $row['status']; ?></td>
                                <td><?php  echo $row['reg_date']; ?></td>
                                <td>
                                    <form action="" method="post" onsubmit="return(confirm('User delete confirmation, Click to proceed'))">
                                        <input type="hidden" name="delete_user_id" value="<?php echo $row['user_id']; ?>">
                                        <button type="submit" name="userdelete_btn" class="btn btn-danger"> <i class="fa-solid fa-trash-can"></i></button>
                                        <button type="button" onclick="window.location.href='users_edit.php?edit_id=<?php echo $row['user_id']; ?>'" class="btn btn-success"> <i class="fa-solid fa-user-pen"></i></button>
                                    </form>
                                </td>
                            </tr>
                        <?php
                            }
                        }
                        else {
                            echo "No Record Found";
                        }
                        ?>
                    </tbody>
                </table>

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



</div>
<!-- /.container-fluid -->
<p id="error" style='display: none '><?php echo $error?></p>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<script>
    var error = document.getElementById('error');

    if (error.textContent == "delete_success") {
      swal("Successful!", "User account was deleted successfully", "success");
        setTimeout(() => {
            window.location.href = 'users.php'
        }, 3000);
    }else if (error.textContent == "error") {
      swal("ERROR!", "Sorry!!!, Something went wrong, try later or contact support", "error");
    }

</script>
<?php
include('includes/scripts.php');
include('includes/footer.php');
?>
