/**
 * 推荐客户管理 API
 */
import request from '@/utils/request';
import { MODULES_API_URL } from '@/config/setting';

/** 推荐状态枚举 */
export const ReferralStatusOptions = [
  { value: 0, label: '待确认' },
  { value: 1, label: '有效' },
  { value: 2, label: '无效' },
  { value: 3, label: '已结算' }
];

/** 推荐记录 */
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 | number;
  referralStatus?: number;
  invalidReason?: string;
  invalidTime?: string;
  confirmedTime?: string;
  settledTime?: string;
  createTime?: string;
}

/** 查询参数 */
export interface ReferralParam {
  pageNum?: number;
  pageSize?: number;
  referralStatus?: number;
  referrerId?: number;
  customerName?: string;
  customerPhone?: string;
  referralCode?: string;
  startDate?: string;
  endDate?: string;
}

/**
 * 分页查询推荐记录
 */
export async function pageReferral(params: ReferralParam) {
  const res = await request.get<{
    code: number;
    message: string;
    data: {
      list: ReferralRecord[];
      total: number;
      pageNum: number;
      pageSize: number;
    };
  }>(MODULES_API_URL + '/app/lead/referral/admin/page', { params });
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 获取推荐统计
 */
export async function getStatistics(params: ReferralParam) {
  const res = await request.get<{
    code: number;
    message: string;
    data: Record<string, number>;
  }>(MODULES_API_URL + '/app/lead/referral/admin/statistics', { params });
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 确认推荐有效
 */
export async function confirmReferral(id: number) {
  const res = await request.put<{ code: number; message: string }>(
    MODULES_API_URL + '/app/lead/referral/admin/confirm/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 作废推荐
 */
export async function invalidateReferral(id: number, reason?: string) {
  const res = await request.put<{ code: number; message: string }>(
    MODULES_API_URL + '/app/lead/referral/admin/invalid/' + id,
    { reason }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 结算推荐费
 */
export async function settleReferral(
  id: number,
  amount?: number,
  remarks?: string
) {
  const res = await request.put<{ code: number; message: string }>(
    MODULES_API_URL + '/app/lead/referral/admin/settle/' + id,
    { amount, remarks }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量结算推荐费
 */
export async function batchSettleReferrals(ids: number[]) {
  const res = await request.put<{ code: number; message: string }>(
    MODULES_API_URL + '/app/lead/referral/admin/settle/batch',
    ids
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 导出推荐数据
 */
export async function exportReferrals(params: ReferralParam) {
  const res = await request.get<{
    code: number;
    message: string;
    data: Record<string, unknown>[];
  }>(MODULES_API_URL + '/app/lead/referral/admin/export', { params });
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
