<template>
  <m-animate enter="fadeInLeft" leave="fadeOutLeft" :duration="300">
    <div v-if="show" class="container">
      <n-collapse
        id="basic-info"
        arrow-placement="right"
        default-expanded-names="1"
        accordion
      >
        <template #arrow>
          <n-icon size=".08rem">
            <CaretForward />
          </n-icon>
        </template>
        <n-collapse-item name="1">
          <div
            class="info-wrapper"
            :class="{
              short: organization.source.length === 0,
            }"
          >
            <div class="sum">
              <div v-for="item in sum" :key="item.name">
                <p>
                  <m-count class="count" :value="item.value" />
                  <span>{{ item.unit }}</span>
                </p>
                <p>{{ item.name }}</p>
              </div>
            </div>
            <div class="box">
              <p class="title">关系在街道党员分类</p>
              <div class="content">
                <div class="pie">
                  <m-pie :option="pieOption" :dataset="member" />
                </div>
                <div>
                  <div v-for="(item, i) in member.source" :key="item.name">
                    <p>
                      <i class="dot" :style="`background:${color1[i]}`" />
                      {{ item.name }}
                    </p>
                    <p>
                      <span>
                        <m-count :value="item.ratio" :decimal="1" /> %
                      </span>
                      <span><m-count :value="item.value" /> 位</span>
                    </p>
                  </div>
                </div>
              </div>
            </div>
            <div v-show="organization.source.length > 0" class="box">
              <p class="title">党组织分类</p>
              <div class="content">
                <div class="pie">
                  <m-pie
                    :option="{ ...pieOption, color: color2 }"
                    :dataset="organization"
                  />
                </div>
                <div>
                  <div
                    v-for="(item, i) in organization.source"
                    :key="item.name"
                  >
                    <p>
                      <i class="dot" :style="`background:${color2[i]}`" />
                      {{ item.name }}
                    </p>
                    <p>
                      <span>
                        <m-count :value="item.ratio" :decimal="1" /> %
                      </span>
                      <span><m-count :value="item.value" /> 个</span>
                    </p>
                  </div>
                </div>
              </div>
            </div>
            <div class="box">
              <p class="title">关系在街道党员年龄分布</p>
              <div class="content">
                <m-line :option="lineOption" :dataset="age" />
              </div>
            </div>
          </div>
        </n-collapse-item>
      </n-collapse>
      <m-animate enter="fadeInUp" leave="fadeOutDown" :duration="200">
        <div v-if="visible" class="tag-box">
          <span
            v-for="tag in tagList"
            :key="tag.name"
            :class="{ on: tag.key === curTagKey }"
            @click="clickTag(tag)"
            >{{ tag.name }}</span
          >
        </div>
      </m-animate>
    </div>
  </m-animate>
  <div
    v-if="tips.show && visible"
    class="tips"
    :class="{ offset: filterDrawerIsShow }"
  >
    <div>
      <div>
        <p><m-count class="count" :value="tips.value" /> <span>个</span></p>
        <span>{{ tips.name }}总数</span>
      </div>
    </div>
    <template v-if="tips.name.includes('两新')">
      <div>
        <div>
          <p>
            <m-count class="count" :value="tips.outStreetValue" />
            <span>个</span>
          </p>
          <span>{{ tips.name }}（街道外）</span>
        </div>
      </div>
      <div>
        <div>
          <p>
            <m-count class="count" :value="tips.independent" /> <span>个</span>
          </p>
          <span>独立支部数</span>
        </div>
      </div>
      <div>
        <div>
          <p><m-count class="count" :value="tips.combine" /> <span>个</span></p>
          <span>联合支部数</span>
        </div>
      </div>
    </template>
  </div>
</template>

<script lang="ts" setup>
import { ChartTypes } from '@/components/MyComponent'
import { ref, computed, onMounted, watch } from 'vue'
import { CaretForward } from '@vicons/ionicons5'
import { useFetchOrg } from '@/hooks/useFetch'
import store from '@/store'

const show = computed(() => store.state.showBasicInfo)

const visible = computed(() => store.state.showTag)
const sum = computed(() => store.state.basicInfo.sum)
const member = computed(() => store.state.basicInfo.member)
const organization = computed(() => store.state.basicInfo.organization)
const age = computed(() => store.state.basicInfo.age)
const filterDrawerIsShow = computed(() => store.state.showFilterDrawer)

const color1 = ['#FAADB8', '#C1474F', '#6D2C29']
const color2 = ['#FAADB8', '#DD505E', '#A53D40', '#6D2C29']
const pieOption: ChartTypes.PieOption = {
  color: color1,
  legend: { show: false },
  series: [
    {
      type: 'pie',
      radius: ['60%', '78%'],
      center: ['42%', '60%'],
      label: {
        show: false,
      },
      itemStyle: {
        borderWidth: 3,
        borderColor: '#fff',
      },
    },
  ],
}
const lineOption: ChartTypes.LineOption = {
  color: ['#DD505E'],
  legend: { show: false },
  yAxis: [
    {
      type: 'value',
      minInterval: 1,
      // maxInterval: 100,
      // interval: 100,
      splitLine: {
        lineStyle: {
          type: 'dashed',
        },
      },
    },
  ],
  xAxis: [
    {
      type: 'category',
      boundaryGap: true,
      axisTick: {
        interval: 0,
      },
      axisLabel: {
        interval: 0,
        color: '#000',
      },
    },
  ],
  series: [
    {
      type: 'line',
      smooth: false,
    },
  ],
}
onMounted(() => {
  store.dispatch('getBasicInfo')
})

const curTagKey = computed(() => store.state.curTag)
const tagList = [
  { name: '机关事业单位党组织', key: '机关事业单位' },
  { name: '居民区党组织', key: '居民区' },
  { name: '“两新”党组织', key: '两新' },
]

interface Tips {
  show: boolean
  name: string
  value: number
  outStreetValue: number
  independent?: number
  combine?: number
}
const tips = ref<Tips>({
  show: false,
  name: '',
  value: 0,
  outStreetValue: 0,
})
const clickTag = async ({ key, name }: any) => {
  if (key === curTagKey.value) {
    store.commit('SET_MAP_POINTS')
    store.commit('SET_CUR_TAG', '')
    tips.value = {
      show: false,
      name: '',
      value: 0,
      outStreetValue: 0,
    }
    return
  }
  store.commit('SET_CUR_TAG', key)
  let data: any[] = []
  if (!filterDrawerIsShow.value) {
    const res = await useFetchOrg({
      q: `paths @ "类别" && string == "${key}"`,
    })
    data = res
      .map((item: any) => item.extra)
      .filter((item: any) => item?.['是否虚拟组织'] === false)
    store.commit('SET_MAP_POINTS', data)
    tips.value = {
      show: true,
      name,
      value: data.length || 0,
      outStreetValue:
        data.filter(
          (item) => !item['所属街道'] || item['所属街道'] !== '花木街道',
        )?.length || 0,
      independent:
        data.filter((item) => item?.['类别中类'] === '独立支部')?.length || 0,
      combine:
        data.filter((item) => item?.['类别中类'] === '联合支部')?.length || 0,
    }
  }
  store.commit('SET_CUR_TAG', key)
}

watch(
  () => visible.value,
  (visible) => {
    if (!visible) {
      tips.value = {
        show: false,
        name: '',
        value: 0,
        outStreetValue: 0,
      }
      if (!store.state.showBuildingDrawer) {
        store.commit('SET_MAP_POINTS')
      }
      store.commit('SET_CUR_TAG', '')
    }
  },
)
watch(
  () => store.state.curTag,
  (val) => {
    if (!val) {
      tips.value = {
        show: false,
        name: '',
        value: 0,
        outStreetValue: 0,
      }
    }
  },
)
watch(
  () => store.state.mapPoints,
  (points: any) => {
    if (!filterDrawerIsShow.value || !visible.value || !curTagKey.value) return
    tips.value = {
      show: true,
      name: tagList.find((item) => item.key === curTagKey.value)?.name || '',
      value: points.length || 0,
      outStreetValue:
        points.filter(
          (item) => !item['所属街道'] || item['所属街道'] !== '花木街道',
        )?.length || 0,
    }
  },
)
</script>

<style lang="stylus" scoped>
@import '../../components/MyComponent/main.styl'
.container
  position fixed
  top .48rem
  left .05rem
  width 3rem
.tips
  position fixed
  top .48rem
  right .05rem
  $blur()
  font-size .1rem
  transition right .3s ease-in-out
  &.offset
    right 2.9rem
  >div
    border-radius .06rem
    padding .08rem .09rem
    background rgba(249,250,250,.5)
    margin-bottom .08rem
    >div
      border-radius inherit
      padding inherit
      background $white-bg
      .count
        font-size .18rem
        color $red
        font-family $font-din
      >span
        display block
        margin-top .04rem
        color $gray
#basic-info
  width 100%
  background $bg
  border-radius .02rem
  $blur()
  .info-wrapper
    height 82vh
    box-sizing border-box
    padding .03rem .08rem
    display flex
    flex-direction column
    transition height .2s ease-in-out
    &.short
      height 60vh
    .sum
      display flex
      justify-content space-between
      margin-bottom .04rem
      >div
        background $white-bg
        width 32%
        border-radius .04rem
        padding .03rem .05rem
        box-sizing border-box
        font-size .09rem
        p
          &:last-child
            color $gray
        .count
          color $red
          font-size .2rem
          margin-right .05rem
    .box
      background $white-bg
      border-radius .04rem
      padding .05rem .03rem
      box-sizing border-box
      margin .04rem 0
      flex 1
      display flex
      flex-direction column
      overflow hidden
      .title
        font-family $font-ping-medium
        margin-left 6%
      .content
        flex 1
        display flex
        justify-content space-around
        overflow hidden
        >div
          &.pie
            width 1.4rem
            height 1rem
          &:last-child
            display flex
            flex-direction column
            justify-content center
          .dot
            display inline-block
            width .04rem
            height @width
            border-radius 50%
            margin-right .04rem
            margin-bottom .02rem
          span
            font-family $font-din
            margin-left .12rem
            margin-right .2rem
            &:last-child
              float right
.tag-box
  background $bg
  $blur()
  width 100%
  height .3rem
  position absolute
  left 0
  margin-top .05rem
  border-radius .02rem
  display flex
  justify-content space-between
  padding .05rem .08rem
  box-sizing border-box
  >span
    $flex-align()
    $text-overflow()
    justify-content center
    background #fff
    border-radius .03rem
    cursor pointer
    font-size .09rem
    width 32%
    &:hover
    &.on
      background $light-red
      color $red
      box-shadow 0 0 .04rem rgba(0,0,0,.5)
</style>
<style lang="stylus">
@import '../../components/MyComponent/main.styl'
#basic-info
  .n-collapse-item__header
    position relative
    display flex
    height .3rem
    border-radius .02rem
    padding 0 .12rem
    font-family $font-ping-medium
    font-size .14rem
    .n-collapse-item__header-main
      justify-content flex-end
    &:before
      position absolute
      left 0.12rem
      content '基本情况'
      height 0.14rem
      border-left 0.02rem solid #dd505e
      padding-left 0.05rem
      display flex
      align-items center
  .n-collapse-item__content-inner
    padding 0
</style>
