<template>
	<view>
		<u-loading-page :loading="getLoading" loading-text="题库更新中..."></u-loading-page>
		<view v-if="!getLoading">
			<view class="chapter_item p14" v-for="(item,index) of chapterList" :key="index" @tap="toQuestion(item.configItemCode)">
				{{item.configItemName}}
			</view>
		</view>
	</view>
</template>

<script>
	import {
		querySysConfigList,
		queryQuestionId
	} from '@/jtools/api/question';
	import {
		mapState,
		mapActions
	} from 'pinia' //引入映射函数
	import useQuestionStore from '@/jtools/store/question' //引入store
	import storage from '@/jtools/storage';
	export default {
		data() {
			return {
				chapterList:[],
				subject:'1'
			}
		},
		onLoad(op){
			if(op.subject){
				this.subject=op.subject
			}
			this.getChapterList()
		},
		computed: {
			...mapState(useQuestionStore, ["loading_subject4", "loading_subject1", "version"]), //映射函数，取出tagslist
			getLoading() {
				return this.loading_subject4 && this.loading_subject1
			}
		},
		methods:{
			...mapActions(useQuestionStore, ['getAllQuestion']),
			getQuestion(param,title) {
						queryQuestionId({
							versionId: this.version,
							carTypeId: storage.get('carType') || '1001',
							subject: this.subject,
							...param,
						}).then(async (resp) => {
								if (resp.code === '0000') {
									if(resp.data&&resp.data.length){
										const arr = resp.data
										const listJson = JSON.stringify(arr)
										uni.navigateTo({
											url: "/pages/questionBank/questionBank?navTitle=" + title + "&subject=" + this.subject + "&questionIdList=" + listJson
										})
									}else{
										uni.showToast({
											title: '暂无题目',
											icon: 'none'
										})
									}
								}else if (resp.code === '4001') {
							uni.showToast({
								title: '当前题库非最新版，请更新~',
								icon: 'none'
							})
							this.getAllQuestion()
						}
					})
			},
			getChapterList(){
				const carTypeId=storage.get('carType') || '1001'
				const key=this.subject=='1'?'ChapterOfSubjectOne':'ChapterOfSubjectFour'
				querySysConfigList(carTypeId,'ChapterOfSubjectOne').then(resp=>{
					if(resp.code==='0000'){
						this.chapterList=resp.data
					}
				})
			},
			toQuestion(code){
				this.getQuestion({chapter:code},'章节技巧')
			}
		}
	}
</script>

<style scoped>
	.chapter_item{
		border-bottom: 1px solid rgb(210,209,214);
		background-color: #fff;
	}
	.chapter_item:hover{
		background-color: rgb(210,209,214);
	}
</style>