<template>
  <view>
  <view class="custom-navbar" :style="{paddingTop:`${statusBarHeight}rpx`,background:backgroundColor}">
    <view class="navbar-content"
		  :style="{'border-bottom':borderBottom}">
      <view class="left-area">
        <view class="back-btn" @click="goBack" v-if="showBack">
          <!-- <text class="iconfont icon-back">&#xe679;</text> -->
		  <image :src="leftImg" mode="aspectFit" class="back-icon"></image>
        </view>
        <slot name="left"></slot>
      </view>
      
      <text class="title" :style="'color:' + titleColor + ';'">{{ title }}</text>
      
      <view class="right-area">
        <slot name="right">
		  <image :src="headleSrc"
				 v-if="showHeadle"
				 mode="aspectFit"
				 class="iconfont icon-headle"
				 @click="onHeadleClick"></image>
        </slot>
      </view>
    </view>
	
  </view>
  
  <view class="header-top" :style="{ height: `${statusBarHeight + 88}rpx` }"></view>
  </view>
</template>

<script>
export default {
  name: 'CustomNavbar',
  props: {
    // 标题
    title: {
      type: String,
      default: ''
    },
    // 是否显示返回按钮
    showBack: {
      type: Boolean,
      default: true
    },
    // 是否显示用户图标
    showHeadle: {
      type: Boolean,
      default: false
    },
	headleSrc: {
      type: String,
      default: ''
	},
    // 背景色
    backgroundColor: {
      type: String,
      default: '#ffffff'
    },
    // 标题颜色
    titleColor: {
      type: String,
      default: '#333333'
    },
    // 下划线
    borderBottom: {
      type: String,
      default: '1rpx solid rgba(0, 0, 0, 0.05)'
    },
	nowStyle: {
      type: String,
      default: '1rpx solid rgba(0, 0, 0, 0.05)'
    },
	leftImg:{
		type: String,
		default: '/static/images/back.png'
	},
	backgroundImage: {
      type: String
    },
  },
  data() {
    return {
      statusBarHeight: 0,
    }
  },
 async created() {
     // 获取状态栏高度
     const systemInfo = await uni.getSystemInfoSync()
 	let screenWidth = systemInfo.screenWidth;
     this.statusBarHeight =  this.pxToRpx(systemInfo.statusBarHeight,screenWidth);
   },
   methods: {
     pxToRpx(px,screenWidth) {
      if (typeof px !== 'number') {
        throw new Error('请传入数字类型的px值');
      }
      return px * (750 / screenWidth);
    },
    goBack() {
      this.$emit('back')
      uni.navigateBack({
        delta: 1
      })
    },
    onHeadleClick() {
      this.$emit('onHeadleClick')
    }
  }
}
</script>

<style>
.custom-navbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 9999;
}
.header-top {
  height: 88rpx;
  background: transparent;
}
.navbar-content {
  height: 88rpx;
  display: flex;
  flex-flow: row nowrap;
  align-items: center;
  justify-content: space-between;
  padding: 0 30rpx 0 24rpx;
}

.right-area, .icon-headle {
  width: 46rpx;
  height: 46rpx;
  display: flex;
  align-items: center;
}
.left-area, .back-icon {
  width: 36rpx;
  height: 36rpx;
  display: flex;
  align-items: center;
}

.back-btn {
  /* padding: 20rpx;
  margin-left: -20rpx; */
}

.title {
  /* position: absolute;
  left: 50%;
  transform: translateX(-50%); */
  font-size: 36rpx;
  font-weight: 500;
  color: #333333;
}

/* 小程序适配 */
/* #ifdef MP-WEIXIN */
.custom-navbar {
  padding-top: calc(var(--status-bar-height) + constant(safe-area-inset-top));
  padding-top: calc(var(--status-bar-height) + env(safe-area-inset-top));
}
/* #endif */
</style> 