<template>
	<view class="page-container">
		<vod-image :src="launch" mode="aspectFill" class="bg"></vod-image>
		<view v-if="adv" class="adv" @click="openAD(adv.adv_link)">
			<vod-image v-if="adv.adv_image" :src="adv.adv_image" class="adv" @load="adLoad" @error="error"></vod-image>
			<u-count-down v-if="adv.adv_show_time" :time="adv.adv_show_time * 1000" ref="countDown" format="ss" :autoStart="false" @change="timeChange" @finish="toHome">
				<text class="time">{{ showTime.seconds }}s</text>
			</u-count-down>
		</view>
		<view v-if="loading" class="loading-text">
			<text>{{ loadingText }}</text>
		</view>
		<u-popup :show="guideShow" mode="center" bgColor="transparent" :custom-style="{ width: '80%', maxWidth: '300px' }">
			<image src="/static/images/guide.png" mode="widthFix" class="guide-img" />
		</u-popup>
		<u-popup :show="safariShow" :round="20" mode="center" :custom-style="{ width: '80%', maxWidth: '300px' }">
			<view class="safari align-center">
				<view class="safari-tip">
					<text>请使用iPhone Safari</text>
					<image src="/static/icon/safari.png" mode="widthFix" />
					<text>浏览器打开</text>
				</view>
				<view class="copy-link" @click="copyLink">复制链接</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
import Base64 from '@/utils/base64';
import { app } from '@/utils/api';
import { getApiDomain } from '@/utils/request';
import APPUpdate from '@/plugins/APPUpdate';
import vodImage from '@/components/vod-image.vue';

export default {
	components: {
		vodImage
	},
	data() {
		return {
			launch: '',
			adv: {},
			showTime: {},
			guideShow: false,
			safariShow: false,
			loading: false,
			loadingText: ''
		};
	},
	async onLoad() {
		if (this.isFullApp()) {
			// #ifdef APP-PLUS
			if (uni.getSystemInfoSync().platform == 'ios') {
				uni.getNetworkType({
					success: function (res) {
						if (res.networkType === 'none') {
							uni.showToast({
								title: '当前无网络连接，请检查网络设置',
								icon: 'none'
							});
						} else {
							uni.redirectTo({
								url: '/pages/splash/splash'
							});
						}
					}
				});
			} else {
				if (process.env.NODE_ENV === 'development') {
				} else {
				}
			}

			// #endif
			// #ifdef APP-PLUS
			if (this.isVpn()) {
				uni.showToast({
					title: '请关闭代理再试试吧~',
					icon: 'none'
				});
				return;
			}
			// #endif
			this.loading = true;
			this.loadingText = '正在启动，请稍候..';
			let apiDomain = uni.getStorageSync('API_DOMAIN');
			if (apiDomain == '' || apiDomain.length == 0) {
				this.loadingText = '正在检查线路，请稍候..';
				apiDomain = await getApiDomain();
				if (apiDomain.length == 0) {
					this.loadingText = '线路检查出错，请检查网络设置';
					uni.showToast({
						title: '当前线路不通，请先检查您的网络设置后进行访问',
						icon: 'none',
						duration: 5000
					});
					return;
				}
				this.loadingText = '正在初始化，请稍候..';
				await getApp().init();
				await this.init();
				app.home().then((res) => {});
				app.indexList(0).then((res) => {});
				app.vodClass().then((res) => {});
			} else {
				this.loadingText = '正在初始化，请稍候..';
				await this.$onLaunched;
				this.init();
				app.home().then((res) => {});
				app.indexList(0).then((res) => {});
				app.vodClass().then((res) => {});
			}
		} else {
			if (uni.getSystemInfoSync().platform == 'ios') {
				this.guideShow = true;
			} else {
				this.safariShow = true;
			}
		}
	},
	methods: {
		error() {
			this.loading = false;
			this.$refs.countDown.start();
		},
		init() {
			const advList = getApp().globalData.advList || [];

			if (advList.length > 0) {
				this.loadingText = '正在加载广告..';
				this.adv = advList[Math.floor(Math.random() * advList.length)];
			} else {
				this.launch = getApp().globalData.launch;

				if (this.launch) {
				} else {
					// #ifdef APP
					if (uni.getSystemInfoSync().platform != 'ios') {
						this.launch = '/static/images/splash.png';
					}
					// #endif
					// #ifdef H5
					this.launch = '/static/images/splash.png';
					// #endif
				}

				this.loadingText = '准备就绪，即将进入应用..';
				setTimeout(this.toHome, 3000);
			}
		},
		adLoad() {
			this.loading = false;
			this.$refs.countDown.start();
		},
		toHome() {
			uni.reLaunch({
				url: '/pages/home/index'
			});
			// uni.switchTab({
			// 	url: '/pages/home/index'
			// })
		},
		timeChange(e) {
			this.showTime = e;
		},
		copyLink() {
			uni.setClipboardData({
				data: window.location.href,
				showToast: false,
				success: () => {
					uni.showToast({
						icon: 'none',
						title: '已复制~!'
					});
				}
			});
		}
	}
};
</script>

<style lang="scss">
.page-container {
	height: 100%;
	background-color: $uni-bg-color;

	.bg,
	.adv {
		width: 100%;
		height: 100%;
		position: absolute;
		left: 0;
		top: 0;
	}

	.adv {
		.u-count-down {
			width: 60px;
			padding: 5px 10px;
			text-align: center;
			background-color: rgba(0, 0, 0, 0.6);
			color: #fff;
			border-radius: 15px;
			position: absolute;
			top: 20px;
			right: 20px;
		}
	}

	.loading-text {
		width: 100%;
		text-align: center;
		position: absolute;
		bottom: 50px;

		uni-text {
			max-width: 80%;
			padding: 8px 15px;
			color: #a0a0a0;
			font-size: 12px;
			background-color: #000;
			border-radius: 16px;
			display: inline-block;
		}
	}

	.guide-img {
		width: 100%;
		border-radius: 20px;
	}

	.safari {
		padding: 20px 15px;
		flex-direction: column;

		.safari-tip {
			line-height: 25px;
			font-size: 16px;
			font-weight: 500;

			uni-image {
				width: 25px;
				margin: 0 4px;
				vertical-align: middle;
			}
		}

		.copy-link {
			margin-top: 15px;
			width: 125px;
			height: 40px;
			color: #fff;
			background-color: #468df2;
			text-align: center;
			line-height: 40px;
			border-radius: 20px;
		}
	}
}
</style>
