<style lang="less">
.component-page-top-bar-custom {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  padding-bottom: 16px;
  box-sizing: border-box;
  z-index: 3;
  .page-top-bar {
    position: absolute;
    bottom: 16px;
    left: 0;
    right: 0;
    min-height: 30px;
    .icon {
      position: absolute;
      left: 32rpx;
      top: 0;
      width: 76rpx;
      height: 30px;
      line-height: 30px;
      .icon-content {
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
      }
      .icon_home {
        width: 64rpx;
        height: 64rpx;
      }
      .icon_define {
        width: 24rpx;
        height: 45rpx;
      }
    }
    .title {
      font-weight: 500;
      font-size: 34rpx;
      line-height: 48rpx;
      color: rgba(0, 0, 0, 0.8);
      text-align: center;
    }
  }
}
</style>

<template>
  <div
    class="component-page-top-bar-custom"
    :style="{'height': pageTopBarHeight + 'px', 'background-color': bgColor}"
  >
    <div class="page-top-bar">
      <slot></slot>
      <div v-if="icon || iconType" class="icon" v-on:click="emitIconClick">
        <block v-if="iconType == 'home' || isFirstPage">
          <image class="icon_home icon-content" src="{{h5StaticPath}}/icon_navigate_home.svg" />
        </block>
        <block v-else>
          <image class="icon_define icon-content" src="{{icon}}" />
        </block>
      </div>
      <div class="title" v-if="title">{{title}}</div>
    </div>
  </div>
</template>

<script>
import wepy from '@wepy/core';
import config from '@/config';

const BottomHeight = 16;
('单位px');
wepy.component({
  props: {
    bgColor: {
      type: String,
      default: 'transparent'
    },
    iconType: String,
    icon: String,
    iconClickedEmitId: String,
    title: String
  },
  ready() {
    let menuButtonBoundingClientRect = wx.getMenuButtonBoundingClientRect();

    // 加上右上角胶囊按钮的边框2
    this.pageTopBarHeight =
      menuButtonBoundingClientRect.top +
      menuButtonBoundingClientRect.height +
      BottomHeight;
    this.$emit('component-page-top-bar-custom-ready', this.pageTopBarHeight);

    let pages = getCurrentPages();
    if (pages.length <= 1) {
      this.isFirstPage = true;
    }
  },
  data: {
    isFirstPage: false,
    pageTopBarTop: 0,
    pageTopBarHeight: 72,
    h5StaticPath: config.h5StaticPath
  },
  methods: {
    emitIconClick(e) {
      if (this.iconClickedEmitId) {
        this.$emit(this.iconClickedEmitId);
      } else {
        if (this.isFirstPage) {
          wx.redirectTo({ url: '/pages/homev2' });
        } else {
          wx.navigateBack();
        }
      }
    }
  }
});
</script>
