<?php
// reset_admin.php — jalankan sekali lalu hapus demi keamanan
include 'koneksi.php';
error_reporting(E_ALL);
ini_set('display_errors', 1);

$username = 'admin';
$password = 'admin123';
$hash     = password_hash($password, PASSWORD_DEFAULT);

// cek apakah user admin sudah ada
$stmt = $conn->prepare("SELECT id FROM users WHERE username=? LIMIT 1");
$stmt->bind_param('s', $username);
$stmt->execute();
$res = $stmt->get_result();

if ($res && $res->num_rows > 0) {
    // update password
    $u = $conn->prepare("UPDATE users SET password=? WHERE username=?");
    $u->bind_param('ss', $hash, $username);
    if ($u->execute()) {
        echo "✅ Password untuk user '<b>{$username}</b>' berhasil diupdate jadi '<b>{$password}</b>'.";
    } else {
        echo "❌ Gagal update password: " . $u->error;
    }
} else {
    // buat user baru
    $i = $conn->prepare("INSERT INTO users (username, password, role) VALUES (?, ?, 'admin')");
    $i->bind_param('ss', $username, $hash);
    if ($i->execute()) {
        echo "✅ User '<b>{$username}</b>' berhasil dibuat dengan password '<b>{$password}</b>'.";
    } else {
        echo "❌ Gagal membuat user: " . $i->error;
    }
}
?>
