<template>
	<view class="ezy-study-page ezy-yingyu-study-page">
		<view class="study-school-year" @click="clickGradeTerm">{{gradeTerm}}</view>
		<view class="ezy-study-wrap" @touchstart="onTouchStart" @touchend="onTouchEnd">
			<view @click="handleCheckCatalogue" class="chapter-title-box">{{options.zhangName}}</view>
			<!-- 动物类型 -->
			<view :class="currentGrowth()"></view>
			<view>
				<!-- 小岛 -->
				<view class="brand-item" v-for="(item, index) in options.jieList" :key="item.jieId"
					@click="listClick(item, index)" :class="getClass(options.jieList,index,isVip)">
					<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 !== 'VIP'">
						<!-- 序号 -->
						<view class="brand-icon">{{ item.number }}</view>
						<!-- 锁 -->
						<view v-if="item.firstFlag !='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);
	onShow(() => growthType.value = cacheManager.get('auth').growthType)

	const $emit = defineEmits(['clickGradeTerm', 'onLeft', 'onRight', 'handleCheckCatalogue', 'listClick'])
	const props = defineProps({
		options: {
			type: Object,
		},
		gradeTerm: {
			type: String,
		},
	})
	const isVip = getUserIdentity();
	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) {
		data.daeFlag = true
		nextTick(() => {
			animalNum.value = index
		})
		setTimeout(() => {
			$emit('listClick', data);
		}, 1000)
		
	}

	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 getZhangContent(data) {
		console.log(data.length, 'data.length');
		let length = data.length;
		// 初始将字符串平分成两半
		let halfLength = Math.floor(length / 2);

		// 插入换行符
		let firstLine = data.slice(0, halfLength);
		let secondLine = data.slice(halfLength);
		console.log(firstLine + '\n' + secondLine, 'firstLine + + secondLine');
		return firstLine + '\n' + secondLine;
	}

	// 获取节class
	function getClass(data, index, isVip) {
		let brandActive = '';
		if (isVip === 'VIP') {
			brandActive = 'brand-active';
		} else if (data[index].firstFlag == 1) {
			brandActive = 'brand-active';
		} else {
			brandActive = '';
		}
		let indexLast = data.length - 1;
		// 判断最后一个为名称是否为单元测试，是单元测试则返回ceshi-jie
		if (index === indexLast && data[data.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>