<template>
  <div class="portStats">
     <!-- <p>注册统计</p> -->
      <Row class="portStats__query">
        <span>{{$t('date')}}：</span>
        <Col>
            <DatePicker :value="dateRange" :options="optionsFuture" type="daterange" placement="bottom-end" style="width: 400px" @on-change="changeDateRange"></DatePicker>
        </Col>
        <Button type="primary" class="portStats__query__btn" @click="getQuery">{{$t('refresh')}}</Button>
    </Row>
    <div ref="myChart" id="my_chart" :style="{width: '100%', height: '400px'}"></div>
    <Table border width="80%" :columns="columnsList" :data="loginData" :loading="registerLoad" class="portStats__tab">
    </Table>
  </div>
</template>

<script>
import { GetDataFlow } from '@/api/stats'
import { getToken, defaultDateRange } from '@/libs/util'
import echarts from 'echarts'
export default {
  data () {
    return {
      token: getToken(),
      registerLoad: false,
      xAxisTimeData: [], // 时间数据
      allData: [], // 全部注册
      monthAgo: [], // 同比上个月
      loginData: [], // 全部数据
      dateRange: ['', ''],
      columnsList: [
        {
          title: this.$t('date'),
          key: 'time',
          align: 'center'
        },
        {
          title: this.$t('reg_amount'),
          key: 'total',
          align: 'center'
        },
        {
          title: this.$t('compare_last_month'),
          key: 'monthAgo',
          align: 'center'
        }
      ],
      optionsFuture: {
        disabledDate (date) {
          return date && date.valueOf() > Date.now()
        }
      }
    }
  },
  created () {
    let dr = defaultDateRange()
    this.dateRange[0] = dr.starttime
    this.dateRange[1] = dr.endtime
    this.getQuery()
  },
  mounted () {
  },
  methods: {
    getQuery () {
      this.registerLoad = true
      const formdata = new FormData()
      formdata.append('access_token', this.token)
      formdata.append('start_time', this.dateRange[0])
      formdata.append('end_time', this.dateRange[1])
      GetDataFlow(formdata).then(res => {
        this.loginData = res.data.list
        this.xAxisTimeData = []
        this.allData = []
        this.registerLoad = false
        this.loginData.forEach(item => {
          this.xAxisTimeData.push(item.time)
          this.allData.push(item.total)
          this.monthAgo.push(item.monthAgo)
        })
        this.createEchars()
      })
    },
    changeDateRange (e) {
      // 包含结束当天
      this.dateRange = [e[0] + ' 00:00:00', e[1] + ' 23:59:59']
    },
    createEchars () {
      let myChartData = echarts.init(this.$refs.myChart)
      let option = {
        // 标题
        title: {
          text: this.$t('account_reg_stat'),
          subtext: this.$t('daily_reg_num')
        },
        tooltip: {
          trigger: 'axis'
        },
        // 标注
        legend: {
          right: '20%',
          data: [this.$t('reg_amount'), this.$t('compare_last_month')]
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        toolbox: {
          feature: {
            dataZoom: {
              yAxisIndex: 'none'
            },
            dataView: { readOnly: false },
            magicType: { type: ['line', 'bar'] },
            restore: {},
            saveAsImage: {}
          }
        },
        // X轴
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: this.xAxisTimeData
        },
        // Y轴
        yAxis: {
          type: 'value'
        },
        // 显示
        series: [
          {
            name: this.$t('reg_amount'),
            type: 'line',
            data: this.allData,
            markPoint: {
              data: [
                { type: 'max', name: this.$t('max_value') },
                { type: 'min', name: this.$t('min_value') }
              ]
            },
            markLine: {
              data: [
                { type: 'average', name: this.$t('agv_value') }
              ]
            }
          },
          {
            name: this.$t('compare_last_month'),
            type: 'line',
            data: this.monthAgo,
            markPoint: {
              data: [
                { type: 'max', name: this.$t('max_value') },
                { type: 'min', name: this.$t('min_value') }
              ]
            },
            markLine: {
              data: [
                { type: 'average', name: this.$t('agv_value') }
              ]
            }
          }
        ]
      }
      myChartData.setOption(option)
      // 建议加上以下这一行代码，不加的效果图如下（当浏览器窗口缩小的时候）。超过了div的界限（红色边框）
      window.addEventListener('resize', function () { myChartData.resize() })
    }
  }

}
</script>

<style  lang="scss" scoped>
.portStats{
  &__query{
    height:72px;
    background:rgba(255,255,255,1);
    box-shadow:0px 0px 30px 0px rgba(0,0,0,0.05);
    border-radius:10px;
    display: flex;
    align-items: center;
    margin-bottom: 20px;
    padding: 0 30px;
    &__btn{
      margin-left: 30px;
    }
  }
  &__tab{
    background: #fff;
    border-radius: 10px;
  }
}
#my_chart{
  padding: 20px;
  background-color: #fff;
  border-radius: 10px;
  margin-bottom: 20px;
}
/deep/.ivu-input{
  text-align: center;
}
</style>
