<script>
	// #ifdef APP-PLUS
	import APPUpdate, {
		getCurrentNo
	} from '@/uni_modules/zhouWei-APPUpdate/js_sdk/appUpdate';
	import {
		callErrorCode
	} from "@/utils/commonDicts.js";
	import { KeepLive } from '@/uni_modules/android-keeplive'
	// #endif
	import {
		simpleDebounce
	} from "@/utils/util.js";
	export default {
		data() {
			return {
				g_wakelock: null,
			}
		},
		onLaunch: function() {

			//#ifdef APP-PLUS
			APPUpdate();

			getCurrentNo((data) => {
				this.$store.commit("app/SET_CURRENTVERSION", data.versionName);
			});

			plus.globalEvent.addEventListener('plusMessage', (msg) => {
				if (msg.data.args.data.name == 'postMessage') {
					const {
						type,
						data,
						action
					} = msg.data.args.data.arg;
					if (action === "message") {
						uni.$u.toast("e:" + data);
					} else if (action == "singInState") {
						this.$store.commit("app/SET_SIGNINSTATE", data)
					} else if (action == "registerInfo") {
						this.$store.commit("app/SET_REDIS_INFO", data)
					} else if (action == "disconnected") {
						// 重连不上 重新创建
						this.$store.dispatch("app/createWv");
					} else if (action === "phoneStatus") {
						this.$store.commit("app/SET_DIALING", data)
					} else if (action == "outbound") {
						data.action = "outbound";
						this.$store.dispatch("app/saveUnionAppCall", data)
					} else if (action == "incoming") {
						data.action = "incoming";
						// this.$store.commit("app/SET_ISREJECT", false);
						this.$store.dispatch("app/saveUnionAppCall", data).then(res => {
							uni.navigateTo({
								url: `/pages/callweb/callweb?phone=${data.phone}`
							})
						})
					}
				}
			});

			// this.addFlags();
			
			this.$store.dispatch("call/startPhoneListener");
			
			this.startKeepLive();


			// #endif
		},
		onShow: function() {
			this.$store.dispatch("app/register");
		},
		methods: {
			startKeepLive(){
				const keep =new KeepLive();
				keep.setTitle("时与APP");
				keep.setContent("后台运行中");
				// keep.setLargeIcon("icon");
				keep.setSmallIcon("icon"); //图标名字  图标放在 插件下面的 res/drawable 文件夹下
				keep.onAddBackgroundCallback(function(res){
				    // console.log("后台运行 "+res)
				})
				keep.onAddScrrenListenerCallback(function(res){
				    // console.log("屏幕开启状态 "+res)
				})  
				keep.setWakeLock(1,"keeptag");// 设置唤醒类型
				
				keep.setAutoStartEnable(true)
				if(!keep.checkAppNotification()){
				    keep.onOpenNotificationSetting(function(res){
				        keep.register();
				        var ignoring=  keep.isIgnoringBatteryOptimizations();
				        if(!ignoring){
				            keep.requestIgnoreBatteryOptimizations();
				        }
				    })
				}else{
				    keep.register();
				    var ignoring=  keep.isIgnoringBatteryOptimizations();
				    if(!ignoring){
				        keep.requestIgnoreBatteryOptimizations();
				    }
				}
			},
			wakeLock() {
				//Android  
				var main = plus.android.runtimeMainActivity();
				var Context = plus.android.importClass("android.content.Context");
				var PowerManager = plus.android.importClass("android.os.PowerManager");
				var pm = main.getSystemService(Context.POWER_SERVICE);
				this.g_wakelock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ANY_NAME");
				this.g_wakelock.acquire();
			},
			// 禁止截屏
			addFlags() {
				// #ifdef APP-PLUS
				let osname = plus.os.name
				if (osname == "Android") {
					var activity = plus.android.runtimeMainActivity()
					plus.android.invoke(plus.android.invoke(activity, "getWindow"), "addFlags", 0x00002000)
				}
				// #endif
			},
			clearFlags() {
				// #ifdef APP-PLUS
				let osname = plus.os.name
				if (osname == "Android") {
					var activity = plus.android.runtimeMainActivity()
					plus.android.invoke(plus.android.invoke(activity, "getWindow"), "clearFlags", 0x00002000)
				}
				// #endif
			},
		},
	}
</script>

<style lang="scss">
	.card_wrap {
		margin: 0 20px;
		background-color: #fff;
		border-radius: 20rpx;
		overflow: hidden;
		margin-bottom: 20px;
	
		.header_wrap {
			display: flex;
			justify-content: space-between;
			padding: 10px;
			border-bottom: 1px solid #ddd;
			background-color: #fff;
	
			.header_left {
				font-size: 30rpx;
			}
	
			.haeder_right {
				display: flex;
				align-items: center;
	
				text {
					font-size: 28rpx;
					color: #b8b8b8;
					margin-right: 20rpx;
				}
	
				::v-deep .u-icon__icon {
					font-size: 24rpx !important;
				}
			}
		}
	}
	/*#ifndef APP-NVUE*/
	.required {
		&::after {
			content: "*";
			color: red;
			margin-left: 6rpx;
		}
	}

	.form_required {
		position: relative;

		&::after {
			position: absolute;
			left: 12rpx;
			top: 50%;
			transform: translate(0%, -50%);
			content: "*";
			color: red;
			margin-left: 6rpx;
		}
	}

	/*#endif*/
	.u-cell__value {
		color: #999999 !important;
	}

	/*#ifndef APP-NVUE*/
	page {
		background-color: $uni-bg-color;
	}

	/*#endif*/
	.u-nav-slot {
		color: #108cff;
	}

	// 时间选择器不要高
	/*#ifndef APP-NVUE*/
	::v-deep .uni-date__x-input {
		height: auto !important;
		font-size: 28rpx !important;
	}

	/*#endif*/
	.u-navbar__content__title {
		font-weight: bold;
	}

	@import "@/uni_modules/uview-ui/index.scss";
	/*每个页面公共css */
</style>