import { defineStore } from 'pinia'
import { getGameList } from '@/api/index'
import Message from '@/utils/Message'

interface Params {
    keywords:string
    pagesize:number
    page:number
    tag_id:number
    type:number
  }
export const search = defineStore('search', {
  // 推荐使用 完整类型推断的箭头函数
  // persist: true, // 持久化储存
  state: () => {
    return {
      // 所有这些属性都将自动推断其类型
      params: {
        keywords: '',
        pagesize: 10,
        page: 1,
        tag_id: 0,
        type: 0
      } as Params,
      prefix: '',
      total: 0,
      searchList: [] as any[]
    }
  },
  getters: {

  },
  actions: {
    loadMore() {
      this.params.page += 1
      this.getData()
    },
    async onSearch() {
      this.params.page = 1
      this.searchList = []
      await this.getData()
    },
    async getData() {
      // from 2 手机登录   1密码登录
      await getGameList(this.params).then((res) => {
        if (res.data.code === 200 && res.data.data) {
          console.log('res', res)
          this.prefix = res.data.data.prefix
          this.total = res.data.data.total
          this.searchList = this.searchList.concat(res.data.data.lists || [])
        }
      }).catch((error) => {
        console.log(error)
        Message.error(error.data.msg)
      })
    }
  }
})
