import { responsesmodel } from './model/queryModal';
import { defHttp } from '@/utils/http/axios';
import { BasicFetchResult } from '@/api/model/baseModel';
import { UploadFileParams } from '#/axios';
import { AxiosProgressEvent } from 'axios';
import { useGlobSetting } from '@/hooks/setting';

// files
export interface files {
  id: string;
  folderId: string;
  fileName: string;
  filePath: string;
  description: string;
  fileType: string;
  fileSize: string;
  extension: string;
  enable: boolean;
  sortCode: number;
  deleteMark: false;
  createUserId: string;
  createUserName: string;
  createTime: string;
  thumbnail: string;
  belongApp: string;
  belongAppId: string;
}
export type filesModel = BasicFetchResult<files>;

export interface UploadApiResult {
  message: string;
  code: number;
  url: string;
}

enum Api {
  // 加载附件列表
  Get_Load = '/api/Files/Load',
  // 删除附件
  Post_Delete = '/api/Files/Delete',
  // 批量上传文件接口 <br>客户端文本框需设置name='files'
  Post_Upload = '/api/Files/Upload',
}

const { apiUrl } = useGlobSetting();

// 加载附件列表
export function fun_Load(params) {
  return defHttp.get<filesModel>({
    url: Api.Get_Load,
    params,
  });
}
// 删除附件
export function fun_Delete(params) {
  return defHttp.post<responsesmodel>({
    url: Api.Post_Delete,
    params,
  });
}

// 批量上传文件接口 <br>客户端文本框需设置name='files'
export function fun_Upload(
  params: UploadFileParams,
  onUploadProgress: (progressEvent: AxiosProgressEvent) => void,
) {
  return defHttp.uploadFile<UploadApiResult>(
    {
      url: apiUrl + Api.Post_Upload,
      onUploadProgress,
      timeout: 300000,
    },
    params,
  );
}

export const uploadFile = (params) =>
  defHttp.post({
    url: Api.Post_Upload,
    params,
    headers: {
      'Content-type': 'multipart/form-data',
    },
  });
