import {
	defineStore
} from 'pinia';
import http from '@/jtools/request/index';
import {
	queryQuestion
} from '@/jtools/api/question';
import storage from '@/jtools/storage';

const question = defineStore({
	id: 'question',
	state: () => ({
		currentCartype: storage.get('carType') || '1001',
		orderQuestion_subject1: [], //科目一顺序做题
		orderQuestion_subject4:[],//科目二顺序做题
		currentIndex_subject1:0,//科目一索引 顺序做题
		currentIndex_subject4:0,//科目四索引 顺序做题
		curSubject:storage.get('curSubject') || '1'
	}),

	actions: {
		//改变当前科目
		changeSubject(val){
			this.curSubject=val
			storage.set('curSubject',val)
		},
		// 获取顺序做题科目1
		getOrderQuestion_sub1() {
				this.curSubject='1'
				storage.set('curSubject','1')
			if(!this.orderQuestion_subject1.length){
				queryQuestion({
					carTypeId: this.currentCartype,
					subject: '1',
					// questionIdList:[10982,10983,10985,10986]
				}).then(res => {
					if (res.code == '0000') {
						this.orderQuestion_subject1 = res.data
						const falseList =storage.get('wrongList_subject1') || []
						const trueList =storage.get('rightList_subject1')|| []
						const falseArr=[]
						const rightArr=[]
						this.orderQuestion_subject1.forEach(item=>{
							if(falseList.includes(item.questionId)){
								falseArr.push(item.questionId)
							}
							if(trueList.includes(item.questionId)){
								rightArr.push(item.questionId)
							}
						})
						storage.set('wrongList_subject1',falseArr)
						storage.set('rightList_subject1',rightArr)
					}
				})
			}
		},
		// 获取顺序做题科目4
		getOrderQuestion_sub4() {
				this.curSubject='4'
				storage.set('curSubject','4')
			if(!this.orderQuestion_subject4.length){
				queryQuestion({
					carTypeId: this.currentCartype,
					subject: '4',
					// questionIdList:[10982,10983,10985,10986]
				}).then(res => {
					if (res.code == '0000') {
						this.orderQuestion_subject4 = res.data
						const falseList =storage.get('wrongList_subject4') || []
						const trueList =storage.get('rightList_subject4')|| []
						const falseArr=[]
						const rightArr=[]
						this.orderQuestion_subject4.forEach(item=>{
							if(falseList.includes(item.questionId)){
								falseArr.push(item.questionId)
							}
							if(trueList.includes(item.questionId)){
								rightArr.push(item.questionId)
							}
						})
						storage.set('wrongList_subject4',falseArr)
						storage.set('rightList_subject4',rightArr)
					}
				})
			}
		},
		//获取索引
		getCurrentIndex(index,val){
			this[`currentIndex_subject${val}`]=index
		}
	}
});

export default question;