/* AI_CHANGELOG
| 日期 | Prompt | 变更 |
|------|--------|------|
| 2026-03-23 | 角色路由+页面权限守卫 | onShow 添加 checkPageAccess 权限守卫 |
*/
import { checkPageAccess } from '../../utils/auth-guard'
import { fetchNotes, deleteNote } from '../../services/api'
import type { Note } from '../../utils/mock-data'
import { formatRelativeTime } from '../../utils/time'

/** 带展示时间的备注项 */
interface NoteDisplay extends Note {
  timeLabel: string
}

/** 每页条数 */
const PAGE_SIZE = 20

Page({
  data: {
    pageState: 'loading' as 'loading' | 'empty' | 'error' | 'normal',
    notes: [] as NoteDisplay[],
    /** 系统状态栏高度（px），用于自定义导航栏顶部偏移 */
    statusBarHeight: 20,
    /** 当前页码 */
    page: 1,
    /** 是否正在加载更多 */
    isLoadingMore: false,
    /** 是否还有更多数据 */
    hasMore: true,
  },

  onLoad() {
    const sysInfo = wx.getWindowInfo()
    this.setData({ statusBarHeight: sysInfo.statusBarHeight || 20 })
    this.loadData()
  },

  onShow() {
    // 权限守卫：检查登录状态、账号禁用、角色权限
    checkPageAccess('pages/notes/notes')
  },

  /** 首次加载 / 刷新：重置分页，从第 1 页开始 */
  async loadData() {
    this.setData({ pageState: 'loading', page: 1, hasMore: true })
    wx.showLoading({ title: '加载中...', mask: true })

    try {
      const res = await fetchNotes({ page: 1, pageSize: PAGE_SIZE })
      const notes: NoteDisplay[] = res.notes.map((n) => ({
        ...n,
        timeLabel: formatRelativeTime(n.createdAt),
      }))
      const hasMore = res.hasMore !== false && notes.length >= PAGE_SIZE
      this.setData({
        pageState: notes.length > 0 ? 'normal' : 'empty',
        notes,
        hasMore,
      })
    } catch {
      this.setData({ pageState: 'error' })
    } finally {
      wx.hideLoading()
    }
  },

  /** 触底加载更多 */
  async onReachBottom() {
    if (this.data.isLoadingMore || !this.data.hasMore) return

    const nextPage = this.data.page + 1
    this.setData({ isLoadingMore: true })

    try {
      const res = await fetchNotes({ page: nextPage, pageSize: PAGE_SIZE })
      const newNotes: NoteDisplay[] = res.notes.map((n) => ({
        ...n,
        timeLabel: formatRelativeTime(n.createdAt),
      }))
      const hasMore = res.hasMore !== false && newNotes.length >= PAGE_SIZE
      this.setData({
        notes: [...this.data.notes, ...newNotes],
        page: nextPage,
        hasMore,
        isLoadingMore: false,
      })
    } catch {
      // 加载更多失败时不改变页面状态，仅停止 loading
      this.setData({ isLoadingMore: false })
      wx.showToast({ title: '加载失败', icon: 'none' })
    }
  },

  /** 返回上一页 */
  onBack() {
    wx.navigateBack()
  },

  /** 错误态重试 */
  onRetry() {
    this.loadData()
  },

  /** 删除备注 */
  onDeleteNote(e: WechatMiniprogram.BaseEvent) {
    const noteId = e.currentTarget.dataset.id as string
    wx.showModal({
      title: '删除备注',
      content: '确定要删除这条备注吗？删除后无法恢复。',
      confirmColor: '#e34d59',
      success: async (res) => {
        if (res.confirm) {
          try {
            await deleteNote(noteId)
          } catch {
            // 静默：mock 阶段可能失败，不影响 UI
          }
          const notes = this.data.notes.filter((n) => n.id !== noteId)
          this.setData({
            notes,
            pageState: notes.length > 0 ? 'normal' : 'empty',
          })
          wx.showToast({ title: '已删除', icon: 'success' })
        }
      },
    })
  },

  /** 下拉刷新：重置分页重新加载 */
  async onPullDownRefresh() {
    await this.loadData()
    wx.stopPullDownRefresh()
  },
})
