import { request } from '@/utils/request'

// ErrorNotification 类型定义
export interface ErrorNotification {
  id?: number

  subscriptionKey?:  string

  title?:  string

  subscriptionDeviceIds?:  string

  subscriptionDiscoverField?:  string

  sendingMethod?:  string

  discoverValue?:  string

  detectionMethod?:  string

  notifier?:  string

  createBy?: number

  crateTime?:  string

  notifierContent?:  string

  abnormalReporting?: number

}

// ErrorNotification API
export const errorNotificationApi = {
  // 分页查询
  page(params: { page: number; pageSize: number }) {
    return request({ url: '/biz/errorNotification/page', method: 'get', params })
  },

  // 获取详情
  detail(id:  string) {
    return request({ url: `/biz/errorNotification/${id}`, method: 'get' })
  },

  // 新增
  create(data: ErrorNotification) {
    return request({ url: '/biz/errorNotification', method: 'post', data })
  },

  // 修改
  update(data: ErrorNotification) {
    return request({ url: '/biz/errorNotification', method: 'put', data })
  },

  // 删除
  delete(ids:  string[]) {
    return request({ url: `/biz/errorNotification/${ids.join(',')}`, method: 'delete' })
  },

  // 导出
  export(params?: { ids?:  string[] }) {
    const p: Record<string, any> = {}
    if (params?.ids?.length) p.ids = params.ids.join(',')
    return request({ url: `/biz/errorNotification/export`, method: 'get', params: p, responseType: 'blob' })
  },

  // 导入
  importData(file: File) {
    const formData = new FormData()
    formData.append('file', file)
    return request<{ success: number; fail: number; errors: string[] }>({
      url: `/biz/errorNotification/import`,
      method: 'post',
      data: formData,
      headers: { 'Content-Type': 'multipart/form-data' }
    })
  },

  // 下载导入模板
  downloadTemplate() {
    return request({ url: `/biz/errorNotification/template`, method: 'get', responseType: 'blob' })
  }
}
