<template>
  <chart-container class="bar-chart-base">
    <div class="left-chart-base">
      <!-- <div class="legend">
      <span v-for="item in legend" :key="item.label" class="legend-item"
        :style="{ fontSize: isFullscreen ? '0.85vw' : '0.73vw' }">{{ item.label }}</span>
    </div> -->

      <div id="factoryEnergyChart" style="width: 100%; height: 100%"></div>
    </div>
  </chart-container>
</template>
<script>
import { debounce } from "@/utils/debounce";
import * as echarts from "echarts";
import ChartContainer from "../../components/ChartContainer.vue";

// import CopilotButton from "./chartButton"
export default {
  components: {
    ChartContainer
    // CopilotButton,
  },
  name: "Energy",
  props: {
    // legend: {
    //   type: Array,
    //   required: true,
    // },
    type: {
      type: String,
      default:'目标产量'
    },
    energyCockpits: {
      type: Array,
      required: true,
    },
  },
  data() {
    return {
      isFullscreen: false,
      actualOptions: null,
      chart: "",
      data: [],
      orderXAxis:[],
      // obj:{
      //   seriesArr: [],
      //   targetProduceArr: [],
      //   planIn: [],
      //   acIn: [],
      //   acProduceArr: [],
      //   scrapNum: [],
      //   processNum: [],
      // },
      options: {
        color: ["#FFD160", "#2760FF", "#12FFF5"],
        grid: {
          left: "6%",
          right: "3%",
          bottom: "15%",
          top: "15%",
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            // type: "cross",
            crossStyle: {
              color: "rgba(237,237,237,0.5)",
            },
          },
          extraCssText: 'box-shadow: 0px 4px 8px 0px rgba(0, 0, 0, 0.38); border- radius: 4px;opacity: 0.6;backdrop- filter: blur(6px);',
          backgroundColor: '#001829',
          borderColor: '#001829',
          formatter: params => {
            console.log('params', params)
            var res = ``;
            for (var i = 0, l = params.length; i < l; i++) {
              let color = Object.prototype.toString.call(params[i].color) == "[object String]" ? params[i].color : params[i].color.colorStops ? params[i].color.colorStops[0].color : ''
              // console.log(item.color, color);
              res +=
                `<span style='display:inline-block;margin-right:4px;width:10px;height:10px;background-color:${color}'></span>` +
              `<span style='display:inline-block;width:150px;color:rgba(255,255,255,0855);font-size:14px;'>${params[0].axisValueLabel}</span>` +
              `<span style='color:rgba(255,255,255,0.65);font-size:14px;'>${params[i].value ? params[i].value + '片' : 0 + '片'}</span>`;
            }
            return res;
          },
        },
        xAxis: {
          axisTick: {
            show: false,
          },
          axisLine: {
            lineStyle: {
              color: "#4561AE",
            },
          },
          axisLabel: {
            interval: 0,//设置横坐标为斜
            rotate:45,
            color: "rgba(255, 255, 255, 0.7)",
            fontSize: 12,
            // formatter: function (value) {
            //   console.log(value);

            //   return value;
            // },
          },
          data: [],
        },
        // dataZoom: [
        //   {
        //     // show: true,
        //     start: 0,
        //     end: 50
        //   }
        //   ],
        yAxis: {
            type: "value",
            name: "单位/片",
            position: 'left',
            nameTextStyle: {
              color: "rgba(255, 255, 255, 0.7)",
              fontSize: 12,
              align: "right",
            },
            axisTick: {
              show: false,
            },
            axisLabel: {
              color: "rgba(255, 255, 255, 0.7)",
              fontSize: 10,
            },
            axisLine: {
              show: true,
              lineStyle: {
                color: "#4561AE",
              },
            },
            splitLine: {
              lineStyle: {
                color: "#4561AE",
              },
            },
          },
        series: [
          // {
          //   name: "",
          //   data: [],
          //   type: "line",
          //   areaStyle: {
          //     color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
          //       { offset: 0, color: "#FFD160" + "80" },
          //       { offset: 0.5, color: "#FFD160" + "20" },
          //       { offset: 1, color: "#FFD160" + "00" },
          //     ]),
          //   },
          //   lineStyle: {
          //     width: 2,
          //   },
          //   symbol: "circle",
          //   symbolSize: 8,
          //   emphasis: {
          //     focus: "series",
          //   },
          // },
          // {
          //   name: "",
          //   data: [],
          //   type: "line",
          //   yAxisIndex: 1,
          //   areaStyle: {
          //     color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
          //       { offset: 0, color: "#2760FF" + "80" },
          //       { offset: 0.5, color: "#2760FF" + "20" },
          //       { offset: 1, color: "#2760FF" + "00" },
          //     ]),
          //   },
          //   lineStyle: {
          //     width: 2,
          //   },
          //   symbol: "circle",
          //   symbolSize: 8,
          //   emphasis: {
          //     focus: "series",
          //   },
          // },
          {
            name: "",
            data: [],
            type: "bar",
            // yAxisIndex: 1,
            // areaStyle: {
            //   color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            //     { offset: 0, color: "#12FFF5" + "80" },
            //     { offset: 0.5, color: "#12FFF5" + "20" },
            //     { offset: 1, color: "#12FFF5" + "00" },
            //   ]),
            // },
            barWidth: 12,
            itemStyle: {
              borderRadius: [10, 10, 0, 0],
              color: {
                type: "linear",
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [
                  {
                    offset: 0,
                    color: "#57abf8", // 0% 处的颜色
                  },
                  {
                    offset: 1,
                    color: "#364BFE66", // 100% 处的颜色
                  },
                ],
                global: false, // 缺省为 false
              },
            },
            symbol: "circle",
            symbolSize: 8,
            emphasis: {
              focus: "series",
            },
          },
        ],
      },
    };
  },
  computed: {
    isOpen() {
      return this.$store.getters.sidebar.opened;
    },
  },
  watch: {
    type(val) {
      this.initChart()
    },
    /** 全屏状态切换时，对柱子粗细和字体大小进行相应调整 */
    // isFullscreen(val) {
    //   this.actualOptions.series.map((item) => {
    //     item.barWidth = val ? 18 : 12;
    //   });
    //   this.actualOptions.xAxis.axisLabel.fontSize = val ? 18 : 12;
    //   this.actualOptions.yAxis.axisLabel.fontSize = val ? 18 : 12;
    //   this.actualOptions.yAxis.nameTextStyle.fontSize = val ? 18 : 12;
    //   this.initOptions(this.actualOptions);
    // },
    // series(val) {
    //   if (!val) {
    //     this.initOptions(this.options);
    //     return;
    //   }
    //   const actualOptions = JSON.parse(JSON.stringify(this.options));
    //   actualOptions.series[0].data = val[0].data;
    //   actualOptions.series[0].name = val[0].name;
    //   actualOptions.series[1].data = val[1].data;
    //   actualOptions.series[1].name = val[1].name;
    //   actualOptions.series[2].data = val[2].data;
    //   actualOptions.series[2].name = val[2].name;
    //   this.actualOptions = actualOptions;
    //   this.initOptions(actualOptions);
    // },
    // energyCockpits() {
    //   this.initChart();
    // },
    isOpen(val) {
      this.canvasReset();
    },
  },
  mounted() {
    // if (screenfull.isEnabled) {
    //   screenfull.on("change", () => {
    //     this.isFullscreen = screenfull.isFullscreen;
    //   });
    // }
    this.canvasReset();
    window.addEventListener("resize", this.canvasReset);
  },
  beforeDestroy() {
    if (this.chart) {
      this.chart.dispose();
    }
  },
  destroyed() {
    window.removeEventListener("resize", this.canvasReset);
  },
  methods: {
    canvasReset() {
      debounce(() => {
        this.initChart();
      }, 500)();
    },
    initChart() {
      if (this.type === '目标产量') {
        this.orderXAxis = []
        this.data = []
        if (this.energyCockpits) {
          this.energyCockpits.forEach(ele => {
            this.data.push(ele.targetProduction)
            if (ele.workOrderNumber.length > 4) {
              ele.workOrderNumber = ele.workOrderNumber.substring(0, 4) + "..";
            }
            this.orderXAxis.push(ele.workOrderNumber)
          });
        }
        // this.initChart(targetProduceArr, orderXAxis)
      } else if (this.type === '计划投入') {
        this.orderXAxis = []
        this.data = []
        if (this.energyCockpits) {
          this.energyCockpits.forEach(ele => {
            this.data.push(ele.plannedInvestment)
            if (ele.workOrderNumber.length > 4) {
              ele.workOrderNumber = ele.workOrderNumber.substring(0, 4) + "..";
            }
            this.orderXAxis.push(ele.workOrderNumber)
          });
        }
      } else if (this.type === '实际投入') {
        this.orderXAxis = []
        this.data = []
        if (this.energyCockpits) {
          this.energyCockpits.forEach(ele => {
            this.data.push(ele.actualInvestment)
            if (ele.workOrderNumber.length > 4) {
              ele.workOrderNumber = ele.workOrderNumber.substring(0, 4) + "..";
            }
            this.orderXAxis.push(ele.workOrderNumber)
          });
        }
      } else if (this.type === '实际产出') {
        this.orderXAxis = []
        this.data = []
        if (this.energyCockpits) {
          this.energyCockpits.forEach(ele => {
            this.data.push(ele.actualProduction)
            if (ele.workOrderNumber.length > 4) {
              ele.workOrderNumber = ele.workOrderNumber.substring(0, 4) + "..";
            }
            this.orderXAxis.push(ele.workOrderNumber)
          });
        }
      } else if (this.type === '废品数量') {
        this.orderXAxis = []
        this.data  = []
        if (this.energyCockpits) {
          this.energyCockpits.forEach(ele => {
            this.data.push(ele.wasteNum)
            if (ele.workOrderNumber.length > 4) {
              ele.workOrderNumber = ele.workOrderNumber.substring(0, 4) + "..";
            }
            this.orderXAxis.push(ele.workOrderNumber)
          });
        }
      } else if (this.type === '待再加工') {
        this.orderXAxis = []
        this.data = []
        if (this.energyCockpits) {
          this.energyCockpits.forEach(ele => {
            console.log(ele.reworkNum);
            this.data.push(ele.reworkNum)
            if (ele.workOrderNumber.length > 4) {
              ele.workOrderNumber = ele.workOrderNumber.substring(0, 4) + "..";
            }
            this.orderXAxis.push(ele.workOrderNumber)
          });
        }
      }

      // let seriesArr = [
      //   {
      //     name: "水",
      //     energyType: 1,
      //     data: [],
      //   },
      //   {
      //     name: "电",
      //     energyType: 2,
      //     data: [],
      //   },
      //   {
      //     name: "气",
      //     energyType: 3,
      //     data: [],
      //   },
      // ];
      // if (this.energyCockpits.length > 0) {
      //   let dataArr = this.energyCockpits.map((item) => {
      //     return item.groupName;
      //   });
      //   energyxAxis = Array.from(new Set(dataArr));
      //   n = energyxAxis.length;
      //   seriesArr[0].data = Array.from({ length: n }, () => 0);
      //   seriesArr[1].data = Array.from({ length: n }, () => 0);
      //   seriesArr[2].data = Array.from({ length: n }, () => 0);
      //   for (let i = 0; i < this.energyCockpits.length; i++) {
      //     for (let j = 0; j < energyxAxis.length; j++) {
      //       if (this.energyCockpits[i].groupName === energyxAxis[j]) {
      //         if (this.energyCockpits[i].energyType === 1) {
      //           seriesArr[0].data[j] = this.energyCockpits[i].totalEnergyValue;
      //         } else if (this.energyCockpits[i].energyType === 2) {
      //           seriesArr[1].data[j] = this.energyCockpits[i].totalEnergyValue;
      //         } else if (this.energyCockpits[i].energyType === 3) {
      //           seriesArr[2].data[j] = this.energyCockpits[i].totalEnergyValue;
      //         }
      //       }
      //     }
      //   }
      // }

      // actualOptions.series[0].name = seriesArr[0].name;
      // actualOptions.series[1].data = seriesArr[1].data;
      // actualOptions.series[1].name = seriesArr[1].name;
      // actualOptions.series[2].data = seriesArr[2].data;
      // actualOptions.series[2].name = seriesArr[2].name;
      if (this.chart) {
        this.chart.dispose();
      }
      this.chart = echarts.init(document.getElementById("factoryEnergyChart"));
      // const actualOptions = JSON.parse(JSON.stringify(this.options));
      this.options.xAxis.data = this.orderXAxis;
      this.options.series[0].data = this.data;
      // this.actualOptions = actualOptions;
      this.chart.setOption(this.options);
    },
  },
};
</script>
<style scoped lang="scss">
.left-chart-base {
  // position: relative;
  height: 100%;

  .legend {
    position: absolute;
    top: 5.2vh;
    right: 4vw;
  }

  .legend-item {
    position: relative;
    // font-size: 12px;
    margin-right: 1.34vw;

    &::before {
      content: "";
      display: inline-block;
      // width: 10px;
      // height: 10px;
      width: 0.531vw;
      height: 0.531vw;
      background-color: #ccc;
      border-radius: 2px;
      margin-right: 0.22vw;
    }
  }

  .legend-item:nth-child(1):before {
    // width: 12px;
    // height: 2px;
    width: 0.638vw;
    height: 0.1064vw;
    background-color: #ffd160;
    position: absolute;
    top: 50%;
    // left: -16px;
    left: -0.851vw;
    transform: translateY(-50%);
  }

  .legend-item:nth-child(1):after {
    background-color: #ffd160;
    content: "";
    display: inline-block;
    position: absolute;
    // width: 6px;
    // height: 6px;
    width: 0.3191vw;
    height: 0.3191vw;
    border-radius: 100%;
    top: 50%;
    left: -0.851vw;
    // left: -16px;
    transform: translateY(-50%) translateX(50%);
  }

  .legend-item:nth-child(2):before {
    // width: 12px;
    // height: 2px;
    width: 0.638vw;
    height: 0.1064vw;
    background-color: #2760ff;
    position: absolute;
    top: 50%;
    // left: -16px;
    left: -0.851vw;
    transform: translateY(-50%);
  }

  .legend-item:nth-child(2):after {
    background-color: #2760ff;
    content: "";
    display: inline-block;
    position: absolute;
    // width: 6px;
    // height: 6px;
    width: 0.3191vw;
    height: 0.3191vw;
    border-radius: 100%;
    top: 50%;
    left: -0.851vw;
    // left: -16px;
    transform: translateY(-50%) translateX(50%);
  }

  .legend-item:nth-child(3):before {
    // width: 12px;
    // height: 2px;
    width: 0.638vw;
    height: 0.1064vw;
    background-color: #12fff5;
    position: absolute;
    top: 50%;
    // left: -16px;
    left: -0.851vw;
    transform: translateY(-50%);
  }

  .legend-item:nth-child(3):after {
    background-color: #12fff5;
    content: "";
    display: inline-block;
    position: absolute;
    // width: 6px;
    // height: 6px;
    width: 0.3191vw;
    height: 0.3191vw;
    border-radius: 100%;
    top: 50%;
    left: -0.851vw;
    // left: -16px;
    transform: translateY(-50%) translateX(50%);
  }
}
</style>
