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

const
  formData = ref({}),
  chart1Ref = ref(), chart2Ref = ref(), chart3Ref = ref(), chart4Ref = ref(), chart5Ref = ref()
let chart1, chart2, chart3, chart4, chart5
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()
  },
  initChart1 = () => {
    if (!chart1) chart1 = echarts.init(chart1Ref.value)
    const option = {
      tooltip: {
        show: true,
        trigger: 'axis',
        valueFormatter: (value) => value
      },
      grid: {
        bottom: 25,
        top: 10,
        right: 30
      },
      xAxis: {
        type: 'category',
        data: detailData.value?.regionExpert.map(i => i.name),
        axisLabel: {
          align: 'center',
          interval: 0,
          rotate: 20
        }
      },
      yAxis: {
        name: '',
        type: 'value'
      },
      color,
      series: [
        {
          name: '入驻专家数',
          data: detailData.value?.regionExpert.map(i => i.num),
          type: 'bar',
          barWidth: 20
        }
      ]
    }

    chart1.setOption(option)
  },
  initChart2 = () => {
    if (!chart2) chart2 = echarts.init(chart2Ref.value)
    const option = {
      tooltip: {
        show: true,
        trigger: 'axis',
        valueFormatter: (value) => value
      },
      grid: {
        bottom: 20,
        top: 10,
        right: 30
      },
      xAxis: {
        type: 'category',
        data: detailData.value?.threeYearsAdded?.map(i => i.name),
        axisLabel: {
          align: 'center',
          interval: 0
        }
      },
      yAxis: {
        name: '',
        type: 'value'
      },
      color,
      series: [
        {
          name: '新增专家数',
          data: detailData.value?.threeYearsAdded?.map(i => i.num),
          type: 'line'
        }
      ]
    }
    chart2.setOption(option)
  },
  tabPosition = ref('省维度'),
  changTab = (val) => {
    tabPosition.value = val
    initChart3()
  },
  initChart3 = () => {
    if (!chart3) chart3 = echarts.init(chart3Ref.value)
    const option = {
      legend: {
        show: false
      },
      tooltip: {
        show: true,
        trigger: 'item'
      },
      series: [
        {
          data: detailData.value?.typeDistribution[tabPosition.value]?.map(i => {
            return {
              name: i.name,
              value: i.num
            }
          }),
          radius: ['45%', '80%'],
          type: 'pie',
          itemStyle: {
            borderRadius: 4
          },
          label: {
            formatter: function (params) {
              return params.name + '\n' + params.value + `${params.name === '财务专家' ? '+2' : ''}`
            },
            color: '#333333'
          },
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        }
      ]
    }
    chart3.setOption(option)
  },
  initChart4 = () => {
    if (!chart4) chart4 = echarts.init(chart4Ref.value)
    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'
      }])
    ]
    const option = {
      tooltip: {
        show: true,
        trigger: 'item'
      },
      color: colorList,
      series: [
        {
          radius: ['45%', '80%'],
          data: detailData.value?.types?.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 = {
      legend: {
        show: false
      },
      tooltip: {
        show: true,
        trigger: 'item'
      },
      series: [
        {
          data: detailData.value?.levels?.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)'
            }
          }
        }
      ]
    }
    chart5.setOption(option)
  },
  detailData = ref(),
  getData = async () => {
    const res = await expert({ year: formData.value.projectYear })
    detailData.value = res.data
    getChartData()
  }
onMounted(async () => {
  getData()
})
</script>

<template>
  <el-card class="mb-16">
    <el-form
      v-if="false"
      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-center text-center mt-24">
      <div class="flex-1">
        <p class="text-primary text-32 font-bold" style="font-family: DIN;">{{ detailData?.total||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?.thisYearAdded||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?.active||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>
        <template #header>
          <div class="flex items-center justify-between">
            <p>专家类型分布-擅长方向</p>
            <el-radio-group v-model="tabPosition" @change="changTab">
              <el-radio-button label="省维度">省维度</el-radio-button>
              <el-radio-button label="市维度">市维度</el-radio-button>
            </el-radio-group>
          </div>
        </template>
        <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="专家等级">
        <div ref="chart5Ref" class="h-[230px]"></div>
      </el-card>
    </el-col>
  </el-row>
</template>
