import request from '@/utils/request'

// 获取项目上传数据状态
export function getUploadStatus(projectId) {
  return request({
    url: '/ccdi/project/' + projectId + '/upload-status',
    method: 'get'
  })
}

// 上传文件
export function uploadFile(projectId, uploadType, file) {
  const formData = new FormData()
  formData.append('file', file)
  formData.append('uploadType', uploadType)
  return request({
    url: '/ccdi/project/' + projectId + '/upload',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 删除文件
export function deleteFile(projectId, uploadType) {
  return request({
    url: '/ccdi/project/' + projectId + '/file/' + uploadType,
    method: 'delete'
  })
}

// 获取名单库选项
export function getNameListOptions() {
  return request({
    url: '/ccdi/project/name-list/options',
    method: 'get'
  })
}

// 更新名单库选择
export function updateNameListSelection(projectId, data) {
  return request({
    url: '/ccdi/project/' + projectId + '/name-lists',
    method: 'put',
    data: data
  })
}

// 执行数据质量检查
export function executeQualityCheck(projectId) {
  return request({
    url: '/ccdi/project/' + projectId + '/quality-check',
    method: 'post'
  })
}

// 解析证件号码文件
export function parseIdCardFile(file) {
  const formData = new FormData()
  formData.append('file', file)
  return request({
    url: '/ccdi/file-upload/parse-id-card-file',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 拉取行内/金综流水
export function pullBankInfo(data) {
  return request({
    url: '/ccdi/file-upload/pull-bank-info',
    method: 'post',
    data
  })
}

// 生成报告
export function generateReport(projectId) {
  return request({
    url: '/ccdi/project/' + projectId + '/generate-report',
    method: 'post'
  })
}

// 查询导入状态
export function getImportStatus(taskId) {
  return request({
    url: '/ccdi/project/upload-status/' + taskId,
    method: 'get'
  })
}

// ========== 批量文件上传相关接口 ==========

/**
 * 批量上传文件
 * @param {Number} projectId 项目ID
 * @param {Array<File>} files 文件数组
 * @returns {Promise} 返回 batchId
 */
export function batchUploadFiles(projectId, files) {
  const formData = new FormData()
  files.forEach(file => {
    formData.append('files', file)
  })
  formData.append('projectId', projectId)
  
  return request({
    url: '/ccdi/file-upload/batch',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    timeout: 300000  // 5分钟超时
  })
}

/**
 * 查询文件上传记录列表
 * @param {Object} params 查询参数
 */
export function getFileUploadList(params) {
  return request({
    url: '/ccdi/file-upload/list',
    method: 'get',
    params
  })
}

/**
 * 查询文件上传统计
 * @param {Number} projectId 项目ID
 */
export function getFileUploadStatistics(projectId) {
  return request({
    url: `/ccdi/file-upload/statistics/${projectId}`,
    method: 'get'
  })
}

/**
 * 查询文件上传详情
 * @param {Number} id 记录ID
 */
export function getFileUploadDetail(id) {
  return request({
    url: `/ccdi/file-upload/detail/${id}`,
    method: 'get'
  })
}

/**
 * 按记录ID删除上传记录
 * @param {Number} id 记录ID
 */
export function deleteFileUploadRecord(id) {
  return request({
    url: `/ccdi/file-upload/${id}`,
    method: 'delete'
  })
}
