<template>
	<view class="ezy-xuexi-page">
		<view class="icon-title-navBar-box">
			<text class="nav-bar-title">学习</text>
		</view>
		<chanpin1 v-if="chanpinId==1" ref="chanpin1Ref"></chanpin1>
		<chanpin2 v-if="chanpinId==2" ref="chanpin2Ref"></chanpin2>
		<chanpin3 v-if="chanpinId==3" ref="chanpin3Ref"></chanpin3>
		<chanpin4 v-if="chanpinId==4" ref="chanpin4Ref"></chanpin4>

		<tipMiddleDialog ref="tipDialogRef" :closeFlag='false' :title="tipTitle" @confirm-btn="BanbenConfirmBtn"
			:notClose="true" :content="tipContent"></tipMiddleDialog>
		<tipMiddleDialog ref="tipDialogRef2" :closeFlag='false' :title="tipTitle" @confirm-btn="BanbenConfirmBtn"
			:showTip="true" :notClose="true" :content="tipContentAndroid">
		</tipMiddleDialog>
		<!-- 底部 tabBar -->
		<custom-tab-bar :show="true" :current-index="currentTabIndex" />
	</view>
</template>

<script>
	import CustomTabBar from '@/components/custom-tabbar/index.vue';
	import cacheManager from "@/utils/cacheManager.js";
	import {
		onLoad,
		onShow,
		onHide,
		onUnload
	} from "@dcloudio/uni-app"
	import {
		toast
	} from '../../utils/common';
	import chanpin1 from './chanpin1'
	import chanpin2 from './chanpin2'
	import chanpin3 from './chanpin3'
	import chanpin4 from './chanpin4'
	import {
		banbenImpinfo
	} from "@/api/login.js"
	import tipMiddleDialog from '@/components/dialog/tipMiddleDialog.vue';
	import {
		useVersionUpdate
	} from "@/utils/versionUpdate.js";
	import {
		getVersion
	} from "../../api/login";
	import config from '../../config.js'

	export default {
		setup() {
			const {
				initDownload
			} = useVersionUpdate()

			return {
				initDownload
			}
		},
		data() {
			return {
				currentTabIndex: 1,
				banbenId: '',
				chanpinId: '',
				tipTitle: '升级提醒',
				tipContentAndroid: '您的APP不是最新版本，部分功能不能使用，请升级至最新版本！',
				tipContent: '您的APP不是最新版本，部分功能不能使用，请升级至最新版本！',
				updateUrl: '',
				version: config.appInfo.version
			}
		},
		components: {
			CustomTabBar,
			chanpin1,
			chanpin2,
			chanpin3,
			chanpin4,
			tipMiddleDialog
		},
		onLoad(options) {
			this.getLoginInit()
			uni.hideTabBar()
		},
		onShow() {
			this.currentTabIndex = 1
			const cacheData = cacheManager.get('contentInfo');
			if (cacheData) {
				console.log('使用缓存数据');
				this.chanpinId = cacheData.chanpinId
				this.switchUseCache(cacheData)
			} else {
				console.log('重新请求数据,请求版本内容详情接口');
				this.getBanbenInfo();
			}
		},
		onHide() {
			console.log('学习页面隐藏')
		},
		onUnload() {
			// 页面卸载无需特殊处理
		},
		methods: {
			getLoginInit() {
				const systemInfo = uni.getSystemInfoSync();
				getVersion({}).then(res => {
					if (this.version != res.data.version) {
						this.updateUrl = res.data.updateUrl

						if (systemInfo.platform == 'ios') {
							this.$refs.tipDialogRef.handleShow();
						} else {
							this.$refs.tipDialogRef2.handleShow();
						}
					}
				})
			},
			BanbenConfirmBtn() {
				const systemInfo = uni.getSystemInfoSync();
				if (systemInfo.platform == 'ios') {
					const appStoreUrl = 'itms-apps://itunes.apple.com/app/6741551741'
					plus.runtime.openURL(appStoreUrl)
				} else {
					this.initDownload(this.updateUrl)
				}
			},
			switchUseCache(data) {
				console.log('data',data.chanpinId);
				switch (Number(data.chanpinId)) {
					case 1:
						this.$nextTick(() => {
							this.$refs.chanpin1Ref.initData()
						})
						break;
					case 2:
						this.$nextTick(() => {
							this.$refs.chanpin2Ref.initData()
						})
								break;
					case 3:
						this.$nextTick(() => {
							this.$refs.chanpin3Ref.initData()
						})
								break;
					case 4:
						this.$nextTick(() => {
							this.$refs.chanpin4Ref.initData()
						})
						break;
					default:

				}
			},
			switchGetNeiRongInfo(data) {
				console.log('data', data);
				switch (Number(data.chanpinId)) {
					case 1:
						const banbenId = data.banbenId
						this.$nextTick(() => {
							this.$refs.chanpin1Ref.initData(banbenId)
						})
						break;
					case 2:
						const dengjiId2 = data.dengjiId
						this.$nextTick(() => {
							this.$refs.chanpin2Ref.initData(dengjiId2)
						})
						break;	
							case 3:
						const banbenId3 = data.banbenId
						this.$nextTick(() => {
							this.$refs.chanpin3Ref.initData(banbenId3)
						})
						break;
					case 4:
						const dengjiId4 = data.dengjiId
						this.$nextTick(() => {
							this.$refs.chanpin4Ref.initData(dengjiId4)
						})
						break;
					default:

				}
			},
			getBanbenInfo() {
				banbenImpinfo({}).then(res => {
					if (res.code == 0) {
						console.log('res', res)
						if (res.data.chanpinId == 0) {
							toast('请先进行选课！')
							uni.switchTab({
								url: '/pages/chanpinXuanze/index'
							})
							return;
						}


						this.chanpinId = res.data.chanpinId
						this.switchGetNeiRongInfo(res.data)
					} else {
						toast('请求版本内容详情接口数据异常')
						return
					}
				}).catch(() => {
					return false
				})
			},
		},
		computed: {}
	}
</script>