const { request } = require('../../utils/request')

Page({
  data: {
    loading: false,
    list: []
  },

  onShow() {
    this.fetchList()
  },

  onPullDownRefresh() {
    this.fetchList(true)
  },

  async fetchList(fromPullDown = false) {
    this.setData({ loading: true })
    try {
      const data = await request({ url: '/content/posts/inbox?page=1&page_size=80' })
      const list = (data.items || []).map((item) => ({
        ...item,
        created_text: (item.created_at || '').replace('T', ' ').slice(0, 19)
      }))
      this.setData({ list })
    } finally {
      this.setData({ loading: false })
      if (fromPullDown) wx.stopPullDownRefresh()
    }
  }
})
