<template>
  <div id="nav-bar">
    <div class="left">
      <img class="logo" src="@images/logo.png" />
      <p class="title">花木街道数字孪生城市</p>
      <b>|</b>
      <span>党建信息系统</span>
    </div>
    <div class="right">
      <n-input
        v-model:value="searchKey"
        class="search-input"
        placeholder="请输入党员姓名或党组织名称"
        :loading="isLoading"
        clearable
        :on-clear="removeResult"
        :on-focus="onFocus"
        :on-blur="onBlur"
      >
        <template #prefix>
          <img class="icon" src="@images/search2.png" />
        </template>
      </n-input>
      <div class="align-center">
        <n-icon
          class="btn"
          :class="{ on: showFilterDrawer || showBuildingDrawer }"
          size=".12rem"
          @click="onClick('filter')"
        >
          <svg-icon :data="nav1" />
        </n-icon>
        <n-icon
          class="btn"
          :class="{ on: showActivityDrawer }"
          size=".12rem"
          @click="onClick('activity')"
        >
          <svg-icon :data="nav2" original />
        </n-icon>
        <n-icon
          class="btn"
          :class="{ on: showStructModal }"
          size=".12rem"
          @click="onClick('struct')"
        >
          <svg-icon :data="nav3" original />
        </n-icon>
      </div>
      <b>|</b>
      <n-popover trigger="hover" class="top-menu" width="13rem">
        <template #trigger>
          <div class="align-center menu-btn">
            <n-icon
              size=".12rem"
              style="margin-right: 0.05rem; margin-bottom: 0.03rem"
            >
              <svg-icon :data="nav5" original />
            </n-icon>
            选择菜单
          </div>
        </template>
        <div class="menu">
          <div class="parent">
            <div
              v-for="menu in menus"
              :key="menu.key"
              :class="{ on: curMenuKey === menu.key }"
              @click="curMenuKey = menu.key"
            >
              {{ menu.name }}
            </div>
          </div>
          <div class="children">
            <div
              v-for="(child, i) in curMenu?.children || []"
              :key="i"
              :class="{ child: !child.list }"
            >
              <template v-if="child.list">
                <h3>{{ child.name }}</h3>
                <p
                  v-for="p in child.list"
                  :key="p.name"
                  :class="{ link: !!p.id }"
                  :title="p.name"
                  @click.prevent="toPage(p.id)"
                >
                  {{ p.name }}
                </p>
              </template>
              <template v-else>
                <p
                  :key="child.name"
                  :class="{ link: !!child.id }"
                  :title="child.name"
                  @click.prevent="toPage(child.id)"
                >
                  {{ child.name }}
                </p>
              </template>
            </div>
          </div>
        </div>
      </n-popover>
    </div>
  </div>

  <SearchModal
    :visible="showResult"
    :data="resultData"
    :search-key="searchKey"
    @select="handleSelect"
    @isClickOutSide="(val) => (showResult = !val || isFocus)"
  />
</template>

<script lang="ts" setup>
import { computed, ref, watch } from 'vue'
import store from '@/store'
import { useFetchOrg, useFetchMember } from '@/hooks/useFetch'
import useDebounce from '@/hooks/useDebounce'
import SearchModal from './search-modal.vue'
import nav1 from '@images/nav1.svg'
import nav2 from '@images/nav2.svg'
import nav3 from '@images/nav3.svg'
import nav5 from '@images/nav5.svg'

const showFilterDrawer = computed(() => store.state.showFilterDrawer)
const showBuildingDrawer = computed(() => store.state.showBuildingDrawer)
const showActivityDrawer = computed(() => store.state.showActivityDrawer)
const showStructModal = computed(() => store.state.showStructModal)
const menus = computed(() => store.state.menus)
const curMenuKey = ref('101')
const curMenu = computed(() =>
  menus.value.find((menu: any) => menu.key == curMenuKey.value),
)

const onClick = (type: string) => {
  store.commit('SET_FILTER_DRAWER', false)
  store.commit('SET_ACTIVITY_DRAWER', false)
  store.commit('SET_STRUCT_MODAL', false)
  store.commit('SET_BUILDING_DRAWER', false)
  store.commit('SET_SHOW_TAG', false)
  store.commit('SET_SHOW_BUILDING_DETAIL', false)
  switch (type) {
    case 'filter':
      store.commit('SET_FILTER_DRAWER', true)
      break
    case 'activity':
      store.commit('SET_ACTIVITY_DRAWER', true)
      break
    case 'struct':
      store.commit('SET_STRUCT_MODAL', true)
      store.commit('SET_SHOW_BASIC_INFO', false)
      break
    default:
      break
  }
}

const options = [
  {
    label: '选项1',
    key: '1',
  },
  {
    label: '选项2',
    key: '2',
  },
  {
    label: '选项3',
    key: '3',
  },
]

const searchKey = ref<string | undefined>(undefined)
const isLoading = ref(false)
const showResult = ref(false)
const resultData = ref<any>({})

const isFocus = ref(false)
const onFocus = () => {
  store.commit('SET_SHOW_TAG', false)
  isFocus.value = true
  if (searchKey.value) {
    showResult.value = true
  }
}
const onBlur = () => {
  isFocus.value = false
  if (!showResult.value) {
    store.commit('SET_SHOW_TAG', true)
  }
}
watch(
  () => showResult.value,
  (val) => {
    store.commit('SET_SHOW_TAG', !val && !isFocus.value)
  },
)
const removeResult = () => {
  isLoading.value = false
  showResult.value = false
  resultData.value = {}
}
const handleSearch = useDebounce(async function () {
  isLoading.value = true
  const orgs = await useFetchOrg({
    q: `paths @ "党组织名称" && string @ "${searchKey.value}",paths @ "党组织编号" && string @ "${store.state.auth.code}"`,
  })
  const members = await useFetchMember({
    q: `paths @ "姓名" && string @ "${searchKey.value}",paths @ "党组织编号" && string @ "${store.state.auth.code}"`,
  })
  resultData.value = {
    organization: orgs,
    member: members,
  }
  isLoading.value = false
  if (!searchKey.value) {
    showResult.value = false
    return
  }
  showResult.value = true
})
watch(
  () => searchKey.value,
  (val) => {
    if (!val) {
      removeResult()
      return
    }
    handleSearch()
  },
)

const handleSelect = ({ extra }: any) => {
  const point = [
    {
      上海2000纬度:
        extra['上海2000纬度'] ||
        extra['居住地上海2000纬度'] ||
        extra['党组织上海2000纬度'],
      上海2000经度:
        extra['上海2000经度'] ||
        extra['居住地上海2000经度'] ||
        extra['党组织上海2000经度'],
      ...extra,
    },
  ]
  store.commit('SET_MAP_POINTS', point)
  showResult.value = false
}

const toPage = (id?: string) => {
  if (!id) return
  window.parent._global_datamap_emit &&
    window.parent._global_datamap_emit.emit('CHANGE_PAGE_BY_ID_EVENT', id)
}
</script>

<style lang="stylus" scoped>
@import '../../components/MyComponent/main.styl'
.align-center
  $flex-align()
#nav-bar
  width 100vw
  height .42rem
  background rgba(252,252,252,.85)
  display flex
  align-items center
  justify-content space-between
  padding .1rem .18rem
  box-sizing border-box
  font-family $font-ping-medium
  $blur()
  b
    color #EAECEF
    margin 0 .1rem
    font-size .13rem
  .left
    $flex-align()
    font-size .13rem
    .logo
      width .25rem
      height @width
      margin-right .13rem
    .title
      font-size .18rem
      font-weight 500
    span
      background $light-red
      color $red
      border-radius .03rem
      padding 0 .1rem
      font-weight 500
  .right
    $flex-align()
    width 50%
    // max-width 60%
    // min-width 50%
    justify-content flex-end
    .search-input
      width 2.4rem
      font-size .1rem
      height .26rem
      line-height @height
      margin-right .1rem
    .menu-btn
      cursor pointer
      font-size .11rem
      white-space nowrap
      &:hover
        color $red
    .btn
      cursor pointer
      margin 0 .08rem
      padding .04rem
      border-radius .02rem
      &:hover
      &.on
        color $red
        background $light-red
    .icon
      width .12rem
      height @width
      margin-left -0.02rem
</style>
<style lang="stylus">
@import '../../components/MyComponent/main.styl'
#nav-bar
  .n-input__input-el
    height .26rem
    line-height @height
    font-family $font-ping
  .n-input--focus
    .n-input__border
      border .01rem solid $red

.top-menu
  min-height 3rem
  .menu
    height 100%
    border-radius .06rem
    display flex
    color $font
    background $white-bg
    .parent
      margin .15rem 0
      width 1rem
      font-size .12rem
      font-family $font-ping-medium
      >div
        border-radius .06rem
        box-sizing border-box
        padding .08rem .12rem
        cursor pointer
        margin-bottom .05rem
        &:hover
        &.on
          background $light-red
          color $red
    .children
      flex 1
      >div
        display inline-block
        margin 0 .1rem
        vertical-align top
        h3
          font-size .13rem
          margin-bottom .1rem
        p
          width 1.4rem
          margin .1rem 0
          font-size .1rem
          $text-overflow()
          &.link
            color #000
            font-weight bold
            cursor pointer
            &:hover
              color $red
        &.child
          margin-top .1rem
          p
            margin .05rem 0
</style>
