<template>
  <m-card title="勤务分析" mode="4">
    <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>
      <div v-if="rankType.length > 0" class="duty-type">
        <div v-for="item in rankType" :key="item.name">
          <div class="circle">
            <Circle
              class="circle-item"
              :value="+item.value"
              :stroke-width="8"
              :color="['#F7933E', 'rgba(253,211,116,.9)']"
              :rate="2.4"
            />
          </div>
          <p>{{ item.name }}</p>
        </div>
      </div>
      <m-empty v-else />
    </div>
    <div class="rank">
      <m-sub2>分类排行</m-sub2>
      <div v-if="rankList.length > 0" class="content">
        <div v-for="item in rankList" :key="item.name">
          <span>{{ item.name }}</span>
          <div v-if="showChart" class="progress">
            <m-progress :color="['#FF692D', '#F7973D']" :value="item.rate" />
          </div>
          <m-count class="count" :value="item.value" />
        </div>
      </div>
      <m-empty v-else />
    </div>
    <div class="bar-chart">
      <m-sub2>居委事件数量</m-sub2>
      <div v-if="showChart && barOption.series.length > 0" class="chart">
        <m-bar :dataset="barData" :option="barOption" />
      </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>
      <m-empty v-else />
    </div>
  </m-card>
</template>

<script lang="ts">
import { computed, defineComponent, ref, watch } from 'vue'
import store from '@/store'
import Circle from '../components/circle.vue'

export default defineComponent({
  name: 'DutyAnalysis',
  components: { Circle },
  setup() {
    // const fontSize = computed(() => Math.floor((screen.height * 1.6) / 100))
    const showChart = computed(() => store.state.curView.type !== 'street')
    const options = ref([
      { name: '日', key: 'day' },
      { name: '周', key: 'week' },
      { name: '月', key: 'month' },
    ])
    const curOption = ref('month')
    watch(
      () => store.state.curView.id,
      (id) => {
        if (!id) return
        store.dispatch('getDutyRank', {
          gridId: id,
          managementType: '综合',
        })
        store.dispatch('getDutyEvents', {
          gridId: id,
          managementType: '综合',
        })
        store.dispatch('getDutyPlaces', {
          gridId: id,
          managementType: '综合',
        })
        store.dispatch('getDutyRanking', {
          gridId: id,
          managementType: '综合',
        })
      },
      { immediate: true }
    )

    const rankList = computed(() => {
      const data = store.state.dutyRanking[curOption.value] || []
      const total = data.reduce((acc, cur) => acc + cur.num, 0)
      const result = data.map((item: any) => ({
        name: item.managementType,
        rate: +Math.ceil((item.num / total) * 100).toFixed(0),
        value: item.num,
        unit: '件',
        color: '#fff',
      }))
      // if (result.length > 0) {
      //   const [a, b, c] = result
      //   return [b, a, c]
      // }
      return result || []
    })
    const placeList = computed(() => {
      const data = store.state.dutyPlaces[curOption.value] || []
      return data.map((item: any) => ({
        name: item.address,
        value: item.num,
        type: '路段',
      }))
    })
    const barData = computed(() => {
      const data = store.state.dutyEvents[curOption.value] || []
      const types: string[] = []
      data.forEach((item: any) =>
        types.push(
          ...item.data.map((e: any) => e.businsessType || e.businessType)
        )
      )
      const dic: any = {}
      const defaultData: any = {}
      const dimensions = [
        { name: 'name', displayName: '居委' },
        ...Array.from(new Set(types)).map((item: string, i: number) => {
          const name = `data${i + 1}`
          dic[item] = name
          defaultData[name] = 0
          return {
            name,
            displayName: '数量',
          }
        }),
      ]
      return {
        dimensions,
        source: data.map((item: any) => {
          const datas: { [key: string]: unknown } = {}
          item.data.forEach((e: any) => {
            datas[dic[e.businsessType || e.businessType]] = e.num || 0
          })
          return {
            name: item.cmtName,
            ...defaultData,
            ...datas,
          }
        }),
      }
    })
    const barOption = computed(() => ({
      // color: ['#5BD5FF', '#6EB629', '#FFCE34', '#FF9D27', '#826AFA'],
      color: [['#FF692D', 'rgba(253,211,116,.5)']],
      legend: {
        // right: '2%',
        // left: '10%',
        // top: '-2%',
        // itemWidth: fontSize.value * 0.6,
        // itemHeight: fontSize.value * 0.6,
        show: false,
      },
      grid: { top: '5%', right: '2%' },
      series: [
        ...new Array(barData.value.dimensions.length - 1 || 0).keys(),
      ].map(() => ({
        type: 'pictorialBar',
        symbol: 'path://M0,10 L10,10 C5.5,10 5.5,5 5,0 C4.5,5 4.5,10 0,10 z',
      })),
    }))

    const rankType = computed(() => {
      const data = store.state.dutyRank[curOption.value] || []
      if (data.length === 0) return []
      const total = data.reduce((acc, cur) => acc + cur.num, 0)
      return data.map((item: any) => {
        return {
          name: item.managementType || '无',
          value: +Math.ceil((item.num / total) * 100).toFixed(0),
        }
      })
    })
    return {
      rankType,
      showChart,
      options,
      curOption,
      rankList,
      placeList,
      barData,
      barOption,
    }
  },
})
</script>

<style lang="stylus" scoped>
@import '../../components/MyComponent/main.styl'
.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
  margin-top .2rem
  .content
    height .8rem
    display flex
    flex-direction column
    justify-content space-around
    >div
      display flex
      align-items center
      justify-content space-between
      .progress
        flex 1
        margin 0 .1rem
      span
        color #ccc
      .count
        font-family $font-barlow
        font-size .12rem
.place
  margin-top .1rem
  .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
.bar-chart
  height 1rem
  .chart
    width 100%
    height 90%
.duty-type
  width 100%
  height .6rem
  display flex
  flex-wrap wrap
  >div
    width 33.3%
    text-align center
    .circle
      width 100%
      height 80%
      background-image url('@/assets/images/circle-bg.png')
      background-repeat no-repeat
      background-size 50%
      background-position 49% 0%
      .circle-item
        padding-top 0.04rem
</style>
