import request from '@/utils/request';
import { OrderMain, OrderProduct, OrderStatusStats, OrderCustomerFlowSaveBo, OrderCustomerFlowLinkBo, OrderCustomerFlow } from './orderTypes';

// ==================== 订单管理 ====================

/**
 * 查询当前企业的订单列表
 */
export function getOrderList(params?: any) {
  return request({
    url: '/order/pcOrder/list',
    method: 'get',
    params: params
  });
}

/**
 * 查询订单状态统计
 */
export function getOrderStatusStats() {
  return request({
    url: '/order/pcOrder/statusStats',
    method: 'get'
  });
}

/**
 * 查询订单详情
 */
export function getOrderInfo(id: number) {
  return request({
    url: `/order/pcOrder/${id}`,
    method: 'get'
  });
}

/**
 * 根据订单ID查询订单商品明细
 */
export function getOrderProducts(orderIds: number[]) {
  return request({
    url: '/order/pcOrder/products',
    method: 'get',
    params: { orderIds: orderIds.join(',') }
  });
}

/**
 * 根据订单ID查询订单商品明细
 */
export function getOrderProductsWithAvailableQty(orderIds: number[]) {
  return request({
    url: '/order/pcOrder/productsWithAvailableQty',
    method: 'get',
    params: { orderIds: orderIds.join(',') }
  });
}

/**
 * 取消订单
 */
export function cancelOrder(data: { id: number; orderStatus?: string }) {
  return request({
    url: '/order/pcOrder/cancel',
    method: 'put',
    data: data
  });
}

/**
 * 审核订单
 * 企业客户审核自己企业的订单
 */
// export function checkOrderStatus(data: { id: number; checkStatus: string }) {
//   return request({
//     url: '/order/pcOrder/checkStatus',
//     method: 'put',
//     data: data
//   });
// }

export function checkOrderStatus(data: any) {
  return request({
    url: '/order/pcOrder/checkOrder',
    method: 'post',
    data: data
  });
}

/**
 * 删除订单
 */
export function deleteOrder(ids: number[]) {
  return request({
    url: `/order/pcOrder/${ids.join(',')}`,
    method: 'delete'
  });
}

/**
 * 订单支付
 */
export const orderPay = (params: any) => {
  return request({
    url: '/order/pcOrder/orderPay',
    method: 'post',
    data: params
  });
};

/**
 * 根据批量确认收货
 */
export function batchConfirmation(orderIds: number[]) {
  return request({
    url: '/order/pcOrder/batchConfirmation',
    method: 'get',
    params: { orderIds: orderIds.join(',') }
  });
}

// ==================== 订单管理 ====================

/**
 * 查询当前企业的已评价订单
 */
export function getEvalutionList(params?: any) {
  return request({
    url: '/order/pcOrderEvaluationHeader/list',
    method: 'get',
    params: params
  });
}

/**
 * 新增订单评价
 */
export function addOrderEvaluation(data: any) {
  return request({
    url: '/order/pcOrderEvaluationHeader',
    method: 'post',
    data: data
  });
}

export function getOrderEvaluation(id: number) {
  return request({
    url: `/order/pcOrderEvaluationHeader/${id}`,
    method: 'get'
  });
}

export function getOrderEvaluationHeader(orderId: string | number) {
  return request({
    url: `/order/pcOrderEvaluationHeader/info/byOrder`,
    method: 'get',
    params: {
      orderId
    }
  });
}

// ==================== 订单流程管理 ====================

/**
 * 新增订单流程（包含节点）
 */
export function addOrderFlow(data: OrderCustomerFlowSaveBo) {
  return request({
    url: '/order/pcOrderFlow/save',
    method: 'post',
    data: data
  });
}

/**
 * 编辑订单流程（包含节点）
 */
export function updateOrderFlow(data: OrderCustomerFlowSaveBo) {
  return request({
    url: '/order/pcOrderFlow/save',
    method: 'put',
    data: data
  });
}

/**
 * 删除订单流程
 */
export function deleteOrderFlow(id: number | string) {
  return request({
    url: `/order/pcOrderFlow/${id}`,
    method: 'delete'
  });
}

/**
 * 审核订单流程
 */
export function auditOrderFlow(data: OrderCustomerFlowLinkBo) {
  return request({
    url: '/order/pcOrderFlow/audit',
    method: 'put',
    data: data
  });
}

/**
 * 查询订单流程详情
 */
export function getOrderFlowDetail(id: number | string) {
  return request({
    url: `/order/pcOrderFlow/${id}`,
    method: 'get'
  });
}

/**
 * 查询订单流程列表
 */
export function getOrderFlowList(params?: any) {
  return request({
    url: '/order/pcOrderFlow/list',
    method: 'get',
    params: params
  });
}

/**
 * 开启订单流程
 */
export function startOrderFlow(id: number | string) {
  return request({
    url: `/order/pcOrderFlow/startFlow/${id}`,
    method: 'put'
  });
}

/**
 * 关闭订单流程
 */
export function closeOrderFlow(id: number | string) {
  return request({
    url: `/order/pcOrderFlow/closeFlow/${id}`,
    method: 'put'
  });
}

//=============== 订单退货原因 ===============

export function getReturnReason(params?: any) {
  return request({
    url: '/system/pcOrderReturnReason/list',
    method: 'get',
    params: params
  });
}
