<?php
header('Content-Type: application/json');
require_once __DIR__ . '/../config/database.php';

$id = $_POST['id'] ?? null;
$pin_code = $_POST['pin_code'] ?? null;
$role = $_POST['role'] ?? null;

if (!$id) {
    echo json_encode(['success' => false, 'error' => 'ID required']);
    exit;
}

if ($pin_code) {
    $stmt = $mysqli->prepare("UPDATE users SET pin_code=?, role=? WHERE id=?");
    $stmt->bind_param("ssi", $pin_code, $role, $id);
} else {
    $stmt = $mysqli->prepare("UPDATE users SET role=? WHERE id=?");
    $stmt->bind_param("si", $role, $id);
}

$ok = $stmt->execute();
echo json_encode(['success' => $ok]);
?>
