<template>
	<view class="ezy-xxjl-page">
		<view class="icon-title-navBar-box">
			<view @click="handleBack" class="nav-bar-icon"></view>
			<text class="nav-bar-title">学习记录</text>
		</view>
		<view class="ezy-tab-border">
			<uni-segmented-control :current="data.current" :values="data.items" active-color="#3A7FE9"
				@clickItem="onChangeTab" class="ezy-tab-box" />
			<view class="xxjl-content-box">
				<view v-if="data.current === 0">
					<scroll-view scroll-y="true" refresher-enabled="true" :refresher-triggered="data.shuxue.loading"
						:refresher-threshold="50" refresher-background="transparent" @refresherrefresh="onRefresh"
						class="xxjl-scroll-view">
						<!--数学-->
						<uni-list>
							<uni-list-item v-for="item in data.shuxue.list" class="xxjl-item-box">
								<template v-slot:body>
									<view @click="goDao(item,1)">
										<view class="item-date-row">
											<view class="data-item">
												<icon class="data-icon"></icon>
												<text>{{ item.createTime }}</text>
											</view>
											<!-- 这里加点击事件 wgy看这里 -->
											<view class="data-item del-item">
												<icon @click.stop="deleteJilu(item)" class="del-icon"></icon>
											</view>
										</view>
										<view class="item-zhang-row">
											<icon class="zhang-icon"></icon>
											<view>{{ item.levelName }}</view>
										</view>
										<view class="item-jie-row">
											<text>{{ item.zhangName }}</text>
										</view>
										<view class="item-jie-row">
											<text>{{ item.jieName }}</text>
										</view>
									</view>

								</template>
							</uni-list-item>
							<uni-load-more :status="data.shuxue.state" @click="getMore(0)"
								:contentText="data.shuxue.contentText"></uni-load-more>
						</uni-list>
					</scroll-view>
				</view>
				<view v-if="data.current === 1">
					<scroll-view scroll-y="true" refresher-enabled="true" :refresher-triggered="data.yingyu.loading"
						:refresher-threshold="50" refresher-background="transparent" @refresherrefresh="onRefresh"
						class="xxjl-scroll-view">
						<!--英语-->
						<uni-list>
							<uni-list-item v-for="item in data.yingyu.list" class="xxjl-item-box">
								<template v-slot:body>
									<view @click="goDao(item,2)">
										<view class="item-date-row">
											<view class="data-item">
												<icon class="data-icon"></icon>
												<text>{{ item.createTime }}</text>
											</view>
											<!-- 这里加点击事件 wgy看这里 -->
											<view class="data-item del-item">
												<icon @click.stop="deleteJilu(item)" class="del-icon"></icon>
											</view>
										</view>
										<view class="item-zhang-row">
											<icon class="zhang-icon"></icon>
											<view>{{ item.levelName }}</view>
										</view>
										<view class="item-jie-row">
											<text>{{ item.zhangName }}</text>
										</view>
										<view class="item-jie-row">
											<text>{{ item.jieName }}</text>
										</view>
									</view>
								</template>
							</uni-list-item>
							<uni-load-more :status="data.yingyu.state" @click="getMore(1)"
								:contentText="data.yingyu.contentText"></uni-load-more>
						</uni-list>
					</scroll-view>
				</view>
			</view>
		</view>
		<tip-small-dialog ref="deleteJiluDialogRef" @confirm-btn="deleteJiluBtn"
			:content="deleteJiluContent"></tip-small-dialog>
		<CustomTabBar></CustomTabBar>
	</view>
</template>

<script setup>
	import {
		reactive,
		ref
	} from "vue";
	import {
		xuexiJilu,
		xuexiJiluDelete
	} from "@/api/my.js";
	import {
		userZhangInfo,
		userLocate,
		userZhangForntInfo,
		userZhangNextInfo,
		getCommonZhangInfo
	} from "@/api/learnPlan.js"
	import {
		onLoad
	} from "@dcloudio/uni-app";
	import cuoti from "@/components/chengji/chengji.vue";
	import tipSmallDialog from '@/components/dialog/tipSmallDialog.vue';
	import {
		getWrongInfo
	} from "@/api/wrong";
	import {
		getWordListYk,
		getWordList,
		wordXuewan
	} from "@/api/word.js"
	import {
		toast,
		getUserIdentity
	} from "@/utils/common";
	import CustomTabBar from '@/components/custom-tabbar/custom-tabbar.vue';
	import cacheManager from "@/utils/cacheManager.js"
	const zhangId = ref(null);
	const nianji = ref(null);
	const subjectId = ref(null);
	const deleteJiluDialogRef = ref(null);
	const deleteJiluContent = '你确定要执行这个操作吗？';
	const wrongRef = ref(null);
	const deleteId = ref('');
	const localAuth = ref(null);
	const data = reactive({
		items: ['数学', '英语'],
		current: 0,
		shuxue: {
			page: 0,
			list: [],
			loading: false,
			state: 'more',
			contentText: {
				contentdown: '查看更多',
				contentrefresh: '加载中',
				contentnomore: '没有更多'
			}

		},
		yingyu: {
			page: 0,
			list: [],
			loading: false,
			state: 'more',
			contentText: {
				contentdown: '查看更多',
				contentrefresh: '加载中',
				contentnomore: '没有更多'
			}
		},
		wrongList: [],
	})
	subjectId.value = data.current + 1;


	function goDao(data, subjectId) {

		console.log('data', data);
		console.log('subjectId', subjectId);

		if (subjectId == 2 && data.typeId == 1) {
			cacheManager.remove('zhangInfo')
			cacheManager.updateObject('auth', {
				levelId: data.levelId + ',' + data.zhangId,
				typeId: data.typeId,
				subjectId: subjectId,
			})
			localAuth.value = cacheManager.get('auth');
			data.subjectId = subjectId
			getZhangInfoNewYingyu(data)
		} else {
			cacheManager.remove('zhangInfo')
			cacheManager.updateObject('auth', {
				levelId: data.levelId,
				typeId: data.typeId,
				subjectId: subjectId,
			})
			localAuth.value = cacheManager.get('auth');
			data.subjectId = subjectId
			getZhangInfo(data)
		}
	}

	function goDanciList(data) {
		getWordList({
			jieId: data.jieId
		}).then(res => {
			if (res.code == 0 && res.data.wordList.length > 0) {
				uni.redirectTo({
					url: '/pages/wordList/wordList?jieId=' + data.jieId
				})
			} else {
				toast("wordList数据错误!");
				return false
			}
		})
	}


	function listClick(data) {
		if (data.subjectId == 2 && data.typeId == 1) {
			goDanciList(data)
		} else {
			if (data.type == 2) {
				// 最后一项
				goKaoshi(data)
			} else {
				goLookShipin(data)
			}
		}
	}

	function goKaoshi(data) {
		uni.redirectTo({
			url: `/pages/unitTest/index?jieId=` + data.jieId
		})
	}

	function goLookShipin(data, index) {
		if (data.typeId == 1) {
			// 1新 2旧
			if (!data.videoId) {
				toast("videoId 丢失!");
				return false
			}
			uni.redirectTo({
				url: '/pages/study/lookShipinNew?jieId=' + data.jieId
			})
		} else {
			if (!data.videoId) {
				toast("videoId 丢失!");
				return false
			}
			uni.redirectTo({
				url: '/pages/study/lookShipin?jieId=' + data.jieId
			})
		}
	}


	function getZhangInfoNewYingyu(item) {
		const arr = localAuth.value.levelId.split(",");
		let req = {
			levelId: arr[0],
			zhangId: arr[1]
		}
		userZhangInfo(req).then(res => {
			if (res.code == 0) {
				cacheManager.set('zhangInfo', res.data)
				setTimeout(() => {
					cacheManager.updateObject('zhangInfo', {
						curZid: item.zid
					})
					listClick(item)
					recordZhangJie()
				}, 100)

			}
		}).catch((err) => {
			toast('版本变动,请重新登录！1')
			cacheManager.clearAll();
			uni.reLaunch({
				url: '/pages/login/index'
			});
		})
	}


	function getZhangInfo(item) {
		let req = {
			levelId: item.levelId
		}
		userZhangInfo(req).then(res => {
			if (res.code == 0) {
				cacheManager.set('zhangInfo', res.data)
				setTimeout(() => {
					cacheManager.updateObject('zhangInfo', {
						curZhangId: item.zhangId
					})
					listClick(item)
					recordZhangJie()
				}, 100)

			}
		}).catch((err) => {
			toast('版本变动,请重新登录！2')
			cacheManager.clearAll();
			uni.reLaunch({
				url: '/pages/login/index'
			});
		})
	}

	function recordZhangJie() {

		let req = {
			levelId: localAuth.value.levelId,
			userId: localAuth.value.userId,
			subjectId: currentProduct.value,
		}
		userLocate(req).then(res => {

		})
	}

	function deleteJilu(item) {
		deleteId.value = item.id
		deleteJiluDialogRef.value.handleShow();
	}

	function deleteJiluBtn(id) {
		let req = {
			id: deleteId.value
		}
		xuexiJiluDelete(req).then(res => {
			if (res.code == 0) {
				if (data.current === 0) {
					data.shuxue.list = [];
					data.shuxue.page = 0;
				} else {
					data.yingyu.list = [];
					data.yingyu.page = 0;
				}
				// 重新加载数据
				refreshData(data.current);
			} else {
				toast('失败，请稍后重试')
			}
		}).catch(err => {
			toast('失败，请稍后重试')
		})
	}

	function handleBack() {
		uni.redirectTo({
			url: '/pages/my/index'
		})
		uni.$emit('back-outpage')
	}

	function handleBackFromCuoti() {
		wrongRef.value.closePopup();
		uni.$emit('back-outpage')
	}

	function onChangeTab(e) {
		if (data.current !== e.currentIndex) {
			data.current = e.currentIndex;
			subjectId.value = data.current + 1;
			if (data.current == 0) {
				data.shuxue.page = 0
			} else if (data.current == 1) {
				data.yingyu.page = 0
			}
			refreshData(data.current);
		}
	}

	function refreshData(code) {
		const opt = {
			page: 1,
			size: 10, // 固定查询10条
			subjectId: data.current + 1 // 前台索引加1为学科cardId
		}

		if (code == 0) {
			data.shuxue.list = [];
			// 数学
			data.shuxue.state = 'loading';
			data.shuxue.page++;
			opt.page = data.shuxue.page;
		} else if (code == 1) {
			data.yingyu.list = [];
			// 英语
			data.yingyu.state = 'loading';
			data.yingyu.page++;
			opt.page = data.yingyu.page;
		}
		xuexiJilu(opt).then(res => {
			if (code == 0) {
				data.shuxue.list = data.shuxue.list.concat(res.data.data);
				data.shuxue.loading = false;
			} else if (code == 1) {
				data.yingyu.list = data.yingyu.list.concat(res.data.data);
				data.yingyu.loading = false;
			}
			if (code == 0) {
				if (res.data.total > data.shuxue.list.length) {
					// 数学
					data.shuxue.state = 'more';
					data.shuxue.loading = false;
				} else {
					// 数学
					data.shuxue.state = 'no-more';
					data.shuxue.loading = false;

				}
			} else if (code == 1) {
				if (res.data.total > data.yingyu.list.length) {
					// 英语
					data.yingyu.state = 'more';
					data.yingyu.loading = false;
				} else {
					// 英语
					data.yingyu.state = 'no-more';
					data.yingyu.loading = false;
				}
			}
		}).catch(err => {
			if (code == 0) {
				// 数学
				data.shuxue.state = 'more';
				data.shuxue.loading = false;
			} else if (code == 1) {
				// 英语
				data.yingyu.state = 'more';
				data.yingyu.loading = false;
			}
		})
	}

	function getMore(code) {
		console.log('12312312312');
		const opt = {
			page: 1,
			size: 10, // 固定查询10条
			subjectId: data.current + 1 // 前台索引加1为学科cardId
		}

		if (code == 0) {

			if (data.shuxue.state == 'no-more') return;
			// 数学
			data.shuxue.state = 'loading';
			data.shuxue.page++;
			opt.page = data.shuxue.page;
		} else if (code == 1) {
			// 英语
			if (data.yingyu.state == 'no-more') return;
			data.yingyu.state = 'loading';
			data.yingyu.page++;
			opt.page = data.yingyu.page;
		}
		xuexiJilu(opt).then(res => {
			if (code == 0) {
				data.shuxue.list = data.shuxue.list.concat(res.data.data);
				data.shuxue.loading = false;
			} else if (code == 1) {
				data.yingyu.list = data.yingyu.list.concat(res.data.data);
				data.yingyu.loading = false;
			}
			if (code == 0) {
				if (res.data.total > data.shuxue.list.length) {
					// 数学
					data.shuxue.state = 'more';
					data.shuxue.state_text = '加载更多';
					data.shuxue.loading = false;
				} else {
					// 数学
					data.shuxue.state = 'no-more';
					data.shuxue.state_text = '没有更多啦';

					data.shuxue.loading = false;
				}
			} else if (code == 1) {
				if (res.data.total > data.yingyu.list.length) {
					// 英语
					data.yingyu.state = 'more';
					data.yingyu.state_text = '加载更多';
					data.yingyu.loading = false;
				} else {
					// 英语
					data.yingyu.state = 'no-more';
					data.yingyu.state_text = '没有更多啦';

					data.yingyu.loading = false;
				}
			}
		}).catch(err => {
			if (code == 0) {
				// 数学
				data.shuxue.state = 'more';
				data.shuxue.state_text = '加载更多';
				data.shuxue.loading = false;
			} else if (code == 1) {
				// 英语
				data.yingyu.state = 'more';
				data.yingyu.state_text = '加载更多';
				data.yingyu.loading = false;
			}
		})
	}

	// function formatListToUse(list) {
	// 	list.forEach((item, index) => {
	// 		item.mta_show = false;
	// 		if (item.type == 3) {
	// 			item.result = JSON.parse(item.result);
	// 			item.placeholders = item.result.map((item, cindex) => `[bank${cindex+1}]`)
	// 			item.reply = item.reply ? JSON.parse(item.reply) : item.result.map(() => '');
	// 		}

	//      if (item.type == 4) {
	//        // 特殊题型英语题
	//        const audioList = item.audios ? item.audios.split(',') : [];
	//        item.placeholders = audioList.map((item, cindex) => `[yingyu${cindex+1}]`)
	//        item.audioList = audioList;
	//      }
	// 	})
	// }
	function onRefresh() {
		if (data.current == 0) {
			data.shuxue.page = 0;
			data.shuxue.list = [];
			data.shuxue.loading = true;
		} else if (data.current == 1) {
			data.yingyu.page = 0;
			data.yingyu.list = [];
			data.yingyu.loading = true;
		}
		refreshData(data.current);
	}

	onLoad(() => {
		getMore(data.current);
	})
</script>

<style>

</style>