import {
  PAGE_ORDER_LIST,
  PAGE_ORDER_DETAIL,
  PAGE_ORDER_REVIEW_LIST,
  PAGE_ORDER_REVIEW_EDIT,
  PAGE_ORDER_REVIEW_VIEW,
  PAGE_ORDER_AFTERSALE_LIST,
  PAGE_ORDER_AFTERSALE_DETAIL,
  PAGE_ORDER_AFTERSALE_SUBMIT
} from '@/utils/pageRoute'
import { ORDER_TAB } from '@/constants/order'

/** 防止 @click="fn" 误传事件对象进 URL */
function safeQueryValue(value, fallback) {
  return typeof value === 'string' && value ? value : fallback
}

/** 订单列表（可带 tab） */
export function goOrderList(tab = ORDER_TAB.ALL) {
  uni.navigateTo({
    url: `${PAGE_ORDER_LIST}?tab=${safeQueryValue(tab, ORDER_TAB.ALL)}`
  })
}

/** 订单详情（可选带 aftersaleStatus，用于隐藏「申请售后」） */
export function goOrderDetail(orderId, aftersaleStatus) {
  if (!orderId) return
  const parts = [`orderId=${orderId}`]
  if (aftersaleStatus != null && aftersaleStatus !== '') {
    parts.push(`aftersaleStatus=${aftersaleStatus}`)
  }
  uni.navigateTo({
    url: `${PAGE_ORDER_DETAIL}?${parts.join('&')}`
  })
}

/** 评价列表 */
export function goReviewList(tab = 'PENDING') {
  uni.navigateTo({
    url: `${PAGE_ORDER_REVIEW_LIST}?tab=${safeQueryValue(tab, 'PENDING')}`
  })
}

/** 评价编辑（一行一评须传 orderItemId） */
export function goReviewEdit(orderId, orderItemId) {
  if (!orderId) return
  const parts = [`orderId=${orderId}`]
  if (orderItemId != null && orderItemId !== '') {
    parts.push(`orderItemId=${orderItemId}`)
  }
  uni.navigateTo({
    url: `${PAGE_ORDER_REVIEW_EDIT}?${parts.join('&')}`
  })
}

/** 查看商品行评价 */
export function goReviewView(orderId, orderItemId) {
  if (!orderId || orderItemId == null || orderItemId === '') return
  uni.navigateTo({
    url: `${PAGE_ORDER_REVIEW_VIEW}?orderId=${orderId}&orderItemId=${orderItemId}`
  })
}

/** 售后列表 */
export function goAftersaleList(tab = 'IN_PROGRESS') {
  uni.navigateTo({
    url: `${PAGE_ORDER_AFTERSALE_LIST}?tab=${safeQueryValue(tab, 'IN_PROGRESS')}`
  })
}

/** 售后详情 */
export function goAftersaleDetail(aftersaleId) {
  if (!aftersaleId) return
  uni.navigateTo({
    url: `${PAGE_ORDER_AFTERSALE_DETAIL}?aftersaleId=${aftersaleId}`
  })
}

/** 提交售后（整单，仅需 orderId） */
export function goAftersaleSubmit(orderId) {
  if (!orderId) return
  uni.navigateTo({
    url: `${PAGE_ORDER_AFTERSALE_SUBMIT}?orderId=${orderId}`
  })
}
