<template>
	<view class="container">
		<view class="robot">
			<image src="../../static/nav_robot.png" mode="aspectFit"></image>
		</view>
		<view class="titleText">SmartAssistant</view>
		<view class="navigatorContent">
			<view class="navigator_box" v-for="(item, index) in dataList" :key="index" @click="clickToItem(item, index)">
				<view v-if="currenIndex == index" class="navigator_box_selected">
					<image :src="item.s_icon" mode="aspectFit"></image>
				</view>
				<view v-else class="navigator_box_normal">{{ item.t }}</view>
			</view>
		</view>
		<view class="userState_login" v-if="is_login == false">
			<view class="user_regist" @click="clickToRegist">注册</view>
			<view class="user_login" @click="clickToLogin">登录</view>
		</view>
		<view class="userState_logout userState_login" v-else @click="clickToLogout">退出登录</view>
	</view>
</template>

<script>
import { nextTick } from "vue"
	export default {
		data() {
			return {
				dataList: [{'t': '首页', 's_icon': '/static/navigator/navigator_home.png'},
				           {'t': 'AI聊天', 's_icon': '/static/navigator/navigator_chat.png'},
						   {'t': 'AI画图', 's_icon': '/static/navigator/navigator_draw.png'}],
				currenIndex: this.selectedIndex,
				is_login: false
			}
		},
		props: {
			selectedIndex: {
				type: Number,
				default: 0
			}
		},
		watch: {
			selectedIndex: {
				handler(newVal) {
					this.currenIndex = newVal
				},
				immediate: true
			},
		},		
		mounted() {
			this.is_login = this.tools.is_login()
			this.$emit('clickToIndex', this.currenIndex)
			
			let self = this
			uni.$on(this.config.notification.login_state_changed, function() {
				self.is_login = self.tools.is_login()
			})
		},
		onUnload() {
			uni.$off(this.config.notification.login_state_changed)
		},
		methods:{
			clickToItem(item, index) {
				if(this.currenIndex === index) {
					return
				}
				this.$emit('clickToIndex', index)
			},
			clickToRegist() {
				uni.showToast({
					icon:'none',
					title:'暂时无法注册',
					duration:2000
				})
			},
			clickToLogin() {
				if(this.tools.is_login()) {
				   console.log('已登录了')
				   return	
				} 
				uni.navigateTo({
					url:'/pages/login/login'
				})
			},
			clickToLogout() {
				let self = this
				uni.showModal({
					content:'是否退出登录?',
					success: function(res) {
						if(res.confirm) {
							self.tools.request_logout(self, (success)=>{
								if(success === true) {
									uni.showToast({
										title:'已退出登录',
										duration:2000
									})
								}
							})
						}
					}
				})
			}
		}
		
	}
</script>

<style lang="scss">
	.container{
		position: fixed;
		z-index: 100;
		width: 100%;
		height: 60px;
		top: 0%;
		left: 0;
		background: linear-gradient(to right, #303373, #C9A391);
		display: flex;
		box-sizing: border-box;
		align-items: center;
		.robot{
			flex-shrink: 0;
			width: 48px;
			height: 48px;
			margin-left: 15px;
			image{
				width: 100%;
				height: 100%;
			}
		}
		.titleText{
			display: flex;
			margin-left: 10px;
			// height: 100%;
			font-size: 30px;
			font-weight: 500;
			color: #ffffff;
		}
		.navigatorContent{
			display: flex;
			flex-direction: row;
			height: 100%;
			align-items: center;
			
			.navigator_box{
				display: flex;
				margin-left: 30px;
				width: 80px;
				height: 67px;
				justify-content: center;
				align-items: center;
				
				.navigator_box_normal{
					font-size: 20px;
					color: #ffffff;
				}
				.navigator_box_selected{
					width: 55px;
					height: 40px;
					image{
						width: 100%;
						height: 100%;
					}
				}
			}
		}
		
		.userState_login{
			position: absolute;
			display: flex;
			box-sizing: border-box;
			height: 100%;
			top: 0px;
			right: 15px;
			align-items: center;
			.user_regist{
				display: flex;
				width: 65px;
				height: 28px;
				border-radius: 28px;
				font-size: 16px;
				color: #878787;
				background-color: #ffffff;
				justify-content: center;
				align-items: center;
			}
			.user_login{
				display: flex;
				width: 65px;
				height: 28px;
				border-radius: 28px;
				font-size: 16px;
				color: #ffffff;
				background-color: #4C9CF1;
				justify-content: center;
				align-items: center;
				margin-left: 15px;
			}
		}
		.userState_logout{
			width: 145px;
			color: #ffffff;
			font-size: 18px;
			justify-content: center;
		}
	}
</style>