<template>
	<view>
		<!-- 答题组件 -->
		<kz-question v-if="questions" mode="TRAINING" :title="cateName" :questions="questions" :questionCount="questionCount" :pageCount="pageCount" :currentPage="currentPage" :viewMode="mode" @loadQuestion="getQuestion"></kz-question>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				cateId: 0,
				cateName: '',
				questions: [],
				questionCount: 0,
				pageCount: 100,
				currentPage: 0,
				lastPage: 0,
				mode: 'normal',
			}
		},
		onLoad(e) {
			this.cateId = e.cateId
			this.cateName = e?.cateName
			this.mode = e.mode
			this.getQuestion()
		},
		methods: {
			// 获取试题（包括延迟获取）
			getQuestion(page = 1, callback = null) {
				console.log('getQuestion', page, this.currentPage, this.lastPage)
				// 避免重复获取
				if (page <= this.currentPage || (this.lastPage && page > this.lastPage)) {
					return
				}
				
				// 请求参数
				let params = {
					page: page,
					page_count: this.pageCount,
					cate_id: this.cateId,
					mode: this.mode
				}
				
				this.http('question/train', params).then(res => {
					// 默认分页方式
				
					if (this.mode == 'normal') {
						this.questionCount = res.data.total
						this.currentPage = res.data.current_page
						this.lastPage = res.data.last_page
						this.questions = this.questions.concat(res.data.data)
					} else {
						// 记忆、随机模式暂时只能获取全部题目了
						this.questions = res.data.data
					}
					
					if (callback) {
						callback()
					}
				})
			}
		}
	}
</script>

<style>

</style>
