<template>
	<view class="content">
		<view class="banner">
			<div class="custom-nav">我的</div>	
			<div class="user-container">
				<view class="avatar">
					<image v-if="userAvatar" class="photo" :src="userAvatar" mode="widthFix" @error="userAvatar=defaultAvatar"></image>
					<image v-else class="photo" :src="defaultAvatar" mode="widthFix"></image>
				</view>
				<view class="bottom">
					<view class="name">
						<text>{{userName}}</text>
					</view>	
					<div class="phone">{{phone}}</div>
				</view>						
			</div>
		</view>
		<view class="zhcx-table">
			<uni-list>
				<uni-list-item title="个人资料"
					link="navigateTo" rightText="修改"
					to="/pages/myCenter/personalData/personalData"
					showArrow></uni-list-item>
				<uni-list-item title="所属院校"
					:rightText="school"
					link="navigateTo"
					to="/pages/myCenter/personalData/personalData"
					showArrow></uni-list-item>
				<uni-list-item title="人脸录入" @click="faceRecording" clickable showArrow></uni-list-item>
			</uni-list>			
		</view>	
		<div class="logoutBt" @click="logout" v-if="isLogin">退出登录</div>
		<getPhone ref="getPhone" v-if="!isLogin" showByPlatform />	
		<view class="faceRecord-box" v-if="cameraModel">
			<camera device-position="front" flash="off" @error="error" class="camera"></camera>
			<button class="takePhotoBt" type="primary" @click="takePhoto">抓拍</button>
			<view>预览</view>
			<image class="facePhoto" mode="widthFix" :src="faceSrc"></image>
			<div class="camera-submit" @click="cameraSubmit">提交</div>
			<div class="camera-submit cancel" @click="cameraCancel">取消</div>
		</view>			
	</view>
</template>

<script>
	import {logout} from '@/api/user.js'
	import getPhone from '@/components/getPhone.vue'		
	export default{
		data(){
			return{
				defaultAvatar:"/static/components/avatar.png",
				userAvatar:"",
				userName:"--",
				phone:"--",
				school:"--",
				faceSrc:"",
				cameraModel:false,
				isLogin:false
			}
		},
		components:{
			getPhone
		},		
		onShow() {
			this.init();
		},	
		methods:{
			initPhoneModal(){
				const accountName=uni.getStorageSync('phone')
				if(!accountName){
					this.isLogin=false
					this.$nextTick(()=>{
						this.$refs['getPhone'].isAuthorize=false
					})
				}else{
					this.isLogin=true
				}			
			},			
			init(){
				this.initPhoneModal()
				let user=uni.getStorageSync('accountInfo');
				if(user){
					user=JSON.parse(user)
					this.userAvatar=user.avatar||this.defaultAvatar;
					this.userName=user.name;	
				}			
			},
			faceRecording(){
				this.cameraModel=true;		
			},
			takePhoto(){
				const ctx = uni.createCameraContext();
				ctx.takePhoto({
					quality: 'high',
					success: (res) => {
						this.faceSrc = res.tempImagePath
					}
				});				
			},
			cameraSubmit(){
				this.cameraModel=false;
				this.faceSrc = "";
			},
			cameraCancel(){
				this.cameraModel=false;
				this.faceSrc = "";			
			},
			logout(){
				uni.showModal({
					title:'提示！',
					content:"是否要退出登录",
					success(res) {
						if (res.confirm) {
							logout().then(()=>{
								uni.clearStorageSync()
								uni.switchTab({
									url:'/pages/index/index'
								})							
							})
						}					
					}
				})
			}			
		}
	}
</script>

<style lang="scss" scoped>
	.content{
		 position: relative;
		.banner{
			height: 580upx;
			background-image: url('/static/center/bg.png');
			background-repeat: no-repeat;
			background-size: 100% 100%;
			position: relative;	
			&::after{
				position: absolute;
				content: "";
				display: block;
				width: 440rpx;
				height: 632rpx;
				right: 0;
				top: 20px;
				background-image:url('/static/center/bgmask.png');
				background-repeat: no-repeat;
				background-size: 100% 100%;
				z-index: 1;
			}
			.custom-nav{
				position: fixed;
				text-align: center;
				top: 24px;
				left: var(--window-left);
				right: var(--window-right);
				height: 88rpx;
				line-height: 88rpx;
				z-index: 998;
				color: #333333;
				background-color: transparent;
				font-size: 32rpx;
				font-weight: 400;
				box-sizing: border-box;
				font-family: PingFang SC;
			}			
			.user-container{
				&{
					display: flex;
					justify-content: flex-start;
					align-items: center;
					position: absolute;
					bottom: 190rpx;
					left: 40rpx;
				}
				.avatar{
					width: 120rpx;
					height: 120rpx;
					border-radius: 50%;
					display: block;
					overflow: hidden;
					background: rgba(255,255,255,0.39);
					border: 2rpx solid #707070;
					.photo{
						width: 120rpx;
						height: 120rpx;
						border-radius: 50%;						
					}
				}
				.bottom{
					&{
						padding-left: 20rpx;
					}
					.name{
						font-size: 36rpx;
						font-family: PingFang SC;
						font-weight: bold;
						line-height: 50rpx;
						color: #FFFFFF;					
					}
					.phone{
						font-size: 28rpx;
						font-family: PingFang SC;
						font-weight: 400;
						line-height: 40rpx;
						color: #FFFFFF;	
						padding-top: 14rpx;
					}
				}
			}
		}
		.zhcx-table{
			width: 710rpx;
			position: absolute;
			left: 20rpx;
			top: 440rpx;
			background-color: #fff;
			z-index: 99;
			border-radius: 12px;
			overflow: hidden;
		}
		.logoutBt{
			width: 424rpx;
			height: 72rpx;
			line-height: 72rpx;	
			text-align: center;			
			position: fixed;
			bottom: 130rpx;
			left: 50%;
			transform: translateX(-50%);					
			background:#3D90F4;
			border-radius: 42rpx;	
			font-size: 32rpx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #FFFFFF;
			z-index: 999;
		}
		.faceRecord-box{
			position: fixed;
			top: 0;
			bottom: 0;
			left: 0;
			right: 0;
			z-index: 9999;
			background-color: #fff;
			display: flex;
			justify-content: center;
			align-items: center;
			flex-direction: column;
			.camera,.facePhoto{
				width: 450rpx;
				height: 450rpx;
				border-radius: 50%;
				border:2rpx solid #eaeaea;
			}
			.takePhotoBt{
				display: inline-block;
				margin: 40rpx 0;
			}
			.camera-submit{
				width: 424rpx;
				height: 72rpx;
				line-height: 72rpx;	
				text-align: center;							
				background:#3D90F4;
				border-radius: 42rpx;	
				font-size: 32rpx;
				font-family: PingFang SC;
				font-weight: 400;
				color: #FFFFFF;	
				margin: 40rpx auto;
				&.cancel{
					color: #fff;
					background-color: #ED834A;
					border-color: #ED834A;
					margin: 0 auto 50rpx;
				}
			}
		}
	}
</style>
