<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 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 /></h1>
  </div>
</template>

<script lang="ts">
import { defineComponent, onBeforeUnmount, ref } 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 = ref('manage')
    const handleThemeSelect = (type: string) => {
      if (curBtn.value === type) return
      curBtn.value = type
      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,
      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-left 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%
    >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
        background url('@/assets/images/btn-bg-on.png') 100% / 100% no-repeat
</style>
