import request from '@/utils/request'

export interface ServiceItem {
  id: number
  base_id: number
  sku: string
  sale_price: number
  provider_name: string
  description: string
  created_at: string
  updated_at: string
  material_name?: string
  spec_model?: string
  unit?: string
}

export interface ServiceListResponse {
  code: number
  msg: string
  data: {
    items: ServiceItem[]
    total: number
    page: number
    per_page: number
  }
}

export interface ServiceQueryParams {
  page?: number
  per_page?: number
  keyword?: string
  start_date?: string
  end_date?: string
  provider_name?: string
}

export interface ServiceCreateRequest {
  base_id: number
  sale_price: number
  provider_name: string
  description?: string
}

export interface ServiceUpdateRequest {
  sale_price?: number
  provider_name?: string
  description?: string
}

export interface MaterialBaseItem {
  id: number
  name: string
  spec: string
  category: string
  unit: string
  type: string
}

// 获取服务权益列表
export function getServiceList(params: ServiceQueryParams) {
  return request<ServiceListResponse>({
    url: '/v1/inbound/service',
    method: 'get',
    params
  })
}

// 创建服务权益
export function createService(data: ServiceCreateRequest) {
  return request({
    url: '/v1/inbound/service',
    method: 'post',
    data
  })
}

// 获取服务权益详情
export function getServiceDetail(id: number) {
  return request<ServiceListResponse>({
    url: `/v1/inbound/service/${id}`,
    method: 'get'
  })
}

// 更新服务权益
export function updateService(id: number, data: ServiceUpdateRequest) {
  return request({
    url: `/v1/inbound/service/${id}`,
    method: 'put',
    data
  })
}

// 搜索基础物料
export function searchMaterialBase(keyword: string) {
  return request<{
    code: number
    msg: string
    data: MaterialBaseItem[]
  }>({
    url: '/v1/inbound/service/search-base',
    method: 'get',
    params: { keyword }
  })
}

// 删除服务权益
export function deleteService(id: number) {
  return request({
    url: `/v1/inbound/service/${id}`,
    method: 'delete'
  })
}
