import request from '@/utils/request'

/* ---------- 闲管家应用配置 ---------- */
export function listErpOpenConfig(query) {
  return request({ url: '/jarvis/erpOpenConfig/list', method: 'get', params: query })
}

export function getErpOpenConfig(id) {
  return request({ url: '/jarvis/erpOpenConfig/' + id, method: 'get' })
}

export function addErpOpenConfig(data) {
  return request({ url: '/jarvis/erpOpenConfig', method: 'post', data })
}

export function updateErpOpenConfig(data) {
  return request({ url: '/jarvis/erpOpenConfig', method: 'put', data })
}

export function delErpOpenConfig(ids) {
  return request({ url: '/jarvis/erpOpenConfig/' + ids, method: 'delete' })
}

/** 闲管家「查询快递公司」（与 Apifox 一致：服务端 POST {} + 签名） */
export function listGoofishExpressCompanies(appKey) {
  return request({
    url: '/jarvis/erpOpenConfig/expressCompanies',
    method: 'get',
    params: appKey ? { appKey } : {}
  })
}

/* ---------- 闲管家订单 ---------- */
export function listGoofishOrder(query) {
  return request({ url: '/jarvis/erpGoofishOrder/list', method: 'get', params: query })
}

/** 日日顺 + 已关联京东：已出库（去重运单等）/待出库 统计，供列表右侧展示 */
export function getGoofishRrsLogisticsStats() {
  return request({ url: '/jarvis/erpGoofishOrder/stats/rrsLogistics', method: 'get' })
}

export function getGoofishOrder(id) {
  return request({ url: '/jarvis/erpGoofishOrder/' + id, method: 'get' })
}

/** 订单状态 / 物流 / 发货 变更日志 */
export function listGoofishOrderEventLogs(orderId) {
  return request({ url: '/jarvis/erpGoofishOrder/' + orderId + '/eventLogs', method: 'get' })
}

/** 变更日志全表分页（排查：与 [goofish-order-event] 落库同源） */
export function listGoofishOrderEventLogPage(query) {
  return request({ url: '/jarvis/erpGoofishOrder/eventLog/list', method: 'get', params: query })
}

export function pullGoofishOrders(appKey, hours) {
  return request({
    url: '/jarvis/erpGoofishOrder/pull/' + encodeURIComponent(appKey),
    method: 'post',
    params: hours != null ? { hours } : {}
  })
}

export function pullAllGoofishOrders(hours) {
  return request({
    url: '/jarvis/erpGoofishOrder/pullAll',
    method: 'post',
    params: hours != null ? { hours } : {}
  })
}

/** 历史全量：按服务端 pull-full-history-days + 时间分段拉 update_time */
export function pullGoofishOrdersFull(appKey) {
  return request({
    url: '/jarvis/erpGoofishOrder/pull/' + encodeURIComponent(appKey) + '/full',
    method: 'post'
  })
}

export function pullAllGoofishOrdersFull() {
  return request({ url: '/jarvis/erpGoofishOrder/pullAll/full', method: 'post' })
}

export function refreshGoofishDetail(id) {
  return request({ url: '/jarvis/erpGoofishOrder/refreshDetail/' + id, method: 'post' })
}

export function retryGoofishShip(id) {
  return request({ url: '/jarvis/erpGoofishOrder/retryShip/' + id, method: 'post' })
}

/** 发货预览：闲鱼订单收件串 vs 即将提交开放平台参数（与自动发货逻辑一致） */
export function previewGoofishShip(id) {
  return request({ url: '/jarvis/erpGoofishOrder/shipPreview/' + id, method: 'get' })
}
