<template>
	<view class="nav-bar" :style="{ backgroundColor: bgColor }">
		<view class="status-bar" :style="{ height: statusBarHeight + 'px' }"></view>
		<view class="nav-content">
			<view class="left-box" @click="goBack" v-if="showBack">
				<!-- 使用 H5 原生 CSS 绘制返回箭头，防止打包后图标丢失 @Author: Antigravity -->
				<view class="back-arrow" :style="{ borderColor: color }"></view>
			</view>
			<view class="title-box">
				<text class="title-text" :style="{ color: color }">{{ title }}</text>
			</view>
			<view class="right-box">
				<slot name="right"></slot>
			</view>
		</view>
	</view>
</template>

<script setup>
import { ref, onMounted } from 'vue'

const props = defineProps({
	title: {
		type: String,
		default: ''
	},
	showBack: {
		type: Boolean,
		default: true
	},
	bgColor: {
		type: String,
		default: '#ffffff'
	},
	color: {
		type: String,
		default: '#000000'
	}
})

const statusBarHeight = ref(44)

onMounted(() => {
	const sysInfo = uni.getSystemInfoSync()
	statusBarHeight.value = sysInfo.statusBarHeight || 44
})

const goBack = () => {
	uni.navigateBack()
}
</script>

<style lang="scss" scoped>
.nav-bar {
	position: sticky;
	top: 0;
	z-index: 999;
	width: 100%;
}

.nav-content {
	height: 44px;
	display: flex;
	align-items: center;
	padding: 0 32rpx;
	position: relative;
}

.left-box {
	position: absolute;
	left: 32rpx;
	z-index: 10;
	/* 扩大点击区域 */
	padding: 20rpx 40rpx 20rpx 0;
}

/* 原生 CSS 绘制返回箭头 @Author: Antigravity */
.back-arrow {
	width: 22rpx;
	height: 22rpx;
	border-left: 4rpx solid;
	border-top: 4rpx solid;
	transform: rotate(-45deg);
	background: transparent;
}

.title-box {
	flex: 1;
	display: flex;
	justify-content: center;
	align-items: center;
}

.title-text {
	font-size: 32rpx;
	font-weight: bold;
}

.right-box {
	position: absolute;
	right: 32rpx;
}
</style>
