<template>
	<view class="ezy-study-page">
		<view class="study-school-year" @click="clickGradeTerm">{{gradeTerm}}</view>
		<view class="ezy-study-wrap">
			<view class="chapter-box" @click="handleCheckCatalogue">{{infoData.numberStr}}</view>
			<view class="chapter-title-box">{{infoData.zhangName}}</view>
			<view>
				<view class="brand-item" v-for="(item,index) in infoData.jieList" :key="index" @click="listClick(item,index)"
					:class="index ==0?'brand-active':''">
					<!-- 	<view class="brand-icon" v-if="index ==0">1</view>
					<view class="brand-lock" v-else></view>
					<view class="brand-content">{{item.jieName}}</view> -->
					<view class="brand-icon" v-if="index === 0">1</view>
					<view class="brand-lock" v-else></view>
					<view class="brand-content" v-if="index !== infoData.jieList.length - 1">{{ item.jieName }}</view>
					<view class="brand-content" v-if="index === infoData.jieList.length - 1">{{ item.jieName }}</view>
				</view>
			</view>

		</view>

		<!-- <uni-button  style="margin-top: 200px;display: block;" @click="goKaoshi">goKaoshi</uni-button>
	 -->
		<!-- <uni-button @click="handleCheckCatalogue">go catalogue</uni-button>
		<uni-button @click="goKaoshi">goKaoshi</uni-button>
		<!-- 蛋 -->
		<egg-dialog ref="eggDialogRef" @eggBtn="eggBtn"></egg-dialog>

		<catalogue ref="catalogueRef" :nianji="routeParams.nianji" :xueqi="routeParams.xueqi"
			@change-zhang="handleChangeZhang"></catalogue>
		<CustomTabBar></CustomTabBar>

	</view>
</template>

<script setup>
	import {
		userZhangFirstInfo,
		userZhangInfo,
		userLocate,
		getCommonZhangInfo
	} from "@/api/learnPlan.js"
	import {
		reactive,
		ref,
		getCurrentInstance,
		onMounted
	} from "vue";
	import {
		onLoad,
	} from '@dcloudio/uni-app';
	import catalogue from "@/components/catalogue/catalogue.vue";
	import CustomTabBar from '@/components/custom-tabbar/custom-tabbar.vue';
	import {
		getAuth
	} from '@/utils/auth.js';
	import {
		useTabBarHistory
	} from '@/utils/emitEvents.js';
	import eggDialog from './eggDialog.vue'
	
	const eggDialogRef = ref(null);

	const catalogueRef = ref(null);
	let gradeTerm = ref('');
	const selectZhang = ref(null);
	let infoData = reactive({
		jieList: [],
		haveFlag: '',
		nianji: '',
		number: '',
		xueqi: '',
		zhangId: '',
		zhangName: '',
		numberStr: '',
	});
	// 来自单元测试的路由参数缓存
	const gradeMapping = {
		1: '一年级',
		2: '二年级',
		3: '三年级',
		4: '四年级',
		5: '五年级',
		6: '六年级'
	};

	const termMapping = {
		1: '上',
		2: '下'
	};
	const routeParams = ref(null);

	onLoad((options) => {
		
	//	 appContext.config.globalProperties.$state
		   // const state = appContext.config.globalProperties.$state;
		console.log(options)
		// 获取路由参数
		routeParams.value = options;
		
		if (JSON.parse(getAuth())) {
			// 已登录
			// 选择年级进入调用此接口
			if (options.flag == 'selectGrades') {
				getZhangFirst()
			} else {
				getZhangInfo()
			}
			// 设置全局变量
				
			// 蛋
			// eggDialogRef.value.eggShow();
			
		} else {
			// 未登录
			getCommonZhang();
		}
	})
	function getCommonZhang(data) {
		let req = data ? {
			nianji: selectZhang.value.nianji,
			xueqi: selectZhang.value.xueqi,
			zhangId: selectZhang.value.zhangId,
		} : {
			nianji: routeParams.value.nianji,
			xueqi: routeParams.value.xueqi,
			zhangId: routeParams.value.zhangId,
		};
		getCommonZhangInfo(req).then(res => {
			infoData.jieList = res.data.jieList
			infoData.haveFlag = res.data.haveFlag
			infoData.nianji = res.data.nianji
			infoData.number = res.data.number
			infoData.numberStr = res.data.numberStr
			infoData.xueqi = res.data.xueqi
			infoData.zhangId = res.data.zhangId
			infoData.zhangName = res.data.zhangName
			gradeTerm.value = translateData(res.data);
			useTabBarHistory().setTabBarStorage({nianji:res.data.nianji,zhangId:res.data.zhangId,xueqi:res.data.xueqi})
		})
	}
	
	
	function recordZhangJie() {
		let req = {
			nianji: routeParams.value.nianji,
			userId: JSON.parse(getAuth()).userId,
			zhangId: infoData.zhangId,
			xueqi: routeParams.value.xueqi,

		}
		userLocate(req).then(res => {

		})
	}

	function getZhangFirst() {
		let req = {
			nianji: routeParams.value.nianji,
			xueqi: routeParams.value.xueqi,

		}
		userZhangFirstInfo(req).then(res => {
			infoData.jieList = res.data.jieList
			infoData.haveFlag = res.data.haveFlag
			infoData.nianji = res.data.nianji
			infoData.number = res.data.number
			infoData.numberStr = res.data.numberStr
			infoData.xueqi = res.data.xueqi
			infoData.zhangId = res.data.zhangId
			infoData.zhangName = res.data.zhangName
			gradeTerm.value = translateData(res.data);
			recordZhangJie()
			console.log(useTabBarHistory());
			useTabBarHistory().setTabBarStorage({nianji:res.data.nianji,zhangId:res.data.zhangId,xueqi:res.data.xueqi})
		})
	}

	function getZhangInfo(data) {
		let req = data ? {
			nianji: selectZhang.value.nianji,
			xueqi: selectZhang.value.xueqi,
			zhangId: selectZhang.value.zhangId,
		} : {
			nianji: routeParams.value.nianji,
			xueqi: routeParams.value.xueqi,
			zhangId: routeParams.value.zhangId,
		};
		userZhangInfo(req).then(res => {
			infoData.jieList = res.data.jieList
			infoData.haveFlag = res.data.haveFlag
			infoData.nianji = res.data.nianji
			infoData.number = res.data.number
			infoData.numberStr = res.data.numberStr
			infoData.xueqi = res.data.xueqi
			infoData.zhangId = res.data.zhangId
			infoData.zhangName = res.data.zhangName
			gradeTerm.value = translateData(res.data);
			recordZhangJie()
			useTabBarHistory().setTabBarStorage({nianji:res.data.nianji,zhangId:res.data.zhangId,xueqi:res.data.xueqi})
		})
	}

	function translateData(data) {
		return gradeMapping[data.nianji] + termMapping[data.xueqi]
	}

	function handleChangeZhang(data) {
		console.log("章", data);
		selectZhang.value = data;
		getZhangInfo(true)
	}

	function goKaoshi(data) {
		let zhangId = selectZhang.value ? selectZhang.value.zhangId : infoData.zhangId;
		let nianji = routeParams.value.nianji;
		let xueqi = routeParams.value.xueqi;
		uni.navigateTo({
			url: `/pages/unitTest/index?jieId=${data.jieId}&zhangId=${zhangId}&nianji=${nianji}&xueqi=${xueqi}`
		})
	}
	function goLookShipin(data) {
		data.videoId = 'b997f16cb9cb474cb93526cff77d8801'
		data.progressMarkers = [{
		    offset: 30,
		    isCustomized: true,
		    coverUrl: 'https://alivc-demo-vod.aliyuncs.com/image/cover/9A3F562E595E4764AD1DD546FA52C6E5-6-2.png',
		    title: 'test title',
		    time: '04:01-07:50',
		    describe: 'test string',
		   }, {
		    offset: 150,
		    isCustomized: true,
		    coverUrl: 'https://alivc-demo-vod.aliyuncs.com/image/cover/1E7F402241CD4C0F94AD2BBB5CCC3EC7-6-2.png',
		    title: 'test title',
		    time: '04:05-07:50',
		    describe: 'test string',
		   }, {
		    offset: 250,
		    isCustomized: true,
		    coverUrl: 'https://alivc-demo-vod.aliyuncs.com/image/cover/553AEA01161342C8A2B1756E83B69B5B-6-2.png',
		    title: 'test title',
		    time: '04:07-07:50',
		    describe: 'test string',
		   }, {
		    offset: 550,
		    isCustomized: true,
		    coverUrl: 'https://alivc-demo-vod.aliyuncs.com/image/cover/553AEA01161342C8A2B1756E83B69B5B-6-2.png',
		    title: 'test title',
		    time: '04:09-07:50',
		    describe: 'test string',
		   }, {
		    offset: 320,
		    isCustomized: true,
		    coverUrl: 'https://alivc-demo-vod.aliyuncs.com/image/cover/553AEA01161342C8A2B1756E83B69B5B-6-2.png',
		    title: 'test title',
		    time: '04:11-07:50',
		    describe: 'test string',
		   }, {
		    offset: 650,
		    isCustomized: true,
		    coverUrl: 'https://alivc-demo-vod.aliyuncs.com/image/cover/553AEA01161342C8A2B1756E83B69B5B-6-2.png',
		    title: 'test title',
		    time: '04:15-07:50',
		    describe: 'test string',
		   }]
		data.zhangId = infoData.zhangId
		data.nianji = infoData.nianji
		data.xueqi = infoData.xueqi
		const pageData = {
			//	videoId:data.videoId,
			videoId: 'b997f16cb9cb474cb93526cff77d8801',
			progressMarkers: [{
				offset: 30,
				isCustomized: true,
				coverUrl: 'https://alivc-demo-vod.aliyuncs.com/image/cover/9A3F562E595E4764AD1DD546FA52C6E5-6-2.png',
				title: 'test title',
				describe: 'test string',
			}, {
				offset: 150,
				isCustomized: true,
				coverUrl: 'https://alivc-demo-vod.aliyuncs.com/image/cover/1E7F402241CD4C0F94AD2BBB5CCC3EC7-6-2.png',
				title: 'test title',
				describe: 'test string',
			}, {
				offset: 250,
				isCustomized: true,
				coverUrl: 'https://alivc-demo-vod.aliyuncs.com/image/cover/553AEA01161342C8A2B1756E83B69B5B-6-2.png',
				title: 'test title',
				describe: 'test string',
			}, {
				offset: 550,
				isCustomized: true,
				coverUrl: 'https://alivc-demo-vod.aliyuncs.com/image/cover/553AEA01161342C8A2B1756E83B69B5B-6-2.png',
				title: 'test title',
				describe: 'test string',
			}, {
				offset: 320,
				isCustomized: true,
				coverUrl: 'https://alivc-demo-vod.aliyuncs.com/image/cover/553AEA01161342C8A2B1756E83B69B5B-6-2.png',
				title: 'test title',
				describe: 'test string',
			}, {
				offset: 650,
				isCustomized: true,
				coverUrl: 'https://alivc-demo-vod.aliyuncs.com/image/cover/553AEA01161342C8A2B1756E83B69B5B-6-2.png',
				title: 'test title',
				describe: 'test string',
			}],
		}
		uni.navigateTo({
			//url: '/pages/study/lookShipin?studyData=' + JSON.stringify(pageData)
			url: '/pages/study/lookShipin?studyData=' + JSON.stringify(data)
		})
	}
	

	function listClick(data,index) {
		
		if (!JSON.parse(getAuth())) {
			uni.redirectTo({
				url: '/pages/login/index'
			})
			return;
		}
		
		
		if(index ==infoData.jieList.length-1){
			// 最后一项
			goKaoshi(data)
		}else{
			goLookShipin(data)
		}
	}

	function handleCheckCatalogue() {
		catalogueRef.value.showPopup();
	}

	function clickGradeTerm() {
		uni.navigateTo({
			url: '/pages/selectGradesTerms/index'
		})
	}

	function eggBtn() {
		console.log('点击：开启提分之旅');
	}
</script>

<style>

</style>