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

const
  formData = ref({}),
  chart1Ref = ref(), chart2Ref = ref(), chart3Ref = ref()
let chart1, chart2, chart3
const color = new echarts.graphic.LinearGradient(0, 0, 1, 1, [{
  offset: 0,
  color: '#20DEEF'
}, {
  offset: 1,
  color: '#007FED'
}])
const
  getChartData = async () => {
    initChart1()
    initChart2()
    initChart3()
  },
  initChart1 = () => {
    if (!chart1) chart1 = echarts.init(chart1Ref.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 = {
      legend: {
        show: false
      },
      tooltip: {
        show: true,
        trigger: 'item'
      },
      color: colorList,
      series: [
        {
          data: detailData.value?.twoYearsAvg.map(i => {
            return {
              ...i,
              value: i.amount
            }
          }),
          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 option = {
      tooltip: {
        show: true,
        trigger: 'axis',
        valueFormatter: (value) => value
      },
      grid: {
        bottom: 42,
        top: 10,
        right: 30
      },
      xAxis: {
        type: 'category',
        data: detailData.value?.twoYearsPricing.map(i => i.name),
        axisLabel: {
          interval: 0,
          hideOverlap: false
        }
      },
      yAxis: {
        name: '',
        type: 'value'
      },
      color,
      series: [
        {
          data: detailData.value?.twoYearsPricing.map(i => i.amount),
          type: 'line',
          name: '核价费',
          areaStyle: {}
        }
      ]
    }
    chart2.setOption(option)
  },
  initChart3 = () => {
    if (!chart3) chart3 = echarts.init(chart3Ref.value)

    const option = {
      tooltip: {
        show: true,
        trigger: 'axis',
        valueFormatter: (value) => value
      },
      grid: {
        bottom: 42,
        top: 10,
        right: 30
      },
      xAxis: {
        type: 'category',
        data: detailData.value?.twoYearsReviews.map(i => i.name),
        axisLabel: {
          interval: 0,
          hideOverlap: false
        }
      },
      yAxis: {
        name: '',
        type: 'value'
      },
      color,
      series: [
        {
          name: '评审费',
          data: detailData.value?.twoYearsReviews.map(i => i.amount),
          type: 'line',
          areaStyle: {}
        }
      ]
    }
    chart3.setOption(option)
  },
  detailData = ref(),
  getData = async () => {
    const res = await projectCost({ 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?.reviewAmount||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?.pricingAmount||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="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="chart1Ref" class="h-[230px]"></div>
      </el-card>
    </el-col>
  </el-row>
</template>
