import axios from '@/utils/request'
// import { serialize } from 'object-to-formdata'
const base = '/tofly-sxgk'
const uris = {
  project: {
    page: `${base}/other/getUserPrjs`,
    set: `${base}/other/setPrj`
  }
}

export interface IQueryCommon extends IPagination {
  hitCount?: boolean
  optimizeCountSql?: boolean
  orders?: { asc?: boolean; column?: string }[]
  pages?: number
  searchCount?: boolean
}

export type IRes<T = any> = Promise<{
  code: number
  message: string
  result: IQueryCommon & { records: T }
}>

export type IResult<T = any> = Promise<{
  code: number
  message: string
  result: T
}>

export interface IPagination {
  current?: number
  size?: number
  total?: number
}

export interface IProject {
  id: number
  name: string
  code: string
  createTime: string
  unit: string
  gcfw: string
  xmtz: string
  xmzq: string
  xmjs: string
  dataUser: string
  geo_info: string
  needSpv: boolean
  needPhoto: boolean
}

export interface IOrg {
  orgType: string
  orgName: string
  orgCode: string
  prjList: IProject[]
  id: number
}

export interface IDictionary {
  createTime: string
  createUser: number
  id: number
  key: string
  notes: string
  sort: number
  ulevel: string
  value: string
}
export interface ICreator {
  createTime: string
  createUser: number
  createUserName: string
}
export interface IUpdater {
  updateTime: string
  updateUser: number
  updateUserName: string
}
export interface IOrgProjectCombine extends IProject, Omit<IOrg, 'prjList'> {
  orgId: number
}
export const fetchProjects = () => axios.request<IResult<IOrg[]>>({ url: uris.project.page, method: 'get' })

export const setProjectKey = (id: number) =>
  axios.request<IRes<boolean>>({ url: uris.project.set, method: 'post', data: { prjId: id } })

export const addMenu = (data) => axios.request<IRes<boolean>>({ url: '/base/menu', method: 'post', data })

export const fetchDictionary = (codeKey: string) =>
  axios.request<IResult<IDictionary[]>>({
    url: '/base/code/list',
    method: 'get',
    params: { ulevel: 2, codeKey, size: 9999 }
  })

export interface IFile extends ICreator {
  id?: number
  projectId: number
  tableName: string
  fileType: number
  filePath: string
  statusFlag: number
  fileName: string
  otherName: string
  fileFormat: string
  fileSize: string
}
