<?php
include 'koneksi.php';

$id_user  = $_POST['id_user'];
$name     = $_POST['name'];
$username = $_POST['username'];
$jabatan  = $_POST['jabatan'];
$role     = $_POST['role'];
$status   = $_POST['status'];
$password = $_POST['password'];

if (!empty($password)) {
    $password_hashed = password_hash($password, PASSWORD_DEFAULT);
    $sql = "UPDATE users SET name=?, username=?, password=?, jabatan=?, role=?, status=? WHERE id_user=?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("ssssssi", $name, $username, $password_hashed, $jabatan, $role, $status, $id_user);
} else {
    $sql = "UPDATE users SET name=?, username=?, jabatan=?, role=?, status=? WHERE id_user=?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("sssssi", $name, $username, $jabatan, $role, $status, $id_user);
}

if ($stmt->execute()) {
    header("Location: data_user.php?status=updated");
    exit;
} else {
    echo "Gagal update data: " . $stmt->error;
}
?>
