<template>
  <div id="site-selector" class="site-selector">
    <a-select
      :value="curViewType"
      dropdown-class-name="site-selector-drop-down"
      @select="handleChange"
    >
      <a-select-option
        v-for="option in options"
        :key="option.type"
        :value="option.type"
      >
        {{ option.name }}
      </a-select-option>
    </a-select>
  </div>
</template>

<script lang="ts">
import { computed, defineComponent } from 'vue'
import store from '@/store'
import { useRouter } from 'vue-router'

export default defineComponent({
  name: 'SiteSelector',
  setup() {
    const curViewType = computed(() => store.state.curView.type)
    const options = computed(() => [
      ...store.state.viewOptions,
      ...store.state.communityOptions,
    ])
    const router = useRouter()
    const handleChange = (type: string) => {
      store.commit(
        'SET_CURRENT_VIEW',
        options.value.find((option) => type === option.type)
      )
      if (store.state.curView.id) {
        store.dispatch('initStationData', store.state.curView.id)
      }
      if (isNaN(Number(type))) {
        // 小区以外的屏
        router.push({
          name: 'main',
        })
      } else {
        router.push({
          name: 'community',
          query: { id: type },
        })
      }
    }
    return {
      curViewType,
      options,
      handleChange,
    }
  },
})
</script>

<style lang="stylus">
@import '../../components/MyComponent/main.styl'

$bg = rgba(13, 39, 76, .7)
$size = .1rem
$height = .26rem

#site-selector.site-selector
  position fixed
  top .45rem
  left calc(16vw + .1rem)
  z-index 99
  .ant-select
    background $bg
    min-width .9rem
    .ant-select-selector
      min-width .9rem
      border-top none
      border-left none
      border-right none
      height $height
      .ant-select-selection-item
        font-size $size
        line-height $height
        text-align center
        color #fff
        font-weight bold
    .ant-select-arrow
      font-size $size * 0.8
.site-selector-drop-down
  &.ant-select-dropdown
    background $bg
    font-size $size
    .ant-select-item
      font-size $size
      line-height $height / 2
    .ant-select-item-option-selected:not(.ant-select-item-option-disabled)
      background $bg
      font-size $size
</style>
