import store, { AppDispatch } from ".."
import http from "@/utils/http"
import { HomeSortType } from "@/types"

// 获取分类
export const getSortAction = () => {

  return async (dispatch: AppDispatch) => {
    const res = await http.post('/api/show/getTree', { type: 'texture' })
    let data = res.data.map((v: HomeSortType) => ({
      ...v,
      value: v.id,
      label: v.name
    }))
    data.unshift({ value: -1, label: '全部分类' })
    dispatch({ type: 'home/setSort', payload: data })
  }
}

// 获取年代
export const getAgeAction = () => {

  return async (dispatch: AppDispatch) => {
    const res = await http.post('/api/show/getTree', { type: 'age' })

    let data = res.data.map((v: HomeSortType) => ({
      ...v,
      value: v.id,
      label: v.name
    }))
    data.unshift({ value: -1, label: '全部年代' })
    dispatch({ type: 'home/setAge', payload: data })
  }
}


// 获取列表数据
export const getListAction = (data: any, flag?: boolean) => {

  return async (dispatch: AppDispatch) => {
    const res = await http.post('/api/show/goods/list', data)
    let list
    if (flag) list = [...store.getState().homeStore.modelInfo.list, ...res.data.records]
    else list = res.data.records

    const total = res.data.total
    dispatch({ type: 'home/setList', payload: { list, total } })

  }
}


// 扫码进页面获取的单个文物详情
export const getModelInfo = (id: string) => {
  return async (dispatch: AppDispatch) => {
    const res = await http.get(`/api/show/goods/detail/${id}`)
    dispatch({ type: 'home/Model', payload: res.data })
  }
}


