<template>
  <view
    :class="themeName"
    :style="{
      paddingTop: paddingTop + 'px',
      width: '100%',
      overflow: pageOverflow,
    }"
  >
    <!-- #ifndef H5 -->
    <k-navigation-bar
      :title="title"
      :backDelta="backDelta"
      :transparent="transparent"
      :navStyle="navStyle"
      :backToTabbar="backToTabbar"
      :showLeftCapsule="showLeftCapsule"
      :showHomeButton="showHomeButton"
      :rightLinkColor="rightLinkColor"
      :rightLink="rightLink"
    >
      <slot name="title" />
      <template v-slot:right>
        <slot name="right" />
      </template>
    </k-navigation-bar>
    <!-- #endif -->
    <!-- #ifdef H5 -->
    <k-navigation-bar-h5
      v-if="!isWeixinH5"
      :title="title"
      :backDelta="backDelta"
      :transparent="transparent"
      :navStyle="navStyle"
      :backToTabbar="backToTabbar"
      :showLeftCapsule="showLeftCapsule"
      :showHomeButton="showHomeButton"
      :rightLinkColor="rightLinkColor"
      :rightLink="rightLink"
    >
      <slot name="title" />
      <template v-slot:right>
        <slot name="right" />
      </template>
    </k-navigation-bar-h5>
    <!-- #endif -->
    <slot />
    <view
      v-if="safeArea"
      :style="{ backgroundColor: safeAreaColor }"
      class="safe-area"
    />
  </view>
</template>

<script lang="ts">
import navBarProps from '../k-navigation-bar/props';
import { getNavigationBarHeight } from '../../utils/device';
import { isInWeixinH5 } from '../../utils';
import Vue, { VueConstructor } from 'vue';
type NavBarPropType = {
  [Property in keyof typeof navBarProps.props]: typeof navBarProps.props[Property]['default'];
};
export default (Vue as VueConstructor<Vue & NavBarPropType>).extend({
  name: 'k-page',
  mixins: [navBarProps],
  data() {
    return {
      paddingTop: 0,
      isWeixinH5: false,
    };
  },
  props: {
    themeName: {
      type: String,
      default: '',
    },
    safeArea: {
      type: Boolean,
      default: true,
    },
    safeAreaColor: {
      type: String,
      default: 'transparent',
    },
    pageOverflow: {
      type: String,
      default: '',
    },
  },
  // #ifdef H5
  watch: {
    title: {
      handler(newVal) {
        // eslint-disable-next-line no-console
        console.log('change title in k-page: ', newVal);
        if (newVal) {
          setTimeout(() => {
            uni.setNavigationBarTitle({
              title: newVal,
            });
          }, 500);
        }
      },
      deep: true,
      immediate: true,
    },
  },
  // #endif
  created: function () {
    // console.log('k-page created');
    this.isWeixinH5 = isInWeixinH5();
    this.paddingTop =
      this.transparent || this.isWeixinH5 ? 0 : getNavigationBarHeight();
  },
});
</script>

<style scoped lang="scss">
.navigation-bar {
  width: 100%;
  background: var(--k-bg-color-navigation-bar, #f4f7ff);
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1001;
}

.transparent {
  background: transparent;
}

.capsule {
  position: absolute;
  background: rgba(0, 0, 0, 0.15);
  border: 0.5px solid rgba(255, 255, 255, 0.25);
  display: flex;
  align-items: center;
}

.back,
.home {
  flex: 1;
  display: flex;
  align-items: center;
}

.back {
  padding-left: 10px;
}

.divider {
  width: 0.5px;
  height: 18px;
  background: rgba(255, 255, 255, 0.25);
}

.home {
  flex-direction: row-reverse;
  padding-right: 10px;
}

.title {
  color: #fff;
  font-size: 17px;
  line-height: 24px;
  font-weight: 500;
  position: absolute;
  bottom: 10px;
  left: 50%;
  transform: translateX(-50%);
}

@supports (bottom: constant(safe-area-inset-bottom)) or
  (bottom: env(safe-area-inset-bottom)) {
  .safe-area {
    /* stylelint-disable-next-line function-no-unknown */
    height: constant(safe-area-inset-bottom);
    /* stylelint-disable-next-line */
    height: env(safe-area-inset-bottom);
  }
}
</style>
