<template>
	<view class="ezy-study-page">
		<shuxueZhangjie @clickGradeTerm="clickGradeTerm" @onLeft="onLeft" @onRight="onRight"
			@handleCheckCatalogue="handleCheckCatalogue" :options="infoData">
		</shuxueZhangjie>
		<!-- 蛋 -->
		<egg-dialog ref="eggDialogRef" @eggBtn="eggBtn"></egg-dialog>
		<catalogue ref="catalogueRef" @change-zhang="handleChangeZhang"></catalogue>
		<CustomTabBar></CustomTabBar>
	</view>

</template>
<script setup>
	import {
		userZhangInfo,
		userLocate,
		userZhangForntInfo,
		userZhangNextInfo,
		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 shuxueZhangjie from './product/shuxue.vue';
	import {
		useTabBarHistory
	} from '@/utils/emitEvents.js';
	import eggDialog from './eggDialog.vue'
	import {
		toast
	} from "../../utils/common";
	import cacheManager ,{useXuekeNianji}from "@/utils/cacheManager.js";
	const {
		updateXueke,
		getXueke
	} = useXuekeNianji();
	const eggDialogRef = ref(null);
	const catalogueRef = ref(null);
	const selectZhang = ref(null);
	let infoData = reactive({
		jieList: [],
		haveFlag: '',
		nianji: '',
		number: '',
		cardId: '',
		zhangId: '',
		zhangName: '',
		numberStr: '',
	});
	onLoad(() => {
		// 获取路由参数

		init()

	})

	function init(){
		if (cacheManager.get('auth')) {
			//会员 取auth
			selectZhang.value = cacheManager.get('auth');
			
			console.log(selectZhang.value);
			// 已登录
			// 选择年级进入调用此接口
			//options.flag == 'selectGrades'
			getZhangInfo()
			// 蛋
			// eggDialogRef.value.eggShow();
		} else {
			// 未登录 游客 
			getCommonZhang()
	
		}
	}
	
	function getZhangInfo(data) {
		let req = {
			nianji: selectZhang.value.nianji,
			cardId: selectZhang.value.cardId,
			zhangId: selectZhang.value.zhangId,
		}
		userZhangInfo(req).then(res => {
			cacheManager.set('zhangInfo', res.data)
			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.cardId = res.data.cardId
			infoData.zhangId = res.data.zhangId
			infoData.zhangName = res.data.zhangName
			recordZhangJie()
			getNewData()
			// if (cacheManager.get('zhangInfo')) {
			//     cacheManager.remove('zhangInfo')
			//     cacheManager.set('zhangInfo', res.data)
			// } else {
			//     cacheManager.set('zhangInfo', res.data)
			// }
		})
	}

	function getCommonZhang(data) {
		
		let req = {
			nianji: getXueke().nianji,
			cardId: getXueke().cardId,
			zhangId: selectZhang.value? selectZhang.value.zhangId :0,
		}
		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.cardId = res.data.cardId
			infoData.zhangId = res.data.zhangId
			infoData.zhangName = res.data.zhangName
			//getNewData()
		})
	}



	function onLeft(event) {
		console.log('用户左滑了');
		let req = {
			nianji: selectZhang.value.nianji,
			zhangId: selectZhang.value.zhangId,
			cardId: selectZhang.value.cardId,
		}
		userZhangForntInfo(req).then(res => {
			cacheManager.set('zhangInfo', res.data)
			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.cardId = res.data.cardId
			infoData.zhangId = res.data.zhangId
			infoData.zhangName = res.data.zhangName
			recordZhangJie()
			getNewData()
		})
	}

	function onRight(event) {
		console.log('用户又滑了');
		let req = {
			nianji: selectZhang.value.nianji,
			zhangId: selectZhang.value.zhangId,
			cardId: selectZhang.value.cardId,
		}
		userZhangNextInfo(req).then(res => {
			cacheManager.set('zhangInfo', res.data)
			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.cardId = res.data.cardId
			infoData.zhangId = res.data.zhangId
			infoData.zhangName = res.data.zhangName
			recordZhangJie()
			getNewData()
		})
	}

	function getNewData() {
		selectZhang.value.zhangId = cacheManager.get('zhangInfo').zhangId;
		selectZhang.value.cardId = cacheManager.get('zhangInfo').cardId;
		selectZhang.value.nianji = cacheManager.get('zhangInfo').nianji;

	}

	function recordZhangJie() {
		let req = {
			nianji: selectZhang.value.nianji,
			userId: cacheManager.get('auth').userId,
			zhangId: infoData.zhangId,
			cardId: selectZhang.value.cardId,

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

		})
	}

	function handleChangeZhang(data) {
		selectZhang.value = data;
		init()
	}

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

	function goLookShipin(data) {

		data.progressMarkers = [{
			offset: 30,
			isCustomized: true,
			coverUrl: 'https://alivc-demo-vod.aliyuncs.com/image/cover/9A3F562E595E4764AD1DD546FA52C6E5-6-2.png',
			title: '变式1',
			time: '00:00:30',
			describe: 'test string',
		}, {
			offset: 120,
			isCustomized: true,
			coverUrl: 'https://alivc-demo-vod.aliyuncs.com/image/cover/1E7F402241CD4C0F94AD2BBB5CCC3EC7-6-2.png',
			title: '变式2',
			time: '00:02:00',
			describe: 'test string',
		}, {
			offset: 300,
			isCustomized: true,
			coverUrl: 'https://alivc-demo-vod.aliyuncs.com/image/cover/553AEA01161342C8A2B1756E83B69B5B-6-2.png',
			title: '变式3',
			time: '00:05:00',
			describe: 'test string',
		}]
		data.zhangId = infoData.zhangId
		data.nianji = infoData.nianji
		data.cardId = infoData.cardId
		uni.navigateTo({
			//url: '/pages/study/lookShipin?studyData=' + JSON.stringify(pageData)
			url: '/pages/study/lookShipin?studyData=' + JSON.stringify(data)
		})
	}


	function listClick(data, index) {
		if (!cacheManager.get('auth')) {
			toast("当前为游客模式请登录！")
			uni.redirectTo({
				url: '/pages/login/index'
			})
			return;
		}

		// if(index !=0&&data.jieId!=79){

		//             toast("付费章节！")
		//             return false
		// }

		if (!data.vipFlag) {
			toast("付费章节！")
			return false
		}
		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>