import { statusObj } from '@/utils/dataChange'
import http from '@/utils/http'
import { MessageFu } from '@/utils/message'
import store, { AppDispatch } from '..'

/**
 * 征集管理页面点击新增
 */
export const object7AddAPI = (data?: any) => {
  return http.post('cms/collect/save', { ...data })
}

/**
 * 里面的一条藏品添加
 */
export const goodsSonAddAPIZJ = (data: any, id: any, isZJbyDJ?: boolean): any => {
  return async (dispatch: AppDispatch) => {
    const res: any = await http.post('cms/collect/goods/save', data)

    if (res.code === 0) {
      const oldData = isZJbyDJ
        ? store.getState().object1Store.isZJListAc
        : store.getState().loginStore.goodsTableListZJ
      MessageFu.success('操作成功!')
      let newData
      if (id) {
        // 是编辑 覆盖之前的数据
        newData = oldData.map((v: any) => {
          if (v.id === id) return res.data
          else return v
        })
      } else newData = [res.data, ...oldData] // 是新增，在前面添加一条数据
      dispatch({
        type: isZJbyDJ ? 'object1/getIsZJListAc' : 'login/setGoodsSonListZJ',
        payload: newData
      })
    }
  }
}

/**
 * 获取征集管理列表信息
 */
export const getObject7List = (data: any): any => {
  return async (dispatch: AppDispatch) => {
    // 获取列表数据
    const res: any = await http.post('cms/collect/pageList', data)
    const list = res.data.records
    list.forEach((v: any) => {
      v.statusTxt = statusObj[v.status]
    })
    const obj = {
      list,
      total: res.data.total
    }
    dispatch({ type: 'object7/getList', payload: obj })
  }
}

/**
 * 获取征集管理列表顶部数字信息
 */
export const getObject7ListNum = (): any => {
  return async (dispatch: AppDispatch) => {
    // 获取统计数据
    const res: any = await http.get('cms/collect/countByStatus')

    const data = [
      { id: null, name: '全部', num: res.data.all ? res.data.all : 0 },
      { id: 0, name: '待办理', num: res.data[0] ? res.data[0] : 0 },
      { id: 1, name: '待审核', num: res.data[1] ? res.data[1] : 0 },
      { id: 2, name: '审核不通过', num: res.data[2] ? res.data[2] : 0 },
      { id: 3, name: '已完成', num: res.data[3] ? res.data[3] : 0 }
    ]
    dispatch({ type: 'object7/getListNum', payload: data })
  }
}

/**
 * 删除外层表格数据
 */
export const object7DelAPI = (id: number) => {
  return http.get(`cms/collect/remove/${id}`)
}

/**
 * 通过id获取信息
 */
export const object7infoOutAPI = (id: number) => {
  return http.get(`cms/collect/detail/${id}`)
}
/**
 * 通过id获取表格信息
 */
export const getObj7InfoTableAPI2 = (id: number) => {
  return http.get(`cms/collect/goods/list/${id}`)
}
/**
 * 通过id获取表格信息
 */
export const getObj7InfoTableAPI = (id: number): any => {
  return async (dispatch: AppDispatch) => {
    // 获取统计数据
    const res: any = await http.get(`cms/collect/goods/list/${id}`)
    dispatch({ type: 'login/setGoodsSonListZJ', payload: res.data })
  }
}

/**
 * 删除里面的表格藏品 s
 */
export const delInTablesAPI7 = (ids: string) => {
  return http.get(`cms/collect/goods/removes/${ids}`)
}

/**
 * 删除最里面的附件或者封面图 s
 */
export const delInfileAPI7 = (ids: string) => {
  return http.get(`cms/collect/goods/file/dels/${ids}`)
}

/**
 * 通过id获取里面的藏品详情
 */
export const getInfoInAPI7 = (id: number) => {
  return http.get(`cms/collect/goods/detail/${id}`)
}

/**
 * 审核藏品信息
 */
export const auditObject7API = (data: any) => {
  return http.post('cms/collect/audit', data)
}
