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

const prefix = 'workbench/';

/** 工作台布局项（后端按角色下发） */
export type WorkbenchLayoutItem = {
  id: number;
  widget_code: string;
  title: string;
  sort: number;
  config: null | Record<string, any>;
};

/** 医生今日摘要（与医生小程序工作台同口径） */
export type DoctorSummaryResult = {
  range: string;
  prescription_count: number;
  wait_accept: number;
  accepting: number;
  completed: number;
  prescription_amount: string;
};

/** 药师待审摘要 */
export type PharmacistSummaryResult = {
  pending_total: number;
};

/** 诊所今日摘要 */
export type ClinicSummaryResult = {
  wait_accept: number;
  accepting: number;
  completed: number;
  wait_delivery: number;
};

/** 订单管理员摘要 */
export type OrderSummaryResult = {
  wait_delivery: number;
  refunding: number;
};

/** 平台今日总览（超管/系统管理员） */
export type PlatformOverviewResult = {
  order_count: number;
  register_count: number;
  revenue: number;
  store_input_count: number;
};

/** 待办中心单项（count>0 需要处理，path 为跳转路由） */
export type TodoCenterItem = {
  code: string;
  label: string;
  count: number;
  path: string;
};

/** 待办中心结果 */
export type TodoCenterResult = {
  items: TodoCenterItem[];
};

/** 未来7天预约：单日分布 */
export type AppointmentUpcomingDay = {
  date: string;
  label: string;
  week: string;
  count: number;
};

/** 未来7天预约统计（卡片） */
export type AppointmentUpcomingResult = {
  total: number;
  days: AppointmentUpcomingDay[];
};

/** 未来7天预约明细行 */
export type AppointmentUpcomingItem = {
  register_id: number;
  order_no: string;
  visit_date: string;
  week: string;
  slot_name: string;
  start_time: string;
  end_time: string;
  slot_label: string;
  patient_name: string;
  doctor_name: string;
  store_name: string;
  is_pay: number;
  price: string;
  status: number;
  status_text: string;
};

/** 未来7天预约明细分页结果 */
export type AppointmentUpcomingListResult = {
  items: AppointmentUpcomingItem[];
  total: number;
};

/**
 * 获取当前登录角色的工作台布局
 */
export async function getWorkbenchLayoutApi() {
  return requestClient.get<WorkbenchLayoutItem[]>(`${prefix}layout`);
}

/**
 * 医生今日摘要
 */
export async function getDoctorSummaryApi(range: string = 'today') {
  return requestClient.get<DoctorSummaryResult>(`${prefix}doctor-summary`, {
    params: { range },
  });
}

/**
 * 药师待审摘要
 */
export async function getPharmacistSummaryApi() {
  return requestClient.get<PharmacistSummaryResult>(
    `${prefix}pharmacist-summary`,
  );
}

/**
 * 诊所今日摘要
 */
export async function getClinicSummaryApi() {
  return requestClient.get<ClinicSummaryResult>(`${prefix}clinic-summary`);
}

/**
 * 订单管理员摘要
 */
export async function getOrderSummaryApi() {
  return requestClient.get<OrderSummaryResult>(`${prefix}order-summary`);
}

/**
 * 平台今日总览（超管/系统管理员）
 */
export async function getPlatformOverviewApi() {
  return requestClient.get<PlatformOverviewResult>(
    `${prefix}platform-overview`,
  );
}

/**
 * 待办中心（超管/系统管理员）
 */
export async function getTodoCenterApi() {
  return requestClient.get<TodoCenterResult>(`${prefix}todo-center`);
}

/**
 * 未来7天预约号统计（卡片：总数 + 按天分布，scope 后端按登录身份判定）
 */
export async function getAppointmentUpcomingApi() {
  return requestClient.get<AppointmentUpcomingResult>(
    `${prefix}appointment-upcoming`,
  );
}

/**
 * 未来7天预约明细（卡片点击弹窗：分页 + 日期筛选）
 */
export async function getAppointmentUpcomingListApi(data: {
  page: number;
  page_size: number;
  visit_date?: string;
}) {
  return requestClient.get<AppointmentUpcomingListResult>(
    `${prefix}appointment-upcoming-list`,
    { params: data },
  );
}
