import request from '@/utils/request';
import { AxiosPromise } from 'axios';
import { FileCategoryVO, FileCategoryForm, FileCategoryQuery, TableDataInfo } from './types';

/**
 * 查询文件分类列表
 * @param query 查询参数
 * @returns Promise<TableDataInfo<FileCategoryVO>>
 */
export const listFileCategory = (query?: FileCategoryQuery): AxiosPromise<TableDataInfo<FileCategoryVO>> => {
  // 移除空值
  const cleanQuery: Record<string, any> = {};
  if (query) {
    Object.keys(query).forEach(key => {
      if (query[key as keyof FileCategoryQuery] !== undefined && query[key as keyof FileCategoryQuery] !== null && query[key as keyof FileCategoryQuery] !== '') {
        cleanQuery[key] = query[key as keyof FileCategoryQuery];
      }
    });
  }

  return request({
    url: '/system/file/category/list',
    method: 'get',
    params: cleanQuery
  });
};

/**
 * 查询文件分类详细
 * @param id 分类ID
 * @returns Promise<FileCategoryVO>
 */
export const getFileCategory = (id: number): AxiosPromise<FileCategoryVO> => {
  return request({
    url: `/system/file/category/${id}`,
    method: 'get'
  });
};

/**
 * 新增文件分类
 * @param data 分类数据
 * @returns Promise<any>
 */
export const addFileCategory = (data: FileCategoryForm): AxiosPromise<any> => {
  return request({
    url: '/system/file/category',
    method: 'post',
    data: data
  });
};

/**
 * 修改文件分类
 * @param data 分类数据
 * @returns Promise<any>
 */
export const updateFileCategory = (data: FileCategoryForm): AxiosPromise<any> => {
  return request({
    url: '/system/file/category',
    method: 'put',
    data: data
  });
};

/**
 * 删除文件分类
 * @param id 分类ID
 * @returns Promise<any>
 */
export const delFileCategory = (id: number | number[]): AxiosPromise<any> => {
  const ids = Array.isArray(id) ? id.join(',') : id;
  return request({
    url: `/system/file/category/${ids}`,
    method: 'delete'
  });
};

/**
 * 查询文件分类树结构
 * @returns Promise<FileCategoryVO[]>
 */
export const listFileCategoryTree = (): AxiosPromise<FileCategoryVO[]> => {
  return request({
    url: '/system/file/category/tree',
    method: 'get'
  });
};

/**
 * 根据类型查询分类列表
 * @param type 分类类型
 * @returns Promise<FileCategoryVO[]>
 */
export const listFileCategoryByType = (type: number): AxiosPromise<FileCategoryVO[]> => {
  return request({
    url: `/system/file/category/type/${type}`,
    method: 'get'
  });
}; 