import {
  SectionAddParams,
  SectionFileParams,
  SectionUpdateParams,
  TreeResfulParams,
  TreeResultModel
} from '../model/tree'
import { defHttp } from '/@/utils/http/axios'

import { ErrorMessageMode } from '/@/utils/http/axios/types'

enum Api {
  Section = '/contract/section',
  SectionAll = '/contract/section/all',
  AddSection = '/contract/section/add', // 新增项目节
  UpdateSection = '/contract/section/save', // 更新项目节
  UpOrDownSection = '/contract/section/serial', // 上下移
  UpgradeOrRelegateSection = '/contract/section/depth', // 升降级
  SaveSectionFile = '/contract/file', // 保存项目节附件
  DeleteSection = '/contract/section' // 删除项目节
}

/**
 * @description: 获取设计表树结构
 */
export function sectionAllApi(mode: ErrorMessageMode = 'message') {
  return defHttp.get<TreeResultModel[]>(
    {
      url: Api.SectionAll
    },
    {
      errorMessageMode: mode
    }
  )
}

/**
 * @description: 合同项目节上下、升降级
 */
export function treeResfulApi(
  { id, type, operation }: TreeResfulParams,
  mode: ErrorMessageMode = 'message'
) {
  return defHttp.post(
    {
      url: type === 'depth' ? Api.UpgradeOrRelegateSection : Api.UpOrDownSection,
      params: { id, operation }
    },
    {
      errorMessageMode: mode
    }
  )
}

/**
 * @description 获取单个项目节详情
 */
export function treeDetailApi(id: string, mode: ErrorMessageMode = 'message') {
  return defHttp.get(
    {
      url: Api.Section,
      params: { id }
    },
    {
      errorMessageMode: mode
    }
  )
}

/**
 * @description 添加目录
 */
export function sectionAddApi(params: SectionAddParams, mode: ErrorMessageMode = 'message') {
  return defHttp.post(
    {
      url: Api.AddSection,
      params
    },
    {
      errorMessageMode: mode
    }
  )
}

/**
 * @description 更新目录
 */
export function sectionUpdateApi(params: SectionUpdateParams, mode: ErrorMessageMode = 'message') {
  return defHttp.post(
    {
      url: Api.UpdateSection,
      params
    },
    {
      errorMessageMode: mode
    }
  )
}

/**
 * @description 保存附件id
 */
export function saveSectionFileApi(params: SectionFileParams, mode: ErrorMessageMode = 'message') {
  return defHttp.post(
    {
      url: Api.SaveSectionFile,
      params
    },
    {
      errorMessageMode: mode
    }
  )
}

/**
 * @description 删除项目节
 */
export function delSectionApi(params: { id: string }, mode: ErrorMessageMode = 'message') {
  return defHttp.delete(
    {
      url: Api.DeleteSection,
      params
    },
    {
      errorMessageMode: mode
    }
  )
}
