<template>
	<view class="nav" :style="navHeight" :class="className">
		<view class="nav_bar" :style="navBarHeight">
			<view class="icon" v-if="backVisible">
				<template v-if="backVisible">
					<view class="iconfont icon_home" @click="BackPage" :style="iconfontHeight" v-if="homeVisible">
						<view class="icomoon icon-home_black">
							<i class="inline path1"></i>
							<i class="inline path2"></i>
							<i class="inline path3"></i>
						</view>
					</view>
					<i class="iconfont icon-zuojiantou" @click="BackPage" :style="iconfontHeight" v-else></i>
				</template>
				<slot name="icon"></slot>
			</view>
			<view class="title">
				<view class="name">
					<slot name="content"></slot>
				</view>
				<view class="save" :class="{active: notNullVisible}" :style="saveHeight" v-if="saveVisible" @click="save">{{ saveText }}</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			className: {
				type: String
			},
			backVisible: {
				type: [Boolean, String],
				default: false
			},
			backIndex: {
				type: Number,
			},
			scroll: {
				type: [Number, String],
				default: 0
			},
			saveVisible: {
				type: [Boolean, String],
				default: false
			},
			saveText: {
				type: String,
				default: '保存'
			},
			notNull: {
				type: String,
			}
		},
		data() {
			return {
				StatusBar: this.System.statusBarHeight,
				CustomBar: this.CustomBar,
				opacityValue: 0,
				notNullVisible: false,
				homeVisible: false // 左上角是否是首页图标 反之是后退图标
			}
		},
		watch: {
			scroll (value) {
				this.opacityValue = value * 0.005
			},
			notNull (value) {
				this.getNotNull(value)
			}
		},
		computed: {
			navHeight() {
				return `height: ${this.CustomBar}px;`;
			},
			navBarHeight() {
				let capsule = uni.getMenuButtonBoundingClientRect()
				let paddingRight = this.System.windowWidth - capsule.left
				return `height: ${this.CustomBar - this.StatusBar}px; padding-top: ${this.StatusBar}px; padding-right: ${paddingRight}px;`;
			},
			iconfontHeight() {
				return `height: ${this.CustomBar - this.StatusBar}px; width: ${this.CustomBar - this.StatusBar}px;`;
			},
			saveHeight() {
				let capsule = uni.getMenuButtonBoundingClientRect()
				return `height: ${capsule.height}px;`;
			}
		},
		created() {
			this.saveVisible && this.getNotNull(this.notNull)
			const pages = getCurrentPages()
			let currentRoute  = pages[pages.length - 1].route // 获取当前页⾯路由
			this.homeVisible = ['pages/share/authSchool', 'pages/share/team', 'pages/share/communication'].includes(currentRoute)
		},
		methods: {
			// 非空判断
			getNotNull (value) {
				this.notNullVisible = !value
			},
			// 后退
			BackPage() {
				let pages = getCurrentPages(); // 当前页面
				let beforePage = pages[pages.length - 2]; // 前一个页面
				if (getCurrentPages().length < 2 && 'undefined' !== typeof __wxConfig) {
					let url = '/' + __wxConfig.pages[0]
					return uni.redirectTo({
						url
					})
				}
				uni.navigateBack({
					delta: this.backIndex || 1
				})
			},
			// 保存
			save () {
				this.$emit('savePick', 'save')
			}
		}
	}
</script>

<style lang="scss">
	.nav {
		position: fixed;
		top: 0;
		right: 0;
		left: 0;
		z-index: 99;
		&.nav_F6 {
			background-color: $F6;
		}
		&.black_nav {
			// background-color: $E32121;
			.nav_bar {
				.icon {
					.iconfont {
						color: $D2D2D2;
					}
				}
				.title {
					color: $D2D2D2;
				}
			}
		}
		.nav_bar {
			display: flex;
			align-items: center;
			padding: 0 26rpx;
		
			.icon {
				margin-right: -12rpx;
				margin-left: -26rpx;
				position: relative;
				z-index: 2;
				
				.iconfont {
					font-size: 42rpx;
					float: left;
					width: 96rpx;
					color: $bg-0;
					display: flex;
					align-items: center;
					justify-content: center;
					&.icon_home {
						margin-right: 10rpx;
						.pic {
							height: 40rpx;
							width: 40rpx;
						}
					}
				}
			}
			
			.title {
				padding-right: 26rpx;
				font-size: 36rpx;
				font-weight: 600;
				color: $bg-0;
				flex: 1;
				display: flex;
				align-items: center;
				
				.name {
					flex: 1;
				}
				
				.save {
					background-color: $mc;
					border-radius: 100rpx;
					margin-left: 20rpx;
					font-size: 28rpx;
					color: $white;
					width: 144rpx;
					height: 64rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					&.active {
						background-color: $dcdcdc;
						color: $B18181;
					}
				}
			}
		}
	}
</style>
