<template>
	<view>
		<!-- 工作台头图 -->
		<view class="top">
			<image :src="header"></image>
		</view>


		<!-- 工作台矿切换 -->
		<!-- <view v-if="app_mine_code == 'ningmeijituan'">
			<scroll-view scroll-x>
				<view class="tab">
					<view class="item" @click="change_active(index,item.code)" v-for="(item,index) in tab" :key="index">
						<view class="text" :class="active == index?'active':''">{{item.name}}</view>
					</view>
				</view>
			</scroll-view>
		</view> -->
		<!-- nav -->
		<view class="section">
			<view class="list">
				<view class="item" v-for="(item,index) in nav" :key="index" @click="go_link(item)">
					<view class="icon">
						<image :src="item.icon" mode="aspectFill"></image>
					</view>
					<view class="name">
						<view class="num"
							v-if="item.link.indexOf('http://oa_system.nxmy.com:8011/?m=ying&d=we&num=daiban&hideheader=true') != -1 && num_1 > 0">
							{{num_1}}
						</view>
						<view class="num"
							v-if="item.link.indexOf('http://oa_system.nxmy.com:8011/?m=ying&d=we&num=todo&hideheader=true') != -1 && num_2 > 0">
							{{num_2}}
						</view>
						<view class="num" v-if="item.link.indexOf('closed_loop_management') != -1 && num_3 > 0">
							{{num_3}}
						</view>

						<view class="name_title">{{item.title}}</view>
						<view class="sub_title">{{item.sub_title}}</view>
					</view>
				</view>
			</view>
		</view>

		<view class="section" v-for="(item,index) in module" :key="index">
			<view class="title">
				<view class="icon">
					<image :src="item.icon" mode=""></image>
				</view>
				<view class="text">{{item.title}}</view>
			</view>
			<view class="list">
				<view class="item" v-for="(item_2,index_2) in item.children" :key="index_2" @click="go_link(item_2)">
					<view class="icon">
						<image :src="item_2.icon" mode=""></image>
					</view>
					<view class="name">
						<view class="name_title">{{item_2.title}}</view>
						<view class="sub_title">{{item_2.sub_title}}</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 {
		data() {
			return {
				// 默认煤矿
				app_mine_code: "",
				// 当前煤矿编码
				mine_code: "",
				base_url: "",
				tab: [{
						name: "宁煤公司",
						code: "ningmeijituan"
					},
					{
						name: "羊场湾煤矿",
						code: "yangchangwan"
					},
					{
						name: "枣泉煤矿",
						code: "zaoquan"
					},
					{
						name: "清水营煤矿",
						code: "qingshuiying"
					},
					{
						name: "金家渠煤矿",
						code: "jinjiaqu"
					},
					{
						name: "石槽村煤矿",
						code: "shicaocun"
					},
					{
						name: "金凤煤矿",
						code: "jinfeng"
					},
					{
						name: "物业公司",
						code: "wuyegongsi"
					},
					{
						name: "宁东运营部",
						code: "ningdongyunying"
					}
				],

				active: 0,
				// 头部图片
				header: "",
				nav: [],
				module: [],

				num_1: 0,
				num_2: 0,
				num_3: 0,


				staff_num: "",
				password: "",

				url: "",

				app_id: "",
				gps: "",
				deviceID: "",
				phoneType: ""
			};
		},
		onLoad() {
			this.staff_num = uni.getStorageSync('user').staff_num
			// 初始化当前煤矿编码
			this.app_mine_code = uni.getStorageSync('mine_code')
			this.mine_code = uni.getStorageSync('mine_code')
			this.base_url = set_base_url(this.app_mine_code)

			this.get_workbench_index()
		},
		onShow() {
			this.get_workflow_todo_query()
		},
		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

			},

			change_active(index, code) {
				this.active = index
				this.mine_code = code

				// 根据矿编码切换首页接口不同的请求基础路径
				this.base_url = set_base_url(this.mine_code)
				this.get_workbench_index()
			},
			// 获取基础数据
			// get_workbench_index() {
			// 	this.$api.workbench_index({

			// 	}).then((res) => {
			// 		// console.log(res.data.data)
			// 		this.header = res.data.data.header
			// 		this.nav = res.data.data.nav
			// 		this.module = res.data.data.module
			// 	})
			// },

			get_workbench_index() {
				// uni.showLoading({
				// 	mask: true
				// })
				uni.request({
					url: this.base_url + "/workbench/index",
					header:{
						'accesskey': "b364b449a18af327867f7edc3431b541"
					},
					success: (res) => {
						// uni.hideLoading()
						// console.log(res.data.data)
						this.header = res.data.data.header
						this.nav = res.data.data.nav
						this.module = res.data.data.module
					}
				})
			},

			// 获取消息提醒数量
			get_workflow_todo_query() {
				// 闭环管理
				this.$api.five_fixed_fiveFixedNewCategory({

				}).then((res) => {
					// console.log(res.data.content.data)

					let data = res.data.content.data
					let num = 0

					data.forEach((item, index) => {
						num = num + item.no_num
					})

					this.num_3 = num

					if (this.mine_code == 'ningdongyunying') {
						if (this.num_3 > 0) {
							uni.showTabBarRedDot({
								index: 2,
							})
						}
					}
				})

				this.$api.workflow_todo_query({
					staff_num: uni.getStorageSync('user').staff_num,
					query_type: 2
				}).then((res) => {
					this.num_1 = res.data.content.data

					if (this.mine_code == 'ningdongyunying') {
						if (this.num_1 > 0) {
							uni.showTabBarRedDot({
								index: 2,
							})
						}
					}
				})

				this.$api.workflow_todo_query({
					staff_num: uni.getStorageSync('user').staff_num,
					query_type: 1
				}).then((res) => {

					this.num_2 = res.data.content.data

					if (this.mine_code == 'ningdongyunying') {
						if (this.num_2 > 0) {
							uni.showTabBarRedDot({
								index: 2,
							})
						}
					}


				})
			},
			// 宁煤信息平台
			go_link(item) {
				let link = item.link

				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('用户点击取消');
									}
								}
							});
						})
					}
					// #endif
				} else 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()
					}
				} else {
					goToLink(item, this.mine_code)
				}


			},


		},

	}
</script>


<style lang="scss">
	page {
		background-color: #F2FAF7;
		margin-bottom: 20rpx;
	}

	.top {
		image {
			width: 750rpx;
			height: 270rpx;
			display: block;
		}
	}

	.tab {
		margin-top: 20rpx;
		display: flex;

		background-color: #FFFFFF;

		box-sizing: border-box;
		padding: 0 20rpx;

		.item {
			background-color: #FFFFFF;

			.text {
				box-sizing: border-box;

				height: 90rpx;
				line-height: 90rpx;

				width: 188rpx;
				text-align: center;
				overflow: hidden;
				white-space: nowrap;
				text-overflow: ellipsis;

				font-size: 30rpx;
				color: #666666;
			}

			.active {
				font-weight: 700;
				color: #000000;
				border-bottom: 4rpx solid #009FE8;
			}

		}
	}


	.section {
		margin-top: 20rpx;
		background-color: #FFFFFF;

		padding: 25rpx;

		.title {
			display: flex;
			align-items: center;
			height: 90rpx;

			.icon {
				margin-right: 20rpx;

				image {
					width: 50rpx;
					height: 50rpx;
					display: block;
				}
			}

			.text {
				font-weight: 700;
				font-size: 34rpx;
			}
		}

		.list {
			margin-top: 30rpx;
			width: 700rpx;
			overflow: hidden;

			.item {

				float: left;
				margin-bottom: 30rpx;

				width: 350rpx;

				display: flex;
				align-items: center;
				height: 140rpx;

				.icon {
					margin-bottom: 10rpx;

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


				}

				.name {
					position: relative;

					margin-left: 20rpx;

					.num {
						z-index: 10;
						position: absolute;
						top: -10rpx;
						right: -50rpx;

						width: 40rpx;
						text-align: center;
						line-height: 40rpx;
						background-color: #F56C6C;
						border-radius: 50%;
						color: #FFF;
						font-size: 26rpx;
					}

					.name_title {
						font-size: 28rpx;
						margin-bottom: 10rpx;
					}

					.sub_title {
						font-size: 24rpx;
						color: #999;
					}
				}
			}

			.item:nth-child(2n) {
				box-sizing: border-box;
				border-left: 1rpx solid #F2FAF7;

				padding-left: 40rpx;

			}

		}
	}


	.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>
