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

const
  formData = ref({}),
  chart1Ref = ref(), chart2Ref = ref(), chart3Ref = ref(), chart5Ref = ref(), chart6Ref = ref()
let chart1, chart2, chart3, 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()
    initChart5()
    initChart6()
  },
  initChart1 = () => {
    if (!chart1) chart1 = echarts.init(chart1Ref.value)
    const option = {
      tooltip: {
        show: true,
        trigger: 'axis',
        valueFormatter: (value) => value
      },
      grid: {
        bottom: 20,
        top: 10,
        right: 30
      },
      xAxis: {
        type: 'category',
        data: detailData.value?.threeYearsAlarms.map(i => i.name),
        axisLabel: {
          align: 'center',
          interval: 0
        }
      },
      yAxis: {
        name: '',
        type: 'value'
      },
      color,
      series: [
        {
          name: '累计报警数',
          data: detailData.value?.threeYearsAlarms.map(i => i.num),
          type: 'bar',
          barWidth: 50
        }
      ]
    }
    chart1.setOption(option)
  },
  initChart2 = () => {
    if (!chart2) chart2 = echarts.init(chart2Ref.value)
    const option = {
      tooltip: {
        show: true,
        trigger: 'axis',
        valueFormatter: (value) => value
      },
      grid: {
        bottom: 45,
        top: 10,
        right: 10,
        left: 70
      },
      xAxis: {
        type: 'category',
        data: detailData.value?.alarmsFlowsNow.map(i => i.name),
        axisLabel: {
          interval: 0,
          hideOverlap: false,
          rotate: 20
        }
      },
      yAxis: {
        name: '',
        type: 'value'
      },
      color,
      series: [
        {
          name: '报警数量',
          data: detailData.value?.alarmsFlowsNow.map(i => i.num),
          type: 'line'
        }
      ]
    }
    chart2.setOption(option)
  },
  initChart3 = () => {
    if (!chart3) chart3 = echarts.init(chart3Ref.value)

    const option = {
      tooltip: {
        show: true,
        trigger: 'axis',
        valueFormatter: (value) => value
      },
      grid: {
        bottom: 20,
        top: 10,
        right: 30,
        left: 80
      },
      xAxis: {
        type: 'value'
      },
      yAxis: {
        type: 'category',
        data: detailData.value?.alarmsFillingNow.map(i => i.name)
      },
      color,
      series: [
        {
          name: '预警数量',
          data: detailData.value?.alarmsFillingNow.map(i => i.num),
          type: 'bar',
          barWidth: 20
        }
      ]
    }
    chart3.setOption(option)
  },
  // initChart4 = () => {
  //   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 (!chart4) chart4 = echarts.init(chart4Ref.value)
  //   const option = {
  //     legend: {
  //       show: false
  //     },
  //     tooltip: {
  //       show: true,
  //       trigger: 'item'
  //     },
  //     color: colorList,
  //     series: [
  //       {
  //         data: [
  //           {
  //             value: '100',
  //             name: '项目初验'
  //           },
  //           {
  //             value: '150',
  //             name: '项目终验'
  //           }
  //         ],
  //         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)'
  //           }
  //         }
  //       }
  //     ]
  //   }
  //   chart4.setOption(option)
  // },
  initChart5 = () => {
    const colorList = [
      new echarts.graphic.LinearGradient(0, 0, 1, 1, [{
        offset: 0,
        color: '#C59400'
      }, {
        offset: 1,
        color: 'rgba(142, 120, 36, 0.50)'
      }]),
      new echarts.graphic.LinearGradient(0, 0, 1, 1, [{
        offset: 0,
        color: 'rgba(197, 90, 70, 1)'
      }, {
        offset: 1,
        color: 'rgba(197, 90, 70, 0.5)'
      }])
    ]
    if (!chart5) chart5 = echarts.init(chart5Ref.value)
    const option = {
      legend: {
        show: false
      },
      tooltip: {
        show: true,
        trigger: 'item'
      },
      color: colorList,
      series: [
        {
          data: detailData.value?.constructionAlarms.map(i => {
            return {
              ...i,
              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)
  },
  initChart6 = () => {
    if (!chart6) chart6 = echarts.init(chart6Ref.value)
    const option = {
      legend: {
        show: false
      },
      tooltip: {
        show: true,
        trigger: 'item'
      },
      series: [
        {
          data: detailData.value?.safetyMonitoring.map(i => {
            return {
              ...i,
              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 earlyWarning({ 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?.accumulatedAlarmsTotal||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?.accumulatedAlarmsFlow||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?.accumulatedAlarmsFilling||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 v-if="false" :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-col :span="12" class="mb-16">
      <el-card header="安全监测">
        <div ref="chart6Ref" class="h-[230px]"></div>
      </el-card>
    </el-col>
  </el-row>
</template>
