import http from '@/utils/http'
import { AppDispatch } from '..'

// 入馆 入藏 登记 删除
export type FourKeyType = '1' | '2' | '3' | '4'

/**
 * 获取分页列表
 */
export const FourAPI_getList = (data: any, key: FourKeyType, exportFlag?: boolean): any => {
  const urlObj = {
    1: 'cms/orderHouse/page',
    2: 'cms/orderHide/page',
    3: 'cms/register/page',
    4: 'cms/orderDel/page'
  }
  const url = urlObj[key]

  if (exportFlag) return http.post(url, data)
  else {
    return async (dispatch: AppDispatch) => {
      const res = await http.post(url, data)
      if (res.code === 0) {
        const obj = {
          list: res.data.records,
          total: res.data.total
        }

        dispatch({ type: 'Four/getList', payload: { key, value: obj } })
      }
    }
  }
}

/**
 * 创建订单
 */
export const FourAPI_create = (key: FourKeyType) => {
  const urlObj = {
    1: 'cms/orderHouse/create',
    2: 'cms/orderHide/create',
    3: 'cms/register/create',
    4: 'cms/orderDel/create'
  }
  const url = urlObj[key]
  return http.get(url)
}

/**
 * 详情
 */
export const FourAPI_getInfo = (key: FourKeyType, id: string) => {
  const urlObj = {
    1: `cms/orderHouse/detail/${id}`,
    2: `cms/orderHide/detail/${id}`,
    3: `cms/register/detail/${id}`,
    4: `cms/orderDel/detail/${id}`
  }
  const url = urlObj[key]
  return http.get(url)
}

/**
 * 保存草稿/添加藏品
 */
export const FourAPI_saveDraft = (key: FourKeyType, data: any) => {
  const urlObj = {
    1: 'cms/orderHouse/saveDraft',
    2: 'cms/orderHide/saveDraft',
    3: 'cms/register/saveDraft',
    4: 'cms/orderDel/saveDraft'
  }
  const url = urlObj[key]
  return http.post(url, data)
}

/**
 * 创建
 */
export const FourAPI_saveCreate = (key: FourKeyType, data: any) => {
  const urlObj = {
    1: 'cms/orderHouse/saveCreate',
    2: 'cms/orderHide/saveCreate',
    3: 'cms/register/saveCreate',
    4: 'cms/orderDel/saveCreate'
  }
  const url = urlObj[key]
  return http.post(url, data)
}

/**
 * 删除
 */
export const FourAPI_del = (key: FourKeyType, id: number) => {
  const urlObj = {
    1: `cms/orderHouse/remove/${id}`,
    2: `cms/orderHide/remove/${id}`,
    3: `cms/register/remove/${id}`,
    4: `cms/orderDel/remove/${id}`
  }
  const url = urlObj[key]
  return http.get(url)
}

/**
 * 撤回
 */
export const FourAPI_revocation = (key: FourKeyType, id: number) => {
  const urlObj = {
    1: `cms/orderHouse/revocation/${id}`,
    2: `cms/orderHide/revocation/${id}`,
    3: `cms/register/revocation/${id}`,
    4: `cms/orderDel/revocation/${id}`
  }
  const url = urlObj[key]
  return http.get(url)
}

/**
 * 点击保存（编辑） 提交（审批）
 */
export const FourAPI_saveApply = (key: FourKeyType, data: any) => {
  const urlObj = {
    1: 'cms/orderHouse/saveApply',
    2: 'cms/orderHide/saveApply',
    3: 'cms/register/saveApply',
    4: 'cms/orderDel/saveApply'
  }
  const url = urlObj[key]
  return http.post(url, data)
}

/**
 * 审批
 */
export const FourAPI_audit = (key: FourKeyType, data: any) => {
  const urlObj = {
    1: 'cms/orderHouse/audit',
    2: 'cms/orderHide/audit',
    3: 'cms/register/audit',
    4: 'cms/orderDel/audit'
  }
  const url = urlObj[key]
  return http.post(url, data)
}
