<template>
  <div class="deviceStatistics card">
    <div class="card-head">
      <div class="title">告警占比</div>
    </div>
    <div class="card-chart">
      <div ref="chartRef2" style="width: 100%; height: 100%"></div>
    </div>
  </div>
</template>
<script setup>
import { statisticsWarningapi } from "@/api/index";
import { onMounted, ref } from "vue";
import * as echarts from "echarts";
import { useResize } from "@/utils/hooks";

const chartRef2 = ref(null);
let myChart = null;
let option = ref({
  tooltip: {
    trigger: "item",
  },
  legend: {
    right: "left",
  },
  series: {
    type: "pie",
    radius: ["20%", "50%"],
    data: [
      { value: 0, name: "SOS告警" },
      { value: 0, name: "围栏告警" },
      { value: 0, name: "破坏告警" },
      { value: 0, name: "心率告警" },
      { value: 0, name: "血氧告警" },
      { value: 0, name: "体温告警" },
    ],
  },
});
const getStatisticsWarningApi = () => {
  statisticsWarningapi().then((res) => {
    option.value.series.data = [
      { value: res?.sosCount, name: "SOS告警" },
      { value: res?.railCount, name: "围栏告警" },
      { value: res?.destroyCount, name: "破坏告警" },
      { value: res?.heartRateCount, name: "心率告警" },
      { value: res?.bloodOxygenCount, name: "血氧告警" },
      { value: res?.tempCount, name: "体温告警" },
    ];
    myChart.setOption(option.value);
  });
};
useResize(() => {
  myChart.resize();
});
onMounted(() => {
  myChart = echarts.init(chartRef2.value);
  myChart.setOption(option.value);
  getStatisticsWarningApi();
});
</script>
<style scoped lang="less">
.card {
  height: 100%;
  background: #ffffff;
  padding: 16px 10px;
  box-sizing: border-box;
  position: relative;
  display: flex;
  border-radius: 5px;
  box-shadow: 0px 0px 12px rgba(0, 0, 0, 0.12);
  margin-bottom: 20px;
  .card-chart {
    height: 90%;
    width: 100%;
    margin-top: 30px;
  }

  .card-head {
    position: absolute;
    color: #061451;
    font-size: 18px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    transform: translateX(-10px);
    margin-bottom: 20px;
    .title {
      &::before {
        display: inline-block;
        margin-right: 10px;
        content: "";
        width: 2px;
        height: 14px;
        border-radius: 20px;
        background: #061451;
      }
    }
    .search {
      display: flex;
      align-items: center;
    }
  }
}
</style>
