import http from '@/utils/http'

const { request } = http

export interface InsuranceFile {
  id?: string
  employeeName: string
  employeeId: string
  fileName: string
  fileType: string
  fileSize?: number
  filePath?: string
  uploadDate?: string
  description?: string
  remarks?: string
}

export interface InsuranceFileListParams {
  employeeName?: string
  employeeId?: string
  fileType?: string
  uploadDateStart?: string
  uploadDateEnd?: string
  current?: number
  size?: number
}

export interface InsuranceFileListResponse {
  records: InsuranceFile[]
  total: number
  current: number
  size: number
}

export interface UploadInsuranceFileParams {
  employeeId: string
  fileType: string
  description?: string
  file: File
}

// 上传保单文件
export function uploadInsuranceFile(data: UploadInsuranceFileParams) {
  const formData = new FormData()
  formData.append('file', data.file)
  formData.append('employeeId', data.employeeId)
  formData.append('fileType', data.fileType)
  if (data.description) {
    formData.append('description', data.description)
  }
  
  return request({
    url: '/insurance-file/upload',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 查询保单文件列表
export function getInsuranceFileList(params: InsuranceFileListParams) {
  return request<InsuranceFileListResponse>({
    url: '/insurance-file/list',
    method: 'get',
    params
  })
}

// 查询文件详情
export function getInsuranceFileDetail(id: string) {
  return request<InsuranceFile>({
    url: `/insurance-file/detail/${id}`,
    method: 'get'
  })
}

// 更新文件信息
export function updateInsuranceFile(id: string, data: Partial<InsuranceFile>) {
  return request({
    url: `/insurance-file/${id}`,
    method: 'put',
    data
  })
}

// 删除文件
export function deleteInsuranceFile(id: string) {
  return request({
    url: `/insurance-file/${id}`,
    method: 'delete'
  })
}

// 批量删除文件
export function batchDeleteInsuranceFiles(ids: string[]) {
  return request({
    url: '/insurance-file/batch',
    method: 'delete',
    data: { ids }
  })
}

// 下载文件
export function downloadInsuranceFile(id: string) {
  return request({
    url: `/insurance-file/download/${id}`,
    method: 'get',
    responseType: 'blob'
  })
}

// 预览文件
export function previewInsuranceFile(id: string) {
  return request({
    url: `/insurance-file/preview/${id}`,
    method: 'get',
    responseType: 'blob'
  })
}

// 获取员工所有文件
export function getEmployeeFiles(employeeId: string) {
  return request<InsuranceFile[]>({
    url: `/insurance-file/employee/${employeeId}`,
    method: 'get'
  })
}

// 按文件类型统计
export function getInsuranceFileStatistics() {
  return request<{
    fileType: string
    count: number
    totalSize: number
  }[]>({
    url: '/insurance-file/statistics',
    method: 'get'
  })
}

// 批量上传文件
export function batchUploadFiles(data: {
  files: File[]
  employeeId: string
  fileType: string
  description?: string
}) {
  const formData = new FormData()
  data.files.forEach((file, index) => {
    formData.append(`files[${index}]`, file)
  })
  formData.append('employeeId', data.employeeId)
  formData.append('fileType', data.fileType)
  if (data.description) {
    formData.append('description', data.description)
  }
  
  return request({
    url: '/insurance-file/batch-upload',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
} 