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

export namespace MesProQrExportApi {
  /** 业务类型 */
  export type BizType =
    | 'BAG_BUSINESS'
    | 'BAG_LABEL'
    | 'BATCH_BUSINESS'
    | 'BATCH_LABEL'
    | 'PALLET_BUSINESS'
    | 'PALLET_LABEL';

  /** 导出方式：SYNC 同步直接下载（秒出）/ ASYNC 后台任务 */
  export type ExportMode = 'ASYNC' | 'SYNC';

  /** 导出任务状态：10 排队中 20 生成中 30 待下载 40 已取消 50 已失败 60 已下载 */
  export type TaskStatus = 10 | 20 | 30 | 40 | 50 | 60;

  /** 导出参数 */
  export interface ExportParams {
    bizType: BizType;
    batchId?: number;
    uuid?: string;
  }

  /** 导出数量与方式 */
  export interface ExportCount {
    mode: ExportMode;
    syncThreshold: number;
    totalCount: number;
  }

  /** 导出任务 */
  export interface ExportTask {
    id: number;
    bizType: string;
    taskName: string;
    status: TaskStatus;
    statusName: string;
    totalCount: number;
    finishedCount: number;
    progress: number;
    fileSize?: number;
    errorMsg?: string;
    downloadable: boolean;
    createTime?: string;
    finishTime?: string;
  }
}

/** 统计导出张数并给出导出方式 */
export function getQrExportCount(params: MesProQrExportApi.ExportParams) {
  return requestClient.get<MesProQrExportApi.ExportCount>(
    '/mes/pro/qr-export/count',
    { params },
  );
}

/** 提交后台导出任务 */
export function submitQrExportTask(data: MesProQrExportApi.ExportParams) {
  return requestClient.post<MesProQrExportApi.ExportTask>(
    '/mes/pro/qr-export/task',
    data,
  );
}

/** 查询导出任务进度 */
export function getQrExportTask(id: number) {
  return requestClient.get<MesProQrExportApi.ExportTask>(
    '/mes/pro/qr-export/task/get',
    { params: { id } },
  );
}

/** 取消导出任务 */
export function cancelQrExportTask(id: number) {
  return requestClient.post<boolean>('/mes/pro/qr-export/task/cancel', null, {
    params: { id },
  });
}

/** 导出任务产物下载地址（不带鉴权头，供流式下载自行附加 token） */
export function getQrExportDownloadPath(id: number) {
  return `/mes/pro/qr-export/task/download?id=${id}`;
}
