import http from '@/utils/http'
import { AppDispatch } from '@/store'
import { Typetable } from '@/pages/Zother/data'
interface uploadExcelReq {
  file: any
  type: string
  dirCode?: string
  isCompress?: boolean
  moduleId?: number
  usage?: string
}

interface ImportZipReq {
  description?: string
  effect?: any
  fileName?: string
  filePath?: string
  id?: number
  importError?: string
  importId?: number
  isTrue?: boolean
  num?: number
  type?: string
}

interface ImportRecordReq {
  fileName: string
  filePath: string
  pcsError: number
  pcsSuccess: number
  pcsTotal: number
  type: string
}
/**
 * 藏品总账-获取藏品详情
 */
export const API_getGoodsInfo = (id: number) => {
  return http.get(`cms/good/ledger/detail/${id}`)
}

/**
 * 藏品总账-获取藏品分页列表
 */
export const API_getGoodsList = (data: any): any => {
  return async (dispatch: AppDispatch) => {
    const res = await http.post(`cms/good/ledger/page`, data)
    if (res.code === 0) {
      const obj = {
        list: res.data.records,
        total: res.data.total
      }

      dispatch({ type: 'C1/getList', payload: obj })
    }
  }
}

/**
 * 藏品总账-获取藏品全部数据
 */
export const API_getGoodsListAll = () => {
  return http.post(`cms/good/ledger/page`, {
    pageSize: 9999,
    pageNum: 1,
    businessStartTime: '',
    businessEndTime: '',
    searchKey: '',
    startTime: '',
    endTime: '',
    status: '',
    typeDictId: '',
    ageDictId: '',
    textureDictId: ''
  })
}

/**
 * 藏品总账-获取藏品列表
 */
export const API_getGoodsLists = (data: any): any => {
  return http.post(`cms/good/ledger/page`, data)
}

/**
 * 藏品总账-导入数据分页
 */
export const API_getImportExcelList = (data: any): any => {
  return async (dispatch: AppDispatch) => {
    const res = await http.post(`cms/importExcel/pageList`, { ...data, type: 'excel' })
    if (res.code === 0) {
      const obj = {
        list: res.data.records,
        total: res.data.total
      }

      dispatch({ type: 'C1/getImportExcelList', payload: obj })
    }
  }
}

/**
 * 藏品总账-导入图片分页
 */
export const API_getImportZipList = (data: any): any => {
  return async (dispatch: AppDispatch) => {
    const res = await http.post(`cms/importZip/pageList`, { ...data, type: 'zip' })
    if (res.code === 0) {
      const obj = {
        list: res.data.records,
        total: res.data.total
      }

      dispatch({ type: 'C1/getImportZipList', payload: obj })
    }
  }
}

/**
 * 藏品总账-导入数据上传附件excel（FormData 格式，用于文件上传+校验）
 */
export const API_uploadImportExcel = (formData: FormData): any => {
  return http.post(`cms/importExcel/uploadExcel`, formData)
}

/**
 * 藏品总账-导入图片上传附件excel zip/rar
 */
export const API_uploadImportZip = (data: uploadExcelReq): any => {
  return http.post(`cms/importZip/uploadZip`, data)
}

/**
 * 藏品总账-导入数据-导入校验的数据
 */
export const API_addImportExcel = (data: Typetable): any => {
  return http.post(`cms/importExcel/addRow`, data)
}

/**
 * 藏品总账-导入图片-导入解压后的图片信息
 */
export const API_addImportZip = (data: ImportZipReq): any => {
  return http.post(`cms/importZip/addZipData`, data)
}

/**
 * 藏品总账-导入数据-新增导入上传附件记录
 */
export const API_addImportExcelRecord = (data: ImportRecordReq): any => {
  return http.post(`cms/importExcel/add`, data)
}

/**
 * 藏品总账-导入图片-新增导入上传附件记录
 */
export const API_addImportZipRecord = (data: ImportRecordReq): any => {
  return http.post(`cms/importZip/add`, data)
}

/**
 * 藏品总账-导入数据-查看导入结果
 */
export const API_getImportExcelResult = (id: number): any => {
  return http.get(`cms/importExcel/detail/${id}`)
}

/**
 * 藏品总账-导入数据-删除导入结果
 */
export const API_deleteImportExcelResult = (id: number): any => {
  return http.get(`cms/importExcel/remove/${id}`)
}

/**
 * 藏品总账-导入图片-查看导入结果
 */
export const API_getImportResult = (id: number): any => {
  return http.get(`cms/importZip/detail/${id}`)
}

/**
 * 藏品总账-导入图片-删除导入结果
 */
export const API_deleteImportResult = (id: number): any => {
  return http.get(`cms/importZip/remove/${id}`)
}

/**
 * 藏品-附件-根据藏品Id查询
 */
type GetGoodsByGoodIdReq = {
  moduleId: number
  moduleName: string
  type: string
}
export const API_getGoodsByGoodId = (data: GetGoodsByGoodIdReq) => {
  return http.post(`cms/good/ledger/file`, data)
}

// ----------------故事模块-----------
/**
 * 故事-新增
 */
export const guShiAPI_add = (data: any) => {
  return http.post('cms/story/save', data)
}
/**
 * 故事-获取列表（完整）
 */
export const guShiAPI_getList = (goodId: number) => {
  const data: any = {
    goodId,
    pageSize: 99999
  }
  return http.post('cms/good/ledger/story', data)
}
/**
 * 故事-详情
 */
export const guShiAPI_getInfo = (id: number) => {
  return http.get(`cms/story/detail/${id}`)
}

/**
 * 故事-删除
 */
export const guShiAPI_dels = (ids: number[]) => {
  return http.post(`cms/story/removes`, ids)
}

// ------------------藏品详情模块

// 藏品-库存信息
export const API_GIsiteInfo = (siteId: number) => {
  return http.get(`cms/good/ledger/siteInfo/${siteId}`)
}

// 藏品-附件设置
export const API_GIfileType = (data: any) => {
  return http.post('cms/good/ledger/setFileEffect', data)
}

// 藏品-日志
export const API_GIlog = (data: any) => {
  return http.post('cms/good/ledger/log', data)
}
