<script lang='ts' setup>

import ChartBox from '@/components/ChartBox/index.vue'
import echarts from '@/config/plugins/echarts'
import { CircelOptions } from '../../../charts'

import { getSummaryFlows } from '@/api/history'

const chart = ref(null)
const chartEl = ref(null)

const props = defineProps<{
  dats: any
}>()

const summary = ref(0)

const legends = ref([
  { color: '#FFA71A', text: '危化品车', num: 0, precent: '0.00%' },
  { color: '#507AFC', text: '客车', num: 0, precent: '0.00%' },
  { color: '#70A9FF', text: '轿车', num: 0, precent: '0.00%' },
  { color: '#A5D80B', text: '货车', num: 0, precent: '0.00%' },
  { color: '#00C2C2', text: '其他', num: 0, precent: '0.00%' }
])

watch(
  () => props.dats,
  e => {
    if (e) {

      const carMap = Object.fromEntries(e.map(item => {
        return [
          item.catType === '未知' ? '其他' : item.catType,
          item
        ]
      }))

      summary.value = carMap['全部'].count
      legends.value = legends.value.map(item => {
        const nowCar = carMap[item.text]
        return {
          ...item,
          num: nowCar.count,
          precent: (nowCar.rate * 100).toFixed(2) + '%'
        }
      })

      setCharts()
    }
  }
)

const setCharts = () => {

  if (!chartEl.value) {
    chartEl.value = echarts.init(chart.value)
  }

  const options = CircelOptions()

  options.series[0].data = legends.value.map(item => {
    return {
      value: item.num,
      name: item.text,
      itemStyle: {
        color: item.color
      }
    }
  })

  chartEl.value.setOption(options)

}

</script>

<template>

<div class="traffic-summary">

  <div class="header">
    <div class="icon"></div>
    <div class="text">通行车次统计</div>
  </div>

  <div class="chart-content">
    <ChartBox>
        <div ref="chart" class="chart"></div>
        <div class="nums">
          <div class="line-x">{{ summary }}</div>
          <div class="line-x">车次</div>
        </div>
    </ChartBox>
  </div>

  <div class="legends">
    <div v-for="(item, x) in legends" :key="x" class="line">
      <div class="col-1">
        <div :style="{ background: item.color }" class="rect"></div>
        <div class="text">{{ item.text }}</div>
      </div>
      <div class="col-2">
        {{ item.num }}辆
      </div>
      <div class="col-3">{{ item.precent }}</div>
    </div>
  </div>

</div>

</template>

<style lang='scss' scoped>

$path-event: '@/assets/images/event/';

.traffic-summary {
  .header {
    margin: 12px 12px 0;
    display: flex;
    align-items: center;
    .icon {
      width: 30px;
      height: 30px;
      background: url($path-event + 'title.png') no-repeat;
      background-size: cover;
    }
    .text {
      height: 28px;
      line-height: 28px;
      width: 150px;
      border-radius: 110px;
      color: white;
      font-weight: 600;
      font-size: 16px;
      padding-left: 12px;
      margin-left: 2px;
      background: linear-gradient(90deg, rgba(87, 115, 162, 0.81) 0%, rgba(87, 115, 162, 0) 99%);
    }
  }
  .chart-content {
    position: relative;
    overflow: hidden;
    height: 318px;
    margin-top: 90px;
    .chart {
      height: 318px;
      width: 318px;
      margin: auto;
    }

    .nums {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      text-align: center;
      color: white;
      .line-x:first-child {
        font-family: PingFang SC;
        font-size: 40px;
        line-height: 40px;
        font-weight: 600;
        margin-bottom: 2px;
      }
    }
  }
  .legends {
    width: 350px;
    margin: 40px auto 0;
    .line {
      height: 28px;
      display: flex;
      color: white;
      margin-bottom: 12px;
      .col-1 {
        display: flex;
        align-items: center;
        width: 140px;
        flex-shrink: 0;
        .rect {
          width: 15px;
          height: 15px;
          margin-right: 10px;
        }
      }
      .col-2 {
        width: 100px;
        flex-shrink: 0;
        text-align: right;
      }
      .col-3 {
        flex-grow: 1;
        text-align: right;
      }
    }
  }
}

</style>