<template>
  <div class="data_body">
    <div class="title">
      <span>工具使用</span>
    </div>
    <div style="width:100%">
      <!-- <img src="../../assets/dataimage/1.png" style="width:90%" /> -->
      <div id="charts_canvas" class="echart" style="width: 100%; height: 100%;margin:0 0 0 1rem"></div>
    </div>
  </div>
</template>

<script>
export default {
  props: ["toolsJson", "tools"],
  data() {
    return {
      chartObj: null,
      ooption: {
        xdata: [],
        sdata: [],
        tools: [],
      },
      // 1电子白板 2便签 3思维导图 4问卷调查 5量规评分 6协同文档
      option: {
        tooltip: {
          trigger: "item",
        },
        xAxis: {
          type: "category",
          data: [],
        },
        yAxis: {
          type: "value",
        },
        series: [
          {
            data: [],
            type: "line",
          },
        ],
      },
    };
  },
  methods: {
    setChart(option) {
      // 雷达图显示的标签
      let newPromise = new Promise((resolve) => {
        resolve();
      });
      //然后异步执行echarts的初始化函数
      newPromise.then(() => {
        const chartObj = this.$echarts.init(
          //劳动课程
          this.$el.querySelector("#charts_canvas")
        );
        this.option.xAxis.data = option.xdata;
        this.option.series[0].data = option.sdata;
        // 初始化雷达图
        this.chartObj = chartObj;
        this.chartObj.setOption(option);
      });
    },
  },
  watch: {
    toolsJson(val) {
      this.ooption = {
        xdata: [],
        sdata: [],
        tools: [],
      };
      console.log(this.tools);
      console.log(this.toolsJson);
      this.tools.forEach((item, index) => {
        // 1电子白板 2便签 3思维导图 4问卷调查 5量规评分 6协同文档
        if (item == 1) {
          this.ooption.xdata.push("电子白板");
        } else if (item == 2) {
          this.ooption.xdata.push("便签");
        } else if (item == 3) {
          this.ooption.xdata.push("思维导图");
        } else if (item == 4) {
          this.ooption.xdata.push("问卷调查");
        } else if (item == 5) {
          this.ooption.xdata.push("量规评分");
        } else if (item == 6) {
          this.ooption.xdata.push("协同文档");
        }
        this.ooption.sdata.push(0);
        this.ooption.tools.push(item);
      });
      this.toolsJson.forEach((item, index) => {
        this.ooption.tools.forEach((x, i) => {
          if (item.tools == x) {
            this.ooption.sdata[i] += item.count;
          }
        });
      });
      console.log(this.ooption);
      if (!this.chartObj) {
        this.setChart(this.ooption);
      } else {
        this.option.xAxis.data = this.ooption.xdata;
        this.option.series[0].data = this.ooption.sdata;
        this.chartObj.setOption(this.option);
      }
    },
  },
  mounted() {
    console.log(this.tools);
    console.log(this.toolsJson);
    this.tools.forEach((item, index) => {
      // 1电子白板 2便签 3思维导图 4问卷调查 5量规评分 6协同文档
      if (item == 1) {
        this.ooption.xdata.push("电子白板");
      } else if (item == 2) {
        this.ooption.xdata.push("便签");
      } else if (item == 3) {
        this.ooption.xdata.push("思维导图");
      } else if (item == 4) {
        this.ooption.xdata.push("问卷调查");
      } else if (item == 5) {
        this.ooption.xdata.push("量规评分");
      } else if (item == 6) {
        this.ooption.xdata.push("协同文档");
      }
      this.ooption.sdata.push(0);
      this.ooption.tools.push(item);
    });
    this.toolsJson.forEach((item, index) => {
      this.ooption.tools.forEach((x, i) => {
        if (item.tools == x) {
          this.ooption.sdata[i] += item.count;
        }
      });
    });
    console.log(this.ooption);
    this.setChart(this.ooption);
    var _this = this;
     window.addEventListener('resize', () => {
      if (_this.chartObj) {
        _this.chartObj.resize();
      }
    })
  },
};
</script>

<style scoped>
.data_body {
  height: 500px;
  display: flex;
  position: relative;
  border-radius: 5px;
  /* border: 1px solid #eee; */
  margin: 10px auto;
  box-sizing: border-box;
  padding: 10px;
  width: 95%;
  background: #fff;
}
.data_body .title {
  position: absolute;
  top: 20px;
  left: 20px;
}
</style>
