import { 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' })

    const temp = [] as any
    res.data.forEach((v: any) => {
      v.children.forEach((p: any) => {
        temp.push({ value: p.id, label: p.name })
      })
    })

    let data = temp
    data.unshift({ value: -1, label: '全部年代' })
    dispatch({ type: 'home/setAge', payload: data })
  }
}


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

  return async (dispatch: AppDispatch) => {
    const res = await http.post('/api/show/goods/list', data)
    const list = res.data.records
    const total = res.data.total
    dispatch({ type: 'home/setList', payload: { list, total } })

  }
}


