<!--
    filename: TransformRate.vue
    author: liubin
    date: 2024-04-29 08:50:34
    description: 转化效率
-->

<template>
  <BarChartBase
    :legend="legend"
    :series="series"
    :xAxis="xAxis"
    in="chipOEE"
    class="chip-oee"
  />
</template>

<script>
import BarChartBase from "@/views/copilot/efficiency/components/sub/bar/BarChartChipTrans.vue";
export default {
  name: "TransformRate",
  components: { BarChartBase },
  props: {
    period: {
      type: String,
      default: "日",
    },
    than: {
      type: String,
      default: '同比',
    },
    transformRate: {
      type: Object,
      default: {}
    }
  },
  data() {
    // 城市数组的顺序必须是固定的
    const cities = ["瑞昌", "邯郸",
      // "株洲", "佳木斯", "成都", "凯盛", "蚌埠"
    ];
    return {
      xAxis: cities,
    };
  },
  computed: {
    legend() {
      let items = [];
      var day1 = new Date();
      var day2 = new Date();
      day1.setTime(day1.getTime() - 24 * 60 * 60 * 1000)
      day2.setTime(day2.getTime() - 48 * 60 * 60 * 1000)
      var yesterday = (day1.getMonth() + 1) + "月" + day1.getDate()
      var dayBeYes = (day2.getMonth() + 1) + "月" + day2.getDate()
      //今天的时间
      // var day2 = new Date();
      // day2.setTime(day2.getTime());
      // var s2 = (day2.getMonth() + 1) + "月" + day2.getDate() + '日';
      const today = new Date().getDate();
      // let yesterday = new Date().getDate() -1;
      const month = new Date().getMonth() + 1;
      const lastMonth = new Date().getMonth() + 1 === 12 ? 1 : new Date().getMonth() + 1 - 1;
      const year = new Date().getFullYear();
      if (this.period === '日' && this.than === '同比') {
        items = [
          { label: `${yesterday}日目标`, color: "#58adfa" },
          { label: `${year - 1}年${yesterday}日`, color: "#12f7f1" },
          { label: `${yesterday}日`, color: "#58adfa" },
        ];
      } else if (this.period === '日' && this.than === '环比') {
        items = [
          { label: `${yesterday}日目标`, color: "#58adfa" },
          { label: `${dayBeYes}日`, color: "#12f7f1" },
          { label: `${yesterday}日`, color: "#58adfa" },
        ];
      } else if (this.period === '周' && this.than === '同比') {
        items = [
          { label: `本周目标`, color: "#58adfa" },
          { label: `${year - 1}年本周`, color: "#12f7f1" },
          { label: `本周`, color: "#58adfa" },
        ];
      } else if (this.period === '周' && this.than === '环比') {
        items = [
          { label: `本周目标`, color: "#58adfa" },
          { label: `上周`, color: "#12f7f1" },
          { label: `本周`, color: "#58adfa" },
        ];
      } else if (this.period === '月' && this.than === '同比') {
        items = [
          { label: `${month}月目标`, color: "#58adfa" },
          { label: `${year - 1}年${month}月`, color: "#12f7f1" },
          { label: `${month}月`, color: "#58adfa" },
          // { label: `${month}月目标`, value: valueTuple[2] },
        ];
      } else if (this.period === '月' && this.than === '环比') {
        items = [
          { label: `${month}月目标`, color: "#58adfa" },
          { label: `${lastMonth}月`, color: "#12f7f1" },
          { label: `${month}月`, color: "#58adfa" },
          // { label: `${month}月目标`, value: valueTuple[2] },
        ];
      } else {
        items = [
          { label: `${year}年目标`, color: "#58adfa" },
          { label: `${year - 1}年`, color: "#12f7f1" },
          { label: `${year}年`, color: "#58adfa" },
          // { label: `${year}年目标`, value: valueTuple[2] },
        ];
      }
      return items
    },
    // transformRate() {
    //   return this.$store.getters.copilot.efficiency.transformRate
    // },
    series() {
      // console.log('aaaaaaaa', this.$store.getters.copilot.efficiency.chipOee);
      const transformRate = this.transformRate
      console.log('chipOee', transformRate)
      let dataList = null;
      switch (this.period) {
        default:
          dataList = [];
          dataList[0] = transformRate.previous;
          dataList[1] = transformRate.current
          dataList[2] = transformRate.target
      }
      console.log('transformRate', dataList)
      return getTemplate(this.period, dataList, this.than);
    },
    // series() {
    //   const { transformRate } = this.$store.getters.copilot.efficiency;
    //   let dataList = null;

    //   switch (this.period) {
    //     case "日":
    //     case "周":
    //       dataList = transformRate?.current;
    //       break;
    //     default:
    //       dataList = [];
    //       dataList[0] = transformRate?.previous;
    //       dataList[1] = transformRate?.current;
    //   }

    //   return getTemplate(this.period, dataList);
    // },
  },
  methods: {},
};

function getTemplate(period, dataList,than) {
  // console.log('dataList',dataList);
  let items = [];
  var day1 = new Date();
  var day2 = new Date();
  day1.setTime(day1.getTime() - 24 * 60 * 60 * 1000)
  day2.setTime(day2.getTime() - 48 * 60 * 60 * 1000)
  var yesterday = (day1.getMonth() + 1) + "月" + day1.getDate()
  var dayBeYes = (day2.getMonth() + 1) + "月" + day2.getDate()
  //今天的时间
  // var day2 = new Date();
  // day2.setTime(day2.getTime());
  // var s2 = (day2.getMonth() + 1) + "月" + day2.getDate() + '日';
  const today = new Date().getDate();
  // let yesterday = new Date().getDate() -1;
  const month = new Date().getMonth() + 1;
  const lastMonth = new Date().getMonth() + 1 === 12 ? 1 : new Date().getMonth() + 1 - 1;
  const year = new Date().getFullYear();
  if (period === '日' && than === '同比') {
    items = [
      {
        name: `${year - 1}年${yesterday}日`,
        data: dataList ? dataList[0] : [],
      },
      {
        name: `${yesterday}日`,
        data: dataList ? dataList[1] : [],
        // : Array.from({ length: 7 }, () => Math.floor(Math.random() * 1000)),
      },
      {
        name: `${yesterday}日目标`,
        data: dataList ? dataList[2] : [],
        // : Array.from({ length: 7 }, () => Math.floor(Math.random() * 1000)),
      },
    ];
  } else if (period === '日' && than === '环比') {
    items = [
      {
        name: `${dayBeYes}日`,
        data: dataList ? dataList[0] : [],
      },
      {
        name: `${yesterday}日`,
        data: dataList ? dataList[1] : [],
        // : Array.from({ length: 7 }, () => Math.floor(Math.random() * 1000)),
      },
      {
        name: `${yesterday}日目标`,
        data: dataList ? dataList[2] : [],
        // : Array.from({ length: 7 }, () => Math.floor(Math.random() * 1000)),
      },
    ];
  } else if (period === '周' && than === '同比') {
    items = [
      {
        name: `${year-1}年本周`,
        data: dataList ? dataList[0] : [],
      },
      {
        name: `本周`,
        data: dataList ? dataList[1] : [],
        // : Array.from({ length: 7 }, () => Math.floor(Math.random() * 1000)),
      },
      {
        name: `本周目标`,
        data: dataList ? dataList[2] : [],
        // : Array.from({ length: 7 }, () => Math.floor(Math.random() * 1000)),
      },
    ];
  } else if (period === '周' && than === '环比') {
    items = [
      {
        name: `上周`,
        data: dataList ? dataList[0] : [],
      },
      {
        name: `本周`,
        data: dataList ? dataList[1] : [],
        // : Array.from({ length: 7 }, () => Math.floor(Math.random() * 1000)),
      },
      {
        name: `本周目标`,
        data: dataList ? dataList[2] : [],
        // : Array.from({ length: 7 }, () => Math.floor(Math.random() * 1000)),
      },
    ];
  } else if (period === '月' && than === '同比') {
    items = [
      {
        name: `${year-1}年${month}月`,
        data: dataList ? dataList[0] : [],
      },
      {
        name: `${month}月`,
        data: dataList ? dataList[1] : [],
        // : Array.from({ length: 7 }, () => Math.floor(Math.random() * 1000)),
      },
      {
        name: `${month}目标`,
        data: dataList ? dataList[2] : [],
        // : Array.from({ length: 7 }, () => Math.floor(Math.random() * 1000)),
      },
    ];
  } else if (period === '月' && than === '环比') {
    items = [
      {
        name: `${lastMonth}月`,
        data: dataList ? dataList[0] : [],
      },
      {
        name: `${month}月`,
        data: dataList ? dataList[1] : [],
        // : Array.from({ length: 7 }, () => Math.floor(Math.random() * 1000)),
      },
      {
        name: `${month}目标`,
        data: dataList ? dataList[2] : [],
        // : Array.from({ length: 7 }, () => Math.floor(Math.random() * 1000)),
      },
    ];
  } else {
    items = [
      {
        name: `${year - 1}年`,
        data: dataList ? dataList[0] : [],
      },
      {
        name: `${year}年`,
        data: dataList ? dataList[1] : [],
        // : Array.from({ length: 7 }, () => Math.floor(Math.random() * 1000)),
      },
      {
        name: `${year}目标`,
        data: dataList ? dataList[2] : [],
        // : Array.from({ length: 7 }, () => Math.floor(Math.random() * 1000)),
      },
    ];
  }
  return items
}
</script>

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