<template name="index">
	<view class="index">
		<view class="temp">
			<view class="btnList">
				<!-- <view class="btnStaff">
					<image src="../../static/5.png" class="btnStaff2" mode=""></image>
					<image src="../../static/4.png" class="btnStaff2  margin-left-sm" @click="staff" mode=""></image>
					<image src="../../static/3.png" class="btnStaff2" @click="fillInDay" mode=""></image>
					<image src="../../static/6.png" class="btnStaff2" @click="temperature" mode=""></image>
				</view> -->
				<view class="park">{{park}}</view>
				<image class="img" :src="imgSrc" mode="widthFix" @click="modalName = 'Image'">
					<view class="tips">入园请出示二维码通行证</view>
					<view class="btnStaff" v-show="securityFlag">
						<image src="../../static/2.png" class="btnStaff2" mode=""></image>
						<image src="../../static/7.png" class="btnStaff2 margin-left-sm" @click="getScancode" style="width: 424upx;" mode=""></image>
					</view>

					<view class="btnStaff" style="padding-top:25upx;" v-show="hiddenRegisterFlag">
						<image src="../../static/8.png" class="btnStaff2" mode=""></image>
						<image src="../../static/9.png" class="btnStaff2  margin-left-sm" @click="leader" style="width: 424upx;" mode=""></image>
					</view>
			</view>
		</view>
		<view class="cu-modal" :class="modalName=='Image'?'show':''" @tap="modalName=''">
			<view class="cu-dialog">
				<view class="bg-img">
					<view class="cu-bar justify-end text-black">
						<view class="action" @tap="modalName = ''">
							<text class="cuIcon-close"></text>
						</view>
					</view>
				</view>
				<image :src="imgSrc" mode="widthFix"></image>
				<view class="cu-bar bg-white">
					<view class="action margin-0 flex-sub  solid-left text-right" @tap="modalName = ''"></view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getSampleId
	} from '@/utils/index.js'
	export default {
		components: {},
		name: "home",
		data() {
			return {
				park: '',
				hiddenRegisterFlag: true,
				imgSrc: '',
				modalName: '',
				flag: true,
				securityFlag: false,
				userId: ''
			};
		},
		onLoad: function(option) {
			wx.showShareMenu({
				withShareTicket: true
			})
		},
		onShow() {
			let that = this
			// #ifdef MP-WEIXIN 
			wx.login({
				success(res) {
					if (res.code) {
						let code = res.code
						that.getUserInfo(code)
					} else {
						console.log('登录失败！' + res.errMsg)
					}
				}
			})
			// #endif 
		},
		methods: {
			leader() {
				uni.navigateTo({
					url: `/pages/leader/leader`
				})
			},
			temperature() {
				uni.showToast({
					icon: 'none',
					title: '即将上线！'
				})
			},

			getImg() {
				this.$http.get(`/sict-ncov/user/qrcode`).then(res => {
					const d = res.data
					if (d.code == 1) {
						this.imgSrc = d.object
					}
				}).catch(err => {
					console.log(err)
				})
			},
			// 员工跳转
			staff() {
				uni.navigateTo({
					url: `/pages/fillIn/fillIn`
				})
			},

			// 安保跳转
			// security() {
			// 	uni.navigateTo({
			// 		url: `/pages/security/security?userId=${this.userId}`
			// 	})
			// },
			getScancode() {
				if (this.$store.state.user.userInfo.roleId && this.$store.state.user.userInfo.roleId == '1003') {
					var _this = this;
					// 允许从相机和相册扫码
					wx.scanCode({
						success: (res) => {
							if (res.result.indexOf('userId') > -1 && res.result.indexOf('date') > -1) {
								let arr = res.result.split("&")
								if (arr.length == 3) {
									_this.userId = arr[0].split("=")[1]
									if (arr[2].split("=")[1] == _this.$store.state.user.userInfo.userOrg.parkId) {
										if (getSampleId() == arr[1].split("=")[1]) {
											if (_this.securityFlag && _this.userId) {
												uni.navigateTo({
													url: `/pages/security/security?userId=${_this.userId}`
												})
											}
										} else {
											uni.showToast({
												icon: 'none',
												title: '二维码已失效！'
											})
										}
									} else {
										uni.showToast({
											icon: 'none',
											title: '不是该园区二维码！'
										})
									}
								} else {
									uni.showToast({
										icon: 'none',
										title: '二维码已失效！'
									})
								}
							} else {
								uni.showToast({
									icon: 'none',
									title: '请扫描小程序首页内的二维码！'
								})
							}

						}
					})
					
					
				} else {
					uni.showToast({
						icon: 'none',
						title: '暂无权限，仅安保人员有此权限！'
					})
				}
			},
			fillInDay() {
				if (!this.$store.state.user.userInfo.idCard) {
					uni.showToast({
						icon: 'none',
						title: '请先上报个人资料！'
					})
				} else {
					uni.navigateTo({
						url: `/pages/fillInDay/fillInDay`
					})
				}
			},
			// 获取用户信息
			getUserInfo(code) {
				this.$http.post(`/sict-ncov/user/auth?code=${code}`).then(res => {
					const d = res.data
					this.hiddenRegisterFlag = true
					this.securityFlag = false
					if (d.code == 1) {
						let userInfo = d.object
						this.park = userInfo.userOrg ? userInfo.userOrg.park : ''
						if (userInfo.roleId == '1003') {
							this.securityFlag = true
						}

						if (userInfo.isVip == '1') {
							this.hiddenRegisterFlag = false
						}

						let obj = Object.assign({}, this.$store.getters.userInfo)
						if (obj.avatarUrl) {
							userInfo.avatarUrl = obj.avatarUrl
						}
						this.$store.dispatch('user/setInfo', Object.assign(obj, userInfo))
						this.$store.dispatch('user/setToken', d.result)
						this.getImg()
					}
				}).catch(err => {
					console.log(err)
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.temp {
		font-size: 40px;
		text-align: center;
		height: 100vh;
		width: 100vw;
		background-repeat: no-repeat;
		background-size: cover;
		background-image: url('http://139.9.163.126/gastric/static/backgroundSict2.jpg');
		.btnList {
			padding-top: 225upx;
			text-align: center;

			.btnSecurity {
				background-color: #92d0f1;
				color: #FFFFFF;
				border-radius: 60upx;
				width: 52%;
				text-align: center;
			}

			.btnSecurity {
				margin-top: 45upx;
			}

			.btnStaff {
				color: #FFFFFF;
				border-radius: 60upx;
			}

			.btnStaff2 {
				text-align: left;
				display: inline-block;
				width: 140upx;
				height: 86upx;
				background-color: #92d0f1;
				color: #FFFFFF;
			}

			.img {
				width: 350upx;
				height: 350upx;
			}

			.tips,
			.park {
				margin-top: 25upx;
				margin-bottom: 45upx;
				font-weight: bold;
				font-size: 32upx;
				color: #FFFFFF;
			}

			.park {
				margin-top: 0;
				margin-bottom: 45upx;
			}
		}
	}
</style>
