import { http } from '@/api/http'
import { httpErrorHandle } from '@/utils'
import { ContentTypeEnum, RequestHttpEnum, ModuleTypeEnum } from '@/enums/httpEnum'
import { ProjectItem, ProjectDetail } from './project'


// * 项目列表
export const projectListApi = async (data?: object) => {
  try {
    const res = await http(RequestHttpEnum.GET)<ProjectItem[]>(`${ModuleTypeEnum.PROJECT}/list`, data)
    return res
  } catch {
    httpErrorHandle()
  }
}

export const getDict =  async(data: object)=>{
  try{
    const res = await http(RequestHttpEnum.GET)(`${ModuleTypeEnum.DICT}`, data)
    return res
  }catch{
    httpErrorHandle()
  }
}

export const addIssue =  async(data: object)=>{
  try{
    const res = await http(RequestHttpEnum.POST)(`${ModuleTypeEnum.ISSUE}`, data)
    return res
  }catch{
    httpErrorHandle()
  }
}

export const getIssue =  async(data: object)=>{
  try{
    const res = await http(RequestHttpEnum.GET)(`${ModuleTypeEnum.ISSUE}`, data)
    return res
  }catch{
    httpErrorHandle()
  }
}


// 新增聊天室历史记录

export const historyMessageRoom =  async(data: object)=>{
  try{
    const res = await http(RequestHttpEnum.POST)(`${ModuleTypeEnum.MESSAGE}`, data)
    return res
  }catch{
    httpErrorHandle()
  }
}

export const historyMessageRoomUpdata =  async(data: object)=>{
  try{
    const res = await http(RequestHttpEnum.PUT)(`${ModuleTypeEnum.MESSAGE}`, data)
    return res
  }catch{
    httpErrorHandle()
  }
}

export const historyMessageRoomList =  async(data?: object)=>{
  try{
    const res = await http(RequestHttpEnum.GET)(`${ModuleTypeEnum.MESSAGE}`, data)
    return res
  }catch{
    httpErrorHandle()
  }
}

export const historyMessageRoomById =  async(id:any)=>{
  try{
    const res = await http(RequestHttpEnum.GET)(`${ModuleTypeEnum.MESSAGE}/${id}`, )
    return res
  }catch{
    httpErrorHandle()
  }
}
export const historyMessageRoomDel=  async(id:any)=>{
  try{
    const res = await http(RequestHttpEnum.DELETE)(`${ModuleTypeEnum.MESSAGE}/${id}`, )
    return res
  }catch{
    httpErrorHandle()
  }
}
// * 新增项目
export const createProjectApi = async (data: object) => {
  try {
    const res = await http(RequestHttpEnum.POST)<{
      /**
       * 项目id
       */
      id: number
    }>(`${ModuleTypeEnum.PROJECT}/create`, data)
    return res
  } catch {
    httpErrorHandle()
  }
}

// * 获取项目
export const fetchProjectApi = async (data: object) => {
  try {
    const res = await http(RequestHttpEnum.GET)<ProjectDetail>(`${ModuleTypeEnum.PROJECT}/getData`, data)
    return res
  } catch {
    httpErrorHandle()
  }
}

// * 保存项目
export const saveProjectApi = async (data: object) => {
  try {
    const res = await http(RequestHttpEnum.POST)(
      `${ModuleTypeEnum.PROJECT}/save/data`,
      data,
      ContentTypeEnum.FORM_URLENCODED
    )
    return res
  } catch {
    httpErrorHandle()
  }
}

// * 修改项目基础信息
export const updateProjectApi = async (data: object) => {
  try {
    const res = await http(RequestHttpEnum.POST)(`${ModuleTypeEnum.PROJECT}/edit`, data)
    return res
  } catch {
    httpErrorHandle()
  }
}

// * 删除项目
export const deleteProjectApi = async (data: object) => {
  try {
    const res = await http(RequestHttpEnum.DELETE)(`${ModuleTypeEnum.PROJECT}/delete`, data)
    return res
  } catch {
    httpErrorHandle()
  }
}

// * 修改发布状态 [-1未发布,1发布]
export const changeProjectReleaseApi = async (data: object) => {
  try {
    const res = await http(RequestHttpEnum.PUT)(`${ModuleTypeEnum.PROJECT}/publish`, data)
    return res
  } catch {
    httpErrorHandle()
  }
}

// * 上传文件
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()
  }
}
