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

/** 资产分类数据类型 */
export interface AssetCategory {
  id: number
  name: string
  code: string
  description?: string
  icon?: string
  color?: string
  parent_id?: number
  parent?: AssetCategory
  level: number
  path?: string
  enabled: boolean
  sort: number
  asset_count: number
  remarks?: string
  created_by?: string
  updated_by?: string
  created_at?: string
  updated_at?: string
  children?: AssetCategory[]
}

/** 分页列表参数 */
export interface CategoryListParams {
  page?: number
  page_size?: number
  sort?: string
  order?: 'asc' | 'desc'
  keyword?: string
}

/** 创建/更新分类参数 */
export interface CategoryFormData {
  id?: number
  name: string
  code: string
  description?: string
  icon?: string
  color?: string
  parent_id?: number | null
  enabled?: boolean
  sort?: number
  remarks?: string
}

/** 获取资产分类列表（分页） */
export const fetchCategoryList = (params?: CategoryListParams) => {
  return request.post('/Assets/v1/category/list', params || {})
}

/** 获取资产分类详情 */
export const fetchCategoryDetail = (id: number) => {
  return request.get(`/Assets/v1/category/detail/${id}`)
}

/** 创建资产分类 */
export const createCategory = (data: CategoryFormData) => {
  return request.post('/Assets/v1/category/create', data)
}

/** 更新资产分类 */
export const updateCategory = (data: CategoryFormData) => {
  return request.put('/Assets/v1/category/update', data)
}

/** 删除资产分类 */
export const deleteCategory = (id: number) => {
  return request.delete(`/Assets/v1/category/delete/${id}`)
}

/** 获取所有资产分类（用于下拉选择） */
export const fetchAllCategories = (keyword?: string) => {
  return request.get('/Assets/v1/category/all', { params: { keyword } })
}

/** 获取资产分类树形结构 */
export const fetchCategoryTree = (keyword?: string) => {
  return request.get('/Assets/v1/category/tree', { params: { keyword } })
}

/** 获取指定分类的子分类列表 */
export const fetchCategoryChildren = (id: number) => {
  return request.get(`/Assets/v1/category/children/${id}`)
}

/** 根据父级分类ID获取分类列表 */
export const fetchCategoryByParent = (parentId: number) => {
  return request.get(`/Assets/v1/category/parent/${parentId}`)
}
