<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);

include('con.php');
include('header.php');

if (!isset($_SESSION['phone'])) {
    header("Location: login.php");
    exit;
}

$username = $_SESSION['phone'];

$successMsg = "";
$errorMsg   = "";

/* ================= USER DATA ================= */
$udata = mysqli_fetch_assoc(
    mysqli_query($ahk_conn, "SELECT * FROM users WHERE phone='$username'")
);

$type = $udata['type'];

/* ================= ACCESS CHECK ================= */
if ($type != 'Distributor' ) {
    die("Access Denied");
}

/* ================= CHANGE PASSWORD ================= */
if (isset($_GET['resetpass'])) {

    $id = intval($_GET['resetpass']);

    $newpass = rand(100000,999999);

    $hashpass = password_hash($newpass, PASSWORD_DEFAULT);

    mysqli_query($ahk_conn,
        "UPDATE users SET password='$hashpass' WHERE id='$id'"
    );

    $successMsg = "New Password: ".$newpass;
}

/* ================= CHANGE STATUS ================= */
if (isset($_GET['status'])) {

    $id = intval($_GET['status']);

    $get = mysqli_fetch_assoc(
        mysqli_query($ahk_conn,
            "SELECT status FROM users WHERE id='$id'"
        )
    );

    $newstatus = ($get['status'] == 1) ? 0 : 1;

    mysqli_query($ahk_conn,
        "UPDATE users SET status='$newstatus' WHERE id='$id'"
    );

    $successMsg = "User status updated successfully";
}

/* ================= DELETE USER ================= */
if (isset($_GET['delete'])) {

    $id = intval($_GET['delete']);

    mysqli_query($ahk_conn,
        "DELETE FROM users WHERE id='$id'"
    );

    $successMsg = "User deleted successfully";
}

/* ================= SEARCH ================= */
$search = "";

if (!empty($_GET['search'])) {

    $s = mysqli_real_escape_string($ahk_conn, $_GET['search']);

    $search = "
        AND (
            name LIKE '%$s%' OR
            phone LIKE '%$s%' OR
            email LIKE '%$s%'
        )
    ";
}

/* ================= FETCH USERS ================= */
$users = mysqli_query($ahk_conn, "
    SELECT *
    FROM users
    WHERE parent='$username'
    $search
    ORDER BY id DESC
");
?>

<!DOCTYPE html>
<html>
<head>

<title>View Users</title>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

<style>

.card-box{
    background:#fff;
    padding:20px;
    border-radius:15px;
    box-shadow:0 8px 25px rgba(0,0,0,.1);
}

.action-btn{
    padding:3px 8px;
    font-size:11px;
}

</style>

</head>

<body class="bg-light">

<div class="container-fluid py-4">

<div class="card-box">

<div class="d-flex justify-content-between align-items-center mb-3">

<h4 class="text-primary mb-0">
My Users
</h4>

<form method="GET" class="d-flex">

<input type="text"
name="search"
class="form-control me-2"
placeholder="Search User">

<button class="btn btn-primary">
Search
</button>

</form>

</div>

<div class="table-responsive">

<table class="table table-bordered table-hover align-middle text-center">

<thead class="table-dark">

<tr>
<th>ID</th>
<th>Name</th>
<th>Phone</th>
<th>Email</th>
<th>Type</th>
<th>Balance</th>
<th>Status</th>

</tr>

</thead>

<tbody>

<?php if(mysqli_num_rows($users) > 0): ?>

<?php while($u = mysqli_fetch_assoc($users)): ?>

<tr>

<td>#<?= $u['id'] ?></td>

<td><?= $u['name'] ?></td>

<td><?= $u['phone'] ?></td>

<td><?= $u['email'] ?></td>

<td>
<span class="badge bg-primary">
<?= $u['type'] ?>
</span>
</td>

<td>
₹<?= number_format($u['balance'],2) ?>
</td>

<td>

<?php if($u['status'] == 1): ?>

<span class="badge bg-success">
Active
</span>

<?php else: ?>

<span class="badge bg-danger">
Inactive
</span>

<?php endif; ?>

</td>



</tr>

<?php endwhile; ?>

<?php else: ?>

<tr>
<td colspan="8">
No Users Found
</td>
</tr>

<?php endif; ?>

</tbody>

</table>

</div>

</div>

</div>

<?php if($successMsg): ?>

<script>
Swal.fire({
    icon:'success',
    title:'Success',
    text:<?= json_encode($successMsg) ?>
});
</script>

<?php endif; ?>

<?php if($errorMsg): ?>

<script>
Swal.fire({
    icon:'error',
    title:'Error',
    text:<?= json_encode($errorMsg) ?>
});
</script>

<?php endif; ?>

<?php include('footer.php'); ?>

</body>
</html>