<script setup name="performanceStatistics">
import { onMounted, ref } from 'vue'
import * as echarts from 'echarts'
import { performance } from '@/http/apis/statisticAnalysis/performanceStatistics'

const
  formData = ref({}),
  chart1Ref = ref(), chart2Ref = ref(), chart3Ref = ref(), chart4Ref = ref(), chart5Ref = ref(), chart6Ref = ref()
let chart1, chart2, chart3, chart4, chart5, chart6
const color = new echarts.graphic.LinearGradient(0, 0, 1, 1, [{
  offset: 0,
  color: '#20DEEF'
}, {
  offset: 1,
  color: '#007FED'
}])
const
  getChartData = async () => {
    initChart1()
    initChart2()
    initChart3()
    initChart4()
    initChart5()
    initChart6()
  },
  initChart1 = () => {
    const colorList = [
      new echarts.graphic.LinearGradient(0, 0, 1, 1, [{
        offset: 0,
        color: 'rgba(32, 183, 96, 1)'
      }, {
        offset: 1,
        color: 'rgba(32, 183, 96, 0.5)'
      }]),
      new echarts.graphic.LinearGradient(0, 0, 1, 1, [{
        offset: 0,
        color: '#20DEEF'
      }, {
        offset: 1,
        color: '#007FED'
      }])
    ]
    if (!chart1) chart1 = echarts.init(chart1Ref.value)
    const option = {
      legend: {
        show: false
      },
      tooltip: {
        show: true,
        trigger: 'item'
      },
      color: colorList,
      graphic: [
        {
          type: 'text', // 通过不同top值可以设置上下显示
          left: 'center',
          top: 'center',
          style: {
            text: '项目数：250',
            textAlign: 'center',
            fill: '#333', // 文字的颜色
            fontSize: 16,
            fontWeight: 500
          }
        }
      ],
      series: [
        {
          data: detailData.value?.isPerformance?.filter(i => i.name !== '项目总数').map(i => {
            return {
              name: i.name,
              value: i.num
            }
          }),
          radius: ['45%', '80%'],
          type: 'pie',
          itemStyle: {
            borderRadius: 4
          },
          label: {
            formatter: '{b}\n{c}',
            color: '#333333'
          },
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        }
      ]
    }
    chart1.setOption(option)
  },
  initChart2 = () => {
    if (!chart2) chart2 = echarts.init(chart2Ref.value)
    const colorList = [
      new echarts.graphic.LinearGradient(0, 0, 1, 1, [{
        offset: 0,
        color: '#ccc'
      }, {
        offset: 1,
        color: '#ccc'
      }]),
      new echarts.graphic.LinearGradient(0, 0, 1, 1, [{
        offset: 0,
        color: '#20DEEF'
      }, {
        offset: 1,
        color: '#007FED'
      }])
    ]
    const option = {
      legend: {
        show: true,
        right: 0,
        orient: 'vertical'
      },
      tooltip: {
        show: true,
        trigger: 'item'
      },
      color: colorList,
      series: [
        {
          data: detailData.value?.selfAppraisalProgress?.map(i => {
            return {
              name: i.name,
              value: i.num
            }
          }),
          type: 'pie',
          itemStyle: {
            borderRadius: 4
          },
          label: {
            formatter: '{b}\n{c}',
            color: '#333333'
          },
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        }
      ]
    }
    chart2.setOption(option)
  },
  initChart3 = () => {
    if (!chart3) chart3 = echarts.init(chart3Ref.value)
    const option = {
      tooltip: {
        show: true,
        trigger: 'item'
      },
      grid: {
        bottom: 20,
        top: 10,
        right: 30
      },
      radar: {
        indicator: detailData.value?.scoreDistribution.map(i => {
          return {
            ...i,
            max: Math.max(detailData.value?.scoreDistribution.map(i => i.num))
          }
        })
      },
      series: [
        {
          name: '星级分布',
          type: 'radar',
          data: [
            {
              value: detailData.value?.scoreDistribution.map(i => i.num)
            }
          ]
        }
      ]
    }
    chart3.setOption(option)
  },
  initChart4 = () => {
    if (!chart4) chart4 = echarts.init(chart4Ref.value)
    const colorList = [
      new echarts.graphic.LinearGradient(0, 0, 1, 1, [{
        offset: 0,
        color: '#ccc'
      }, {
        offset: 1,
        color: '#ccc'
      }]),
      new echarts.graphic.LinearGradient(0, 0, 1, 1, [{
        offset: 0,
        color: '#20DEEF'
      }, {
        offset: 1,
        color: '#007FED'
      }])
    ]
    const option = {
      legend: {
        show: true,
        right: 0,
        orient: 'vertical'
      },
      tooltip: {
        show: true,
        trigger: 'item'
      },
      color: colorList,
      series: [
        {
          data: detailData.value?.reAppraisalProgress?.map(i => {
            return {
              name: i.name,
              value: i.num
            }
          }),
          type: 'pie',
          itemStyle: {
            borderRadius: 4
          },
          label: {
            formatter: '{b}\n{c}',
            color: '#333333'
          },
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        }
      ]
    }
    chart4.setOption(option)
  },
  initChart5 = () => {
    if (!chart5) chart5 = echarts.init(chart5Ref.value)
    const option = {
      tooltip: {
        show: true,
        trigger: 'axis',
        valueFormatter: (value) => value
      },
      grid: {
        bottom: 20,
        top: 10,
        right: 30
      },
      xAxis: {
        type: 'category',
        data: detailData.value?.orgTop10?.map(i => i.name),
        axisLabel: {
          align: 'center',
          interval: 0
        }
      },
      yAxis: {
        name: '',
        type: 'value'
      },
      color,
      series: [
        {
          name: '分数',
          data: detailData.value?.orgTop10?.map(i => i.score),
          type: 'bar',
          barWidth: 20
        }
      ]
    }
    chart5.setOption(option)
  },
  initChart6 = () => {
    if (!chart6) chart6 = echarts.init(chart6Ref.value)
    const colorList = [
      new echarts.graphic.LinearGradient(0, 0, 1, 1, [{
        offset: 0,
        color: '#ccc'
      }, {
        offset: 1,
        color: '#ccc'
      }]),
      new echarts.graphic.LinearGradient(0, 0, 1, 1, [{
        offset: 0,
        color: '#20DEEF'
      }, {
        offset: 1,
        color: '#007FED'
      }])
    ]
    const option = {
      legend: {
        show: true,
        right: 0,
        orient: 'vertical'
      },
      tooltip: {
        show: true,
        trigger: 'item'
      },
      color: colorList,
      series: [
        {
          data: detailData.value?.rectifyProgress?.map(i => {
            return {
              name: i.name,
              value: i.num
            }
          }),
          type: 'pie',
          itemStyle: {
            borderRadius: 4
          },
          label: {
            formatter: '{b}\n{c}',
            color: '#333333'
          },
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        }
      ]
    }
    chart6.setOption(option)
  },
  detailData = ref(),
  getData = async () => {
    const res = await performance({ year: formData.value.projectYear })
    detailData.value = res.data
    getChartData()
  }
onMounted(async () => {
  getData()
})
</script>

<template>
  <el-card class="mb-16">
    <el-form
      ref="formRef"
      :model="formData"
    >
      <el-form-item label="年度">
        <el-date-picker
          v-model="formData.projectYear"
          type="year"
          placeholder="全部"
          format="YYYY"
          value-format="YYYY"
          style="width: 200px"
          @change="getData"
        />
      </el-form-item>
    </el-form>
    <div class="flex justify-between text-center mt-24">
      <div class="flex-1">
        <p class="text-primary text-32 font-bold" style="font-family: DIN;">{{ detailData?.finishedTotal||0 }}</p>
        <p class="text-info text-14">当前已完成绩效评价项目总数</p>
      </div>
      <div class="flex-1">
        <p class="text-primary text-32 font-bold" style="font-family: DIN;">{{ detailData?.finishedSelf||0 }}</p>
        <p class="text-info text-14">当前已完成绩效自评总数</p>
      </div>
      <div class="flex-1">
        <p class="text-primary text-32 font-bold" style="font-family: DIN;">{{ detailData?.finishedReAppraisal||0 }}</p>
        <p class="text-info text-14">当前已完成绩效复评总数</p>
      </div>
      <div class="flex-1">
        <p class="text-primary text-32 font-bold" style="font-family: DIN;">{{ detailData?.rectifyTotal||0 }}</p>
        <p class="text-info text-14">当前需整改项目总数</p>
      </div>
    </div>
  </el-card>
  <el-row :gutter="16">
    <el-col :span="12" class="mb-16">
      <el-card header="绩效评价">
        <div ref="chart1Ref" class="h-[230px]"></div>
      </el-card>
    </el-col>
    <el-col :span="12" class="mb-16">
      <el-card header="自评进度">
        <div ref="chart2Ref" class="h-[230px]"></div>
      </el-card>
    </el-col>
    <el-col :span="12" class="mb-16">
      <el-card header="星级分布">
        <div ref="chart3Ref" class="h-[230px]"></div>
      </el-card>
    </el-col>
    <el-col :span="12" class="mb-16">
      <el-card header="复评进度">
        <div ref="chart4Ref" class="h-[230px]"></div>
      </el-card>
    </el-col>
    <el-col :span="12" class="mb-16">
      <el-card header="绩效TOP10">
        <div ref="chart5Ref" class="h-[230px]"></div>
      </el-card>
    </el-col>
    <el-col :span="12" class="mb-16">
      <el-card header="整改进度">
        <div ref="chart6Ref" class="h-[230px]"></div>
      </el-card>
    </el-col>
  </el-row>
</template>
