<template>
  <div class="my-title">
    <img src="@/assets/images/title-bg.png" class="bg" />
    <div class="date">
      <span>{{ date }}</span>
      <span>{{ time }}</span>
    </div>
    <div v-if="curView.type === 'street'" class="btn-container">
      <div
        v-for="btn in btns"
        :key="btn.name"
        :class="{ on: btn.type === curBtn }"
        @click="handleThemeSelect(btn.type)"
      >
        {{ btn.name }}
      </div>
    </div>
    <h1><slot />{{ curView.name.includes('工作站') ? curView.name : '' }}</h1>
  </div>
</template>

<script lang="ts">
import { defineComponent, onBeforeUnmount, ref, computed } from 'vue'
import { getDate, getTime } from '../util'
import store from '@/store'

export default defineComponent({
  name: 'MyTitle',
  displayName: 'm-title',
  setup() {
    const date = ref(getDate())
    const time = ref('')
    const timer = ref<number | null>(null)
    const btns = ref([
      {
        name: '公共管理',
        type: 'manage',
      },
      {
        name: '公共服务',
        type: 'service',
      },
      {
        name: '公共安全',
        type: 'safety',
      },
    ])
    const curBtn = computed(() => store.state.curTheme)
    const curView = computed(() => store.state.curView)
    const handleThemeSelect = (type: string) => {
      if (curBtn.value === type) return
      store.commit('SET_CURRENT_THEME', type)
    }
    timer.value = setInterval(() => {
      time.value = getTime()
    })
    onBeforeUnmount(() => {
      clearInterval(Number(timer))
      timer.value = null
    })
    return {
      date,
      time,
      timer,
      btns,
      curBtn,
      curView,
      handleThemeSelect,
    }
  },
})
</script>

<style lang="stylus" scoped>
@import '../main.styl'
.my-title
  $full()
  $center()
  justify-content flex-start
  position relative
  background-size cover
  background-position center
  font-weight bold
  .date
    z-index 2
    position absolute
    right 1%
    span
      display block
      text-align right
  .bg
    position absolute
    $full()
    top 0
    left 0
  h1
    font-family $font-weibei
    margin 0 0 0 10%
    z-index 2
    background-clip text
    -webkit-background-clip text
    -webkit-text-fill-color transparent
    background-image linear-gradient(to bottom, #fff, #00C7FF)
    font-size .22rem
    letter-spacing .05rem
  .btn-container
    z-index 2
    display flex
    position absolute
    right 30%
    color #ccc
    >div
      $center()
      padding .03rem .1rem
      box-sizing border-box
      margin-left .1rem
      background url('@/assets/images/btn-bg.png') 100% / 100% no-repeat
      cursor pointer
      &:hover
      &.on
        color #fff
        background url('@/assets/images/btn-bg-on.png') 100% / 100% no-repeat
</style>
