<?php
session_start();
require_once 'config.php';

$id = $_GET['id'];
$stmt = $db->prepare("SELECT * FROM admin_users WHERE id = ?");
$stmt->execute([$id]);
$admin = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$admin) {
    die("Admin tidak ditemukan.");
}

if ($_SERVER["REQUEST_METHOD"] === "POST") {
    $username = $_POST['username'];
    $password = $_POST['password'];

    if (!empty($password)) {
        $password_hash = password_hash($password, PASSWORD_BCRYPT);
        $stmt = $db->prepare("UPDATE admin_users SET username = ?, password_hash = ? WHERE id = ?");
        $stmt->execute([$username, $password_hash, $id]);
    } else {
        $stmt = $db->prepare("UPDATE admin_users SET username = ? WHERE id = ?");
        $stmt->execute([$username, $id]);
    }

    header("Location: manage_admin.php");
    exit;
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Edit Admin</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">
</head>
<body class="p-4 bg-light">
    <div class="container">
        <h2>Edit Admin</h2>
        <form method="post">
            <div class="form-group">
                <label>Username:</label>
                <input type="text" name="username" class="form-control" value="<?= htmlspecialchars($admin['username']) ?>" required>
            </div>
            <div class="form-group">
                <label>Password Baru (kosongkan jika tidak diubah):</label>
                <input type="password" name="password" class="form-control">
            </div>
            <button type="submit" class="btn btn-success">Simpan</button>
            <a href="manage_admin.php" class="btn btn-secondary">Batal</a>
        </form>
    </div>
</body>
</html>
