<template>
    <div id="parkingTimeBox">
        <div id="parkingtime"
             style="width:100%;height:100%;"></div>
        <loading class="loadingBox" ref="loadingData"></loading>
        <!-- 四边小角 -->
<!--        <div class="left-top"></div>-->
<!--        <div class="right-top"></div>-->
<!--        <div class="left-bottom"></div>-->
<!--        <div class="right-bottom"></div>-->
    </div>
</template>
<script>
  import { vehiclechart } from '@/assets/chartApi/chartApi'
  import loading from '../../loading'
  import echarts from 'echarts'
  export default {
    name: 'parking-time',
    props: ['timeSlot', 'vehicleId', 'starttime', 'endtime'],
    components: {
      loading
    },
    data () {
      return {
      }
    },

    methods: {
      initData (chartTime, chartData, timeSlot) {
        // 基于准备好的dom，初始化echarts实例
        var myChart = echarts.init(document.getElementById('parkingtime'))
        // 指定图表的配置项和数据
        let option = {
          grid: {
            left: chartTime.length !== 0 ? '1%' : '3%',
            right: '8%',
            bottom: '6%',
            containLabel: true
          },
          legend: {
            textStyle: {
              color: '#fff'
            }
          },
          tooltip: {
            trigger: 'axis',
            formatter: function (params, ticket, callback) {
              return '<div style="padding: 6px">' +
                '<p>&nbsp;&nbsp;&nbsp;日期：&nbsp;' + params[0].name + '</p>' +
                '<p><span style="background: #03771A;width: 10px;height: 10px;border-radius: 50%;display: inline-block"></span> ' +
                '<span> 停车时长：' + params[0].value + 'h' + '</p>' +
                '</div>'
            }
          },
          xAxis: [
            {
              show: true,
              type: 'category',
              boundaryGap: false,
              data: chartTime.length !== 0 ? chartTime : '暂无数据',
              axisLine: {
                lineStyle: {
                  color: '#5a6379'
                }
              },
              axisLabel: {
                // interval: timeSlot !== 7 ? 'auto' : 0, //横轴信息全部显示
                interval: 'auto',
                // rotate:-30,//-30度角倾斜显示
                formatter: function (value) {
                  if (chartTime.length !== 0) {
                    return value.split('-')[1] + '-' + value.split('-')[2]
                  }
                  return value
                },
                color: '#fff'
              }
            }
          ],
          yAxis: [
            {
              type: 'value',
              name: '停车时长',
              axisLine: {
                lineStyle: {
                  color: '#5a6379'
                }
              },
              nameTextStyle: {
                color: '#fff',
                padding: [0, 0, 0, 50]
              },
              axisLabel: {
                color: '#fff'
              },
              splitLine: {
                lineStyle: {
                  color: '#5a6379'
                }
              }
            }
          ],
          series: [
            {
              name: '停车时长',
              type: 'line',
              smooth: true,
              itemStyle: {
                color: '#03771A'
              },
              data: chartData
            }
          ]
        }
        // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(option)
        myChart.resize()
        window.addEventListener('resize', () => {
          myChart.resize()
        })
      },
      drawLine (timeState, listData) {

      },
      // 获取数据
      getDataDetails (timeSlot, vehicleid) {
        let para = {
          vehicleid: vehicleid,
          // recentday: timeSlot,
          starttime: this.starttime,
          endtime: this.endtime
        }
        let chartTime = ''
        let chartData = ''
        let itemTime = []// 声明时间组
        let itemVeo = [] // 平均速度组
        this.$refs.loadingData.isShow = true
        vehiclechart(para).then(res => {
          if (res.data.result.code === 0) {
            this.$refs.loadingData.isShow = false
            res.data.data.forEach((item, index) => {
              itemTime.push(item.recdate.split(' ')[0])
              itemVeo.push(item.stopduration)
            })
            chartTime = itemTime
            chartData = itemVeo
            this.initData(chartTime, chartData, timeSlot)
          } else {
            this.$refs.loadingData.isShow = false
          }
        }).catch(() => {
          this.$refs.loadingData.isShow = false
        })
      }
    },
    mounted () {
      this.getDataDetails(this.timeSlot, this.vehicleId)
    },
    watch: {
      timeSlot: function (newVal) {
        this.getDataDetails(newVal, this.vehicleId)
      },
      starttime () {
        this.getDataDetails(this.timeSlot, this.vehicleId)
      },
      endtime() {
        this.getDataDetails(this.timeSlot, this.vehicleId)
      }
    }
  }
</script>
<style scoped>
    #parkingTimeBox {
        width: 100%;
        height: 100%;
        background-color: rgba(12, 43, 74, 0.8);
        position: relative;
        color: #2f76db;
        border: 1px solid #446da8;
    }
    .loadingBox{
        position:absolute;
        top:0;
        left:0;
    }
</style>
