<template>
	<view class="container">
		<view class="contentBox">
			<view class="operateBox">
				<view class="operateItemBox" v-for="(item, index) in industryData" :key="index">
					<view class="operateItem" @tap="toWrite(item)">
						<image :src="item.careCellImgUrl" class="operateItemBGImg" mode="widthFix"></image>
						<!-- <image src="/static/logo.png" class="operateItemBGImg" mode="widthFix"></image> -->
						<text class="operateItemText">{{item.txtCareRemarks}}</text>
					</view>
				</view>
				<!-- <view class="operateItemBox">
					<view class="operateItem" @tap="toWrite({id: '08dd2bba-2e5c-4169-8d09-640b5841bf8b'})">
						<image src="/static/logo.png" class="operateItemBGImg" mode="widthFix"></image>
						<text class="operateItemText">电气焊</text>
					</view>
				</view> -->
			</view>
		</view>
	</view>
</template>

<script>
	var that;
	export default {
		options: {
		    styleIsolation: 'isolated'
		},
		data() {
			return {
				openID: '',
				industryData: [],
				latitude: '',
				longitude: ''
			}
		},
		methods: {
			getData(){
				that = this;
				that.onGetData();
				that.updateData();
			},
			// 父页面更新openID
			updateData(){
				that.openID = uni.getStorageSync('userInfo').openID;
			},
			onGetData(){
				that.$admin.reqDirect({
					method: 'GET',
					url: '/api/v1/Snapshot/home'
				}).then(res => {
					that.industryData = res.industrys;
					typeof fun === 'function' && fun();
				}, err => {
					that.$util.msg(err);
				})
			},
			getAuthorizeInfo(fun) {
				uni.authorize({
					scope: 'scope.userLocation',
					success() { // 允许授权
						that.getLocationInfo(fun);
					},
					fail() { // 拒绝授权
						that.openConfirm(fun);
					}
				})
			},
			// 获取地理位置
			getLocationInfo(fun) {
				uni.getLocation({
					type: 'gcj02',
					success: function(res) {
						that.latitude = res.latitude;
						that.longitude = res.longitude;
						typeof fun === 'function' && fun();
					}
				});
			},
			// 当用户第一次拒绝后再次请求授权
			openConfirm(fun) {
				uni.showModal({
					title: '请求授权当前位置',
					content: '需要获取您当前的所在城市，请确认授权',
					success: (res) => {
						if (res.confirm) {
							uni.openSetting({
								success(res) {
									if (res.authSetting['scope.userLocation']) {
										that.getLocationInfo(fun);
									} else {
										typeof fun === 'function' && fun();
									}
								}
							}); // 打开地图权限设置
						} else if (res.cancel) {
							uni.showToast({
								title: '你拒绝了授权，无法获得你所在城市信息',
								icon: 'none',
								duration: 1000,
								success() {
									typeof fun === 'function' && fun();
								}
							})
						}
					}
				});
			},
			// 不同类型隐患上报
			toWrite(obj) {
				let strUrl = obj.pageCareUrl || 'comingSoon'
				// let strUrl = 'comingSoon'
				// let strUrl = '/pagesCare/Write/ReportHiddenDanger'
				// let strUrl = '/pagesCare/Write/LivelihoodFacts'
				// let strUrl = '/pagesCare/Write/WeldingOperations'
				// let strUrl = 'navigateToMiniProgramByAppId?appId=wxc564149bc4d892f3&path=pages/index/index&type=WeldingOperations'
				// let strUrl = 'navigateToExternalLinks?externalUrl=https://zgyjglyzpt.cn'
				if (strUrl.indexOf("navigateToMiniProgramByAppId") !== -1){
					if(!strUrl || strUrl.split('?').length != 2 || strUrl.split('?')[1].split('&').length != 3){
						that.$util.msg('跳转地址配置错误！');
					}else {
						let appId = strUrl.split('?')[1].split('&')[0].split('=')[1] || '';
						let path = strUrl.split('?')[1].split('&')[1].split('=')[1] || '';
						let type = strUrl.split('?')[1].split('&')[2].split('=')[1] || '';
						if (type == 'WeldingOperations'){
							if (!that.openID) {
								that.$util.msg("请先登录", function(){
									uni.switchTab({
										url: "/pages/Index/Mine"
									})
								});
								return;
							}
							// path += '?openId=' + that.openID;
							uni.navigateToMiniProgram({
								appId: appId,
								path: path,
								extraData: {
									'openId': that.openID
								},
								envVersion: 'release',
								fail(err) {
									that.$util.msg('您已取消打开');
								}
							})
						}else if (type == 'HomeworkReporte'){
							uni.navigateToMiniProgram({
								appId: appId,
								path: path,
								envVersion: 'release',
								fail(err) {
									that.$util.msg('您已取消打开');
								}
							})
						}
					}
				}else if (strUrl.indexOf("navigateToExternalLinks") !== -1){
					let externalUrl = strUrl.split('?')[1].split('=')[1];
					// that.getAuthorizeInfo(function(){
						uni.navigateTo({
							url: '/pages/ExternalLinks/Index?externalUrl=' + encodeURIComponent(externalUrl)
						})
					// });
				}else if (strUrl.indexOf("comingSoon") !== -1){
					that.$util.msg("功能即将上线，敬请期待！");
				}else {
					if (!that.openID) {
						that.$util.msg("请先登录", function(){
							uni.switchTab({
								url: "/pages/Index/Mine"
							})
						});
						return;
					}
					uni.navigateTo({
						url: strUrl + '?id=' + encodeURIComponent(obj.id)
					});
				}
			},
		}
	}
</script>

<style lang="less">
	.container {
		background-color: #fff;
	}

	.contentBox {
		height: calc(100vh - 20rpx);
		padding: 20rpx 20rpx 0;
		position: relative;
		z-index: 9;
		background-color: #fff;
		overflow-y: auto;
	}
	
	.operateBox{
		display: flex;
		justify-content: space-between;
		align-items: center;
		flex-flow: wrap;
		align-content: flex-start;
	}
	
	.operateItemBox{
		display: inline-block;
		width: calc(50% - 10rpx);
		padding-bottom: 20rpx;
	}

	.operateItem {
		display: inline-block;
		width: 100%;
		height: 100%;
		text-align: center;
		border-radius: 14rpx;
		position: relative;
	}

	.operateItemBGImg {
		width: 100%;
		height: 100%;
	}
	
	.operateItemText{
		position: absolute;
		bottom: 10%;
		left: 0rpx;
		width: calc(100% - 40rpx);
		padding: 0 20rpx;
		text-align: center;
		font-size: 32rpx;
		line-height: 42rpx;
		color: #000;
		white-space: pre-line;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 3;
	}
</style>