<template>
  <panel-card title="设备数量分析">
    <el-row>
      <el-col :span="12">
        <ve-pie :data="chartDataPie" :extend="extendPie" :legend="legend" />
      </el-col>
      <el-col :span="12">
        <ve-bar :data="chartData" :grid="grid" :title="title" :extend="extend" :settings="chartSettings" />
      </el-col>
    </el-row>
  </panel-card>
</template>

<script>
import { deviceStaticByStatus, deviceStaticsByType } from '@/api/data/dataStatics'
import PanelCard from '@/components/BigData/Card/panelCard'
export default {
  name: 'DeviceCountView',
  components: { PanelCard },
  data() {
    return {
      // 饼状图
      extendPie: {
        series: {
          label: { show: true, position: 'outside', formatter: '{b}:{c}' },
          radius: '45%'
        }
      },
      chartSettingsPie: {
        labelMap: {
          'onlineStatus': '在线状态',
          'deviceCount': '设备数量'
        },
        dimension: 'onlineStatus',
        metrics: 'deviceCount'
      },
      legend: {
        right: 10
      },
      chartDataPie: {
        columns: ['onlineStatus', 'deviceCount'],
        rows: []
      },
      // 柱状图
      extend: {
        xAxis: {
          axisLabel: {
            rotate: 30,
            margin: 30,
            textStyle: {
              align: 'center'
            }
          }
        },
        series: {
          label: { show: true, position: 'right' },
          barMaxWidth: 35 // 最大柱宽度
        }
      },
      grid: {
        right: 60
      },
      chartSettings: {
        itemStyle: {
          'barCategoryGap': 5
        },
        barWidth: 15,
        labelMap: {
          'deviceType': '设备类型',
          'deviceCount': '设备数量'
        },
        dimension: ['deviceType'],
        metrics: ['deviceCount']
      },
      chartData: {
        columns: ['deviceType', 'deviceCount'],
        rows: []
      }
    }
  },
  created() {
    this.fetchDeviceOnline()
    this.fetchDeviceByType()
  },
  methods: {
    fetchDeviceOnline() {
      deviceStaticByStatus().then(response => {
        const data = response.data
        this.chartDataPie.rows = [
          { 'onlineStatus': '在线', 'deviceCount': data.online },
          { 'onlineStatus': '离线', 'deviceCount': data.offline }
        ]
      })
    },
    fetchDeviceByType() {
      deviceStaticsByType().then(response => {
        this.chartData.rows = response.data
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.flagBoxStyle {
  display: flex;
  margin-bottom: 20px;
}
.flagBoxStyle div:nth-child(2){
  line-height: 30px;
  font-weight: 600;
}
.flagStyle {
  width: 4px;
  height: 30px;
  margin-right: 6px;
  background: rgb(64 121 242);
}

</style>
