<template>
	<view class="ezy-study-page ezy-cjdc-study-page">
		<view class="study-school-year" @click="clickGradeTerm">{{gradeTerm}}</view>
		<view class="ezy-study-wrap" @touchstart="onTouchStart" @touchend="onTouchEnd">
			<img class="study-name-img" :src="growthImg"/>
			<!-- <img class="study-book-img" :src="bookImg"/> -->
			<img class="study-book-img" src="@/static/images/study/cjdc/book-default-img.png"/>
			<view @click="handleCheckCatalogue" :class="getTitleClass(zhangName)">{{zhangName}}</view>
			<!-- 动物类型 -->
			<view v-if="growthType!=null" :class="currentGrowth()"></view>
			<view>
				<!-- 小岛 -->

				<view class="brand-item" v-for="(item, index) in options.jieList" :key="item.jieId"
					@click="listClick(item, index,options)" :class="getClass(options,index,isVip,item)">
					<view v-if="isVip === 'VIP'">
						<!-- 序号 -->
					<!-- 	<view class="brand-icon">{{ item.number }}</view> -->
						<!-- 星星 -->
						<view class="brand-finish-icon" v-if="item.studyFlag===1"></view>
						<!-- 箭头 -->
						<view class="brand-arrow" v-if="item.daeFlag &&animalNum == index"></view>
						<!-- 节名称 -->
						<view class="brand-content">{{ item.jieName }}</view>
					</view>
					<view v-if="isVip == 'Not-Vip'">
						<!-- 序号 -->
			<!-- 			<view  class="brand-icon">{{ item.number }}</view> -->
						<view v-if="item.number !=1" class="brand-lock"></view>
						<!-- 箭头 -->
						<view v-if="item.daeFlag  &&  animalNum == index && isVip != 'Visitor'" class="brand-arrow"></view>
						<!-- 节名称 -->
						<view class="brand-content"> {{ item.jieName }} </view>

					</view>
					<view v-if="isVip == 'Visitor'">
						<!-- 锁 -->
		<!-- 				<view  class="brand-icon">{{ item.number }} </view> -->
						<!-- 锁 -->
						<view  v-if="item.number !=1" class="brand-lock"></view>
						<!-- 箭头 -->
						<view v-if="item.daeFlag  &&  animalNum == index && isVip != 'Visitor'" class="brand-arrow"></view>
						<!-- 节名称 -->
						<view class="brand-content"> {{ item.jieName }} </view>
					
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		reactive,
		ref,
		watch,
		getCurrentInstance,
		nextTick,
		onMounted
	} from "vue";
	import cacheManager from "@/utils/cacheManager.js";
	import {
		toast,
		getUserIdentity
	} from "@/utils/common";
	import {
		onShow
	} from '@dcloudio/uni-app';
	const growthType = ref(null);
	const growthImg = ref(null);
	const bookImg = ref(null);
	const youkeImageBook = ref(null);
	const $emit = defineEmits(['clickGradeTerm', 'onLeft', 'onRight', 'handleCheckCatalogue', 'listClick'])
	const props = defineProps({
		options: {
			type: Object,
		},
		youkeImage: {
			String: '',
		},
		youkeImageBook: {
			String: '',
		},
		gradeTerm: {
			type: String,
		},	
		zhangName: {
			type: String,
		},
	})
	onShow(() => {
		growthType.value = cacheManager.get('auth')?cacheManager.get('auth').growthType:props.youkeImage
			growthImg.value = cacheManager.get('auth') ? cacheManager.get('zhangInfo').icon : props.youkeImage;
			// 待修改  wgy看这
			bookImg.value = cacheManager.get('auth') ? cacheManager.get('zhangInfo').icon : props.youkeImage;
			youkeImageBook.value = cacheManager.get('auth') ? cacheManager.get('zhangInfo').zhangIcon : props.youkeImageBook;
			
	})
	const isVip = getUserIdentity();
	console.log('isVip',isVip);
	const gradeMapping = {
		1: '一年级',
		2: '二年级',
		3: '三年级',
		4: '四年级',
		5: '五年级',
		6: '六年级'
	};

	const termMapping = {
		1: ' 数学',
		2: ' 英语'
	};

	let startX = ref(0);
	let isSliding = ref(false);
	let endX = ref(0);
	let gradeTerm = ref('');
	let animalNum = ref(0);

	function clickGradeTerm() {
		$emit('clickGradeTerm');
	}

	function listClick(data,index,data2) {
		data.daeFlag = true
		nextTick(() => {
			animalNum.value = index
		})
		setTimeout(() => {
			$emit('listClick', data,data2,index);
		}, 300)
		
	}

	function currentGrowth(data) {
		if (growthType.value == 0) {
			return 'animal-img dan-img'
		} else if (growthType.value == 1) {
			return 'animal-img xiao-e-img'
		} else if (growthType.value == 2) {
			return 'animal-img zhong-e-img'
		} else {
			return 'animal-img da-e-img'
		}
	}
	// 章名称显示显示
	function getTitleClass(data) {
		console.log(data.length, 'data.length666');
		if(data.length <= 5){
			return 'chapter-title-box'
		}else if(data.length <= 8){
			return 'chapter-big-title-box'
		}else{
			return 'chapter-last-title-box'
		}
	}

	// 获取节class
	function getClass(data, index, isVip,item) {
	
		let brandActive = '';
		if (isVip === 'VIP') {
			brandActive = 'brand-active';
		} else if (item.firstFlag == 1 && isVip =='Not-Vip') {
			brandActive = 'brand-active';
		}else if (item.firstFlag == 1 && isVip =='Visitor' &&item.type ==1){
			brandActive = 'brand-active';
		} else {
			brandActive = '';
		}
		let indexLast = data.jieList.length - 1;
		// 判断最后一个为名称是否为单元测试，是单元测试则返回ceshi-jie
		if (index === indexLast && data.jieList[data.jieList.length - 1].jieName == 'Testing') {
			return 'ceshi-brand-item' + ' ' + brandActive;
		} else {
			return brandActive;
		}
	}

	function handleCheckCatalogue() {
		$emit('handleCheckCatalogue');
	}

	function onTouchStart(event) {
		console.log(event.touches.length);
		isSliding.value = false
		if (event.touches.length === 1) {
			isSliding.value = true;
			startX.value = event.touches[0].pageX;
		} else {
			isSliding.value = false;
			event.preventDefault()
			return
		}
	}

	function onSwipeLeft(event) {
		console.log('11111');
		if (cacheManager.get('auth')) {
			$emit('onLeft');
		}
	}

	function onSwipeRight(event) {
		console.log('22222');
		if (cacheManager.get('auth')) {
			$emit('onRight');
		}

	}

	function onTouchEnd(event) {
		if (isSliding.value) {
			const distanceX = event.changedTouches[0].clientX - startX.value
			if (distanceX > 0) {
				onSwipeLeft();
			} else if (distanceX < 0) {
				onSwipeRight();
			}
			isSliding.value = false
		} else {
			console.log('error');
		}
	}

	function dataRecom(data) {
		const index = data.jieList.findIndex(item => item.studyFlag == 0);

		if (index !== -1) {
			data.jieList[index].daeFlag = true;
			animalNum.value = index
		}
	}
	watch(() => props.options, (newVal, oldVal) => {
		//	console.log('New options:', newVal);
		//	console.log('Old options:', oldVal);
		// 在这里可以根据新的 options 做一些操作，比如发起请求等

		dataRecom(newVal)
	}, {
		deep: true,
		immediate: true
	});
	watch(() => props.gradeTerm, (newVal, oldVal) => {
		//	console.log('New options:', newVal);
		//	console.log('Old options:', oldVal);
		// 在这里可以根据新的 options 做一些操作，比如发起请求等
		gradeTerm.value = newVal

	}, {
		deep: true,
		immediate: true
	});
</script>

<style>

</style>