<template>
  <view>
    <view class="cu-custom" :style="[{height:CustomBar + 'px'}]">
      <view class="cu-bar fixed" :style="style" :class="[bgImage!=''?'none-bg text-white bg-img':'',bgColor]">
        <view class="action" @tap="BackPage" v-if="isBack">
          <text class="cuIcon-back"></text>
          <slot name="backText"></slot>
        </view>

        <view class="action" @tap="gotoIndex" v-if="isHome">
          <text class="cuIcon-home"></text>
        </view>

        <slot name="left"></slot>
        <view class="content" :style="[{top:StatusBar + 'px'}]">
          <slot name="content"></slot>
          {{title}}
        </view>
        <slot name="right"></slot>
      </view>
    </view>
  </view>
</template>

<script>
import { APP_INDEX_PAGE_PATH, STUDENT_PAGE_PATH, MINE_PAGE_PATH } from '../common/tabbarStatic'
export default {
  data () {
    return {
      StatusBar: this.StatusBar,
      CustomBar: this.globalCustomBarHeight
    }
  },
  name: 'cu-custom',
  computed: {
    style () {
      const StatusBar = this.StatusBar
      const CustomBar = this.CustomBar
      const bgImage = this.bgImage

      let style = `height:${CustomBar}px;padding-top:${StatusBar}px;`
      if (this.bgImage) {
        style = `${style}background-image:url(${bgImage});`
      }
      return style
    }
  },
  props: {
    title: {
      type: String,
      default: ''
    },
    bgColor: {
      type: String,
      default: 'bg-white'
    },
    isBack: {
      type: [Boolean, String],
      default: false
    },
    backDelta: {
      type: Number,
      default: 1
    },
    isHome: {
      type: Boolean,
      default: false
    },
    bgImage: {
      type: String,
      default: ''
    },
    backUrl: { // 返回的urlType 对应urlmap
      type: String,
      default: ''
    }
  },
  methods: {
    BackPage () {
      if (this.backUrl) {
        if ([APP_INDEX_PAGE_PATH, STUDENT_PAGE_PATH, MINE_PAGE_PATH].includes(this.backUrl)) {
          return uni.switchTab({ url: this.backUrl })
        } else {
          return uni.redirectTo({ url: this.backUrl })
        }
      }

      // eslint-disable-next-line no-undef
      if (getCurrentPages().length < 2 && typeof __wxConfig !== 'undefined') {
        const url = '/' + __wxConfig.pages[0]
        if (url === APP_INDEX_PAGE_PATH) {
          return uni.switchTab({ url })
        }
        return uni.redirectTo({ url })
      }
      uni.navigateBack({
        delta: this.backDelta
      })
    },
    gotoIndex () {
      // 此处的url必须是配置到页面的 否则不应该使用switchTab
      // const url = '/' + __wxConfig.pages[0]
      return uni.switchTab({ url: APP_INDEX_PAGE_PATH })
    }
  }
}
</script>

<style lang="scss">
.cu-custom .cu-bar{
  // border-bottom: 1px solid #eee;
  .content {
    font-weight: 800;
  }
}
</style>
