import axios from '@/utils/request'
import { ElTableColumn } from 'element-ui/types/table-column'
import { Options, serialize as obj2FormData } from 'object-to-formdata'
const uris = {
  project: {
    page: '/tofly-sxgk/other/getUserPrjs',
    set: '/tofly-sxgk/other/setPrj'
  },
  standard: {
    list: '/survey/targetdictionaryp/list'
  },
  addDictionary: '/base/code'
}

export const serialize = (data: any, options: Options = {}) =>
  obj2FormData(data, { dotsForObjectNotation: true, noFilesWithArrayNotation: true, ...options })

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 ColItem extends Partial<ElTableColumn> {
  /** 是否使用slot展示 */
  _slot?: boolean
}
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 IOriginalDictionary {
  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 interface IFile extends ICreator {
  id?: number
  projectId: number
  tableName: string
  fileType: number
  filePath: string
  statusFlag: number
  fileName: string
  otherName: string
  fileFormat: string
  fileSize: string
}

export interface IBaseStandard {
  targetMax: number
  id: number
  typeId: number
  targetCode: string
  targetName: string
  targetUnit: string
  targetMin: number
  deviceTypeName: string
  dataUploadTime: number
  createUser: number
  createUserName: string
}

export interface IDictionary {
  codeKey: string
  codeRemark: string
  codeValue: string
  createTime: string
  createUser: number
  id: number
  notes: string
  sort: number
  ulevel: string | 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: serialize({ prjId: id })
  })

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

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

export const fetchBaseStandards = () =>
  axios.request<IResult<IBaseStandard[]>>({ url: uris.standard.list, method: 'get' })

/** 保存字典值 */
export const addDictionary = (data: Partial<IOriginalDictionary & { creater?: string }>) =>
  axios.request({ url: uris.addDictionary, method: 'post', data })
