<template>
	<view class="button-sp-area">
		<button type="primary" plain="true" @click="start()">启动</button>
		<view>
			(启动后，前台服务会启动，锁屏、切换后台后，日志仍然会打印)
		</view>
		<button type="primary" plain="true" @click="stop()">停止</button>
		<button type="primary" plain="true" @click="isNotificationEnabled()">查看通知是否启用</button>
		<button type="primary" plain="true" @click="invokeNotification()">唤起通知设置</button>
		<button type="primary" plain="true" @click="ClearALl()">清空日志</button>
		<!--
		<button type="primary" plain="true" @click="Modify()">修改通知信息</button> 
		-->
		<view>
			<rich-text :nodes="log"></rich-text>
		</view>
	</view>
</template>
<script>
	const modal = uni.requireNativePlugin('modal');
	const frontservice = uni.requireNativePlugin('zjw-frontservice');
	var globalEvent = uni.requireNativePlugin('globalEvent');
	export default {
		data() {
			return {
				title: '',
				log: "",
				timer: null,
				count: 0,
				isPlay: false,
				bhtimenum: 0,
			}
		},
		onLoad() {
			this.log = uni.getStorageSync("log");
			let that = this;
			
			globalEvent.addEventListener('position', function(e) {
				uni.getLocation({
					success: (res) => {
						uni.request({
							url: 'http://47.94.208.222:8080/v1/position',
							//url: 'http://hualiyun.cc:7100/v1/position',
							method: 'POST',
							dataType: 'json',
							data:{
								"code": "273523",
								"x": res.longitude,
								"y": res.latitude,
								"z": res.latitude,
								"sn": "2021041516584407",
							},
							success: (data) => {
								// uni.showToast({
								// 	title: '请求成功',
								// 	icon: 'success',
								// 	mask: true,
								// 	duration: 2000
								// });
							},
							fail: (err) => {
								console.log('request fail', err);
								// uni.showModal({
								// 	content: err.errMsg,
								// 	showCancel: false
								// });
							},
							complete: () => {
								// console.log('complete');
							}
						})
					}
				})

				// 定时器回调，每隔delaysec毫秒之后，该函数会被调用一次
				// 执行你的定时逻辑 
				// 执行你的定时逻辑结束
				that.log = "";
				let time = new Date();
				let time1 = time.getFullYear() + '-' + (time.getMonth() + 1) + '-' +
					time.getDate() + ' ' + time.getHours() +
					':' + time.getMinutes() + ':' + time.getSeconds();
				let lo = "<h4> " + time1;
				that.log = lo + that.log;
				//console.log('position'+JSON.stringify(e));

				// 执行你的定时逻辑结束
			});
		},
		methods: {
			ClearALl() {
				this.log = "";
				uni.setStorageSync('log', "");

			},

			isNotificationEnabled() {
				frontservice.isNotificationEnabled({}, result => {
					console.log("C")
					modal.toast({
						message: JSON.stringify(result),
						duration: 1.5
					});
				});
			},
			invokeNotification() {
				frontservice.invokeNotification({}, result => {
					console.log("B")
					modal.toast({
						message: JSON.stringify(result),
						duration: 1.5
					});
				});
			},
			stop() {
				frontservice.stop({}, result => {
					console.log("A")
					modal.toast({
						message: "closed",
						duration: 1.5
					});
				});
			},
			start() {
				// console.log(modal);
				// console.log(frontservice);
				frontservice.start({
					title: "天路通服务",
					big_title: "天路通APP",
					content: "天路通运行中，请勿关闭！",
					"delaysec": 10000,
					"isIgnoringBattery": false, //不开启电池优化
				}, result => {
					console.log(result)
				});
			},
		}
	}
</script>

<style>
	button {
		margin-top: 30upx;
		margin-bottom: 30upx;
	}

	.button-sp-area {
		margin: 0 auto;
		width: 100%;
	}

	.content {
		text-align: center;
		height: 400upx;
	}

	.wrapper {
		flex-direction: column;
		justify-content: center;
	}

	.button {
		width: 200px;
		margin-top: 30px;
		margin-left: 20px;
		padding-top: 20px;
		padding-bottom: 20px;
		border-width: 2px;
		border-style: solid;
		border-color: #458B00;
		background-color: #458B00;
	}

	.text {
		font-size: 30px;
		color: #666666;
		text-align: center;
	}
</style>
