<template>
	<view>
		<view v-if="fixed" :style="{ height: totalHeight + 'px' }"></view>
		<view class="mall-navbar" :style="navbarStyle">
			<view :style="{ height: statusBarHeight + 'px' }"></view>
			<view class="nav-bar" :style="{ height: navBarHeight + 'px' }">
				<view class="nav-left" @tap="handleBack">
					<text v-if="isBack && pagesNum > 1" class="back-icon">‹</text>
					<text v-else-if="isBack" class="home-icon">⌂</text>
				</view>
				<view class="nav-center">
					<view v-if="showSearch" class="search-box" @tap="$emit('search')">
						<text class="search-icon">🔍</text>
						<text class="search-text">{{ searchPlaceholder }}</text>
						<view v-if="searchTag" class="search-tag">
							<text>{{ searchTag }}</text>
							<text class="search-tag-close" @tap.stop="$emit('clearSearch')">✕</text>
						</view>
					</view>
					<text v-else class="nav-title">{{ title }}</text>
				</view>
				<view class="nav-right">
					<slot name="right">
						<view v-if="showStyleToggle" class="style-toggle" @tap="$emit('toggleStyle')">
							<text v-if="currentStyle === 'col-2-20'" class="toggle-icon">☰</text>
							<text v-else class="toggle-icon">⊞</text>
						</view>
					</slot>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	name: 'mall-navbar',
	props: {
		title: {
			type: String,
			default: ''
		},
		isBack: {
			type: Boolean,
			default: true
		},
		showSearch: {
			type: Boolean,
			default: false
		},
		searchPlaceholder: {
			type: String,
			default: ''
		},
		searchTag: {
			type: String,
			default: ''
		},
		showStyleToggle: {
			type: Boolean,
			default: false
		},
		currentStyle: {
			type: String,
			default: 'col-2-20'
		},
		background: {
			type: String,
			default: '#ffffff'
		},
		color: {
			type: String,
			default: '#333333'
		},
		fixed: {
			type: Boolean,
			default: true
		}
	},
	data() {
		return {
			statusBarHeight: 0,
			navBarHeight: 44,
			pagesNum: 1
		}
	},
	computed: {
		totalHeight() {
			return this.statusBarHeight + this.navBarHeight
		},
		navbarStyle() {
			return {
				background: this.background,
				color: this.color,
				position: this.fixed ? 'fixed' : 'relative',
				top: 0,
				left: 0,
				right: 0,
				zIndex: 999
			}
		}
	},
	created() {
		const sysInfo = uni.getSystemInfoSync()
		this.statusBarHeight = sysInfo.statusBarHeight || 0
		this.pagesNum = getCurrentPages().length
	},
	methods: {
		handleBack() {
			if (!this.isBack) return
			if (this.pagesNum > 1) {
				uni.navigateBack({ delta: 1 })
			} else {
				uni.switchTab({ url: '/pages/index/index' })
			}
		}
	}
}
</script>

<style lang="scss" scoped>
.mall-navbar {
	width: 100%;
}
.nav-bar {
	display: flex;
	align-items: center;
}
.nav-left {
	width: 80rpx;
	height: 100%;
	display: flex;
	align-items: center;
	justify-content: center;
	flex-shrink: 0;
}
.back-icon {
	font-size: 48rpx;
	font-weight: bold;
	color: #333;
	line-height: 1;
}
.home-icon {
	font-size: 44rpx;
	color: #333;
	line-height: 1;
}
.nav-center {
	flex: 1;
	display: flex;
	align-items: center;
	justify-content: center;
	overflow: hidden;
}
.nav-title {
	font-size: 36rpx;
	font-weight: bold;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}
.search-box {
	flex: 1;
	height: 64rpx;
	background: #f5f5f5;
	border-radius: 32rpx;
	display: flex;
	align-items: center;
	padding: 0 24rpx;
}
.search-icon {
	font-size: 26rpx;
	margin-right: 10rpx;
	flex-shrink: 0;
}
.search-text {
	font-size: 26rpx;
	color: #999;
}
.search-tag {
	display: flex;
	align-items: center;
	background: #e8e8e8;
	border-radius: 20rpx;
	padding: 2rpx 12rpx;
	margin-left: 10rpx;
}
.search-tag text {
	font-size: 22rpx;
	color: #666;
}
.search-tag-close {
	margin-left: 6rpx;
	font-size: 20rpx;
	color: #999;
}
.nav-right {
	width: 80rpx;
	height: 100%;
	display: flex;
	align-items: center;
	justify-content: center;
	flex-shrink: 0;
}
.style-toggle {
	display: flex;
	align-items: center;
	justify-content: center;
}
.toggle-icon {
	font-size: 40rpx;
	color: #333;
}
</style>