<?php
include('../includes/session.php');
include('../includes/config.php');

// Admin check
if(checkAdmin($udata['type']) == false){
    echo "<script>window.location='index.php';</script>";
    die();
}

include('../template/ahkweb/header.php');

// Password reset
if(isset($_GET['id'], $_GET['reset']) && $_GET['reset'] == 1){
    $id = base64_decode($_GET['id']);
    $vpass = rand(100000,999999); // 6-digit random password
    $pass = password_hash($vpass, PASSWORD_DEFAULT);
    $reset = mysqli_query($ahk_conn, "UPDATE users SET password='$pass' WHERE id='$id'");
    if($reset){
        echo "<script>
            $(function(){
                Swal.fire('Password Reset Successfully', 'New: $vpass', 'success');
            });
        </script>";
    }
}

// Login as user
if(isset($_GET['login'], $_GET['phone']) && $_GET['login'] == 1 && $_GET['phone'] != NULL){
    $phone = base64_decode($_GET['phone']);
    $_SESSION['phone'] = $phone;
    $_SESSION['adminasuser'] = true;
    $_SESSION['adminusername'] = $udata['phone'];
    echo "<script>setTimeout(function(){ window.location=''; }, 0);</script>";
}

include('header.php');

// Admin actions: delete, ban, approve
if(checkAdmin($udata['type']) == true){
    if(isset($_GET['deleteuser'], $_GET['id']) && $_GET['deleteuser'] == 1){
        $id = base64_decode(mysqli_real_escape_string($ahk_conn, $_GET['id']));
        if(mysqli_query($ahk_conn, "DELETE FROM users WHERE id='$id'")){
            showAlert('User Deleted Successfully');
            ahkRedirect('users.php', 1200);
        }
    }

    if(isset($_GET['banid'], $_GET['ban']) && $_GET['ban'] == 1){
        $banid = base64_decode(getSafe($_GET['banid']));
        if(ahkQuery("UPDATE users SET status='0' WHERE id='$banid'")){
            showAlert('User Banned Successfully');
            ahkRedirect('users.php', 1200);
        }
    }

    if(isset($_GET['apid'], $_GET['approve']) && $_GET['approve'] == 1){
        $apid = base64_decode(getSafe($_GET['apid']));
        if(ahkQuery("UPDATE users SET status='1' WHERE id='$apid'")){
            showAlert('User Activated Successfully');
            ahkRedirect('users.php', 1200);
        }
    }
}else{
    header("Location: index.php");
    die();
}
?>

<!--start page wrapper -->
<div class="page-wrapper">
    <div class="page-content">
        <div class="card radius-10">
            <div class="card-body">
                <div class="d-flex align-items-center">
                    <div>
                        <h5 class="mb-0">All Users List</h5>
                    </div>
                </div>
                <hr>
                <div class="table-responsive">
                    <table id="example2" class="table align-middle mb-0">
                        <thead class="table-light">
                            <tr>
                                <th class="text-center">SL.</th>
                                <th class="text-center">Username</th>
                                <th class="text-center">Name</th>
                                <th class="text-center">Email</th>
                                <th class="text-center">Type</th>
                                <th class="text-center">Balance</th>
                                <th class="text-center">Action</th>
                            </tr>
                        </thead>
                        <tbody>
<?php
$res = mysqli_query($ahk_conn, "SELECT * FROM users ORDER BY id DESC");
if(mysqli_num_rows($res) > 0){
    $x = 0;
    while($data = mysqli_fetch_assoc($res)){
        if($data['type'] == 'admin') continue;
        $x++;
?>
        <tr>
            <td class="text-center"><?= $x;?></td>
            <td class="text-center"><?= htmlspecialchars($data['phone']); ?></td>
            <td class="text-center"><?= htmlspecialchars($data['name']); ?></td>
            <td class="text-center"><?= htmlspecialchars($data['email']); ?></td>
            <td class="text-center"><?= strtoupper($data['type']); ?></td>
            <td class="text-center">
                <?= $data['balance'] !== NULL ? strtoupper($data['balance']) : '0.00'; ?>
                <form action="BalanceTransfer.php" method="POST">
                    <input type="hidden" name="username" value="<?= htmlspecialchars($data['email']); ?>">
                    <button class="btn btn-success" type="submit">Balance Transfer</button>
                </form>
            </td>
            <td class="text-center">
                <div class="mr-12 font-24">
                    <a hidden class="text-success bg-light-success" title="Login As User" href="users.php?phone=<?= base64_encode($data['phone']); ?>&login=1">
                        <img height="50px" src="../template/ahkweb/assets/login-rounded-right.png">
                    </a>
                    <a class="text-success bg-light-success" title="Reset Password" href="users.php?id=<?= base64_encode($data['id']); ?>&reset=1"><i class='bx bx-refresh'></i></a>
                    <a class="text-success bg-light-success" href="EditUser.php?id=<?= base64_encode($data['id']); ?>"><i class='bx bx-pencil'></i></a>
                    <?php if($data['status']== '0'){ ?>
                        <a class="text-success bg-light-success" title="Approve User" href="?apid=<?= base64_encode($data['id']); ?>&approve=1"><i class='bx bx-check'></i></a>
                    <?php }else{ ?>
                        <a class="text-danger bg-light-danger" title="Ban User" href="?banid=<?= base64_encode($data['id']); ?>&ban=1"><i class='bx bx-x'></i></a>
                    <?php } ?>
                    <a class="text-danger bg-light-danger" href="?deleteuser=1&id=<?= base64_encode($data['id']); ?>"><i class='bx bx-trash'></i></a>
                </div>
            </td>
        </tr>
<?php
    }
}
?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<!--end page wrapper -->
<?php include('footer.php'); ?>

<!-- Bootstrap JS and plugins -->
<script src="../template/ahkweb/assets/js/jquery.min.js"></script>
<script src="../template/ahkweb/assets/js/bootstrap.bundle.min.js"></script>
<script src="../template/ahkweb/assets/plugins/simplebar/js/simplebar.min.js"></script>
<script src="../template/ahkweb/assets/plugins/metismenu/js/metisMenu.min.js"></script>
<script src="../template/ahkweb/assets/plugins/perfect-scrollbar/js/perfect-scrollbar.js"></script>
<script src="../template/ahkweb/assets/plugins/chartjs/chart.min.js"></script>
<script src="../template/ahkweb/assets/plugins/vectormap/jquery-jvectormap-2.0.2.min.js"></script>
<script src="../template/ahkweb/assets/plugins/vectormap/jquery-jvectormap-world-mill-en.js"></script>
<script src="../template/ahkweb/assets/plugins/jquery.easy-pie-chart/jquery.easypiechart.min.js"></script>
<script src="../template/ahkweb/assets/plugins/sparkline-charts/jquery.sparkline.min.js"></script>
<script src="../template/ahkweb/assets/plugins/jquery-knob/excanvas.js"></script>
<script src="../template/ahkweb/assets/plugins/jquery-knob/jquery.knob.js"></script>
<script>
$(function() {
    $(".knob").knob();
});
</script>
<script src="../template/ahkweb/assets/js/index.js"></script>
<script src="../template/ahkweb/assets/js/app.js"></script>

<!-- DataTables -->
<script src="../template/ahkweb/assets/plugins/datatable/js/jquery.dataTables.min.js"></script>
<script src="../template/ahkweb/assets/plugins/datatable/js/dataTables.bootstrap5.min.js"></script>
<script>
$(document).ready(function() {
    var table = $('#example2').DataTable({
        lengthChange: false,
        buttons: ['copy', 'excel', 'pdf', 'print']
    });
    table.buttons().container().appendTo('#example2_wrapper .col-md-6:eq(0)');
});
</script>

</body>
</html>
