import Content from '@/sheep/layouts/content.vue';
import { request } from '@/sheep/request';
import { CRUD } from '@/sheep/request/crud';

const route = {
  path: 'user',
  name: 'shop.admin.user',
  component: Content,
  meta: {
    title: '用户',
  },
  children: [
    {
      path: 'list',
      name: 'shop.admin.user.list',
      component: () => import('./list/index.vue'),
      meta: {
        title: '用户列表',
      },
    },

    {
      path: 'level',
      name: 'shop.admin.user.level',
      component: () => import('./level/index.vue'),
      meta: {
        title: '会员等级',
      },
    },
    {
      path: 'tag',
      name: 'shop.admin.user.tag',
      component: () => import('./tag/index.vue'),
      meta: {
        title: '会员标签',
      },
    },
  ],
};

const api = {
  // 用户相关 API
  user: {
    // 用户列表
    list: (params) =>
      request({
        url: '/cif/user/list',
        method: 'GET',
        params,
      }),
    // 用户详情
    detail: (userId) =>
      request({
        url: '/cif/user/detail',
        method: 'GET',
        params: { userId },
      }),
    // 其他用户相关API
    ...CRUD('/cif/user', ['edit', 'delete', 'report']),
    myUsers: (data) =>
      request({
        url: '/cif/user/myUsers',
        method: 'POST',
        data,
      }),
  },

  // 保持原有的 list API 以兼容现有代码
  list: {
    ...CRUD('/cif/user', ['list', 'detail', 'edit', 'delete', 'report']),
    userDetail: (userId) =>
      request({
        url: '/cif/user/detail',
        method: 'GET',
        params: { userId },
      }),
    myUsers: (data) =>
      request({
        url: '/cif/user/myUsers',
        method: 'POST',
        data,
      }),
  },
  // 用户详情相关 API
  getUserDetail: (id) =>
    request({
      url: `shop/admin/user/list/${id}`,
      method: 'GET',
    }),
  getUserOrders: (id, params) =>
    request({
      url: `shop/admin/user/list/${id}/orders`,
      method: 'GET',
      params,
    }),
  getUserSubordinates: (id, params) =>
    request({
      url: `shop/admin/user/list/${id}/subordinates`,
      method: 'GET',
      params,
    }),
  getUserCommissions: (id, params) =>
    request({
      url: `shop/admin/user/list/${id}/commissions`,
      method: 'GET',
      params,
    }),
  getUserRecharges: (id, params) =>
    request({
      url: `shop/admin/user/list/${id}/recharges`,
      method: 'GET',
      params,
    }),
  getUserWithdraws: (id, params) =>
    request({
      url: `shop/admin/user/list/${id}/withdraws`,
      method: 'GET',
      params,
    }),
  getUserAddresses: (id, params) =>
    request({
      url: '/mall/user/address/list',
      method: 'POST',
      data: {
        ...params,
        uid: id, // 用户ID筛选
      },
    }),

  // 会员等级相关 API
  level: {
    ...CRUD('shop/admin/user/level'),
  },

  // 会员标签相关 API
  tag: {
    ...CRUD('shop/admin/user/tag'),
  },

  // 其他用户相关 API
  share: (id, params) =>
    request({
      url: `shop/admin/share/${id}`,
      method: 'GET',
      params,
    }),
  coupon: (id, params) =>
    request({
      url: `shop/admin/user/coupon/${id}`,
      method: 'GET',
      params,
    }),
  couponList: (id, params) =>
    request({
      url: `shop/admin/user/coupon/couponList/${id}`,
      method: 'GET',
      params,
    }),
};

// 用户状态处理工具函数
const userUtils = {
  // 获取用户状态文本
  getUserStatusText: (status) => {
    switch (status) {
      case 1:
        return '正常';
      case 2:
        return '禁止提现';
      case 3:
        return '禁止登录';
      case 4:
        return '禁止下单';
      default:
        return '-';
    }
  },
};

export { route, api, userUtils };
