import request from '@/utils/request';

/**
 * 测试 ERP 数据库连接
 */
export function testErpConnection() {
  return request.get<{
    status: string;
    database: string;
    version: string;
    error?: string;
  }>('/erp/test/connection').json();
}

/**
 * ERP 健康检查
 */
export function erpHealth() {
  return request.get<string>('/erp/test/health').json();
}

/**
 * 客户档案接口类型
 */
export interface CustomerVO {
  customerCode: string;
  customerName: string;
  companyCode: string;
  companyName: string;
  brand: string;
  brandName: string;
  contactName: string;
  salesAreaCode: string;
  salesAreaName: string;
  salesPersonCode: string;
  salesPersonName: string;
  saleDocCode: string;
  saleDocName: string;
  pricePlanCode: string;
  pricePlanName: string;
  customerType: string;
  address: string;
  phone: string;
  email: string;
  sdOrgCode: string;
  sdOrgName: string;
  province: string;
  city: string;
  isStop: number;
}

/**
 * 分页查询客户列表
 * 使用动态API: /erp/dynamic/v1/customer/list
 */
export function getCustomerList(params: {
  pageNum: number;
  pageSize: number;
  keyword?: string;
  companyCode?: string;
  salesAreaCode?: string;
  brand?: string;
}) {
  return request.get<{ code: number; msg: string; data: { rows: CustomerVO[]; total: number; code: number; msg: string } }>(
    '/erp/dynamic/v1/customer/list',
    params
  ).json();
}

/**
 * 获取客户详情
 * 使用动态API: /erp/dynamic/v1/customer/detail
 */
export function getCustomerDetail(customerCode: string) {
  return request.get<{ code: number; msg: string; data: CustomerVO }>(`/erp/dynamic/v1/customer/detail?customerCode=${customerCode}`).json();
}

/**
 * 获取销区列表
 * 使用动态API: /erp/dynamic/v1/customer/sales-areas
 */
export function getSalesAreas() {
  return request.get<{ code: number; msg: string; data: CustomerVO[] }>('/erp/dynamic/v1/customer/sales-areas').json();
}

/**
 * 获取品牌列表
 * 使用动态API: /erp/dynamic/v1/customer/brands
 */
export function getBrands() {
  return request.get<{ code: number; msg: string; data: CustomerVO[] }>('/erp/dynamic/v1/customer/brands').json();
}
