<template>
  <moduleEnclosure title="近30天工单趋势">
	  <template v-slot:rightce>
      <v-scroll-view :x="true" :y="false" slidingBgClassName="slidingBgClassName"
        slidingBlockClassName="slidingBlockClassName">
        <div class="tabbar">
          <div
            class="tab"
            :class="tabCurrent == item ? 'active' : ''"
            v-for="(item, index) in tabList"
            :key="index"
            @click="changeTab(item)">
            {{item}}
          </div>
        </div>
      </v-scroll-view>
	    
	  </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';

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

  mounted() {
    this.initData();
  },

  beforeUnmount() {
    
  },

  methods: {
    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;
    },

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

    initData() {
      getWorkorderTrend().then(res => {
        this.tabList = res.data.map(o => o.lx);
        this.tabCurrent = this.tabList[0];
        this.tableData = res.data;
        this.initEcharts();
      })
    },

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

      option = {
        title: {
          text: ''
        },
        tooltip: {
          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',
          data: ['已完工', '未完工'],
          textStyle: {
            color: '#ffffff'
          },
          right: 0,
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        toolbox: {
          // feature: {
          //   saveAsImage: {}
          // }
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          axisLine: {
            lineStyle: {
              color: '#ffffff', // x轴文字颜色
            }
          },
          splitLine: {
            show: true,
            lineStyle: {
              color: '#364D95', // x轴辅助线颜色
            }
          },
          data: this.getDate()
        },
        yAxis: {
          type: 'value',
          name: "（单/台）",
          nameTextStyle: {
            color: "#aaa",
            nameLocation: "start",
            // align: 'right'
          },
          axisLine: {
            lineStyle: {
              color: '#ffffff', // 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 .slidingBgClassName {
  background: rgba(0,0,0,0) !important;
}

::v-deep .slidingBlockClassName {
  background: rgba(0,0,0,0) !important;
}

.all-container {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  padding-top: 16px;
}
.tabbar{
  width: fit-content;
  display: flex;
  .tab{
    flex-shrink: 0;
    width: 50px;
    color: #ffffff;
    border: 1px solid #254280;
    font-size: 12px;
    font-weight: bold;
    padding: 2px 0;
    margin-right: 10px;
    text-align: center;
    cursor: pointer;
    &:last-child {
      margin-right: 0;
    }
  }
  .active{
    border: 1px solid #153781;
    color: #1a8dc8;
  }
}
</style>
