<template>
	<view class="ezy-study-page">
		<view class="study-school-year">{{gradeTerm}}</view>
		<view class="ezy-study-wrap">
			<view class="chapter-box" @click="handleCheckCatalogue">
				{{infoData.numberStr}}
			</view>
			<view class="chapter-title-box">
				{{infoData.zhangName}}
			</view>
			<!-- :class= index ==1?'亮+无锁头':'暗+锁头' -->
			<view class="brand" v-for="(item,index) in infoData.jieList" :key="index" @click="listClick(item)">
				{{item.jieName}}</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
	} from "@/api/learnPlan.js"
	import {
		reactive,
		ref,
		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 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) => {
		console.log(options)
		// 获取路由参数
		routeParams.value = options;
		// 选择年级进入调用此接口
		if (options.flag == 'selectGrades') {
			getZhangFirst()
		} else {
			getZhangInfo()
		}
		// 调用定位章节 （后台记录）

		// 蛋
		// eggDialogRef.value.eggShow();
	})

	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()
		})
	}
	function getZhangInfo() {
		let req = {
			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()
		})
	}

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

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

	function goKaoshi() {
		let zhangId = selectZhang.value.zhangId;
		let nianji = routeParams.value.nianji;
		let xueqi = routeParams.value.xueqi;
		uni.navigateTo({
			url: `/pages/unitTest/index?jieId=1&zhangId=${zhangId}&nianji=${nianji}&xueqi=${xueqi}`
		})
	}

	function listClick(data) {

		data.videoId = 'b997f16cb9cb474cb93526cff77d8801'
		data.progressMarkers = []
		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 handleCheckCatalogue() {
		catalogueRef.value.showPopup();
	}

	function goBack() {
		uni.navigateBack()
	}

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

<style>

</style>