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

const STATUS_OPTIONS = [
  { value: '', label: '全部状态' },
  { value: 'published', label: '发布成功' },
  { value: 'blocked', label: '已拦截' }
]

const VISIBILITY_OPTIONS = [
  { value: '', label: '全部类型' },
  { value: 'public', label: '公开' },
  { value: 'private', label: '私有' },
  { value: 'direct', label: '私信' }
]

const VIS_LABEL = {
  public: '公开信息',
  private: '私有信息',
  direct: '用户私信'
}

const APPEAL_STATUS_TEXT = {
  none: '未发起',
  pending: '处理中',
  approved: '已通过',
  rejected: '已驳回'
}

Page({
  data: {
    loading: false,
    list: [],
    statusOptions: STATUS_OPTIONS,
    statusIndex: 0,
    visibilityOptions: VISIBILITY_OPTIONS,
    visibilityIndex: 0,
    appealPostId: null,
    appealReason: ''
  },

  formatPercent(value, digits = 2) {
    const num = Number(value || 0)
    return `${(num * 100).toFixed(digits)}%`
  },

  onShow() {
    this.fetchList()
  },

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

  onStatusChange(e) {
    this.setData({ statusIndex: Number(e.detail.value || 0) })
    this.fetchList()
  },

  onVisibilityChange(e) {
    this.setData({ visibilityIndex: Number(e.detail.value || 0) })
    this.fetchList()
  },

  async fetchList(fromPullDown = false) {
    this.setData({ loading: true })
    try {
      const status = this.data.statusOptions[this.data.statusIndex].value
      const visibility = this.data.visibilityOptions[this.data.visibilityIndex].value
      const query = `status=${encodeURIComponent(status)}&visibility=${encodeURIComponent(visibility)}&page=1&page_size=80`
      const data = await request({ url: `/content/posts/history?${query}` })
      const list = (data.items || []).map((item) => ({
        ...item,
        created_text: (item.created_at || '').replace('T', ' ').slice(0, 19),
        spam_probability_text: this.formatPercent(item.spam_probability, 2),
        visibility_text: VIS_LABEL[item.visibility] || item.visibility,
        appeal_status_text: APPEAL_STATUS_TEXT[item.appeal_status] || item.appeal_status
      }))
      this.setData({ list })
    } finally {
      this.setData({ loading: false })
      if (fromPullDown) wx.stopPullDownRefresh()
    }
  },

  startAppeal(e) {
    const postId = Number(e.currentTarget.dataset.id)
    this.setData({ appealPostId: postId, appealReason: '' })
  },

  onAppealInput(e) {
    this.setData({ appealReason: e.detail.value || '' })
  },

  cancelAppeal() {
    this.setData({ appealPostId: null, appealReason: '' })
  },

  async submitAppeal() {
    const postId = this.data.appealPostId
    if (!postId) return
    const reason = (this.data.appealReason || '').trim()
    if (reason.length < 2) {
      wx.showToast({ title: '申诉理由至少 2 个字符', icon: 'none' })
      return
    }

    await request({
      url: `/content/posts/${postId}/appeal`,
      method: 'POST',
      data: { reason }
    })
    wx.showToast({ title: '申诉提交成功', icon: 'success' })
    this.setData({ appealPostId: null, appealReason: '' })
    this.fetchList()
  },

  removeItem(e) {
    const id = Number(e.currentTarget.dataset.id)
    wx.showModal({
      title: '删除确认',
      content: '确定删除这条发布记录吗？',
      success: async (res) => {
        if (!res.confirm) return
        await request({ url: `/content/posts/${id}`, method: 'DELETE' })
        wx.showToast({ title: '删除成功', icon: 'success' })
        this.fetchList()
      }
    })
  }
})
