import type { List, Tree } from '@/model';
import type { OrderQuery, OrderModel, OrderLiaisonListModel, OrderLiaisonListQuery, ShipmentModel, ShipmentQuery, PieOrderCountModel,RevenueSharingDetailModel,RevenueSharingDetailQuery } from '@/model/order.model';
import request from '@/request/alova';

// 线下服务  今日指派订单分页列表
export function todayOrderMethod(page: number, size: number) {
  return request.Post<List<OrderModel>>(
    '/fdhb-pc/pieOrderManage/getTodayPieOrders',
    {},
    {
      hitSource: /order$/, // 匹配失效源
      params: { pageNum: page, pageSize: size },
    }
  );
}

// 线下服务  待指派订单分页列表
export function pendingOrderMethod(page: number, size: number) {
  return request.Post<List<OrderModel>>(
    '/fdhb-pc/pieOrderManage/getPendPieOrders',
    {},
    {
      hitSource: /order$/, // 匹配失效源
      params: { pageNum: page, pageSize: size },
    }
  );
}

// 线下服务  全部指派订单分页列表
export function allPieOrderMethod(page: number, size: number, query?: OrderQuery) {
  return request.Post<List<OrderModel>>(
    '/fdhb-pc/pieOrderManage/getAllPieOrders',
    query ?? {},
    {
      hitSource: /order$/, // 匹配失效源
      params: { pageNum: page, pageSize: size },
    }
  );
}

// 实体商品 今日指派订单分页列表
export function todayPhysicalOrderMethod(page: number, size: number) {
  return request.Post<List<OrderModel>>(
    '/fdhb-pc/pieOrderManage/getTodayPieOnlineOrders',
    {},
    {
      hitSource: /order$/, // 匹配失效源
      params: { pageNum: page, pageSize: size },
    }
  );
}

// 实体商品 待指派订单分页列表
export function pendingPhysicalOrderMethod(page: number, size: number) {
  return request.Post<List<OrderModel>>(
    '/fdhb-pc/pieOrderManage/getPendPieOnlineOrders',
    {},
    {
      hitSource: /order$/, // 匹配失效源
      params: { pageNum: page, pageSize: size },
    }
  );
}

// 实体商品 全部指派订单分页列表
export function allPhysicalOrderMethod(page: number, size: number, query?: OrderQuery) {
  return request.Post<List<OrderModel>>(
    '/fdhb-pc/pieOrderManage/getAllPieOnlineOrders',
    query ?? {},
    {
      hitSource: /order$/, // 匹配失效源
      params: { pageNum: page, pageSize: size },
    }
  );
}
// 获取可派单机构
export function getOrderLiaisonListMethod(data: Partial<OrderModel>) {
  return request.Post<List<OrderLiaisonListModel>>(
    '/fdhb-pc/pieOrderManage/getPieCpSupplier',
    {
      collaborateDeptId: data.institutionId,
      conditioningProgramTypes: [data.conditioningProgramType],
      timeStart: data?.timeStart ?? '',
      type: data?.type ?? ''
    },
    {
      hitSource: /order$/, // 匹配失效源
    }
  );
}

// 线下服务 确认指派
export function confirmPieOrderMethod(data: Partial<OrderModel>) {
  return request.Post<List<OrderModel>>(
    `/fdhb-pc/pieOrderManage/pieOrder/${data.id}/${data.conditioningProgramSupplierId}`, {}
    ,
    {
      params: {
        startTime: data?.pieTimeStart ?? '',
        endTime: data?.pieTimeEnd ?? '',
      },
      hitSource: /order$/, // 匹配失效源
    }
  );
}
// 实体商品 确认指派
export function confirmPhysicalOrderMethod(data: Partial<OrderModel>) {
  return request.Post(
    `/fdhb-pc/pieOrderManage/pieOnlineOrder/${data.patientConditioningProgramId}/${data.conditioningProgramSupplierId}`,
    {
      hitSource: /order$/, // 匹配失效源
    },
  );
}

// 获取发货列表
export function getShipmentListMethod(page: number, size: number, query?: ShipmentQuery) {
  return request.Post<List<ShipmentModel>>(
    '/fdhb-pc/deliveryManage/getDelivery',
    query ?? {},
    {
      hitSource: /shipment$/, // 匹配失效源
      params: { pageNum: page, pageSize: size },
    }
  );
}

// 确认发货
export function confirmShipmentMethod(data: Partial<ShipmentModel>) {
  // 如果是配送（receiptType === '0'），传递快递信息；如果是线下取货（receiptType === '1'），不传递
  const params: any = {};
  if (data.receiptType === '0') {
    params.expressType = data.expressType;
    params.expressNo = data.expressNo;
  }
  return request.Post<List<ShipmentModel>>(
    `/fdhb-pc/deliveryManage/confirmDelivery/${data.id}/${data.conditioningProgramSupplierId}/${data.receiptType}`,
    {},
    {
      params,
      hitSource: /shipment$/, // 匹配失效源
    }
  );
}

// 获取指派订单数量
export function getPieOrderCountMethod() {
  return request.Post<PieOrderCountModel>(
    `/fdhb-pc/pieOrderManage/getPieCount`,
    {
      hitSource: /order$/, // 匹配失效源
    }
  );
}

// 获取分账记录列表
export function getRevenueSharingDetailListMethod(page: number, size: number, query?: RevenueSharingDetailQuery) {
  return request.Post<List<RevenueSharingDetailModel>>(
    '/fdhb-pc/profitSharingManage/getProfitSharing',
    query ?? {},
    {
      params: { pageNum: page, pageSize: size },
      hitSource: /revenueSharing$/, // 匹配失效源
    }
  );
} 
