<template>
  <div class="chartsContainer">
    <div class="chartsContainer-title">问题发现</div>
    <div class="chartsContainer-list">
      <div class="chartsContainer-content">
        <div class="chartsContainer-content-top">
          <div class="chartsContainer-content-name">总体问题分布</div>
        </div>
        <div class="chartsContainer-content-content" ref="pie" />
      </div>

      <div class="chartsContainer-content">
        <div class="chartsContainer-content-top">
          <div class="chartsContainer-content-name">问题分布对比</div>
        </div>
        <div class="chartsContainer-content-content" ref="radar" />
      </div>
    </div>
  </div>
</template>

<script setup>
import { problemDistribution, problemComparison } from '@/api/item/items'
import { useTimeOut } from './useTimeOut';
import { useECharts } from '@/hooks/useEcharts.js';
import { ref, computed, reactive, inject } from 'vue'
const provideParams = inject('provideParams')
const pie = ref(null)
const pieData = ref([])
const setChartsOptions = computed(() => {
  return {
    tooltip: {
      trigger: 'item',
      formatter: '{a} <br/>{b}: {c} ({d}%)'
    },
    legend: {
      show: false
    },
    series: [
      {
        name: '问题分布',
        type: 'pie',
        radius: [ '0%', '50%' ], // 环形图设置
        center: [ '50%', '50%' ],
        data: pieData.value.map(item => {
          return {
            name: item.name,
            value: item.total
          }
        }),
        label: {
          show: true,
          formatter: function (params) {
            return params.name + '\n' + params.percent + '%'
          },
          color: '#fff',
          fontSize: 14,
        },
        labelLine: {
          length: 10
        },
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      }
    ]
  }
})
useECharts(pie, setChartsOptions);

const radar = ref(null)
const radarData = reactive({
  grounp: [],
  data: [],
  legend: []
})
const setRadarOptions = computed(() => {
  return {
    tooltip: {
      trigger: 'item'
    },
    legend: {
      data: radarData.legend,
      bottom: 0,
      textStyle: { color: "#fff" },
    },
    grid: {
      top: '5%',
      bottom: '5%',
      left: '5%',
      right: '5%'
    },
    radar: {
      indicator: radarData.grounp,
      axisName: {
        color: '#fff'
      },
       radius: '50%',
    },
    series: [
      {
        type: 'radar',
        data: radarData.data,
      }
    ]
  }
})
useECharts(radar, setRadarOptions);


const permutationRadarDataHandler = (result = []) => {
  const grounpObj = result.reduce((cur, acc) => {
    if (cur[ acc.deptName ]) {
      cur[ acc.deptName ].push(acc)
    } else {
      cur[ acc.deptName ] = [ acc ]
    }
    return cur
  }, {})

  const grounp = Object.entries(grounpObj)

  radarData.legend = grounp.map(item => {
    return item[ 0 ]
  })


  radarData.grounp = grounp[ 0 ] && grounp[ 0 ][ 1 ] ? grounp[ 0 ][ 1 ].map(item => ({ text: item.name })) : []
  const color = ['#408CFF', '#58A55C', '#DD4D43', '#FAC858']
  radarData.data = grounp.map((item, index) => {
    return {
      name: item[ 0 ],
      value: item[ 1 ].map(attr => attr.total),
      // // 为每个数据系列设置不同的颜色
      color: color[ index % color.length ],
      areaStyle: {
        color: color[ index % color.length ],
        opacity: 0.5
      },
      lineStyle: {
        color: color[ index % color.length ],
        width: 2
      },
      itemStyle: {
        color: color[ index % color.length ],
        borderWidth: 2
      }
    }
  })
}

useTimeOut(() => {
  provideParams.value.checkedDepartmentId &&  problemDistribution({
    startDate: provideParams.value.startDate,
    endDate: provideParams.value.endDate,
    checkedBrigadeId: provideParams.value.checkedDepartmentId === 'ALL' ? '' : provideParams.value.checkedDepartmentId
  }).then(res => {
    pieData.value = res.data || []
  })
  provideParams.value.checkedDepartmentId &&  problemComparison({
    startDate: provideParams.value.startDate,
    endDate: provideParams.value.endDate,
    checkedBrigadeId: provideParams.value.checkedDepartmentId === 'ALL' ? '' : provideParams.value.checkedDepartmentId
  }).then(res => {
    permutationRadarDataHandler(res.data)
  })
}, [provideParams])



</script>

<style lang="scss" scoped>
.chartsContainer {
  width: 100%;
  height: 100%;
  position: relative;
  background: linear-gradient(to bottom, rgba(8, 97, 117, 0) 0%, #13a2d6 200%);
  border-radius: 4px;
  overflow: hidden;
  display: flex;
  flex-direction: column;

  .chartsContainer-title {
    height: 42px;
    width: 100%;
    background: linear-gradient(to right, #004387 0%, #090B18 100%);
    border-left: 1px solid #1892CE;
    line-height: 42px;
    color: #fff;
    font-weight: 900;
    font-size: 20px;
    text-indent: 1em;
  }

  .chartsContainer-list {
    padding: 20px 15px 10px;
    box-sizing: border-box;
    display: flex;
    column-gap: 20px;
    flex: 1;
    overflow: hidden;
  }

  .chartsContainer-content {
    display: flex;
    flex-direction: column;
    row-gap: 10px;
    flex: 1;
    overflow: hidden;

    .chartsContainer-content-top {
      display: flex;
      align-items: center;
      color: #78DEF5;

      .chartsContainer-content-name {
        color: #fff;
        font-weight: bold;
        font-size: 16px;
        display: flex;
        align-items: center;

        &::before {
          content: '';
          display: inline-block;
          border-left: 9px solid #1CB6FF;
          border-top: 8px solid transparent;
          border-right: 9px solid transparent;
          border-bottom: 8px solid transparent;
        }
      }
    }

    .chartsContainer-content-describe {
      color: #78DEF5;
      font-size: 12px;
      font-weight: bold;
      display: flex;
      align-items: center;
    }

    .chartsContainer-content-content {
      flex: 1;
    }
  }
}
</style>