<template>
  <div>
    <el-row>
      <el-col :span="12" :offset="6">
        <div class="chart-title">各单位设备安装情况数量统计</div>
      </el-col>
    </el-row>
    <div>
      <el-row :gutter="20" type="flex" justify="end">
        <el-col :span="8" style="margin-right: 30px">
          <el-select v-model="listQuery.deviceType" class="select-right" size="mini" placeholder="设备类型" clearable @change="fetchData">
            <el-option
              v-for="item in deviceTypeList"
              :key="item.value"
              :label="item.name"
              :value="item.value"/>
          </el-select>
        </el-col>
      </el-row>
    </div>
    <div>
      <ve-histogram :data="chartData" :grid="grid" :extend="extend" :settings="chartSettings"/>
    </div>
  </div>
</template>

<script>
import { getDeviceType } from '@/api/device'
import { deviceStaticsByDept } from '@/api/dataStatics'
export default {
  name: 'DeviceCountByDept',
  data() {
    this.extend = {
      xAxis: {
        axisLabel: {
          rotate: 30,
          margin: 30,
          textStyle: {
            align: 'center'
          }
        }
      },
      series: {
        label: { show: true, position: 'top' },
        barMaxWidth: 35 // 最大柱宽度
      }
    }
    this.grid = {
      right: 60
    }
    this.title = {
      text: '设备数量统计'
    }
    this.chartSettings = {
      itemStyle: {
        'barCategoryGap': 5
      },
      barWidth: 15,
      labelMap: {
        'deptName': '权属单位',
        'deviceCount': '设备数量（个）'
      },
      dimension: ['deptName'],
      metrics: ['deviceCount']
    }
    return {
      listQuery: {
        deviceType: ''
      },
      deviceTypeList: [],
      rawData: '',
      chartData: {
        columns: ['deptName', 'deviceCount'],
        rows: []
      }
    }
  },
  mounted() {
    this.fetchDeviceType()
    this.fetchData()
  },
  methods: {
    // 获取设备类型
    fetchDeviceType() {
      getDeviceType(this.listQuery).then(response => {
        this.deviceTypeList = [{
          name: '全部',
          value: ''
        }]
        // 过滤掉该单位不支持的设备类型
        const deviceTypes = this.$store.getters.deviceTypes
        for (const deviceType of response.data) {
          if (deviceTypes.indexOf(deviceType.value) !== -1) {
            this.deviceTypeList.push(deviceType)
          }
        }
        if (this.deviceTypeList.length <= 1) {
          this.showDeviceType = false
        }
      })
    },
    fetchData() {
      console.log('fetchData')
      deviceStaticsByDept(this.listQuery).then(response => {
        this.chartData.rows = response.data
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .chart-title{
    margin: auto;
    text-align: center;
    margin-top: 15px;
    font-size:20px;
    font-weight:600
  }
  .select-right{
    /*margin-right: 25px;*/
    margin-top: 15px;
    /*width: 200px*/
  }
</style>
