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

require_once "auth.php";

$user = null;
$msg  = "";

/* ================= SEARCH USER ================= */

if (isset($_GET['uid'])) {

    $uid = trim($_GET['uid']);

    $stmt = $pdo->prepare("SELECT * FROM user_data WHERE user_id=? LIMIT 1");
    $stmt->execute([$uid]);
    $user = $stmt->fetch(PDO::FETCH_ASSOC);

    if (!$user) {
        $msg = "❌ User not found";
    }
}

/* ================= UPDATE USER ================= */

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $uid     = trim($_POST['user_id']);
    $name    = trim($_POST['name']);
    $damo    = floatval($_POST['damo']);
    $spend   = floatval($_POST['spend']);
    $balance = floatval($_POST['balance']);
    $status  = $_POST['status'];

    $stmt = $pdo->prepare("
        UPDATE user_data 
        SET name=?, damo=?, spend=?, balance=?, status=? 
        WHERE user_id=?
    ");
    $stmt->execute([$name, $damo, $spend, $balance, $status, $uid]);

    $msg = "✅ User updated successfully";

    // Reload updated data
    $stmt = $pdo->prepare("SELECT * FROM user_data WHERE user_id=? LIMIT 1");
    $stmt->execute([$uid]);
    $user = $stmt->fetch(PDO::FETCH_ASSOC);
}
?>

<!DOCTYPE html>
<html>
<head>

<?php include("include/head.php"); ?>

<title>Edit User</title>

</head>

<body id="page-top">

<div id="wrapper">

<?php include("include/slidebar.php"); ?>

<div id="content-wrapper" class="d-flex flex-column">
<div id="content">

<?php include("include/topbar.php"); ?>

<div class="container-fluid">

<!-- 🔎 SEARCH -->
<div class="card mb-4">
<div class="card-header">
<h6 class="m-0 font-weight-bold text-primary">🔎 Search User</h6>
</div>

<div class="card-body">

<form method="get">
<input type="text"
       name="uid"
       class="form-control"
       placeholder="Enter User ID"
       required>
<br>
<button class="btn btn-primary">Search</button>
</form>

</div>
</div>

<!-- 📢 MESSAGE -->
<?php if ($msg) { ?>
<div class="alert alert-info"><?= $msg ?></div>
<?php } ?>

<!-- 👤 USER DETAILS -->
<?php if ($user) { ?>

<div class="card mb-4">
<div class="card-header">
<h6 class="m-0 font-weight-bold text-success">👤 User Details</h6>
</div>

<div class="card-body">

<form method="post">

<input type="hidden" name="user_id" value="<?= $user['user_id'] ?>">

<div class="row">

<div class="col-md-6">
<label>User ID</label>
<input class="form-control"
       value="<?= $user['user_id'] ?>"
       disabled>
</div>

<div class="col-md-6">
<label>Name</label>
<input class="form-control"
       name="name"
       value="<?= htmlspecialchars($user['name']) ?>"
       required>
</div>

<div class="col-md-6">
<label>Total Recharge (Damo)</label>
<input type="number"
       step="0.01"
       name="damo"
       class="form-control"
       value="<?= $user['damo'] ?? 0 ?>"
       required>
</div>

<div class="col-md-6">
<label>Total Spend</label>
<input type="number"
       step="0.01"
       name="spend"
       class="form-control"
       value="<?= $user['spend'] ?? 0 ?>"
       required>
</div>

<div class="col-md-12">
<label>Balance</label>
<input type="number"
       step="0.01"
       name="balance"
       class="form-control"
       value="<?= $user['balance'] ?>"
       required>
</div>

<div class="col-md-12">
<label>Status</label>
<select name="status" class="form-control">
    <option value="ACTIVE" <?= $user['status']=='ACTIVE'?'selected':'' ?>>ACTIVE</option>
    <option value="BANNED" <?= $user['status']=='BANNED'?'selected':'' ?>>BANNED</option>
</select>
</div>

</div>

<br>

<button class="btn btn-success">
💾 Update User
</button>

<?php if ($user['status'] == 'ACTIVE') { ?>
<button type="submit"
        name="status"
        value="BANNED"
        class="btn btn-danger">
🚫 Ban User
</button>
<?php } else { ?>
<button type="submit"
        name="status"
        value="ACTIVE"
        class="btn btn-primary">
✅ Unban User
</button>
<?php } ?>

</form>

</div>
</div>

<?php } ?>

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

</div>

<?php include("include/script.php"); ?>

</body>
</html>
