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

/**
 * 藏品登记页面点击新增
 */
export const object1AddAPI = (data?: any) => {
  return http.post('cms/register/save', { ...data })
}

/**
 * 藏品登记页面上传封面图和附件
 */

// 上传附件的进度条
const UpAsyncLodingDom: any = document.querySelector('#UpAsyncLoding')
const progressDom: any = document.querySelector('#progress')

export const object1AddUpFileAPI = (data: any) => {
  UpAsyncLodingDom.style.opacity = 1

  return http.post('cms/register/goods/file/upload', data, {
    timeout: 30000,
    // 显示进度条
    onUploadProgress: (e: any) => {
      const complete = (e.loaded / e.total) * 100 || 0
      progressDom.style.width = complete + '%'
    }
  })
}

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

    if (res.code === 0) {
      const oldData = store.getState().loginStore.goodsTableList
      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: 'login/setGoodsSonList', payload: newData })
    }
  }
}

/**
 * 获取藏品登记列表信息
 */
export const getObject1List = (data: any): any => {
  return async (dispatch: AppDispatch) => {
    // 获取列表数据
    const res: any = await http.post('cms/register/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: 'object1/getList', payload: obj })
  }
}

/**
 * 获取藏品登记列表顶部数字信息
 */
export const getObject1ListNum = (): any => {
  return async (dispatch: AppDispatch) => {
    // 获取统计数据
    const res: any = await http.get('cms/register/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: 'object1/getListNum', payload: data })
  }
}

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

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

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

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

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

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