//首页
<template>
  <view class="index">
    <TabBar></TabBar>
    <view class="menu">
    	<button
    	  v-for="item in menu"
    	  :key="item.id"
    	  class="menu-item"
    	  @click="handleClick(item.id)"
    	  >{{ item.name }}
			</button>
    </view>
		<!-- 获取手机号popup组件 -->
		<GetPhonePopup :popShow="popShow" @closePop="closePop"/>
  </view>
</template>

<script>
import { mapMutations } from "vuex";
import TabBar from "@/components/tabBar/tabBar.vue";
import { getLogin, getUserProfile } from "@/utils/auth.js";
import { verifyPhone } from "@/api/index.js";
import {getUserJurisdiction} from '@/api/user.js'
import { getLocationParams } from "@/common/utils.js";
import GetPhonePopup from "@/components/base/getphonePopup.vue";
import { subscription } from "@/utils/common.js"

export default {
  components: {
    TabBar, GetPhonePopup
  },
  data() {
    return {
      currentPagePath: "",
      menu: [
        {
          id: "submit",
          name: "访客信息提交",
        },
        {
          id: "check",
          name: "物联网安全监测",
        },
      ],
      phone: "",
	  uid:'',
      popShow: false, //控制授权手机号弹出框
      active: "", //点击的是哪个按钮
    };
  },
  mounted() {
		uni.showShareMenu({
			withShareTicket: true,
			menus: ["shareAppMessage"]
		});
		
    this.phone = uni.getStorageSync("registerPhone");
    getLogin();
  },
	//分享好友
	onShareAppMessage() {
		return {
			title: '环市东院区安防小程序',
			imageUrl: '../../static/share.jpg',
		};
	},
	//分享朋友圈
	// onShareTimeline() {
	// 	return {
	// 		title: '安防平台小程序',
	// 	}
	// },
  onShow() {
    let pages = getCurrentPages();
    const curPage = pages[pages.length - 1];
    this.currentPagePath = curPage.route;
    uni.setStorageSync("currentPagePath", this.currentPagePath);
  },
  methods: {
    // ...mapMutations(['setNavRect'])
    async handleClick(id) {
			// subscription();
      this.active = id;
      const sessionId = uni.getStorageSync("sessionId");
      //如果this.phone为空，先弹出获取手机号，得到手机号之后验证此人是都注册过
      if (!sessionId) {
        await getLogin();
      }
			// if(id === 'submit') {
				this.phone = uni.getStorageSync("registerPhone");
				if (this.phone) {
				  this.checkphone();
				} else {
					//弹获取手机号
				  this.popShow = true;
				}
			// } else {
			// 	// uni.showToast({
			// 	// 	title: '敬请期待',
			// 	// 	icon: 'none'
			// 	// });
			// }
    },
		//通过手机号检查是否存在这个用户
    async checkphone() {
      const res = await verifyPhone(this.phone);
      if (res === "用户未注册") {
        wx.reLaunch({
          url: "/pages/register/register",
        });
      } else {
				uni.setStorageSync("registerPhone", this.phone);
        await getUserProfile(); //获取人员信息
				
			if (uni.getStorageSync("userInfo")) {
			  const userInfo = JSON.parse(uni.getStorageSync("userInfo"));
			  this.uid = userInfo.id
			  if (userInfo.salt === "物业人员") {
				if(this.active === "submit") {
					subscription();
				}
			  } 
			}
				
        if (this.active === "submit") {
          wx.navigateTo({
            url: `/pages/information/information?id=${this.active}`,
          });
        }else {
			// 判断是否有这个路径
			const res = await getUserJurisdiction({uid:this.uid})
			if(res.indexOf('/pages/check/check') !== -1) {
				wx.navigateTo({
				  url: '/pages/check/check',
				});
			}else {
				uni.showToast({
					title: '非业务管理员无权访问',
					icon: 'none'
				});
			}
			
		}
      }
    },
		
		closePop() {
			this.popShow = false;
		}
  },
};
</script>

<style lang="scss" scoped>
.index {
	padding: 0 20rpx;
  padding-top: 40rpx;
  .menu{
		display: flex;
		justify-content: space-around;  
  }
  .menu-item {
    display: flex;
		justify-content: center;
		align-items: center;
		flex: 1;
    height: 320rpx;
    border-radius: 10rpx;
    color: #000;
    background-color: #eee;
    border: 1px solid gray;
		margin-right: 20rpx;
		line-height: 44rpx;
		&:last-child {
			margin-right: 0;
		}
  }
}
</style>
<style lang="scss">
.index {
  .u-popup__content {
    padding: 30rpx 40rpx;
		// padding-bottom: 0;
  }
}
</style>
