Component({
  properties: {
    title: {
      type: String,
      value: "",
    },
    showBack: {
      type: Boolean,
      value: false,
    },
    rightText: {
      type: String,
      value: "",
    },
    theme: {
      type: String,
      value: "blur",
    },
  },

  data: {
    statusBarHeight: 20,
    navBarHeight: 44,
    totalHeight: 64,
    sideWidth: 88,
  },

  lifetimes: {
    attached() {
      this.initMetrics();
    },
  },

  methods: {
    initMetrics() {
      const systemInfo = wx.getSystemInfoSync ? wx.getSystemInfoSync() : {};
      const menuRect = wx.getMenuButtonBoundingClientRect
        ? wx.getMenuButtonBoundingClientRect()
        : null;

      const statusBarHeight = systemInfo.statusBarHeight || 20;
      let navBarHeight = 44;
      let sideWidth = 88;

      if (menuRect && menuRect.top) {
        const gap = menuRect.top - statusBarHeight;
        navBarHeight = menuRect.height + gap * 2;
        sideWidth = Math.max(menuRect.width, 88);
      }

      this.setData({
        statusBarHeight,
        navBarHeight,
        totalHeight: statusBarHeight + navBarHeight,
        sideWidth,
      });
    },

    onTapBack() {
      const pages = getCurrentPages();
      if (pages.length > 1) {
        wx.navigateBack();
      } else {
        wx.switchTab({
          url: "/pages/index/index",
        });
      }
      this.triggerEvent("back");
    },

    onTapRight() {
      this.triggerEvent("righttap");
    },
  },
});
