<template>
  <moduleEnclosure title="近30天工单趋势">
	  <template v-slot:rightce>
      <el-tabs v-model="tabCurrent" type="card" @tab-click="changeTab">
        <el-tab-pane v-for="(item, index) in tabList" :key="index" :label="item" :name="item"></el-tab-pane>
      </el-tabs>
	    
	  </template>
		<div class="all-container">
      <div id="workorderEcharts1" style="width: 100%; height: 100%"></div>
    </div>
  </moduleEnclosure>
</template>

<script>
import vScrollView from 'v-scroll-view'
import moduleEnclosure from '@/components/moduleEnclosure.vue';
import * as echarts from 'echarts';
import { getWorkorderTrend } from '@/api/bigView.js';
import { csize } from '@/utils/const.js';

export default {
  components: {
    moduleEnclosure,
    vScrollView
  },
  data() {
    return {
      tabList: [],
      tabCurrent: '',
      tableData: [],
    };
  },

  mounted() {
    this.dingshishuaxin()
  },

  beforeUnmount() {
    this.mouseenter();
  },

  watch: {
    tabCurrent() {
      this.mouseenter();
      this.dingshishuaxin()
    }
  },

  methods: {
    mouseenter() {
      if (this.TimeId) {
        clearTimeout(this.TimeId)
      }
    },
    dingshishuaxin() {
      this.initData();
      this.TimeId = setTimeout(() => {
        this.setbiuaoji()
      }, 3000)
    },
    getDate() {
      const today = new Date();
      const dates = [];
      for (let i = 0; i < 30; i++) {
        const d = new Date();
        d.setDate(today.getDate() - i);
        dates.unshift(d.toISOString().split('T')[0].slice(5, 11));
      }
      return dates;
    },

    setbiuaoji() {
      var index = this.tabList.indexOf(this.tabCurrent)
      if (index >= (this.tabList.length - 1)) {
        this.tabCurrent = this.tabList[0]
      } else {
        this.tabCurrent = this.tabList[index + 1]
      }
    },

    changeTab(item) {
      this.tabCurrent = item.name;
      this.initEcharts();
    },

    initData() {
      getWorkorderTrend().then(res => {
        this.tabList = res.data.map(o => o.lx);
        if (this.tabCurrent == "") {
          this.setbiuaoji()
        }
        this.tableData = res.data;
        this.initEcharts();
      })
    },

    initEcharts() {
      var chartDom = document.getElementById('workorderEcharts1');
      var myChart = echarts.init(chartDom);
      var option;

      option = {
        title: {
          text: ''
        },
        tooltip: {
          confine: true,
          trigger: 'axis',
          transitionDuration: 0,
          backgroundColor: '#092a66',
          borderColor: '#0462b5',
          borderRadius: 8,
          borderWidth: 2,
          padding: [5, 10],
          axisPointer: {
            type: 'line',
            lineStyle: {
              type: 'dashed',
              color: '#8AB1DC',
            },
          },
          textStyle: {
            color: '#ffffff'
          }
        },
        legend: {
          icon: 'circle',
          itemWidth: 8,
          itemHeight: 8,
          itemGap: 10,
          top: '16',
          right: '10',
          data: ['已完工', '未完工'],
          textStyle: {
            color: '#A9BBCC'
          },
        },
        // grid: {
        //   left: '3%',
        //   right: '4%',
        //   bottom: '3%',
        //   containLabel: true
        // },
        toolbox: {
          // feature: {
          //   saveAsImage: {}
          // }
        },
        grid: {
          x: csize(40),
          y: csize(50),
          x2: csize(20),
          y2: csize(20),
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          axisLabel: {
            fontSize: csize(12),
          },
          axisLine: {
            lineStyle: {
              color: '#8AB1DC', // x轴文字颜色
            }
          },
          splitLine: {
            show: true,
            lineStyle: {
              color: '#364D95', // x轴辅助线颜色
            }
          },
          data: this.getDate()
        },
        yAxis: {
          type: 'value',
          name: "（单/台）",
          nameTextStyle: {
            color: "#aaa",
            nameLocation: "start",
            // align: 'right'
          },
          axisLabel: {
            fontSize: csize(12),
          },
          axisLine: {
            lineStyle: {
              color: '#8AB1DC', // y轴文字颜色
            }
          },
          splitLine: {
            show: true,
            lineStyle: {
              color: '#364D95', // y轴辅助线颜色
            }
          }
        },
        series: [
          {
            name: '已完工',
            type: 'line',
            stack: 'Total',
            itemStyle: {
              normal: {
                color: '#44E28F',
              },
            },
            data: this.tableData.find(o => o.lx == this.tabCurrent).unitNum
          },
          {
            name: '未完工',
            type: 'line',
            stack: 'Total',
            itemStyle: {
              normal: {
                color: '#D73A79',
              },
            },
            data: this.tableData.find(o => o.lx == this.tabCurrent).unitNumWwg
          },
        ]
      };

      option && myChart.setOption(option);
    }
  },
};
</script>

<style scoped lang="scss">

::v-deep .el-tabs {
  width: 100%;
  .el-tabs__header {
    margin-bottom: 0;
    border-bottom: none;
  }
  .el-tabs__nav {
    border: none;
  }
  .el-tabs__nav-prev {
    line-height: 30px;
  }
  .el-tabs__nav-next {
    line-height: 30px;
  }
  .el-tabs__item {
    width: 50px;
    height: 30px;
    line-height: 30px;
    color: #ffffff;
    border: 1px solid #254280 !important;
    font-size: 12px;
    font-weight: bold;
    margin-right: 10px;
    padding: 0 !important;
    text-align: center;
    &:last-child {
      margin-right: 0;
    }
    &.is-active {
      border: 1px solid #153781 !important;
      color: #1a8dc8;
    }
  }
}

.all-container {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  padding-top: 16px;
}
</style>
