<?php
include '../../koneksi/koneksi.php';

$id         = $_POST['id'];
$nama_toko = $_POST['nama_toko'];
$nama_admin = $_POST['nama_admin'];
$username   = $_POST['username'];
$password   = $_POST['password'];
$level      = $_POST['level'];

// jika password tidak diisi, jangan update password
if (!empty($password)) {
    // hash password baru
    $hash = password_hash($password, PASSWORD_DEFAULT);
    $sql = "UPDATE admin 
            SET nama_toko  = '$nama_toko',
                nama_admin = '$nama_admin',
                username   = '$username',
                password   = '$hash',
                level      = '$level'
            WHERE id = '$id'";
} else {
    // update tanpa ubah password
    $sql = "UPDATE admin 
            SET nama_toko  = '$nama_toko',
                nama_admin = '$nama_admin',
                username   = '$username',
                level      = '$level'
            WHERE id = '$id'";
}

$result = mysqli_query($conn, $sql);

if ($result) {
    echo "
    <script>
        alert('Admin berhasil diedit');
        window.location = '../m_admin.php';
    </script>
    ";
} else {
    echo "
    <script>
        alert('Gagal mengedit admin: " . mysqli_error($conn) . "');
        window.location = '../edit_admin.php?id=$id';
    </script>
    ";
}
?>
