import type { CordysAxios } from '@lib/shared/api/http/Axios';
import {
  AddOrderUrl,
  AddOrderViewUrl,
  BatchUpdateOrderUrl,
  DeleteOrderUrl,
  UpdateOrderStageUrl,
  DeleteOrderViewUrl,
  DragOrderViewUrl,
  EnableOrderViewUrl,
  FixedOrderViewUrl,
  GetOrderDetailUrl,
  OrderPageUrl,
  SortOrderUrl,
  OrderDetailSnapshotUrl,
  OrderFormConfigUrl,
  OrderFormConfigSnapshotUrl,
  OrderInContractPageUrl,
  GetOrderTabUrl,
  GetOrderViewDetailUrl,
  GetOrderViewListUrl,
  UpdateOrderUrl,
  UpdateOrderViewUrl,
  UpdateOrderStatusUrl,
  UpdateOrderStatusRollbackUrl,
  SortOrderStatusUrl,
  AddOrderStatusUrl,
  GetOrderStatusConfigUrl,
  DeleteOrderStatusUrl,
  DownloadOrderUrl,
  OrderStatisticUrl,
  SaveAdvanceConfigUrl,
  SwitchOrderCirculationTypeUrl,
} from '@lib/shared/api/requrls/order';
import type { FormDesignConfigDetailParams } from '@lib/shared/models/system/module';
import type { CommonList, TableDraggedParams } from '@lib/shared/models/common';
import type { BatchUpdatePoolAccountParams, CustomerTabHidden } from '@lib/shared/models/customer';
import type { OrderItem, UpdateOrderParams } from '@lib/shared/models/order';
import type { TableQueryParams } from '@lib/shared/models/common';

import type { ViewItem, ViewParams } from '@lib/shared/models/view';
import {
  StageBoardPageQueryParams,
  StageBoardDraggedParams,
  StageBaseParams,
  OpportunityStageConfig,
  UpdateOpportunityStageRollbackParams,
  UpdateStageBaseParams,
  type SaveCirculationConfigParams,
  type UpdateStageParams,
} from '@lib/shared/models/opportunity';
import type { CirculationTypeEnum } from '@lib/shared/enums/opportunityEnum';

export default function useOrderApi(CDR: CordysAxios) {
  // 列表
  function getOrderList(data: StageBoardPageQueryParams) {
    return CDR.post<CommonList<OrderItem>>({ url: OrderPageUrl, data }, { ignoreCancelToken: true });
  }

  // 合同下的列表
  function getOrderInContractList(data: TableQueryParams) {
    return CDR.post<CommonList<OrderItem>>({ url: OrderInContractPageUrl, data });
  }

  // 订单详情
  function getOrderDetail(id: string, approvalTaskId?: string) {
    return CDR.get<OrderItem>({ url: `${GetOrderDetailUrl}/${id}`, params: { approvalTaskId } });
  }

  // 详情快照
  function getOrderDetailSnapshot(id: string, approvalTaskId?: string) {
    return CDR.get<OrderItem>({ url: `${OrderDetailSnapshotUrl}/${id}`, params: { approvalTaskId } });
  }

  // 新增订单
  function addOrder(data: UpdateOrderParams) {
    return CDR.post({ url: AddOrderUrl, data });
  }

  // 更新订单
  function updateOrder(data: UpdateOrderParams, approvalTaskId?: string) {
    return CDR.post({ url: UpdateOrderUrl, data, params: { approvalTaskId } });
  }

  // 批量更新订单
  function batchUpdateOrder(data: BatchUpdatePoolAccountParams) {
    return CDR.post({ url: BatchUpdateOrderUrl, data });
  }

  // 删除订单
  function deleteOrder(id: string) {
    return CDR.get({ url: `${DeleteOrderUrl}/${id}` });
  }

  // 获取表单配置
  function getOrderFormConfig() {
    return CDR.get<FormDesignConfigDetailParams>({
      url: OrderFormConfigUrl,
    });
  }

  // 获取表单配置快照
  function getOrderFormSnapshotConfig(id?: string, approvalTaskId?: string) {
    return CDR.get<FormDesignConfigDetailParams>({
      url: `${OrderFormConfigSnapshotUrl}/${id}`,
      params: { approvalTaskId },
    });
  }

  function downloadOrder(id: string) {
    return CDR.get({ url: `${DownloadOrderUrl}/${id}` });
  }

  // 获取订单tab显隐配置
  function getOrderTab() {
    return CDR.get<CustomerTabHidden>({ url: GetOrderTabUrl });
  }

  // 视图管理
  function addOrderView(data: ViewParams) {
    return CDR.post({ url: AddOrderViewUrl, data });
  }

  function updateOrderView(data: ViewParams) {
    return CDR.post({ url: UpdateOrderViewUrl, data });
  }

  function getOrderViewList() {
    return CDR.get<ViewItem[]>({ url: GetOrderViewListUrl });
  }

  function getOrderViewDetail(id: string) {
    return CDR.get({ url: `${GetOrderViewDetailUrl}/${id}` });
  }

  function fixedOrderView(id: string) {
    return CDR.get({ url: `${FixedOrderViewUrl}/${id}` });
  }

  function enableOrderView(id: string) {
    return CDR.get({ url: `${EnableOrderViewUrl}/${id}` });
  }

  function deleteOrderView(id: string) {
    return CDR.get({ url: `${DeleteOrderViewUrl}/${id}` });
  }

  function dragOrderView(data: TableDraggedParams) {
    return CDR.post({ url: DragOrderViewUrl, data });
  }

  // 更新订单状态配置
  function updateOrderStatus(data: UpdateStageBaseParams) {
    return CDR.post({ url: UpdateOrderStatusUrl, data });
  }

  // 订单状态回退配置
  function updateOrderStatusRollback(data: UpdateOpportunityStageRollbackParams) {
    return CDR.post({ url: UpdateOrderStatusRollbackUrl, data });
  }

  // 订单状态排序
  function sortOrderStatus(data: string[]) {
    return CDR.post({ url: SortOrderStatusUrl, data });
  }

  // 添加订单状态
  function addOrderStatus(data: StageBaseParams) {
    return CDR.post({ url: AddOrderStatusUrl, data });
  }

  // 获取订单状态配置
  function getOrderStatusConfig() {
    return CDR.get<OpportunityStageConfig>({ url: GetOrderStatusConfigUrl }, { ignoreCancelToken: true });
  }

  // 删除订单状态
  function deleteOrderStatus(id: string) {
    return CDR.get({ url: `${DeleteOrderStatusUrl}/${id}` });
  }

  // 更新阶段
  function updateOrderStage(data: UpdateStageParams) {
    return CDR.post({ url: UpdateOrderStageUrl, data });
  }

  // 订单看板拖拽排序
  function sortOrder(data: StageBoardDraggedParams) {
    return CDR.post({ url: SortOrderUrl, data });
  }

  // 订单统计
  function getOrderStatistic(data: TableQueryParams) {
    return CDR.post({ url: OrderStatisticUrl, data }, { ignoreCancelToken: true });
  }

  // 保存高级流转配置
  function saveAdvanceConfig(data: SaveCirculationConfigParams) {
    return CDR.post({ url: SaveAdvanceConfigUrl, data });
  }

  // 切换流转配置
  function switchOrderCirculationType(type: CirculationTypeEnum) {
    return CDR.get({ url: `${SwitchOrderCirculationTypeUrl}/${type}` });
  }

  return {
    getOrderFormConfig,
    getOrderFormSnapshotConfig,
    addOrder,
    getOrderDetail,
    getOrderDetailSnapshot,
    updateOrder,
    batchUpdateOrder,
    deleteOrder,
    getOrderList,
    getOrderInContractList,
    getOrderTab,
    addOrderView,
    updateOrderView,
    getOrderViewList,
    getOrderViewDetail,
    fixedOrderView,
    enableOrderView,
    deleteOrderView,
    dragOrderView,
    updateOrderStatus,
    updateOrderStatusRollback,
    sortOrderStatus,
    addOrderStatus,
    getOrderStatusConfig,
    deleteOrderStatus,
    updateOrderStage,
    sortOrder,
    downloadOrder,
    getOrderStatistic,
    switchOrderCirculationType,
    saveAdvanceConfig,
  };
}
