<script lang="tsx">
import { defineComponent,ref } from "vue";
import store from "@/store";
import { Dialog, Toast } from "vant";
import { useRouter } from "vue-router";
import { json } from "stream/consumers";
import { Console } from "console";
import { RouterBus } from "@/hooks";
export default defineComponent({
	props: {
		listType:{
			type:String,
			default:""
		},
		subjectList:{
			type:Array,
			default:[]
		},
		currentSubjectIndex: {
			type: Number,
			default: 0,
		},
		subjectTotal: {
			type: Number,
			default: 0,
		},

		officialShow: Boolean,
		showSubjectChangePopup: Boolean,
	},
	emits: {
		"update:officialShow": (value: boolean) => {
			return value;
		},
		"redo":()=>{

		},
		"update:showSubjectChangePopup": (value: boolean) => {
			return value;
		},
		nextSubject: () => {},
		lastSubject: () => {},
		
	},
	setup(props, { emit }) {
		const router = useRouter();
		const vehicle = useRouter().currentRoute.value.query.vehicle as string; //路由qvehicle参数
		const subject = useRouter().currentRoute.value.query.subject as string; //路由subject参数
		const isVip = store.getters.getIsVip;
			const { goWrongReview } = new RouterBus();
			const submitResVisible = ref(false)
			const trueNum = ref(0)
			const falseNum = ref(0)
		console.log(router.currentRoute.value);
		// const vipClick = () => {
		// 	if (store.getters.getIsVip || router.currentRoute.value.path == "/exerciseFree") {
		// 		emit("update:officialShow", true);
		// 	} else {
		// 		Dialog.confirm({
		// 			title: "温馨提示",
		// 			message: "请购买会员后使用",
		// 			confirmButtonText: "购买",
		// 		}).then(() => {
		// 			router.push("/buyVip");
		// 		});
		// 	}
		// };
		const goWrongPreviewScore = () => {
			switch(props.listType){
				//本地的错误保存
				case "localWrong":
					
			Dialog.confirm({
				message: "确认交卷吗?",
			})
				.then(() => {
				let wrongSet =	props.subjectList.filter((item:any,index)=>{
					let answers = item.answer.split("-")
						return JSON.stringify(answers.sort()) == JSON.stringify(typeof item.userAnswer === 'string' ? [item.userAnswer] : item.userAnswer.sort())

					})
					window.localStorage.setItem(vehicle+subject,JSON.stringify(wrongSet))
					
					
					


				})
				.catch(() => {
					console.log(props.subjectList,"props")
				});

				break;
				//本地的错误保存

				//线上错题本的保存
				case "wrong":

						Dialog.confirm({
				message: "确认交卷吗?",
			})
				.then(() => {
					router.push({
						path:"/wrongExerciseRes",
						query:{
							title:"错题重做",
							...router.currentRoute.value.query
						}
					})
				})
				.catch(() => {
					
				});


				break;
				//线上错题本的保存
				case "collection":
								Dialog.confirm({
				message: "确认交卷吗?",
			})
				.then(() => {
								
					router.push({
						path:"/collectionExerciseRes",
						query:{
							title:"收集题目",
							...router.currentRoute.value.query
						}
					})
				})
			

				break;
			}





		};
		const showSubmitResVisible = ()=>{
			submitResVisible.value = true
			trueNum.value =0 
			falseNum.value =0
			props.subjectList.forEach((item:any,index)=>{
				let answers = item.answer.split("-")
				if(JSON.stringify(answers.sort()) == JSON.stringify(typeof item.userAnswer === 'string' ? [item.userAnswer] : item.userAnswer.sort())){
						++trueNum.value 
				}
				else if(item.userAnswer.length==0){
					 
				}
				else{
					++falseNum.value
				}
				
			})


		}
		const wrongReDo = ()=>{

			let wrongSet =	props.subjectList.filter((item:any,index)=>{
						if(item.userAnswer.length==0){
							return true
						}
						else{
							let answers = item.answer.split("-")
							let isTrue = JSON.stringify(answers.sort()) == JSON.stringify(typeof item.userAnswer === 'string' ? [item.userAnswer] : item.userAnswer.sort())
						
						return !isTrue
						}
					})
					//将题目重置
					wrongSet.forEach((item:any)=>{
						item.userAnswer = item.questionType !==3?'':[]
							item.optsBack.forEach((_item: any)=>{
								_item.status =0	
							})

					})	
					window.localStorage.setItem(vehicle+subject,JSON.stringify(wrongSet))
					if(wrongSet.length){
						emit('redo')
					}
					else{
						Toast('没有错题了')
					}
					
				
	// 				router.push({
    //     path:"/wrongReviewRes",
    //     query:router.currentRoute.value.query
    // })
					
					
					
				

		}

		return () => {
			return (
				<view>
					<van-dialog confirmButtonText="错题重做"  onConfirm={()=>{
						wrongReDo()
						}
						

					} closeOnPopstate cancelButtonText="关闭" v-model:show={submitResVisible.value} title="答题结果" show-cancel-button>
						
						<div style={'width:100%;padding:0 15px;text-align:center;font-size:30px'}>
							<m-icon size="30px" type="dui" /><span style={'padding:0 10px'}>{trueNum.value}</span>
						</div>
						<div style={'width:100%;padding:0 15px;text-align:center;font-size:30px'}>
							<m-icon size="30px" type="cuo" /><span style={'padding:0 10px'}>{falseNum.value}</span>
						</div>
						
					
					
</van-dialog>
					<van-tabbar placeholder route>
					<van-tabbar-item
						onClick={() => {
							emit("lastSubject");
						}}
						v-slots={{
							icon: () => <m-icon type="shangyiti" />,
						}}>
						上一题
					</van-tabbar-item>
					<van-tabbar-item
						v-slots={{
							icon: () => <m-icon type="dui" />,
						}}>
						{0}
					</van-tabbar-item>
					<van-tabbar-item
						v-slots={{
							icon: () => <m-icon type="cuo" />,
						}}>
						{0}
					</van-tabbar-item>
					<van-tabbar-item
						onClick={() => {
							emit("update:showSubjectChangePopup", true);
						}}
						v-slots={{
							icon: () => <m-icon type="zongtishu" />,
						}}>
						{props.currentSubjectIndex + 1}/{props.subjectTotal}
					</van-tabbar-item>

					<van-tabbar-item
						onClick={() => {
							showSubmitResVisible()
						}}
						v-slots={{
							icon: () => <m-icon type="jiaojuan" />,
						}}>
						交卷
					</van-tabbar-item>
					<van-tabbar-item
						onClick={() => {
							emit("nextSubject");
						}}
						v-slots={{
							icon: () => <m-icon type="xiayiti" />,
						}}>
						下一题
					</van-tabbar-item>
				</van-tabbar>
				</view>
				
			);
		};
	},
});
</script>
