import { http } from '@/api/http';
import { httpErrorHandle } from '@/utils';
import { ContentTypeEnum, RequestHttpEnum, ModuleTypeEnum } from '@/enums/httpEnum';
import { ProjectItem, ProjectDetail } from './project';
import { defHttp } from '@/utils/http/axios';
import { UploadFileParams } from '#/axios';
import { useGlobSetting } from '@/hooks/setting';
import { AxiosProgressEvent } from 'axios';
import { UploadApiResult } from '../sys/model/uploadModel';

enum Api {
  ProjectList = '/api/goview/project/list',
  Create = '/api/goview/project/create',
  Delete = '/api/goview/project/delete',
  Edit = '/api/goview/project/edit',
  Publish = '/api/goview/project/publish',
  getData = '/api/goview/project/getData',
  saveData = '/api/goview/project/save/data',
  projectFile = '/api/goview/project/uploadFile',
  SetMainPage = '/api/goview/project/mainPage/set',
  GetMainPage = '/api/goview/project/mainPage/get',
}
// * 项目列表
export const projectListApi = (params?: ProjectItem) =>
  defHttp.get({ url: Api.ProjectList, params });

// * 新增项目
export function createProjectApi(params) {
  return defHttp.post({
    url: Api.Create,
    params,
  });
}

// * 获取项目
export const fetchProjectApi = (params) => defHttp.get<ProjectDetail>({ url: Api.getData, params });

// * 保存项目
export function saveProjectApi(params) {
  return defHttp.post({
    url: Api.saveData,
    params,
  });
}

// * 修改项目基础信息

export function updateProjectApi(params) {
  return defHttp.post({
    url: Api.Edit,
    params,
  });
}

// * 删除项目

export function deleteProjectApi(params) {
  return defHttp.post({
    url: Api.Delete + '?ids=' + params.ids,
    params,
  });
}
// * 修改发布状态 [-1未发布,1发布]
export const changeProjectReleaseApi = (params: object) =>
  defHttp.post({ url: Api.Publish, params });

// * 上传文件

// export function uploadFile(params) {
//   console.log(params);
//   return defHttp.post({
//     url: Api.projectFile,
//     params,
//   });
// }
const { apiUrl } = useGlobSetting();
const uploadFileUrl = apiUrl + Api.projectFile;

export function projectUploadFile(
  params,
  onUploadProgress: (progressEvent: AxiosProgressEvent) => void,
) {
  console.log(params);
  return defHttp.uploadFile<UploadApiResult>(
    {
      url: uploadFileUrl,
      onUploadProgress,
    },
    params,
  );
}
// export const uploadFile = async (data: object) => {
//   try {
//     const res = await http(RequestHttpEnum.POST)<{
//       /**
//        * 文件地址
//        */
//       fileName: string;
//       fileurl: string;
//     }>(`${ModuleTypeEnum.PROJECT}/upload`, data, ContentTypeEnum.FORM_DATA);
//     return res;
//   } catch {
//     httpErrorHandle();
//   }
// };

// 设置系统主页面
export function setMainPage(params) {
  console.log(params);
  return defHttp.post({
    url: Api.SetMainPage,
    params,
  });
}
// 获取系统主页面
export const getMainPage = () => defHttp.get({ url: Api.GetMainPage });
