import { requestClient } from '#/api/request';

const prefix = 'store/';
/**
 * 获取当前登录的诊所信息
 * @param data
 */
export async function getStoreInfoApi(data: any) {
  return requestClient.get<any>(`${prefix}store-info`, { params: data });
}
/**
 * 获取当前登录的诊所信息
 */
export async function getDoctorMyDoctorOrderApi() {
  return requestClient.get<any>(`${prefix}my-doctor-order`);
}
/**
 * 获取当前登录的诊所信息
 */
export async function getDoctorMyDiseaseListApi() {
  return requestClient.get<any>(`${prefix}my-disease-list`);
}
/**
 * 保存服务价格
 * @param data
 */
export async function saveServicePriceApi(data: Record<string, any>) {
  return requestClient.post<any>(`${prefix}save-service-price`, data);
}
/**
 * 保存折扣比例
 * @param z_sale_percent
 */
export async function saveZSalePercentApi(z_sale_percent: number | string) {
  return requestClient.post<any>(`${prefix}save-zsale-percent`, {
    z_sale_percent,
  });
}
/**
 * 保存轮播图
 * @param data
 */
export async function saveNavUrlApi(data) {
  return requestClient.post<any>(`${prefix}upload-nav`, data);
}
/**
 * 删除轮播图
 * @param data
 */
export async function deleteNavApi(data) {
  return requestClient.post<any>(`${prefix}delete-nav`, data);
}
/**
 * 更新轮播图排序
 * @param ids 按顺序排列的轮播图ID数组
 */
export async function updateNavSortApi(ids: number[]) {
  return requestClient.post<any>(`${prefix}update-nav-sort`, { ids });
}

/**
 * 更新诊所类型
 * @param data 包含clinic_type的对象，clinic_type: 1=西医诊所, 2=中医诊所
 */
export async function updateClinicTypeApi(data: { clinic_type: number }) {
  return requestClient.post<any>(`${prefix}update-clinic-type`, data);
}

/*
   ----------------------------推广员---------------------------------
 */
/**
 * 推广员列表
 */
export async function salespersonListApi() {
  return requestClient.get<any>(`salesperson/list`);
}
/**
 * 创建推广员
 * @param data
 */
export async function salespersonCreateApi(data) {
  return requestClient.post<any>(`salesperson/create`, data);
}
/**
 * 编辑推广员
 * @param data
 */
export async function salespersonUpdateApi(data) {
  return requestClient.post<any>(`salesperson/update`, data);
}

export interface DrugCommissionListItem {
  drug_id: number;
  drug_name: string;
  pinyin_simple?: string;
  image?: string;
  drug_spec: string;
  buy_price: string;
  price: string;
  commission: string;
}

export interface DrugCommissionListResult {
  items: DrugCommissionListItem[];
  total: number;
  page: number;
  pageSize: number;
}

export async function getDrugCommissionListApi(params: Record<string, any>) {
  return requestClient.get<DrugCommissionListResult>(
    `salesperson/drug-commission-list`,
    { params },
  );
}

export async function saveDrugCommissionApi(data: {
  salesperson_id: number;
  items: { drug_id: number; commission: string }[];
}) {
  return requestClient.post<any>(`salesperson/save-drug-commission`, data);
}

export async function getDrugCommissionCopyFromApi(fromSalespersonId: number) {
  return requestClient.get<{ items: { drug_id: number; commission: string }[] }>(
    `salesperson/drug-commission-copy-from`,
    { params: { from_salesperson_id: fromSalespersonId } },
  );
}

export async function getCommissionListApi(params: Record<string, any>) {
  return requestClient.get<any>(`salesperson/commission-list`, { params });
}

export async function getCommissionOrderListApi(params: Record<string, any>) {
  return requestClient.get<any>(`salesperson/commission-order-list`, { params });
}

export async function getSettlementPreviewApi(params: Record<string, any>) {
  return requestClient.get<any>(`salesperson/settlement-preview`, { params });
}

export async function confirmSettlementApi(data: Record<string, any>) {
  return requestClient.post<any>(`salesperson/settlement`, data);
}

export async function getSettlementListApi(params: Record<string, any>) {
  return requestClient.get<any>(`salesperson/settlement-list`, { params });
}

export async function getSettlementDetailApi(params: { id: number }) {
  return requestClient.get<any>(`salesperson/settlement-detail`, { params });
}

export async function getUserBindListApi(params: Record<string, any>) {
  return requestClient.get<any>(`salesperson/user-bind-list`, { params });
}

export async function openAdminAccountApi(data: {
  salesperson_id: number;
  store_id?: number;
}) {
  return requestClient.post<any>(`salesperson/open-admin-account`, data);
}

export async function getPlatformSalespersonListApi(params: Record<string, any>) {
  return requestClient.get<any>(`salesperson/platform-list`, { params });
}

export async function getPlatformStoreOptionsApi(params: { keyword?: string }) {
  return requestClient.get<{ id: number; name: string; position: string }[]>(
    `salesperson/platform-store-options`,
    { params },
  );
}

export async function platformSalespersonCreateApi(data: Record<string, any>) {
  return requestClient.post<any>(`salesperson/platform-create`, data);
}

/** 同门店推广员搜索（Picker） */
export async function salespersonOptionsApi(params: Record<string, any>) {
  return requestClient.get<any[]>(`salesperson/salesperson-options`, { params });
}

/** 下级推广员列表 */
export async function invitedSalespersonListApi(params: Record<string, any>) {
  return requestClient.get<any>(`salesperson/invited-salesperson-list`, { params });
}

/** 手动退回推广员分成（仅已退款/已取消订单） */
export async function reverseCommissionApi(data: {
  order_id: number;
  store_id: number;
  salesperson_id: number;
}) {
  return requestClient.post<any>(`salesperson/reverse-commission`, data);
}
