/**
 * 推荐客户 API（app 模块）
 * 小程序端调用
 */
import request from '@/utils/request'

/**
 * 报备参数
 */
export interface ReferralParam {
  customerName: string
  customerPhone: string
  customerCompany?: string
  requirement?: string
  appointmentTime?: string
  remarks?: string
}

/**
 * 推荐记录
 */
export interface ReferralRecord {
  id: number
  referralCode: string
  referrerId?: number
  referrerName?: string
  referrerPhone?: string
  customerName: string
  customerPhone: string
  customerCompany?: string
  requirement?: string
  appointmentTime?: string
  remarks?: string
  referralFee: string
  referralStatus: number
  referralStatusText?: string
  invalidReason?: string
  invalidTime?: string
  confirmedTime?: string
  settledTime?: string
  createTime: string
}

/**
 * 我的推荐统计
 */
export interface ReferralStats {
  totalCount: number
  pendingCount: number
  validCount: number
  settledCount: number
  pendingAmount: string
  referralCode?: string
}

/**
 * 分页结果
 */
export interface PageResult<T> {
  list: T[]
  total: number
  pageNum: number
  pageSize: number
  pages: number
}

/**
 * 报备客户
 */
export function addReferral(data: ReferralParam) {
  return request.post<{ code: number; message: string; data: ReferralRecord }>(
    '/app/lead/referral/add',
    data
  )
}

/**
 * 获取我的推荐码
 */
export function getMyReferralCode() {
  return request.get<{ code: number; message: string; data: string }>(
    '/app/lead/referral/my/code'
  )
}

/**
 * 获取我的推荐记录（分页）
 */
export function getMyReferrals(params: { pageNum?: number; pageSize?: number }) {
  return request.get<{ code: number; message: string; data: PageResult<ReferralRecord> }>(
    '/app/lead/referral/my',
    { params }
  )
}

/**
 * 获取我的推荐统计
 */
export function getMyStats() {
  return request.get<{ code: number; message: string; data: ReferralStats }>(
    '/app/lead/referral/my/stats'
  )
}

/**
 * 根据推荐码获取推荐人信息
 */
export function getReferrerByCode(code: string) {
  return request.get<{ code: number; message: string; data: { referrerId: number; referrerName: string; referralCode: string } }>(
    `/app/lead/referral/referrer/${code}`
  )
}
