<template>
	<view id="home">
		<teacher-home class="flex-box-page" v-if="userType==1"></teacher-home>
		<pupil-home class="flex-box-page" v-if="userType==2"></pupil-home>
	</view>
</template>
<script>
	import {
		systemUserPersonalData,
	} from '@/pages/api/index.js'
	import teacherHome from '@/pages/views/teacherPage/teacherHome'
	import pupilHome from '@/pages/views/pupilPage/pupilHome'
	export default {
		name: "home",
		components: {
			teacherHome,
			pupilHome,
		},
		data() {
			return {
				userType: 0,
			}
		},
		onLoad() {

		},
		onShow() {
			if(uni.getStorageSync('patrolLoginType')){
				uni.reLaunch({
					url: '/pages_safetyCheck/views/safetyCheck',
				});
				return
			}
			uni.removeStorageSync('planSensorList');
			if (uni.getStorageSync('token') && uni.getStorageSync('userId') && uni.getStorageSync('userType')) {
				this.systemUserPersonalData();
				//写入用户
			} else {
				uni.removeStorageSync('token');
				uni.removeStorageSync('userId');
				uni.removeStorageSync('userType');
				uni.redirectTo({
					url: '/pages/views/login/login',
				});
			}
		},
		methods: {
			async systemUserPersonalData(){
				const {
					data
				} = await systemUserPersonalData();
				if (data.code == 200) {
					// isInitPwd 初始密码 true 是
					// isTutorExist 导师 true 有
					// isPhoneExist 电话 true 有
					// userType 1教职工 2学生
					if(data.data.userType == 1 && !data.data.isPhoneExist){
						//教职工
						uni.redirectTo({
							url: '/pages_basics/views/completeInformation/completeInformation',
						});
					}else if(data.data.userType == 2 &&(!data.data.isTutorExist || !data.data.isPhoneExist)){
						//学生
						uni.redirectTo({
							url: '/pages_basics/views/completeInformation/completeInformation',
						});
					}else if(data.data.isInitPwd){
						this.userType = uni.getStorageSync('userType')
						// 初始密码提示 每次登录只提示一次
						if(uni.getStorageSync('isInitPwd')){
							uni.setStorageSync('isInitPwd', false);
							this.passwrodTips();
						}
					}else{
						this.userType = uni.getStorageSync('userType')
					}
				}
			},
			//初始密码修改提示
			passwrodTips(){
				uni.showModal({
					title: '提示',
					cancelColor: '#999999',
					confirmColor: '#0183FA',
					content: '为了保证信息安全，是否修改默认密码',
					cancelText:'稍后修改',
					confirmText:'修改密码',
					success(res) {
						if (res.confirm) {
							// console.log('确定')
							uni.navigateTo({
								url: '/pages_basics/views/editPassword/editPassword',
							});
						} else if (res.cancel) {
							// console.log('取消')
						}
					}
				})
			},
		},
		onHide() {
			this.userType = 0;
		},
		onUnload() {
			this.userType = 0;
		}
	}
</script>

<style lang="stylus" scoped>
	#home {
		width:100%;
		height:100%;
		display: flex;
		flex-direction: column;
		overflow: hidden;
		.flex-box-page{
			flex:1;
			display: flex;
			flex-direction: column;
			overflow: hidden;
		}
	}
</style>
