<template>
  <div
    id="site-selector"
    class="site-selector"
    :style="`left:${
      isNaN(+curViewType) && curViewType != 'lfs'
        ? 'calc(20vw + .1rem)'
        : isNaN(+curViewType) && curViewType == 'lfs'
        ? 'calc(32vw + .1rem)'
        : 'calc(16vw + .1rem)'
    }`"
  >
    <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(+type) && type != 'lfs') {
        // 小区以外的屏
        router.push({
          name: 'main',
        })
      } else if (isNaN(+type) && type == 'lfs') {
        router.push({
          name: 'raffles',
        })
      } 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, 0.7)
$size = 0.1rem
$height = 0.26rem
#site-selector.site-selector
  position fixed
  top 0.45rem
  z-index 99
  .ant-select
    background $bg
    min-width 0.9rem
    .ant-select-selector
      min-width 0.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>
