import type { AxiosResponse } from 'axios'
import type { PageInfo, Result, TypeDescriptor } from '@cacp/ui'

import request from '@/utils/request'
import type { OrderInfo, OrderCategoryInfo, OrderQueryInfo } from '@/types/order'

const contextPath = '/order'

// 获取分类列表
export async function getCategoryList(): Promise<Result<Array<OrderCategoryInfo>>> {
  const res: AxiosResponse<Result<Array<OrderCategoryInfo>>> = await request.get(`${contextPath}/get-category-list`)
  return res.data
}
// 插入分类
export async function insertCategory(categoryInfo: OrderCategoryInfo): Promise<Result<number>> {
  const res: AxiosResponse<Result<number>> = await request.post(`${contextPath}/insert-category`, categoryInfo)
  return res.data
}
// 更新分类
export async function updateCategory(categoryInfo: OrderCategoryInfo): Promise<Result<number>> {
  const res: AxiosResponse<Result<number>> = await request.post(`${contextPath}/update-category`, categoryInfo)
  return res.data
}
// 删除分类
export async function deleteCategory(categoryCode: string): Promise<Result<number>> {
  const res: AxiosResponse<Result<number>> = await request.post(
    `${contextPath}/delete-category?categoryCode=${categoryCode}`
  )
  return res.data
}
//获取单据
export async function getOrder(orderId: string): Promise<Result<OrderInfo>> {
  const res: AxiosResponse<Result<OrderInfo>> = await request.get(`${contextPath}/get-order?orderId=${orderId}`)
  return res.data
}
//获取单据列表
export async function getOrderList(query: OrderQueryInfo): Promise<Result<PageInfo<OrderInfo>>> {
  const res: AxiosResponse<Result<PageInfo<OrderInfo>>> = await request.post(`${contextPath}/get-order-list`, query)
  return res.data
}
// 插入单据
export async function insertOrder(orderInfo: OrderInfo): Promise<Result<number>> {
  const res: AxiosResponse<Result<number>> = await request.post(`${contextPath}/insert-order`, orderInfo)
  return res.data
}

// 更新单据
export async function updateOrder(orderInfo: OrderInfo): Promise<Result<number>> {
  const res: AxiosResponse<Result<number>> = await request.post(`${contextPath}/update-order`, orderInfo)
  return res.data
}
// 删除单据
export async function deleteOrder(orderId: string): Promise<Result<number>> {
  const res: AxiosResponse<Result<number>> = await request.post(`${contextPath}/delete-order?orderId=${orderId}`)
  return res.data
}
// 获取等级
export async function getLevelList(): Promise<Result<Array<TypeDescriptor>>> {
  const res: AxiosResponse<Result<Array<TypeDescriptor>>> = await request.get(`${contextPath}/get-level-list`)
  return res.data
}
