<template>
	<view>
		<web-view v-if="isWebView" ref="webview" :src="webUrl" :webview-styles="webviewStyles" @message="onMessage" />
	</view>
</template>

<script>
export default {
	components: {},
	props: {
		// 移除prizeList props
	},
	data() {
		const sysinfo = uni.getSystemInfoSync();
		return {
			webviewStyles: {
				height: sysinfo.windowHeight + "px",
				width: sysinfo.windowWidth + "px",
			},
			isWebView: false,
			status: "init",
			lotteryResult: null,
			page: null,
			prize: [],
			webUrl: 'https://zfunbox.cn/m/chou-box-luck.html',
			// webUrl: "http://192.168.1.26:5501/%E5%85%AC%E5%8F%B8/%E5%B7%A5%E4%BD%9C/%E5%8F%8B%E8%BE%BE/%E8%90%BD%E5%9C%B0%E9%A1%B5/chou-box-luck.html",
		};
	},

	onLoad() {
		// this.sendToWeb()

	},
	computed: {
		getWebUrl() {
			return this.webUrl + "?c=" + this.lotteryResult.length;
		}
	},
	watch: {

	},

	methods: {
		init(Prize, page, _lotteryResult) {
			if (Prize.length > 0) {
				Prize = Prize.map(item => {
					return {
						title: item.title,
						imgurl: item.imgurl
					}
				})
			}
			this.prize = Prize;
			this.page = page;
			this.lotteryResult = _lotteryResult;
			this.isWebView = true;
		},
		getWebview() {
			let currentWebview = this.page.$scope.$getAppWebview();
			let webview = currentWebview.children()[0]; // 获取第一个子 webview
			return webview;
		},
		setWebviewAction(action, data) {
			const webview = this.getWebview(); // 获取webview
			//判断data 是否是object对象或者是array对象，如果是的话，将其转换成为数组，然后在转换成为json字符串
			if (typeof data === "object") {
				data = JSON.stringify(data);
			}
			//判断data 是否是array对象，如果是的话，将其转换成为数组，然后在转换成为json字符串
			if (Array.isArray(data)) {
				data = JSON.stringify(data);
			}
			let str = 'OnMessage("' + action + '","' + data + '");';
			// console.log("OnMessage", str);
			webview.evalJS('OnMessage("' + action + '",\'' + data + '\');');
		},
		onMessage(event) {
			console.log("收到网页消息:", event);
			const data = event.detail.data[0];

			if (data.action === "load") {
				// //跳过动画
				// uni.showToast({
				// 	title: "初始化成功",
				// 	icon: "none",
				// });
				this.status = "load";
				//初始化成功
				this.setWebviewAction("addPrizeImage", this.prize);
			} else if (data.action === "InitPrizeSuccess") {
				// //跳过动画
				// uni.showToast({
				// 	title: "奖品加载成功，开始抽奖",
				// 	icon: "none",
				// });
				//初始化奖品成功
				if (this.lotteryResult != null) {
					console.log(this.lotteryResult.length, "this.lotteryResult");
					this.setWebviewAction("addLotteryCount", this.lotteryResult.length);
				}

			} else if (data.action == "addLotteryCountSuccess") {
				this.setWebviewAction("startLottery",
					{
						stopSleep: 2000,
						lotteryResult: this.lotteryResult
					});

			} else if (data.action == "stopLottery") {
				this.isWebView = false;
				if (data.msg == "跳过动画") {
					this.$emit("end", true)
				} else {
					this.$emit("end", false)
				}
			} else if (data.action == "luckyStop") {
				this.$emit("luckyStop", "")
			} else if (data.action == "luckyPlay") {
				this.$emit("luckyPlay", "")
			}
			else {
				console.log(data);
				uni.showToast({
					title: data.msg,
					icon: "none",
				});
			}
		}
	},
	mounted() {
		// 不再自动初始化
	},
};
</script>

<style lang="scss" scoped></style>