import type { PageResult, TabItem } from '@/types/global'
import type {
  orderResult,
  orderCreateParams,
  orderCreateResult,
  orderPreResult,
  orderListResult,
  orderListParams,
  orderPreParams,
  orderEvaluateParams,
  orderEvaluateListParams,
  orderEvaluateListResult,
  orderRefundRequest,
  orderRefundDetailResult,
  orderRefundListResult,
  orderDeliveryResult,
} from '@/types/order'
import { request } from '@/utils/request'

const orderUrl = 'shop/order'
const refundUrl = 'shop/orderRefund'
const evaluateUrl = 'shop/orderEvaluate'

/**
 * 获取购物车下单前信息
 * @param id
 * @returns
 */
export const getCartOrderPreApi = (data: {
  address_id: string | number
  coupon_id: string | number
}) => {
  return request<orderPreResult>({
    method: 'GET',
    url: `${orderUrl}/preCart`,
    data,
  })
}

/**
 * 获取订单下单前信息
 * @param data
 * @returns
 */
export const getOrderPreApi = (data: orderPreParams) => {
  return request<orderPreResult>(
    {
      method: 'GET',
      url: `${orderUrl}/pre`,
      data,
    },
    false,
  )
}

/**
 * 提交订单
 * @returns
 */
export const postOrderApi = (data: orderCreateParams) => {
  return request<orderCreateResult>({
    method: 'POST',
    url: `${orderUrl}/create`,
    data,
  })
}

/**
 * 填写订单-再次购买
 * @param id 订单id
 */
export const getRepurchaseOrderPreByOrderIdApi = (data: {
  id: number | string
  address_id: string | number
  coupon_id: string | number
}) => {
  return request<orderPreResult>({
    method: 'GET',
    url: `${orderUrl}/preRepurchase`,
    data,
  })
}

/**
 * 确认收货
 * @param id 订单id
 */
export const receiveOrderById = (id: number) => {
  return request<orderResult>({
    method: 'POST',
    url: `${orderUrl}/receive`,
    data: {
      id,
    },
  })
}

/**
 * 获取订单物流
 * @param id 订单id
 */
export const getOrderDeliveryByIdApi = (id: number) => {
  return request<orderDeliveryResult[]>({
    method: 'GET',
    url: `${orderUrl}/delivery`,
    data: {
      id,
    },
  })
}

/**
 * 删除订单
 * @param data 订单id集合
 * @returns
 */
export const deleteOrderApi = (data: { id: number[] }) => {
  return request({
    method: 'POST',
    url: `${orderUrl}/delete`,
    data,
  })
}

/**
 * 取消订单
 * @param id 订单id
 * @param data cancelReason 取消理由
 */
export const putOrderCancelByIdApi = (data: {
  id: number | number
  cancel_reason?: string
  time_out?: boolean //是否超时取消
}) => {
  return request<orderResult>({
    method: 'POST',
    url: `${orderUrl}/cancel`,
    data,
  })
}

/**
 * 获取订单列表
 * @param data orderState 订单状态
 */
export const getOrderListApi = (data: orderListParams) => {
  return request<
    PageResult<orderListResult> & {
      tabs: TabItem[]
    }
  >({
    method: 'GET',
    url: `${orderUrl}/list`,
    data,
  })
}

export const getOrderTabsApi = () => {
  return request<TabItem[]>({
    method: 'GET',
    url: `${orderUrl}/tabs`,
  })
}

/**
 * 根据ID获取订单详情
 * @param id
 * @returns
 */
export const getOrderDetailByIdApi = (id: number) => {
  return request<orderResult>({
    method: 'GET',
    url: `${orderUrl}/detail`,
    data: {
      id,
    },
  })
}

/**
 * 添加商品评价
 * @param data
 * @returns
 */
export const postOrderEvaluateApi = (data: orderEvaluateParams) => {
  return request({
    method: 'POST',
    url: `${evaluateUrl}/add`,
    data,
  })
}

/**
 * 获取订单评价列表
 * @param data
 * @returns
 */
export const getOrderEvaluateListApi = (data: orderEvaluateListParams) => {
  return request<PageResult<orderEvaluateListResult[]>>({
    method: 'GET',
    url: `${evaluateUrl}/list`,
    data,
  })
}

/**
 * 获取订单评价tab项
 * @returns
 */
export const getOrderEvaluateTabsApi = (data: { goods_id: string }) => {
  return request<TabItem[]>({
    method: 'GET',
    url: `${evaluateUrl}/tabs`,
    data,
  })
}

/**
 * 提交售后申请
 * @param data
 * @returns
 */
export const postOrderRefundApi = (data: orderRefundRequest) => {
  return request({
    method: 'POST',
    url: `${refundUrl}/refund`,
    data,
  })
}

/**
 * 获取售后列表
 * @param status
 * @returns
 */
export const getOrderRefundListApi = (data: { status: string | number }) => {
  return request<PageResult<orderRefundListResult[]>>({
    method: 'GET',
    url: `${refundUrl}/list`,
    data,
  })
}

/**
 * 售后详情
 * @param data
 * @returns
 */
export const getOrderRefundDetailApi = (data: { id: number }) => {
  return request<orderRefundDetailResult>({
    method: 'GET',
    url: `${refundUrl}/detail`,
    data,
  })
}

/**
 * 用户退货
 */
export const postOrderRefundReturnApi = (data: {
  id: number
  delivery_company_id: number
  delivery_no: string
  delivery_company: string
}) => {
  return request({
    method: 'POST',
    url: `${refundUrl}/refundReturn`,
    data,
  })
}
