import { request } from "@/http/axios"
import { PageDTO, ResponseData, BasePageQuery } from "../type"

export interface ShopGoodsDTO {
  goodsId?: number
  goodsName?: string
  categoryId?: number
  categoryName?: string
  price?: number
  stock?: number
  status?: number
  autoApproval?: number
  coverImg?: string
  creatorId?: number
  creatorName?: string
  createTime?: string
  remark?: string
  cabinetName?: string
  cellNo?: number
  cellNoStr?: string
  totalStock?: number
  usageInstruction?: string
}

export interface SearchShopGoodsQuery extends BasePageQuery {
  goodsName?: string
  categoryId?: number
  status?: number
  autoApproval?: number
  minPrice?: number
  maxPrice?: number
}

/** 获取商品列表 */
export function getGoodsList(query: SearchShopGoodsQuery) {
  return request<ResponseData<PageDTO<ShopGoodsDTO>>>({
    url: "manage/goods/list",
    method: "get",
    params: query
  })
}

/** 新增商品 */
export function addGoods(data: {
  goodsName: string
  categoryId: number
  price: number
  stock: number
  status: number
  autoApproval: number
  coverImg: string
  goodsDetail?: string
  usageInstruction?: string
}) {
  return request<ResponseData<void>>({
    url: "manage/goods",
    method: "post",
    data
  })
}

/** 删除商品 */
export function deleteGoods(goodsIds: number[]) {
  return request<ResponseData<void>>({
    url: `manage/goods/${goodsIds.join(',')}`,
    method: "delete"
  })
}

/** 修改商品 */
export function updateGoods(goodsId: number, data: {
  goodsName?: string
  categoryId?: number
  price?: number
  stock?: number
  status?: number
  autoApproval?: number
  coverImg?: string
  goodsDetail?: string
  usageInstruction?: string
}) {
  return request<ResponseData<void>>({
    url: `manage/goods/${goodsId}`,
    method: "put",
    data
  })
}

/** 获取单个商品信息 */
export function getGoodsInfo(goodsId: number) {
  return request<ResponseData<ShopGoodsDTO>>({
    url: "manage/goods/getGoodsInfo",
    method: "get",
    params: { goodsId }
  })
}