<template>
	<view class="u-nav-bar" :style="{color: color,background: bg}">
		<uni-icons type="arrow-left" class="left-icon" size="30" :color="color" @click="toBack"></uni-icons>
		<view class="title" :title="title" v-if="title">
			{{title}}
		</view>
	</view>
</template>

<script>
	export default {
		name: "u-nav-bar",
		props: {
			title: { // 页面标题
				type: String,
				default: ''
			},
			color: { // 页面颜色
				type: String,
				default: 'white'
			},
			bg: { // 底色颜色
				type: String,
				default: ''
			},
		},
		computed: {
			showNavBar() {
				return Boolean(this.$listeners['on-left']) && this.prevPage
			}
		},
		data() {
			return {
				prevPage: null
			};
		},
		mounted() {
			let pages = getCurrentPages() // 获取栈实例
			let page = pages[pages.length - 1] // 获取当前页面的数据，包含页面路由
			this.prevPage = pages[pages.length - 2] // 获取上个页面的数据，包含页面路由
		},
		methods: {
			toBack() {
				if (Boolean(this.$listeners['on-left'])) {
					this.$emit('on-left')
				} else if (this.prevPage) {
					uni.navigateBack()
				} else {
					console.log('不存在上一页面')
					uni.switchTab({
						url: '/pages/index/index'
					});
				}

			}
		}
	}
</script>

<style lang="scss" scoped>
	.u-nav-bar {
		position: sticky;
		top: 0;
		left: 0;
		z-index: 5;
		width: 100%;
		height: $u-nav-bar-height;
		// padding: 0 32rpx;
		display: flex;
		flex-direction: column;
		justify-content: space-around;
		align-items: flex-start;

		.left-icon {
			display: inline-block;
			cursor: pointer;
			padding: 0 24rpx;
		}

		.title {
			font-size: 36rpx;
			font-weight: bold;
			padding: 0 32rpx;
		}
	}
</style>
