import request from '@/utils/request';
import { AxiosPromise } from 'axios';
import {QueryParams, ReportItem, FetchReportsResponse, AddReportParams} from './types';
import {UnwrapNestedRefs, UnwrapRef} from "vue";

// 获取报告列表
export const fetchReports = (params: QueryParams): AxiosPromise<FetchReportsResponse> => {
  return request({
    url: '/api/knowledge/select',
    method: 'get',
    params: {
      pageNum: params.pageNum,  // 分页参数中的页码
      pageSize: params.pageSize,  // 分页参数中的每页大小
      eventType: params.eventType,  // 事件类型（可选）
      publishDate: params.publishDate,  // 发布日期范围（可选）
      subject: params.subject,  // 关键字（可选）
      sortBy: params.sortBy, // 排序字段
      sortOrder: params.sortOrder  // 排序方式
    }
  });
};

// 新增报告
export const addReport = (data: UnwrapNestedRefs<AddReportParams>): AxiosPromise<void> => {
  return request({
    url: '/api/knowledge/create',
    method: 'post',
    data: data,
  });
};

// 获取报告详情
export const fetchReportDetail = (reportId: string): AxiosPromise<ReportItem> => {
  return request({
    url: `/api/knowledge/detail?reportID=${reportId}`,
    method: 'get',
  });
};

// 修改报告
export const updateReport = (data: {
  summary: UnwrapRef<AddReportParams["summary"]>;
  publishingUnit: UnwrapRef<AddReportParams["publishingUnit"]>;
  reportName: UnwrapRef<AddReportParams["reportName"]>;
  reportId: string;
  subject: UnwrapRef<AddReportParams["subject"]>;
  publishDate: UnwrapRef<AddReportParams["publishDate"]>;
  eventType: UnwrapRef<AddReportParams["eventType"]>;
  fileNames: UnwrapRef<AddReportParams["fileNames"]>
}): AxiosPromise<void> => {
  return request({
    url: `/api/reports/update/${data.reportId}`,
    method: 'put',
    data: data,
  });
};

// 删除报告
export const deleteReport = (reportNumber: string): AxiosPromise<void> => {
  return request({
    url: `/api/reports/delete/${reportNumber}`,
    method: 'delete',
  });
};
