import request from '@/utils/request'

// 查询文件夹列表
export function listFolder(query) {
  return request({
    url: '/system/folder/list',
    method: 'get',
    params: query
  })
}

// 查询文件夹详细
export function getFolder(folderId) {
  return request({
    url: '/system/folder/' + folderId,
    method: 'get'
  })
}

// 新增文件夹
export function addFolder(data) {
  return request({
    url: '/system/folder',
    method: 'post',
    data: data
  })
}

// 修改文件夹
export function updateFolder(data) {
  return request({
    url: '/system/folder',
    method: 'put',
    data: data
  })
}

// 删除文件夹
export function delFolder(folderId) {
  return request({
    url: '/system/folder/' + folderId,
    method: 'delete'
  })
}

// 获取文件夹树
export function getFolderTree(query) {
  return request({
    url: '/system/folder/tree',
    method: 'get',
    params: query
  })
}

// 查询文件列表
export function listFile(query) {
  return request({
    url: '/system/file/list',
    method: 'get',
    params: query
  })
}

// 查询文件详细
export function getFile(fileId) {
  return request({
    url: '/system/file/' + fileId,
    method: 'get'
  })
}

// 上传文件
export function uploadFile(data) {
  return request({
    url: '/system/file/upload',
    method: 'post',
    data: data,
    headers: {
      'Content-Type': 'multipart/form-data',
      'repeatSubmit': false
    }
  })
}

// 修改文件
export function updateFile(data) {
  return request({
    url: '/system/file',
    method: 'put',
    data: data
  })
}

// 删除文件
export function delFile(fileId) {
  return request({
    url: '/system/file/' + fileId,
    method: 'delete'
  })
}

// 下载文件
export function downloadFile(fileId) {
  return request({
    url: '/system/file/download/' + fileId,
    method: 'get',
    responseType: 'blob'
  })
}

// 预览文件
export function previewFile(fileId) {
  return import.meta.env.VITE_APP_BASE_API + '/system/file/preview/' + fileId
}

// 获取文件访问URL
export function getFileUrl(fileId) {
  return request({
    url: '/system/file/url/' + fileId,
    method: 'get'
  })
}

// 移动文件
export function moveFile(fileId, targetFolderId) {
  return request({
    url: '/system/file/move',
    method: 'post',
    params: {
      fileId: fileId,
      targetFolderId: targetFolderId
    }
  })
}

// 复制文件
export function copyFile(fileId, targetFolderId) {
  return request({
    url: '/system/file/copy',
    method: 'post',
    params: {
      fileId: fileId,
      targetFolderId: targetFolderId
    },
    headers: {
      'repeatSubmit': false
    }
  })
}

// 复制文件夹
export function copyFolder(folderId, targetFolderId) {
  return request({
    url: '/system/folder/copy',
    method: 'post',
    params: {
      folderId: folderId,
      targetFolderId: targetFolderId
    },
    headers: {
      'repeatSubmit': false
    }
  })
}

// 查询文件分享列表
export function listFileShare(query) {
  return request({
    url: '/system/file/share/list',
    method: 'get',
    params: query
  })
}

// 获取文件的分享信息
export function getFileShare(fileId) {
  return request({
    url: '/system/file/share/file/' + fileId,
    method: 'get'
  })
}

// 获取文件夹的分享信息
export function getFolderShare(folderId) {
  return request({
    url: '/system/file/share/folder/' + folderId,
    method: 'get'
  })
}

// 创建文件分享
export function createFileShare(fileId, expireDays, needPassword) {
  return request({
    url: '/system/file/share/create',
    method: 'post',
    params: {
      fileId: fileId,
      expireDays: expireDays,
      needPassword: needPassword
    }
  })
}

// 创建文件夹分享
export function createFolderShare(folderId, expireDays, needPassword) {
  return request({
    url: '/system/file/share/createFolder',
    method: 'post',
    params: {
      folderId: folderId,
      expireDays: expireDays,
      needPassword: needPassword
    }
  })
}

// 删除文件分享
export function delFileShare(shareId) {
  return request({
    url: '/system/file/share/' + shareId,
    method: 'delete'
  })
}

// 取消文件分享（通过文件ID）
export function cancelFileShare(fileId) {
  return request({
    url: '/system/file/share/cancel/' + fileId,
    method: 'delete'
  })
}

// 验证文件分享
export function verifyFileShare(shareCode, password) {
  return request({
    url: '/system/file/share/verify/' + shareCode,
    method: 'get',
    params: {
      password: password
    }
  })
}

// 获取分享文件夹下的子文件夹内容
export function getShareFolderFiles(shareCode, folderId) {
  return request({
    url: `/system/file/share/folder/${shareCode}/${folderId}`,
    method: 'get'
  })
}
