<template>
  <div class="servicefix">
    <div class="label">维修统计</div>
    <div id="servicefix" class="d1"></div>
  </div>
</template>

<script>
  import * as Util from "../../../Util";
  import * as DataSql from "../../../stores/DataSql";
  import co from "co";
  import {startDate} from "../../../stores/DrillSql";

  export default {
    name: "ServiceFix",
    data() {
      return {
        xc: '',
        timer:'',
        serviceFix:'',
        metaDate:[],
      }
    },
    beforeDestroy() {
      //清除定时器
      clearInterval(this.timer);
    },
    methods: {
      setBar() {
        this.xc = this.$echarts.init(document.getElementById('servicefix'))
        var scale=this.$parent.$parent.$parent.scale
        var xAxisData = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '', ''];
        var legendData = ['维修完成'];
        var serieData = [];
        var metaDate = this.metaDate
        var yname = '      月份\n(2018-2019)'
        for (var v = 0; v < legendData.length; v++) {
          var serie = {
            name: legendData[v],
            type: 'line',
            symbol: "circle",
            smooth: false,  // true: 光滑曲线  , false: 直线
            symbolSize: 5  ,
            color: '#f7cb32',
            lineStyle: {
              width: 1,
              color: '#f7cb32'
            },
            label: {
              show: false,
              position: 'top',
              textStyle: { //数值样式
                color: '#15dce2',
                fontSize: 14
              }
            },
            areaStyle: {
              color: {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [{
                  offset: 1,
                  color: '#031a2b' // 0% 处的颜色
                },
                  {
                    offset: 0.5,
                    color: '#273531' // 50% 处的颜色
                  },
                  {
                    offset: 0,
                    color: '#444c3e' // 100% 处的颜色
                  }],
                globalCoord: false // 缺省为 false
              },
            },

            data: metaDate
          }
          serieData.push(serie)
        }
        var colors = ["#036BC8", "#4A95FF", "#5EBEFC", "#2EF7F3", "#FFFFFF"]


        this.xc.setOption({
          title: {text: '', textAlign: 'left', textStyle: {color: "#fff", fontSize: "16*scale", fontWeight: "normal"}},
          legend: {
            itemGap: 20,
            width: '90%',
            show: true,
            right:'5%',
            data: legendData,
            y: 'top',
            itemWidth: 12 ,
            itemHeight: 6 ,
            textStyle: {width: '20%', color: "#15dce2", fontSize: 12 },
            formatter: function (params) {
              return params
            },
          },
          color: colors,
          grid: {left: '4%', top: "16%", bottom: "7%", right: "5%", containLabel: true},
          tooltip: {
            alwaysShowContent:true,
            backgroundColor:'rgba(2, 59, 63, 0.45)',
            // borderColor :'rgba(13, 109, 180)',
            borderWidth:0,
            hideDelay:100,
              padding:10*scale,
              textStyle:{
                  fontSize:18*scale
              },
            formatter: function (params) {
              if (params.data == '') {
                return ''
              }
              var result = ''
              params.forEach(function (item) {
                result += item.seriesName + " &nbsp;&nbsp;&nbsp;<sapn style='color:#e1e52a;font-weight: bolder'>" + item.value + "</sapn></br>";
              });
              return "<div style='text-align: center;height: 1px;font-weight: normal'>"+params[0].axisValue + '月</div><br/>' + result
            },
            axisPointer: {type: 'line', lineStyle: {width: 0}},
            trigger: 'axis',
            extraCssText: 'width: auto;height: auto;'
          },
          xAxis: [
            {
              name: yname,
              type: 'category',
              nameTextStyle: {
                fontSize: 12 * scale,
                color: '#15dce2',
                padding: [48 * scale, 0, 0, -70 * scale],
              },
              boundaryGap: true,
              type: 'category',
              axisLine: {show: true, lineStyle: {color: '#04669e', width: 1}},
              axisLabel: {interval: 0, textStyle: {color: '#ffffff', fontSize: 14 * scale}},
              axisTick: {show: false},
              data: xAxisData,
            },
          ],
          yAxis: [
            {
              name: '维修数量',
              nameTextStyle: {
                fontSize: 12 * scale,
                padding: [-8 * scale, 35 * scale, 0, 0],
              },
              axisTick: {show: false},
              splitLine: {show: true, lineStyle: {type: 'dashed', color: '#004371'}},
              axisLabel: {textStyle: {color: '#15dce2', fontSize: 14 * scale}},
              axisLine: {show: false, lineStyle: {color: '#15dce2', fontSize: 14 * scale}},
            },
          ],
          series: serieData
        })
        let idx = -1;
        var dataLen=metaDate.length
        var oroptionmapjsonr_charts1=this.xc
        this.timer= setInterval(()=>{
          idx = (idx+ 1) % dataLen;
          oroptionmapjsonr_charts1.dispatchAction({
            type: 'showTip',
            seriesIndex: 0,
            dataIndex: idx
          });

        }, 3000)
      },
      searchdata(year){
        console.log("-------------------------传进来的年份为")
        console.log(year)
        var startDate=year+'-01-01'
        var endDate= year+1+'-01-01'
        let serviceFix = DataSql.getData(this, 'serviceFix', {startDate:startDate,endDate:endDate},'serviceFix')
        co(serviceFix)
      },

    },
    ready () {
      var date = new Date();
      this.searchdata(date.getFullYear())

    },
    watch:{
      'serviceFix':{
        deep: true,
        handler(val) {
          console.log('-------------------------------进入维修统计')
          console.log(this.serviceFix,'进入维修统计')
          console.log(this.serviceFix.length,'查出维修统计长度')
          if(this.serviceFix.length===0){
            for (let i = 0; i <12 ; i++) {
                this.metaDate[i]=0
            }
          }else{
            for (let i = 0; i <this.serviceFix.length ; i++) {
              this.metaDate[i]=this.serviceFix[i].count
            }
            console.log(this.metaDate)
            for (let i = this.serviceFix.length; i < 12; i++) {
              this.metaDate[i]=0
            }
            console.log('======================================')
            console.log(this.metaDate)
          }

          this.setBar()

        }
      },

    }

  }
</script>

<style scoped>
  .label {
    /*margin-left: 20px;*/
    margin-left: 6%;
    font-size: 0.85em;
    color: #fff;
    width: 100%;
    height: 9%;
  }

  .d1 {
    /*padding: 20px 30px*/
  }

  #servicefix {
    /*height: 332px;*/
    height: 91%;
    width: 100%;
    padding: 2% 2%;
  }
</style>
