1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
const { graphqlRequest } = require('../../lib/utils')
 
function formatTime(dateString) {
  if (!dateString) return '-'
  const date = new Date(dateString)
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
  const hours = String(date.getHours()).padStart(2, '0')
  const minutes = String(date.getMinutes()).padStart(2, '0')
  return `${year}-${month}-${day} ${hours}:${minutes}`
}
 
const LIST_QUERY = `
  query EmployeeReviewApplications($keyword: String, $state: Int, $page: Int, $size: Int) {
    employeeReviewApplications(keyword: $keyword, state: $state, page: $page, size: $size) {
      content {
        id
        playerName
        projectName
        activityName
        state
        stateText
        stateType
        applyTime
      }
      totalElements
      page
      size
    }
  }
`
 
const STATS_QUERY = `
  query EmployeeReviewStats($keyword: String) {
    employeeReviewStats(keyword: $keyword) {
      pendingCount
      approvedCount
      rejectedCount
    }
  }
`
 
Page({
  data: {
    loading: false,
    loadingMore: false,
    hasMore: true,
    tabs: [
      { label: '待审核', value: 0 },
      { label: '已审核', value: 1 },
      { label: '驳回', value: 2 }
    ],
    currentTab: 0,
    searchKeyword: '',
    list: [],
    page: 1,
    pageSize: 10,
    stats: {
      pendingCount: 0,
      approvedCount: 0,
      rejectedCount: 0
    },
    employeeReviewStats: {
      pendingCount: 0,
      approvedCount: 0,
      rejectedCount: 0
    },
    needRefresh: false
  },
 
  onLoad() {
    this.initializeReviewData()
  },
 
  onShow() {
    if (this.data.needRefresh) {
      this.initializeReviewData()
      this.setData({ needRefresh: false })
    }
  },
 
  onPullDownRefresh() {
    this.initializeReviewData().finally(() => {
      wx.stopPullDownRefresh()
    })
  },
 
  onReachBottom() {
    if (this.data.hasMore && !this.data.loadingMore) {
      this.loadList(false)
    }
  },
 
  async initializeReviewData() {
    this.setData({ loading: true, page: 1, list: [], hasMore: true })
 
    try {
      await Promise.all([
        this.loadList(true),
        this.loadStats(this.data.searchKeyword)
      ])
    } catch (error) {
      console.error('初始化审核数据失败:', error)
    } finally {
      this.setData({ loading: false })
    }
  },
 
  async loadStats(keyword) {
    try {
      const variables = {}
      const trimmed = keyword && typeof keyword === 'string' ? keyword.trim() : ''
      if (trimmed) {
        variables.keyword = trimmed
      }
 
      const result = await graphqlRequest(STATS_QUERY, variables)
      if (result && result.employeeReviewStats) {
        this.setData({ stats: result.employeeReviewStats, employeeReviewStats: result.employeeReviewStats })
      }
    } catch (error) {
      console.error('加载审核统计失败:', error)
    }
  },
 
  async loadList(reset = false) {
    if (reset) {
      this.setData({ loading: true })
    } else {
      this.setData({ loadingMore: true })
    }
 
    const nextPage = reset ? 1 : this.data.page + 1
    const keywordInput = this.data.searchKeyword || ''
    const trimmedKeyword = keywordInput.trim()
    const variables = {
      keyword: trimmedKeyword ? trimmedKeyword : null,
      state: this.getStateByTab(this.data.currentTab),
      page: nextPage,
      size: this.data.pageSize
    }
 
    try {
      const result = await graphqlRequest(LIST_QUERY, variables)
      const pageData = result && result.employeeReviewApplications
      const items = pageData && Array.isArray(pageData.content) ? pageData.content : []
      items.forEach(item => {
        item.applyTime = formatTime(item.applyTime)
      })
      const list = reset ? items : this.data.list.concat(items)
      const total = pageData && typeof pageData.totalElements === 'number' ? pageData.totalElements : 0
      const hasMore = nextPage * this.data.pageSize < total
 
      this.setData({
        list,
        page: nextPage,
        hasMore
      })
    } catch (error) {
      console.error('加载审核列表失败:', error)
      wx.showToast({ title: '加载失败', icon: 'none' })
    } finally {
      if (reset) {
        this.setData({ loading: false })
      } else {
        this.setData({ loadingMore: false })
      }
    }
  },
 
  onSearchInput(e) {
    this.setData({ searchKeyword: e.detail.value || '' })
  },
 
  onSearch() {
    this.initializeReviewData()
  },
 
  clearSearch() {
    if (!this.data.searchKeyword) return
    this.setData({ searchKeyword: '' })
    this.initializeReviewData()
  },
 
  switchTab(e) {
    const index = Number(e.currentTarget.dataset.index || 0)
    if (index === this.data.currentTab) {
      return
    }
 
    this.setData({
      currentTab: index,
      page: 1,
      list: [],
      hasMore: true
    })
 
    this.initializeReviewData()
  },
 
  getStateByTab(index) {
    switch (index) {
      case 0:
        return 0
      case 1:
        return 1
      case 2:
        return 2
      default:
        return null
    }
  },
 
  goToDetail(e) {
    const id = e.currentTarget.dataset.id
    if (!id) {
      wx.showToast({ title: '报名ID无效', icon: 'none' })
      return
    }
 
    wx.navigateTo({
      url: `/pages/profile/employee-review-detail?id=${id}`,
      events: {
        auditUpdated: () => {
          this.setData({ needRefresh: true })
        }
      }
    })
  }
})