<template>
  <m-card title="案件分析" class="box">
    <div class="rank">
      <m-sub2>分类排行</m-sub2>
      <div class="nav">
        <div>
          <span
            v-for="option in options"
            :key="option.key"
            :class="{ on: curOption === option.key }"
            @click="curOption = option.key"
          >
            {{ option.name }}
          </span>
        </div>
      </div>
      <div v-if="rankList.length > 0" class="content">
        <m-scroll :length="rankList.length" :limit="4">
          <m-progress
            v-for="rank in rankList"
            :key="rank.name"
            :value="rank.rate"
            :msg="rank"
            :color="['#FF692D', '#F7973D']"
          />
        </m-scroll>
      </div>
      <m-empty v-else />
    </div>
    <div class="place">
      <m-sub2>多发地点</m-sub2>
      <div v-if="placeList.length > 0" class="content">
        <div v-for="item in placeList" :key="item.name">
          <p>{{ item.name }}</p>
          <div>
            <span>{{ item.type }}</span>
            <m-count class="count" :value="item.value" />
          </div>
        </div>
      </div>
    </div>
  </m-card>
</template>

<script lang="ts">
import { defineComponent, ref, computed, watch } from 'vue'
import SubTitle from '../components/sub-title.vue'
import Place from '../components/place.vue'
import store from '@/store'
export default defineComponent({
  name: 'CaseAnalysis',
  components: { SubTitle, Place },
  setup() {
    const options = ref([
      { name: '日', key: 'day' },
      { name: '周', key: 'week' },
      { name: '月', key: 'month' },
    ])
    const curOption = ref('month')

    watch(
      () => curOption.value,
      (time) => {
        if (!time) return
        store.dispatch('GET_CASE_CLASSIFICATION_RANK', {
          managementType: '综合',
        })
        store.dispatch('GET_MOREHAPPEN_ADDR', {
          managementType: '综合',
        })
      },
      { immediate: true }
    )

    const rankList = computed(() => {
      const data = store.state.analysisRank[curOption.value] || []
      const total = data.reduce((acc, cur) => acc + cur.num, 0)
      return data.map((item: any) => ({
        name: item.businsessType || item.businessType,
        rate: +Math.ceil((item.num / total) * 100).toFixed(0),
        value: item.num,
        unit: '件',
      }))
    })

    const placeList = computed(() => {
      const data = store.state.moreHappenAddr[curOption.value] || []
      return data.map((item: any) => ({
        name: item.address,
        value: item.num,
        type: '路段',
      }))
    })

    return {
      options,
      curOption,
      rankList,
      placeList,
    }
  },
})
</script>

<style lang="stylus" scoped>
@import '../../components/MyComponent/main.styl'
.box
  overflow hidden
  .nav
    width 100%
    display flex
    align-items center
    justify-content flex-end
    >div
      width 75%
      &:last-child
        width 20%
        display flex
        >span
          $center()
          width .4rem
          height .12rem
          margin-left .04rem
          color $primary-border
          cursor pointer
          font-size .08rem
          background rgba(50,197,255,.2)
          &:hover
          &.on
            color #fff
            background $secondary-color
  .rank
    overflow hidden
    // height 1rem
    .content
      height 1.2rem
      overflow hidden
  .place
    .content
      display flex
      justify-content space-between
      >div
        width 32%
        height .6rem
        background url('@/assets/images/place-bg.png') center / cover no-repeat
        padding .05rem
        >div
          display flex
          justify-content space-between
          align-items center
          margin-top .05rem
          span
            color #77bff9
            border .01rem solid @color
            background rgba(119, 191, 249, 0.2)
            font-size .08rem
            padding 0 .05rem
          .count
            font-size .14rem
</style>
