<?php
require 'inc/auth_check.php';
require 'inc/header.php';

$hero = $mysqli->query("SELECT * FROM hero WHERE id=1")->fetch_assoc();
$msg = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = $_POST['name'] ?? '';
    $title = $_POST['title'] ?? '';
    $subtitle = $_POST['subtitle'] ?? '';
    $description = $_POST['description'] ?? '';

    // handle upload
    if (!empty($_FILES['profile_image']['name'])) {
        $f = $_FILES['profile_image'];
        $allowed = ['image/png','image/jpeg','image/jpg','image/webp'];
        if ($f['size'] > 2*1024*1024) { $msg = 'File terlalu besar (max 2MB)'; }
        elseif (!in_array(mime_content_type($f['tmp_name']), $allowed)) { $msg = 'Tipe file tidak diizinkan'; }
        else {
            $ext = pathinfo($f['name'], PATHINFO_EXTENSION);
            $newname = uniqid() . '.' . $ext;
            if (!move_uploaded_file($f['tmp_name'], __DIR__ . '/../uploads/profile/' . $newname)) {
                $msg = 'Upload gagal';
            } else {
                // delete old
                if (!empty($hero['profile_image']) && file_exists(__DIR__ . '/../uploads/profile/' . $hero['profile_image'])) {
                    @unlink(__DIR__ . '/../uploads/profile/' . $hero['profile_image']);
                }
                $hero['profile_image'] = $newname;
            }
        }
    }

    if ($msg === '') {
        $stmt = $mysqli->prepare("UPDATE hero SET name=?, title=?, subtitle=?, description=?, profile_image=? WHERE id=1");
        $stmt->bind_param('sssss', $name, $title, $subtitle, $description, $hero['profile_image']);
        $stmt->execute();
        $stmt->close();
        $msg = 'Berhasil disimpan';
        $hero = $mysqli->query("SELECT * FROM hero WHERE id=1")->fetch_assoc();
    }
}
?>
<h2 class="text-xl font-bold mb-4">Edit Hero</h2>
<?php if($msg): ?><div class="mb-4 text-green-600"><?php echo e($msg); ?></div><?php endif; ?>

<form method="post" enctype="multipart/form-data" class="bg-white p-4 shadow rounded">
  <label class="block mb-2">Nama</label>
  <input name="name" value="<?php echo e($hero['name']); ?>" class="w-full p-2 border mb-3">
  <label class="block mb-2">Title</label>
  <input name="title" value="<?php echo e($hero['title']); ?>" class="w-full p-2 border mb-3">
  <label class="block mb-2">Subtitle</label>
  <input name="subtitle" value="<?php echo e($hero['subtitle']); ?>" class="w-full p-2 border mb-3">
  <label class="block mb-2">Description</label>
  <textarea name="description" class="w-full p-2 border mb-3"><?php echo e($hero['description']); ?></textarea>

  <label class="block mb-2">Foto Profil (jpg/png/webp) — max 2MB</label>
  <?php if(!empty($hero['profile_image']) && file_exists(__DIR__ . '/../uploads/profile/' . $hero['profile_image'])): ?>
    <img src="/uploads/profile/<?php echo e($hero['profile_image']); ?>" class="w-24 mb-3">
  <?php endif; ?>
  <input type="file" name="profile_image" class="mb-4">

  <button class="bg-teal-600 text-white py-2 px-4 rounded">Simpan</button>
</form>

</div></body></html>
