import { getMoodList } from '@/api'

export default {
  data() {
    return {
      moodList: [],
      moodPage: 1,
      moodPageSize: 20,
      moodTotal: 0,
      moodLoading: false,
      moodHasMore: true
    }
  },
  methods: {
    async loadMoodList(params = {}) {
      if (this.moodLoading) return
      this.moodLoading = true
      try {
        const res = await getMoodList({
          page: this.moodPage,
          pageSize: this.moodPageSize,
          ...params
        })
        const list = res?.list || res?.data || []
        this.moodTotal = res?.total || list.length
        if (this.moodPage === 1) {
          this.moodList = list
        } else {
          this.moodList = [...this.moodList, ...list]
        }
        this.moodHasMore = this.moodList.length < this.moodTotal
        return list
      } catch (e) {
        console.error('加载心情列表失败:', e)
        return []
      } finally {
        this.moodLoading = false
      }
    },
    async refreshMoodList(params = {}) {
      this.moodPage = 1
      this.moodHasMore = true
      return this.loadMoodList(params)
    },
    async loadMoreMood(params = {}) {
      if (!this.moodHasMore || this.moodLoading) return
      this.moodPage++
      return this.loadMoodList(params)
    },
    resetMoodList() {
      this.moodList = []
      this.moodPage = 1
      this.moodTotal = 0
      this.moodHasMore = true
    },
    removeMoodItem(id) {
      this.moodList = this.moodList.filter(item => item.id !== id)
      this.moodTotal = Math.max(0, this.moodTotal - 1)
    }
  }
}