import type { AxiosResponse } from 'axios'
import type { PageInfo, Result, TypeDescriptor } from '@cacp/ui'
import request from '@/utils/request'
import type { DictTypeQueryInfo, DictTypeInfo, DictTypeOption, AjaxResult } from '@/types/system/dictType'
const contextPath = '/dictType'

// 查询字典类型列表
export async function listType(query: DictTypeQueryInfo): Promise<Result<PageInfo<DictTypeInfo>>> {
  const res: AxiosResponse<Result<PageInfo<DictTypeInfo>>> = await request.post(`${contextPath}/list`,query )
  return res.data
}

// 查询字典类型详细
export async function getType(dictId: number): Promise<Result<DictTypeInfo>> {
  const res: AxiosResponse<Result<DictTypeInfo>> = await request.get(`${contextPath}/${dictId}`)
  return res.data
}

// 新增字典类型
export async function addType(data: DictTypeInfo): Promise<AjaxResult<string>> {
  const res: AxiosResponse<AjaxResult<string>> = await request.post(contextPath, data)
  return res.data
}

// 修改字典类型
export async function updateType(data: DictTypeInfo): Promise<AjaxResult<string>> {
  const res: AxiosResponse<AjaxResult<string>> = await request.put(contextPath, data)
  return res.data
}

// 删除字典类型
export async function delType(dictId: number[]): Promise<AjaxResult<string>> {
  const res: AxiosResponse<AjaxResult<string>> = await request.delete(`${contextPath}/${dictId}`)
  return res.data
}

// 删除采购入库单
export async function remove(dictId: number | number[]): Promise<Result<number>> {
  // 统一转换为数组格式
  const ids = Array.isArray(dictId) ? dictId : [dictId];
  console.log(ids);
  const res: AxiosResponse<Result<number>> = await request.post(
    `${contextPath}/delete-dictTypes?ids`,ids
  )
  return res.data
}

// 刷新字典缓存
export async function refreshCache(): Promise<AjaxResult<string>> {
  const res: AxiosResponse<AjaxResult<string>> = await request.delete(`${contextPath}/refreshCache`)
  return res.data
}

// 获取字典选择框列表
export async function optionselect(): Promise<Result<DictTypeOption[]>> {
  const res: AxiosResponse<Result<DictTypeOption[]>> = await request.get(`${contextPath}/optionselect`)
  return res.data
}
