<template>
  <div id="left-1">
    <el-row>
      <el-col :span="24" class="main">
        <el-col :span="24" class="one">
          <div id="left1" style="width: 100%; height: 17vw"></div>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
// 引入基本模板
let echarts = require('echarts/lib/echarts');
// 引入柱状图组件
require('echarts/lib/chart/bar');
// 引入提示框和title组件
require('echarts/lib/component/tooltip');
require('echarts/lib/component/title');
echarts = require('echarts');
const { purpose } = require('@common/src/layout/deploy/dict');
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: company_base } = createNamespacedHelpers('company_base');
export default {
  name: 'left-1',
  props: {},
  components: {},
  data: function () {
    return {
      purpose: purpose,
      list: [],
      chart: undefined,
    };
  },
  async created() {
    await this.search();
  },
  methods: {
    ...company_base(['query']),
    // 查询企业类型数量
    async search() {
      let typeList = [];
      let res = await this.query();
      if (this.$checkRes(res)) {
        for (const val of this.purpose) {
          let data = res.data.filter((i) => i.purpose_id == val.value);
          typeList.push({ name: val.label, purpose_id: val.value, value: data.length });
        }
      }
      this.$set(this, `list`, typeList);
      if (!this.chart) this.init();
      else this.changeData();
    },
    // 更换chart数据
    changeData() {
      if (!this.chart) return;
      let option = this.getOption(this.list);
      this.chart.setOption(option);
    },
    // 组织chart的数据函数
    getOption(data) {
      let option;
      option = {
        tooltip: { trigger: 'item' },
        legend: { orient: 'vertical', left: 'left', textStyle: { color: '#ffffff' } },
        series: [{ name: '企业类型', type: 'pie', radius: '50%', center: ['50%', '70%'], data }],
      };
      return option;
    },
    async init() {
      var chartDom = document.getElementById('left1');
      var myChart = echarts.init(chartDom);
      let option = this.getOption(this.list);
      option && myChart.setOption(option);
      // myChart.on('click', (params) => {
      //   EventBus.$emit('toCompany', params.data);
      // });
    },
  },
  computed: {
    ...mapState(['user']),
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

<style lang="less" scoped></style>
