<?php
include('session_handler.php');

// Pastikan hanya superadmin boleh akses
if (!checkRole('superadmin')) {
    header('Location: unauthorized.php');
    exit;
}

// Semak parameter ID user
if (!isset($_GET['id'])) {
    echo "Ralat: ID tidak diberikan.";
    exit;
}

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

// Tetapan password baru (contoh default: 123456)
$password_baru = 'zakat123';
$hash = password_hash($password_baru, PASSWORD_DEFAULT);

// ID admin yang buat reset
$admin_id = $_SESSION['user_id'];

// Update password
$stmt = $conn->prepare("UPDATE users SET password = ?, MdfBy = ? WHERE id = ?");
$stmt->bind_param("sii", $hash, $admin_id, $id_user);

if ($stmt->execute()) {
    echo "<script>alert('Password berjaya direset kepada $password_baru'); window.location='view_user.php';</script>";
} else {
    echo "<script>alert('Gagal reset password'); window.location='view_user.php';</script>";
}
?>