import Content from '@/sheep/layouts/content.vue';
import { request } from '@/sheep/request';
import { REPORT } from '@/sheep/request/crud';
import { useI18n } from 'vue-i18n';

// 订单状态配置 - 根据最新状态机调整，使用函数形式支持国际化
export const getOrderStatusConfig = (t) => ({
  // 订单状态 - 新的状态机
  ORDER_STATUS: {
    1: { text: t('modules.order.status.pendingPayment'), type: 'warning' },
    2: { text: t('modules.order.status.cancelled'), type: 'danger' },
    3: { text: t('modules.order.status.paid'), type: 'success' },
    4: { text: t('modules.order.status.refunded'), type: 'info' },
    5: { text: t('modules.order.status.pendingShipment'), type: 'warning' },
    6: { text: t('modules.order.status.closed'), type: 'info' },
    7: { text: t('modules.order.status.shipped'), type: 'primary' },
    8: { text: t('modules.order.status.completed'), type: 'success' },
    9: { text: t('modules.order.status.recycled'), type: 'info' },
  },

  // 退款状态
  REFUND_STATUS: {
    0: { text: t('modules.order.refundStatus.noRefund'), type: 'info' },
    1: { text: t('modules.order.refundStatus.applying'), type: 'warning' },
    2: { text: t('modules.order.refundStatus.refunded'), type: 'success' },
    3: { text: t('modules.order.refundStatus.refunding'), type: 'primary' },
  },

  // 拼团状态
  PINK_STATUS: {
    1: { text: t('modules.order.groupStatus.pending'), type: 'warning' },
    2: { text: t('modules.order.groupStatus.success'), type: 'success' },
    3: { text: t('modules.order.groupStatus.failed'), type: 'danger' },
  },

  // 中奖状态
  WIN_STATUS: {
    0: { text: t('modules.order.winStatus.notWon'), type: 'info' },
    1: { text: t('modules.order.winStatus.won'), type: 'success' },
  },

  // 支付方式
  PAY_TYPE: {
    yue: t('modules.order.payType.balance'),
    alipay: t('modules.order.payType.alipay'),
    balance: t('modules.order.payType.balance'),
    offline: t('modules.order.payType.offline'),
    wechat: t('modules.order.payType.wechat'),
  },
});

// 保持向后兼容的静态配置（用于不需要国际化的场景）
export const ORDER_STATUS = {
  ORDER_STATUS: {
    1: { text: '待支付', type: 'warning' },
    2: { text: '订单取消', type: 'danger' },
    3: { text: '已支付', type: 'success' },
    4: { text: '失败已退款', type: 'info' },
    5: { text: '待发货', type: 'warning' },
    6: { text: '已关闭', type: 'info' },
    7: { text: '待收货', type: 'primary' },
    8: { text: '订单完成', type: 'success' },
    9: { text: '已回收', type: 'info' },
  },
  REFUND_STATUS: {
    0: { text: '未退款', type: 'info' },
    1: { text: '申请中', type: 'warning' },
    2: { text: '已退款', type: 'success' },
    3: { text: '退款中', type: 'primary' },
  },
  PINK_STATUS: {
    1: { text: '未成团', type: 'warning' },
    2: { text: '已成团', type: 'success' },
    3: { text: '成团失败', type: 'danger' },
  },
  WIN_STATUS: {
    0: { text: '未中奖', type: 'info' },
    1: { text: '已中奖', type: 'success' },
  },
  PAY_TYPE: {
    yue: '账户余额',
    alipay: '支付宝',
    balance: '余额支付',
    offline: '线下支付',
    wechat: '微信支付',
  },
};

// 获取订单状态信息
export function getOrderStatusInfo(status) {
  return ORDER_STATUS.ORDER_STATUS[status] || { text: '未知', type: 'info' };
}

// 获取退款状态信息
export function getRefundStatusInfo(refundStatus) {
  return ORDER_STATUS.REFUND_STATUS[refundStatus] || { text: '未知', type: 'info' };
}

// 获取拼团状态信息
export function getPinkStatusInfo(pinkStatus) {
  return ORDER_STATUS.PINK_STATUS[pinkStatus] || { text: '未知', type: 'info' };
}

// 获取中奖状态信息
export function getWinStatusInfo(winStatus) {
  return ORDER_STATUS.WIN_STATUS[winStatus] || { text: '未知', type: 'info' };
}

// 获取支付方式文本
export function getPayTypeText(payType) {
  return ORDER_STATUS.PAY_TYPE[payType] || payType || '--';
}
import { SELECT, CRUD } from '@/sheep/request/crud';

const route = {
  path: 'order',
  name: 'shop.admin.order',
  component: Content,
  meta: {
    title: '订单',
  },
  children: [
    {
      path: 'list',
      name: 'shop.admin.order.list',
      component: () => import('@/app/shop/admin/order/order/index.vue'),
      meta: {
        title: '订单管理',
      },
    },
    {
      path: 'aftersale',
      name: 'shop.admin.order.aftersale',
      component: () => import('@/app/shop/admin/order/aftersale/index.vue'),
      meta: {
        title: '售后管理',
      },
    },
    {
      path: 'invoice',
      name: 'shop.admin.order.invoice',
      component: () => import('@/app/shop/admin/order/invoice/index.vue'),
      meta: {
        title: '订单发票',
      },
    },
    {
      path: 'setting',
      name: 'shop.admin.order.setting',
      component: () => import('@/app/shop/admin/order/setting/index.vue'),
      meta: {
        title: '订单设置',
      },
    },
  ],
};

const api = {
  order: {
    ...CRUD('mall/order'),
    exportDelivery: (params, filename, typeName) =>
      REPORT('mall/order', params, filename, typeName),
    // 发货
    dispatch: (data) =>
      request({
        url: '/mall/order/batchDelivery',
        method: 'POST',
        data,
      }),
    // 上传发货单发货
    dispatchByUpload: (data) =>
      request({
        url: '/shop/admin/order/order/dispatch',
        headers: {
          'Content-Type': 'multipart/form-data',
        },
        method: 'POST',
        data,
      }),
    // 导入发货单
    importDelivery: (data) =>
      request({
        url: 'mall/order/importDelivery',
        headers: {
          'Content-Type': 'multipart/form-data',
        },
        method: 'POST',
        data,
        responseType: 'blob', // 支持文件流下载
      }),
    customDispatch: (data) =>
      request({
        url: '/shop/admin/order/order/customDispatch',
        method: 'POST',
        data,
      }),
    editMemo: (id, data) =>
      request({
        url: `/shop/admin/order/order/editMemo/${id}`,
        method: 'PUT',
        data,
      }),
    // 取消订单
    cancel: (ids) => {
      // 如果是数组，转换为逗号分隔的字符串；如果已经是字符串，直接使用
      const idsString = Array.isArray(ids) ? ids.join(',') : String(ids);
      return request({
        url: '/mall/order/cancel',
        method: 'POST',
        params: { orderNo: idsString },
      });
    },
    // 退款订单
    refund: (ids) => {
      // 如果是数组，转换为逗号分隔的字符串；如果已经是字符串，直接使用
      const idsString = Array.isArray(ids) ? ids.join(',') : String(ids);
      return request({
        url: '/mall/order/refund',
        method: 'POST',
        params: { orderNo: idsString },
      });
    },
    // 联系用户
    contact: (id) =>
      request({
        url: `/shop/admin/order/order/contact/${id}`,
        method: 'POST',
      }),
    // 关闭订单
    close: (id) =>
      request({
        url: `/shop/admin/order/order/close/${id}`,
        method: 'PUT',
      }),
    applyRefundRefuse: (id) =>
      request({
        url: `/shop/admin/order/order/applyRefundRefuse/${id}`,
        method: 'PUT',
      }),
    fullRefund: (id, data) =>
      request({
        url: `/shop/admin/order/order/fullRefund/${id}`,
        method: 'PUT',
        data,
      }),
    updateExpress: (id, params) =>
      request({
        url: `/shop/admin/order/order/updateExpress/${id}`,
        method: 'GET',
        params,
      }),
    changeFee: (id, data) =>
      request({
        url: `/shop/admin/order/order/changeFee/${id}`,
        method: 'PUT',
        data,
      }),
    editConsignee: (id, data) =>
      request({
        url: `/shop/admin/order/order/editConsignee/${id}`,
        method: 'PUT',
        data,
      }),
    offlineRefuse: (id) =>
      request({
        url: `/shop/admin/order/order/offlineRefuse/${id}`,
        method: 'POST',
      }),
    offlineConfirm: (id) =>
      request({
        url: `/shop/admin/order/order/offlineConfirm/${id}`,
        method: 'POST',
      }),
    // 获取拼团信息
    getPinkList: (orderNo) =>
      request({
        url: `/mall/pink/orderPink`,
        params: { orderNo },
        method: 'GET',
      }),
  },
  aftersale: {
    ...CRUD('shop/admin/order/aftersale', ['list', 'detail']),
    getType: () =>
      request({
        url: '/shop/admin/order/aftersale/getType',
        method: 'GET',
      }),
    refuse: (id, params) =>
      request({
        url: `/shop/admin/order/aftersale/refuse/${id}`,
        method: 'PUT',
        params,
      }),
    refund: (id, params) =>
      request({
        url: `/shop/admin/order/aftersale/refund/${id}`,
        method: 'PUT',
        params,
      }),
    completed: (id) =>
      request({
        url: `/shop/admin/order/aftersale/completed/${id}`,
        method: 'PUT',
      }),
    addLog: (id, data) =>
      request({
        url: `/shop/admin/order/aftersale/addLog/${id}`,
        method: 'PUT',
        data,
      }),
  },
  invoice: {
    ...CRUD('shop/admin/order/invoice', ['list']),
    confirm: (id, data) =>
      request({
        url: '/shop/admin/order/invoice/confirm/' + id,
        method: 'PUT',
        data,
      }),
  },
  setting: {
    ...CRUD('shop/admin/order/setting'),
    select: (params) => SELECT('shop/admin/order/setting', params),
  },
};

export { route, api };
