<template>
  <div class="upanapp-header">
    <div class="upanapp-header-left">
      <slot name="overwrite-left">
        <transition :name="transition">
          <a class="upanapp-header-back" @click.preventDefault v-show="_leftOptions.showBack" @click="onClickBack">{{ typeof _leftOptions.backText === 'undefined' ? $t('back_text') : _leftOptions.backText}}</a>
        </transition>
        <transition :name="transition">
          <div class="left-arrow" @click="onClickBack" v-show="_leftOptions.showBack"></div>
        </transition>
      </slot>
      <slot name="left"></slot>
    </div>
    <h1 class="upanapp-header-title" @click="$emit('on-click-title')" v-if="!shouldOverWriteTitle">
      <slot>
        <transition :name="transition">
          <span v-show="title">{{title}}</span>
        </transition>
      </slot>
    </h1>
    <div class="upanapp-header-title-area" v-if="shouldOverWriteTitle">
      <slot name="overwrite-title"></slot>
    </div>
    <div class="upanapp-header-right">
      <a class="upanapp-header-more" @click.preventDefault @click="$emit('on-click-more')" v-if="rightOptions.showMore"></a>
      <slot name="right"></slot>
    </div>
  </div>
</template>

<i18n>
back_text:
  en: Back
  zh-CN: 返回
</i18n>

<script>
import objectAssign from 'object-assign'

export default {
  name: 'ua-header',
  props: {
    leftOptions: Object,
    title: String,
    transition: String,
    rightOptions: {
      type: Object,
      default () {
        return {
          showMore: false
        }
      }
    }
  },
  beforeMount () {
    if (this.$slots['overwrite-title']) {
      this.shouldOverWriteTitle = true
    }
  },
  computed: {
    _leftOptions () {
      return objectAssign({
        showBack: true,
        preventGoBack: false
      }, this.leftOptions || {})
    }
  },
  methods: {
    onClickBack () {
      if (this._leftOptions.preventGoBack) {
        this.$emit('on-click-back')
      } else {
        this.$router ? this.$router.back() : window.history.back()
      }
    }
  },
  data () {
    return {
      shouldOverWriteTitle: false
    }
  }
}
</script>

<style lang="less">
@import '../../styles/variable.less';

.upanapp-header {
  position: relative;
  padding: 3px 0;
  box-sizing: border-box;
  background-color: @header-background-color;
}
.upanapp-header .upanapp-header-title {
  line-height: 40px;
  text-align: center;
  font-size: 18px;
  font-weight: 400;
  color: @header-title-color;
}
.upanapp-header-title-area, .upanapp-header .upanapp-header-title {
  margin: 0 88px;
  height: 40px;
  width: auto;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.upanapp-header .upanapp-header-title > span {
  display: inline-block;
}
.upanapp-header .upanapp-header-left,.upanapp-header .upanapp-header-right {
  position: absolute;
  top: 14px;
  display: block;
  font-size: 14px;
  line-height: 21px;
  color: @header-text-color;
}
.upanapp-header .upanapp-header-left a,.upanapp-header .upanapp-header-left button,.upanapp-header .upanapp-header-right a,.upanapp-header .upanapp-header-right button {
  float: left;
  margin-right: 8px;
  color: @header-text-color;
}
.upanapp-header .upanapp-header-left a:active,.upanapp-header .upanapp-header-left button:active,.upanapp-header .upanapp-header-right a:active,.upanapp-header .upanapp-header-right button:active {
  opacity: .5
}
.upanapp-header .upanapp-header-left {
  left: 18px
}
.upanapp-header .upanapp-header-left .upanapp-header-back {
  padding-left: 16px
}
.upanapp-header .upanapp-header-left .left-arrow {
  position: absolute;
  width: 30px;
  height: 30px;
  top: -5px;
  left: -5px;

  &:before {
    content: "";
    position: absolute;
    width: 12px;
    height: 12px;
    border: 1px solid @header-arrow-color;
    border-width: 1px 0 0 1px;
    transform: rotate(315deg);
    top: 8px;
    left: 7px;
  }
}
.upanapp-header .upanapp-header-right {
  right: 15px
}
.upanapp-header .upanapp-header-right a,.upanapp-header .upanapp-header-right button {
  margin-left: 8px;
  margin-right: 0
}
.upanapp-header .upanapp-header-right .upanapp-header-more:after {
  content: "\2022\0020\2022\0020\2022\0020";
  font-size: 16px;
}
.upanapp-header-fade-in-right-enter-active {
  animation: fadeinR .5s;
}
.upanapp-header-fade-in-left-enter-active {
  animation: fadeinL .5s;
}
@keyframes fadeinR {
  0% {
    opacity: 0;
    transform: translateX(150px);
  }
  100% {
    opacity:1;
    transform:translateX(0);
  }
}
@keyframes fadeinL{
  0% {
    opacity: 0;
    transform: translateX(-150px);
  }
  100% {
    opacity: 1;
    transform: translateX(0);
  }
}
</style>
