'use client'

import { useRouter } from "next/navigation"

export default function DeleteNewsButton({ id }: { id: number }) {
  const router = useRouter()

  const handleDelete = async () => {
    if (!confirm("确定要删除这条新闻吗？")) {
      return
    }

    try {
      const res = await fetch(`/api/admin/news/${id}`, {
        method: "DELETE",
      })

      if (res.ok) {
        router.refresh()
      }
    } catch (error) {
      alert("删除失败")
    }
  }

  return (
    <button
      onClick={handleDelete}
      className="text-red-600 hover:text-red-700 text-sm"
    >
      删除
    </button>
  )
}
