<template>
	<view class="index">
		<image class="loginLogo" src="@/static/personLocation/bg.jpg"></image>
		<view class="index-items">
			<!-- <view class="index-item" @click="goNavigateTo('/pages/personLocation/home/home')">
				<image src="@/static/personLocation/train.png" mode="heightFix"></image>
				人员定位
			</view> -->
			<view class="index-item" @click="goNavigateTo('/pages/personLocation/exam/index/index')">
				<image src="@/static/personLocation/book.png" mode="heightFix"></image>
				培训考试
			</view>
			<!-- <view class="index-item">
				<image src="@/static/personLocation/qrcode.png" mode="heightFix"></image>
				人员二维码
			</view> -->
			<view class="index-item" @click="goNavigateTo('/pages/personLocation/emergencyalarm/emergencyalarm',1)">
				<image src="@/static/personLocation/alarm.png" mode="heightFix"></image>
				一键报警
			</view>
		</view>
		<!-- <view class="weft" @click="goNavigateTo('/pages/emergencyalarm/emergencyalarm')">
			SOS
		</view> -->
	</view>
</template>

<script>
	import {
		emergencyRecordAdd,
		emergencyRecordSendNotice,
		emergencyRecordEdit
	} from "@/api/index.js"
	export default {
		data() {
			return {
				projectSn: "",
				workerInfoId: "",
				id: "",
				emergencyInfo: {
					emergencyTypeId: "",
					emergencyDetail: "",
					livePicture: "",
					liveVideoSituation: "",
					incidentSite: "",
					longitude: "",
					latitude: "",
				},
			}
		},
		onShow() {
			this.projectSn = uni.getStorageSync('devInfoList').projectSn;
			this.workerInfoId = uni.getStorageSync('devInfoList').id;
		},
		methods: {
			orientation() {
				uni.showModal({
					title: '提示',
					content: '请打开定位服务',
					success: ({
						confirm,
						cancel
					}) => {

						if (confirm) {
							// android平台
							if (uni.getSystemInfoSync().platform == 'android') {
								var Intent = plus.android.importClass('android.content.Intent');
								var Settings = plus.android.importClass('android.provider.Settings');
								var intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
								var main = plus.android.runtimeMainActivity();
								main.startActivity(intent); // 打开系统设置GPS服务页面
							}

							// ios平台
							if (uni.getSystemInfoSync().platform == 'ios') {
								var UIApplication = plus.ios.import("UIApplication");
								var application2 = UIApplication.sharedApplication();
								var NSURL2 = plus.ios.import("NSURL");
								var setting2 = NSURL2.URLWithString("App-Prefs:root=Privacy&path=LOCATION");
								application2.openURL(setting2);
								plus.ios.deleteObject(setting2);
								plus.ios.deleteObject(NSURL2);
								plus.ios.deleteObject(application2);
							}
						}

						// 用户取消前往开启定位服务
						if (cancel) {
							// do sth...
						}
					}
				});
			},
			goNavigateTo(url, type) {
				const that = this;
				if (type == 1) {

					// 定位开启状态 true=开启，false=未开启
					let bool = false

					// android平台
					if (uni.getSystemInfoSync().platform == 'android') {
						var context = plus.android.importClass("android.content.Context");
						var locationManager = plus.android.importClass("android.location.LocationManager");
						var main = plus.android.runtimeMainActivity();
						var mainSvr = main.getSystemService(context.LOCATION_SERVICE);
						bool = mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)
					}

					// ios平台
					if (uni.getSystemInfoSync().platform == 'ios') {
						var cllocationManger = plus.ios.import("CLLocationManager");
						var enable = cllocationManger.locationServicesEnabled();
						var status = cllocationManger.authorizationStatus();
						plus.ios.deleteObject(cllocationManger);
						bool = enable && status != 2
					}
					// 未开启定位功能
					if (bool === false) {
						this.orientation();
						return
					}
					emergencyRecordAdd({
						projectSn: this.projectSn,
						emergencyTypeId: -1,
						alarmPersonId: this.workerInfoId,
					}).then(result => {
						if (result.code == 200) {
							// uni.showToast({
							// 	title: "登录成功"
							// })
							that.id = result.result.id;
							console.log(result);
						} else {
							uni.showToast({
								title: result.message,
								icon: "none"
							})
						}
					}).finally(() => {
						uni.showModal({
							title: '提示信息',
							content: '报警信息已经发送给救援人员，是否需要补充表单信息？',
							cancelText: "取消",
							confirmText: "确认",
							success: function(res) {
								if (res.confirm) {
									uni.navigateTo({
										url: `${url}?id=${that.id}`,
									});
								} else if (res.cancel) {
									console.log('用户点击取消');
									uni.showLoading({
										title: "报警中..."
									})
									uni.getLocation({
										type: 'gcj02',
										// type: 'wgs84',
										geocode: true, //设置该参数为true可直接获取经纬度及城市信息
										isHighAccuracy: "true",
										accuracy: "best", // 精度值为20m
										success: function(res) {
											console.log(res)
											// const latitude = parseFloat(res.latitude);
											// const longitude = parseFloat(res.longitude);
											that.emergencyInfo.longitude = parseFloat(res
												.longitude);
											that.emergencyInfo.latitude = parseFloat(res
												.latitude);
											that.addressList = [
												res.address.province,
												res.address.city,
												res.address.district,
												res.address.street,
												res.address.poiName,
											]
											that.emergencyInfo.incidentSite = that
												.addressList.map(
													item => item).join('')

											console.log(111111111);
											console.log(that.emergencyInfo.longitude, that
												.emergencyInfo.latitude);

											emergencyRecordEdit({
												id: that.id,
												projectSn: that.projectSn,
												incidentSite: that.emergencyInfo
													.incidentSite,
												longitude: that.emergencyInfo
													.longitude,
												latitude: that.emergencyInfo
													.latitude,
											}).then(result => {
												if (result.code == 200) {
													uni.showToast({
														title: result
															.message,
													})
													emergencyRecordSendNotice({
														projectSn: that.projectSn,
														incidentSite: that.emergencyInfo
															.incidentSite,
														id: that.id,
													}).then(result => {
														if (result.code == 200) {
															uni.hideLoading(); //关闭提示
														} else {
															uni.showToast({
																title: result
																	.message,
																icon: "none"
															})
														}
													})
												} else {
													uni.showToast({
														title: result
															.message,
														icon: "none"
													})
													uni.hideLoading(); //关闭提示
												}
											})
											
											// that.addrDel = res;
										},
										fail: function() {
											uni.showToast({
												title: '获取地址失败，将导致部分功能不可用',
												icon: 'none'
											});
										}
									});
								}
							}
						});
					})

				} else {
					uni.navigateTo({
						url,
					});
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.index {
		height: 100%;
		background: linear-gradient(135deg, #CFD6FF 0%, #ECEEFE 61%, #FFFFFF 100%);
		position: relative;
		padding: 0 40rpx 0 50rpx;
		overflow-y: scroll;

		.loginLogo {
			margin-top: 40rpx;
			width: 100%;
			height: 380rpx;
			border-radius: 20rpx;
		}

		.index-items {
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;
			padding: 40rpx;
		}

		.index-item {
			width: 100%;
			height: 182rpx;
			// border: 2rpx solid #efefef;
			background: #9FB8F7;
			box-shadow: 0 8rpx 8rpx 0 rgba(0, 0, 0, 0.25);
			border-radius: 32rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			margin-top: 40rpx;
			font-size: 64rpx;
			color: #FFFFFF;

			image {
				width: 100rpx;
				height: 100rpx;
			}
		}

		.weft {
			width: 100rpx;
			height: 100rpx;
			color: white;
			background-color: #D81E06;
			border-radius: 50%;
			display: flex;
			align-items: center;
			justify-content: center;
			position: absolute;
			right: 8rpx;
			top: 60%;
		}
	}
</style>