<template>
	<view>
		<view class="linksList">
			<view class="title" v-if="linksList[0].title">{{linksList[0].title}}</view>

			<view v-if="linksList[0].types == 11">
				<view class="list_11">
					<view class="item" v-for="item in linksList" :key="item.id" @click="go_page(item)">
						<view class="left">
							<view class="icon">
								<view class="img"
									v-if="item.imageUrl == '../../static/images/link.png' || item.imageUrl == ''">
									<uni-icons type="paperclip" size="30"></uni-icons>
								</view>
								<view class="img" v-else>
									<image :src="item.imageUrl" mode=""></image>
								</view>
							</view>
							<view class="name">{{item.linkName}}</view>
						</view>
						<view class="right">
							<uni-icons type="arrowright" size="20"></uni-icons>
						</view>
					</view>
				</view>
			</view>
			<view v-if="linksList[0].types == 12">
				<view class="list_12">
					<view class="item" v-for="item in linksList" :key="item.id" @click="go_page(item)">
						<view class="icon">
							<view class="img"
								v-if="item.imageUrl == '../../static/images/link.png'  || item.imageUrl == ''">
								<uni-icons type="paperclip" size="30"></uni-icons>
							</view>
							<view class="img" v-else>
								<image :src="item.imageUrl" mode=""></image>
							</view>
						</view>
						<view class="name">{{item.linkName}}</view>
					</view>
				</view>
			</view>
			<view v-if="linksList[0].types == 13">
				<view class="list_13">
					<view class="item" v-for="item in linksList" :key="item.id" @click="go_page(item)">
						<view class="icon">
							<view class="img"
								v-if="item.imageUrl == '../../static/images/link.png'  || item.imageUrl == ''">
								<uni-icons type="paperclip" size="30"></uni-icons>
							</view>
							<view class="img" v-else>
								<image :src="item.imageUrl" mode=""></image>
							</view>
						</view>
						<view class="name">{{item.linkName}}</view>
					</view>
				</view>
			</view>
			<view v-if="linksList[0].types == 14">
				<view class="list_14">
					<view class="item" v-for="item in linksList" :key="item.id" @click="go_page(item)">
						<view class="icon">
							<view class="img"
								v-if="item.imageUrl == '../../static/images/link.png'  || item.imageUrl == ''">
								<uni-icons type="paperclip" size="30"></uni-icons>
							</view>
							<view class="img" v-else>
								<image :src="item.imageUrl" mode=""></image>
							</view>
						</view>
						<view class="name">{{item.linkName}}</view>
					</view>
				</view>
			</view>
		</view>
	
	
		<!-- 宁煤信息平台登录框 -->
		<uni-popup ref="popup" type="center" @maskClick="maskClick">
			<view class="login_box">
				<view class="input_box">
					<view class="label">工号:</view>
					<view class="box">
						{{staff_num}}
					</view>
				</view>
				<view class="input_box">
					<view class="label">登录密码(宁煤信息平台):</view>
					<view class="box">
						<input type="password" v-model="password" />
					</view>
				</view>
				<view class="btn" @click="login()">确认</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		set_base_url
	} from '@/common/set_base_url.js'
	import time from "@/common/time.js"
	import {
		diffTime
	} from "@/common/time.js"
	import {
		goToLink
	} from "@/common/common.js"
	export default {
		props: [
			"linksList",
			"mine_code"
		],
		data() {
			return {
				staff_num: "",
				password: "",
				
				url: "",
				
				app_id: "",
				gps: "",
				deviceID: "",
				phoneType: ""
			};
		},
		mounted() {
			this.staff_num = uni.getStorageSync('user').staff_num
		},
		methods: {
			maskClick() {
				this.password = ""
			},
			login() {
				// #ifdef APP-PLUS
				if (this.password == '') {
					uni.showToast({
						icon: "none",
						title: "密码不能为空！"
					})
					return
				}
			
				this.$refs.popup.close()
				// 获取gps
				uni.getLocation({
					type: 'wgs84',
					success: (res) => {
						console.log('当前位置的经度：' + res.longitude);
						console.log('当前位置的纬度：' + res.latitude);
			
						this.gps = res.longitude + ',' + res.latitude
						// 获取设备信息
						plus.device.getInfo({
							success: (e) => {
								console.log(JSON.stringify(e.uuid));
								console.log(plus.device.model)
			
								this.deviceID = JSON.stringify(e.uuid)
								this.phoneType = plus.device.model
			
			
								uni.request({
									method: "POST",
									url: "http://mtyyxs.shnxmyjt.com:33300/api/OAuth/Login",
									header: {
										'Content-Type': 'application/json;charset=UTF-8',
										'caller': 'UnifiedPortal'
									},
									data: {
										phoneType: this.phoneType,
										password: this.password,
										gps: this.gps,
										userID: this.staff_num,
										resolvingPower: "851*393",
										deviceID: this.deviceID,
										ip: "112312313"
									},
									success: (res) => {
										console.log(this.password)
										console.log(res)
			
										if (res.data.code = '11002') {
											uni.showToast({
												icon: "none",
												title: res.data.msg
											})
			
											if (res.data.data.length > 20) {
												uni.setStorageSync('ningmei_token', res
													.data.data)
												uni.setStorageSync(
													'ningmei_token_time',
													new Date().format("yyyy-MM-dd")
													)
												uni.navigateTo({
													url: "../../index/ningMeiXinXiPingTai/ningMeiXinXiPingTai",
													success: (res) => {
														// 通过eventChannel向被打开页面传送数据
														res.eventChannel
															.emit(
																'acceptDataFromOpenerPage', {
																	data: this
																		.url
																})
													}
												})
											}
										}
									}
								})
			
							}
						});
			
			
					}
				})
				// #endif
			
			},
			go_page(item){
				// console.log(item)
				
				let link = item.linkUrl
				// console.log(link)
				
				// OA系统
				if(link.indexOf("oa_system.nxmy.com") != -1 ){
					uni.navigateTo({
						url:"../../origanization/OA/h5/h5",
						success: (res) =>{
							// 通过eventChannel向被打开页面传送数据
							res.eventChannel.emit('acceptDataFromOpenerPage', {
								url: link,
								title: item.title
							})
						}
					})
					
					return
				}
				
				
				if (link.indexOf("nativeApp://") != -1) {
					let app_name = link.match(/nativeApp:\/\/(\S*)\?/)[1]
					let app_down_link = link.split('?')[1]
				
					// #ifdef APP
					if (plus.os.name == 'Android') {
				
						plus.runtime.launchApplication({
							pname: app_name
						}, (e) => {
							console.log('Open system default browser failed: ' + e.message)
				
							uni.showModal({
								title: app_name,
								content: '未安装此应用、是否下载？',
								success: (res) => {
									if (res.confirm) {
										console.log('用户点击确定');
				
										uni.navigateTo({
											url: "../../index/h5/h5?url=" + app_down_link
										})
									} else if (res.cancel) {
										console.log('用户点击取消');
									}
								}
							});
						})
					}
					
					return
					// #endif
				}
				
				if (link.indexOf("http://mtyyxs.shnxmyjt.com") != -1) {
					console.log("信息平台")
				
					this.url = item
					// 有登录记录
					let token = uni.getStorageSync('ningmei_token')
					let time = uni.getStorageSync('ningmei_token_time')
					// 30天刷新一次
					let diffTimeShow = diffTime(time, new Date().format("yyyy-MM-dd")).split(' ')[0];
					console.log(diffTimeShow)
					if (token.length > 20 && diffTimeShow < 30) {
						uni.navigateTo({
							url: "../../index/ningMeiXinXiPingTai/ningMeiXinXiPingTai",
							success: (res) => {
								// 通过eventChannel向被打开页面传送数据
								res.eventChannel.emit('acceptDataFromOpenerPage', {
									data: this.url
								})
							}
						})
					} else {
						this.$refs.popup.open()
					}
					
					return
				}
				
				// 二维码
				if(link.indexOf("pageId") != -1 && link.indexOf("app:") == -1){
					let pageId = ""
					function GetQueryString(name) {
						var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
						var r = link.match(reg);
						if (r != null) return unescape(r[2]);
						return null;
					}
					console.log(GetQueryString("pageId"))
					if (GetQueryString("pageId")) {
						pageId = GetQueryString("pageId")
					} else {
						pageId = link.split('=')[1]
					}
					
					uni.navigateTo({
						url:"../../index/record/record?pageId=" + pageId + "&mine_code=" + this.mine_code,
					})
				}
				// h5
				if(link.indexOf("pageId") == -1 && link.indexOf("app:") == -1){
					if(link.indexOf("huiyiList") != -1){
						uni.navigateTo({
							url:"../../workbench/meeting_arrangements/meeting_arrangements"
						})
					}else{
						uni.navigateTo({
							url: "../../index/record/h5/h5?url=" + link
						})
					}
				}
				// 原生
				if(link.indexOf("pageId") == -1 && link.indexOf("app:") != -1){
					// console.log("原生")
					if (link.indexOf("/production/production") != -1) {
						uni.switchTab({
							url: "../../tabbar/production/production"
						})
					}
					
					
					if (link.indexOf("app_url") != -1) {
						// console.log(link.split('app_url=')[1] + "&title=" + item.linkName + "&mine_code=" + this.mine_code)
						uni.navigateTo({
							url:link.split('app_url=')[1] + "&title=" + item.linkName + "&mine_code=" + this.mine_code
						})
						
					}
					
					if (link.indexOf("nativeApp://") != -1) {
						
						let app_name = link.match(/nativeApp:\/\/(\S*)\?/)[1]
						let app_down_link = link.split('?')[1]
					
						console.log(app_name,app_down_link)
					
						// #ifdef APP
						if (plus.os.name == 'Android') {
					
							plus.runtime.launchApplication({
								pname: app_name
							}, (e) => {
								console.log('Open system default browser failed: ' + e.message)
					
								uni.showModal({
									title: app_name,
									content: '未安装此应用、是否下载？',
									success: (res) => {
										if (res.confirm) {
											console.log('用户点击确定');
					
											uni.navigateTo({
												url: "../../index/h5/h5?url=" + app_down_link
											})
										} else if (res.cancel) {
											console.log('用户点击取消');
										}
									}
								});
							})
						}
						// #endif
					} 
					
				}
				
				
			
			}
		}
	}
</script>

<style lang="scss">
	.linksList {
		.title {
			line-height: 30px;
			border-left: 4px solid #009FE8;
			border-radius: 4px;
			padding-left: 10px;
			margin-bottom: 10px;
		}

		// 一行一项
		.list_11 {
			.item {
				display: flex;
				justify-content: space-between;
				align-items: center;

				padding: 10px 0;
				border-bottom: 1px solid #f4f4f4;

				.left {
					display: flex;
					align-items: center;

					.icon {
						width: 60px;
						line-height: 80rpx;
						margin-right: 10rpx;

						.img {
							width: 60px;
							height: 80rpx;
							line-height: 80rpx;
							text-align: center;

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

					.name {
						width: 500rpx;
						white-space: nowrap;
						overflow: hidden;
						text-overflow: ellipsis;
					}
				}

				.right {}
			}
		}

		// 一行两项
		.list_12 {
			display: flex;
			flex-wrap: wrap;

			.item {
				width: 320rpx;
				display: flex;
				align-items: center;
				margin-left: 10rpx;
				// border-right: 1px solid #f4f4f4;
				padding-right: 10rpx;
				margin-bottom: 10px;

				.icon {
					margin-right: 30rpx;

					.img {
						image {
							width: 90rpx;
							height: 90rpx;
						}
					}
				}

				.name {
					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;
				}
			}

			.item:nth-child(2n) {
				border-right: none;
			}
		}

		// 一行三项
		.list_13 {
			overflow: hidden;

			.item {
				float: left;
				width: 210rpx;
				text-align: center;
				margin-bottom: 10px;
				margin-right: 35rpx;

				.icon {
					width: 210rpx;

					.img {
						width: 210rpx;
						line-height: 30px;
						text-align: center;

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

				.name {
					margin-top: 10rpx;

					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;
				}
			}

			.item:nth-child(3n) {
				margin-right: 0;
			}
		}

		// 一行四项
		.list_14 {
			overflow: hidden;

			.item {
				float: left;
				width: 175rpx;
				text-align: center;
				margin-bottom: 10px;

				.icon {
					width: 175rpx;

					.img {
						width: 175rpx;
						line-height: 30px;
						text-align: center;

						image {
							width: 50px;
							height: 50px;
						}
					}
				}

				.name {
					margin-top: 10rpx;

					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;
				}
			}
		}

	}
	
	.login_box {
		width: 500rpx;
		padding: 30rpx 50rpx;
		border-radius: 20rpx;
		margin: 0 auto;
		background-color: #FFFFFF;
	
		.input_box {
			margin-bottom: 30rpx;
	
			.label {
				color: #6C6F74;
				margin-bottom: 20rpx;
			}
	
			.box {
				background: #FFFFFF;
				border-radius: 8rpx;
				border: 2rpx solid #E9EBF2;
	
				box-sizing: border-box;
				padding: 20rpx 25rpx;
	
			}
		}
	
		.btn {
			width: 500rpx;
			margin: 0 auto;
			border-radius: 8rpx;
			background-color: #009FE8;
			line-height: 90rpx;
			color: #FFFFFF;
			text-align: center;
		}
	}
</style>
