import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { createLargeScreen, deleteLargeScreen, getLargeScreen, updateLargeScreen } from './api'
import { message } from 'antd'

// 创建
export function useCreateLargeScreen() {
  const queryClient = useQueryClient()

  return useMutation({
    mutationFn: (data: { name: string; type: string; content: string }) => createLargeScreen(data),
    onSuccess: () => {
      message.success('创建成功')
      queryClient.invalidateQueries({ queryKey: ['largeScreenList'] })
    },
  })
}

// 更新
export function useUpdateLargeScreen() {
  const queryClient = useQueryClient()

  return useMutation({
    mutationFn: (data: { id: string; name: string; type: string; content: string }) => updateLargeScreen(data),
    onSuccess: () => {
      message.success('更新成功')
      queryClient.invalidateQueries({ queryKey: ['largeScreenList'] })
    },
  })
}
// 删除
export function useDeleteLargeScreen() {
  const queryClient = useQueryClient()

  return useMutation({
    mutationFn: (data: { id: string }) => deleteLargeScreen(data),
    onSuccess: () => {
      message.success('删除成功')
      queryClient.invalidateQueries({ queryKey: ['largeScreenList'] })
    },
  })
}

export function useViewLargeScreen(id: string) {
  return useQuery({
    queryKey: ['largeScreen', id],
    queryFn: () => getLargeScreen({ id }),
    select: (res) => res.data,
    enabled: !!id,
    staleTime: 0,
  })
}
