<template>
  <view class="navbar">
    <!-- 状态栏占位 -->
    <view
      v-if="safeAreaInsetTop"
      class="status-bar"
      :style="{ height: statusBarHeight + 'px', backgroundColor: bgColor }"
    ></view>

    <!-- 导航栏主体 -->
    <view
      class="navbar-content"
      :class="[contentClass, fixed ? 'navbar-fixed' : '', bordered ? 'navbar-border' : '']"
      :style="{
        backgroundColor: bgColor,
        height: navHeight + 'px',
        top: fixed ? (safeAreaInsetTop ? statusBarHeight : 0) + 'px' : '0',
      }"
    >
      <!-- 左侧区域 -->
      <view class="navbar-left" @click="handleClickLeft">
        <view v-if="leftArrow" class="back-icon">
          <view class="i-carbon-chevron-left text-[40rpx] text-[#333] font-semibold icon-class" />
        </view>
        <slot name="left"></slot>
      </view>

      <!-- 中间标题区域 -->
      <view class="navbar-title">
        <slot name="title">
          <text class="title-text">{{ title }}</text>
        </slot>
      </view>

      <!-- 右侧区域 -->
      <view class="navbar-right">
        <slot name="right"></slot>
      </view>
    </view>

    <!-- 占位元素 -->
    <view
      v-if="placeholder && fixed"
      :style="{
        height: `${navHeight}px`,
        backgroundColor: bgColor,
      }"
    ></view>
    <slot name="background"></slot>
  </view>
</template>

<script lang="ts" setup>
import { computed } from 'vue'

const props = defineProps({
  title: {
    type: String,
    default: '',
  },
  leftArrow: {
    type: Boolean,
    default: false,
  },
  fixed: {
    type: Boolean,
    default: false,
  },
  placeholder: {
    type: Boolean,
    default: false,
  },
  bordered: {
    type: Boolean,
    default: true,
  },
  safeAreaInsetTop: {
    type: Boolean,
    default: true,
  },
  bgColor: {
    type: String,
    default: '#ffffff',
  },
  contentClass: {
    type: String,
    default: 'justify-between',
  },
})

const emit = defineEmits(['clickLeft'])

// 获取系统信息
const systemInfo = uni.getWindowInfo()
const deviceInfo = uni.getDeviceInfo()
const statusBarHeight = systemInfo.statusBarHeight || 0

// 动态计算导航栏高度
const navHeight = computed(() => {
  // 获取屏幕信息
  const { screenWidth } = systemInfo
  const { platform } = deviceInfo

  // 将px转换为rpx的比例
  const ratio = 750 / screenWidth

  // 根据平台设置不同的导航栏高度
  if (platform === 'ios') {
    return 88 / ratio // iOS 一般是 44pt，转换为px
  } else if (platform === 'android') {
    return 96 / ratio // Android 一般是 48dp，转换为px
  } else {
    return 88 / ratio // 默认值
  }
})

const handleClickLeft = () => {
  emit('clickLeft')
}
</script>

<style scoped>
.navbar {
  width: 100%;
}

.status-bar {
  width: 100%;
  background-color: inherit;
}

.navbar-content {
  width: 100%;
  display: flex;
  align-items: center;
  /* justify-content: space-between; */
  padding: 0 16rpx;
  box-sizing: border-box;
  background-color: #fff;
}

.navbar-fixed {
  position: fixed;
  left: 0;
  width: 100%;
  z-index: 99;
}

.navbar-border {
  border-bottom: 1rpx solid #eee;
}

.navbar-left {
  display: flex;
  align-items: center;
  height: 100%;
}

.back-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
}

.navbar-title {
  /* flex: 1; */
  text-align: center;
  overflow: hidden;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.title-text {
  font-size: 34rpx;
  color: #333;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.navbar-right {
  display: flex;
  align-items: center;
  min-width: 52rpx;
  justify-content: flex-end;
  height: 100%;
}

.back-text {
  font-size: 48rpx;
  font-style: normal;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #333;
}
</style>
