TaskFilter.vue
4.91 KB
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
<template>
<div class="task-filter bg-white">
<div class="flex gap-2">
<!-- 大作业选择 -->
<div class="flex-1 min-w-0" @click="showTaskPicker = true">
<div class="flex items-center justify-between bg-gray-50 rounded px-3 py-2 border border-gray-100">
<span class="text-sm text-gray-700 truncate mr-1">{{ selectedTaskName || '全部作业' }}</span>
<van-icon name="arrow-down" color="#9ca3af" size="12" />
</div>
</div>
<!-- 小作业选择 -->
<div class="flex-1 min-w-0" @click="handleSubtaskClick">
<div class="flex items-center justify-between bg-gray-50 rounded px-3 py-2 border border-gray-100"
:class="{ 'opacity-50': !selectedTaskId }">
<span class="text-sm text-gray-700 truncate mr-1">{{ selectedSubtaskName || '全部小作业' }}</span>
<van-icon name="arrow-down" color="#9ca3af" size="12" />
</div>
</div>
</div>
<!-- 大作业弹窗 -->
<van-popup v-model:show="showTaskPicker" position="bottom" round>
<van-picker
:columns="taskColumns"
@confirm="onConfirmTask"
@cancel="showTaskPicker = false"
show-toolbar
title="选择作业"
/>
</van-popup>
<!-- 小作业弹窗 -->
<van-popup v-model:show="showSubtaskPicker" position="bottom" round>
<van-picker
:columns="subtaskColumns"
@confirm="onConfirmSubtask"
@cancel="showSubtaskPicker = false"
show-toolbar
title="选择小作业"
/>
</van-popup>
</div>
</template>
<script setup>
import { ref, computed, watch, onMounted } from 'vue'
import { getTeacherTaskListAPI, getTeacherTaskDetailAPI } from '@/api/teacher'
import { showToast } from 'vant'
const props = defineProps({
groupId: {
type: [String, Number],
default: ''
}
})
const emit = defineEmits(['change', 'popup-visible-change'])
// 状态
const showTaskPicker = ref(false)
const showSubtaskPicker = ref(false)
// 监听弹窗显示状态
watch([showTaskPicker, showSubtaskPicker], ([showTask, showSubtask]) => {
emit('popup-visible-change', showTask || showSubtask)
})
const taskList = ref([])
const subtaskList = ref([])
const selectedTaskId = ref('')
const selectedSubtaskId = ref('')
// 计算属性 - 选项列表
const taskColumns = computed(() => {
const list = taskList.value.map(item => ({ text: item.title, value: item.id }))
return [{ text: '全部作业', value: '' }, ...list]
})
const subtaskColumns = computed(() => {
const list = subtaskList.value.map(item => ({ text: item.title, value: item.id }))
return [{ text: '全部小作业', value: '' }, ...list]
})
// 计算属性 - 显示名称
const selectedTaskName = computed(() => {
if (!selectedTaskId.value) return '全部作业'
const found = taskList.value.find(item => item.id === selectedTaskId.value)
return found ? found.title : ''
})
const selectedSubtaskName = computed(() => {
if (!selectedSubtaskId.value) return '全部小作业'
const found = subtaskList.value.find(item => item.id === selectedSubtaskId.value)
return found ? found.title : ''
})
// 获取大作业列表
const fetchTaskList = async () => {
if (!props.groupId) {
taskList.value = []
return
}
try {
const res = await getTeacherTaskListAPI({
group_id: props.groupId,
limit: 1000,
page: 0
})
if (res.code === 1) {
taskList.value = res.data || []
}
} catch (error) {
console.error('获取作业列表失败:', error)
}
}
// 监听 groupId 变化
watch(() => props.groupId, (newVal) => {
fetchTaskList()
}, { immediate: true })
// 获取大作业详情(含小作业)
const fetchTaskDetail = async (taskId) => {
if (!taskId) {
subtaskList.value = []
return
}
try {
const res = await getTeacherTaskDetailAPI({ id: taskId })
if (res.code === 1) {
subtaskList.value = res.data.subtask_list || []
}
} catch (error) {
console.error('获取作业详情失败:', error)
}
}
// 事件处理
const onConfirmTask = async ({ selectedOptions }) => {
const option = selectedOptions[0]
if (selectedTaskId.value !== option.value) {
selectedTaskId.value = option.value
// 重置小作业
selectedSubtaskId.value = ''
subtaskList.value = []
if (option.value) {
await fetchTaskDetail(option.value)
}
emitChange()
}
showTaskPicker.value = false
}
const onConfirmSubtask = ({ selectedOptions }) => {
const option = selectedOptions[0]
selectedSubtaskId.value = option.value
emitChange()
showSubtaskPicker.value = false
}
const handleSubtaskClick = () => {
if (!selectedTaskId.value) {
showToast('请先选择作业')
return
}
showSubtaskPicker.value = true
}
const emitChange = () => {
emit('change', {
task_id: selectedTaskId.value,
subtask_id: selectedSubtaskId.value
})
}
</script>
<style lang="less" scoped>
.task-filter {
// 可以在这里添加额外的样式
}
</style>