import request from '@/utils/request';
import type { ApiResult } from '@/api';

/**
 * 会员注册记录相关API
 */

/**
 * 会员注册记录
 */
export interface MemberRegister {
  id: number;
  dealerId: number;
  dealerName?: string;
  dealerPhone?: string;
  phone: string;
  realName?: string;
  memberFee: number;
  memberFeeStatus: number;
  registerFee: number;
  registerFeeStatus: number;
  userId?: number;
  nickName?: string;
  status: number;
  comments?: string;
  createTime: string;
}

/**
 * 注册费支付信息
 */
export interface RegisterPayInfo {
  id: number;
  phone: string;
  realName?: string;
  registerFee: number;
  dealerName?: string;
  dealerPhone?: string;
}

/**
 * 获取我的下级注册列表
 */
export async function getMyRegisterList(params?: { page?: number; limit?: number }) {
  const res = await request.get<ApiResult<MemberRegister[]>>('/shop/shop-member-register/my/list', params);
  return res.data || [];
}

/**
 * 录入下级
 */
export async function addSubordinate(data: { phone: string; realName?: string }) {
  const res = await request.post<ApiResult<null>>('/shop/shop-member-register/add', data);
  return res;
}

/**
 * 修改下级信息
 */
export async function updateSubordinate(data: { id: number; phone?: string; realName?: string }) {
  const res = await request.put<ApiResult<null>>('/shop/shop-member-register/update', data);
  return res;
}

/**
 * 检查手机号是否可注册
 */
export async function checkPhone(phone: string) {
  const res = await request.get<ApiResult<{
    canRegister: boolean;
    message?: string;
    registerFee?: number;
    memberFee?: number;
  }>>('/shop/shop-member-register/check', { phone });
  return res.data;
}

/**
 * 获取注册费支付信息
 */
export async function getRegisterPayInfo(phone: string) {
  const res = await request.get<ApiResult<RegisterPayInfo>>('/shop/shop-member-register/pay/info', { phone });
  return res.data;
}

/**
 * 获取当前用户的注册状态
 */
export async function getMyRegisterStatus() {
  const res = await request.get<ApiResult<{
    isDealer: boolean;
    dealerId?: number;
    dealerName?: string;
    register?: MemberRegister;
  }>>('/shop/shop-member-register/my/status');
  return res.data;
}

/**
 * 获取会员配置信息
 */
export async function getMemberConfig() {
  const res = await request.get<ApiResult<{
    memberFee: number;
    registerFee: number;
  }>>('/shop/shop-member-register/config');
  return res.data;
}
