<template>
  <div class="register-logout-count-charts">
    <div id="videoCharts" ref="videoCharts" :style="'width: 100%;height:' + height + ';'" />
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { formatDate } from '@/utils'

export default {
  name: 'RegisterLogoutCountCharts',
  props: {
    height: {
      type: String,
      default: '600px'
    },
    chartsData: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      videoRealTimeCharts: null
    }
  },
  computed: {
    ...mapGetters(['language'])
  },
  watch: {
    chartsData: {
      handler() {
        this.renderCharts()
      },
      deep: true
    },
    language() {
      this.renderCharts()
    }
  },
  mounted() {
    this.videoRealTimeCharts = this.$echarts.init(this.$refs.videoCharts)
    this.renderCharts()
    window.addEventListener('resize', () => {
      this.videoRealTimeCharts.resize()
    })
  },
  methods: {
    handleListToChartsData() {
      const chartsData = {
        newMales: [],
        maleLogouts: [],
        newFamles: [],
        femaleLogouts: []
      }
      const newMales = chartsData.newMales
      const maleLogouts = chartsData.maleLogouts
      const newFamles = chartsData.newFamles
      const femaleLogouts = chartsData.femaleLogouts
      if (this.chartsData.length > 0) {
        this.chartsData.forEach(item => {
          const date = formatDate(item.statisticsTime, 'yyyy-MM-dd')
          newMales.push([date, (item.maleCount || 0)])
          maleLogouts.push([date, (item.maleLogout || 0)])
          newFamles.push([date, (item.femaleCount || 0)])
          femaleLogouts.push([date, (item.femaleLogout || 0)])
        })
      }
      return chartsData
    },
    renderCharts() {
      if (!this.videoRealTimeCharts) {
        return
      }
      const thatDayData = this.handleListToChartsData()
      this.videoRealTimeCharts.setOption({
        color: ['#66b3ff', '#ce90e8', '#ff9c6e', '#5cdbd3'],
        tooltip: {
          trigger: 'axis',
          backgroundColor: 'rgba(50,50,50,0.5)',
          axisPointer: {
            type: 'cross',
            label: {
              lineStyle: { color: '#009688' },
              crossStyle: { color: '#008acd' },
              shadowStyle: { color: 'rgba(200,200,200,0.2)' }
            }
          }
        },
        grid: {
          x: 20,
          y: 50,
          x2: 20,
          y2: 0,
          containLabel: true,
          borderColor: '#eee'
        },
        toolbox: { color: ['#1e90ff', '#1e90ff', '#1e90ff', '#1e90ff'], effectiveColor: '#ff4500' },
        xAxis: [{
          show: false,
          type: 'category',
          boundaryGap: false,
          splitArea: {
            show: true,
            areaStyle: { color: ['rgba(250,250,250,0.1)', 'rgba(200,200,200,0.1)'] }
          },
          axisLabel: {
            rotate: 38
          },
          axisLine: { lineStyle: { color: '#b7bdc7' }},
          splitLine: { lineStyle: { color: ['#eee'] }},
          splitNumber: 24
        }],
        yAxis: [{
          type: 'value',
          axisTick: { show: true, length: 0 },
          splitNumber: 5,
          splitLine: { lineStyle: { color: ['#eee'] }},
          axisLine: { lineStyle: { color: '#b7bdc7' }}
        }],
        legend: { y: 10, textStyle: { color: '#8e929b' }},
        series: [
          {
            name: this.$t('pages.dashboard.dailyRegisterUser.newMale'),
            type: 'line',
            stack: this.$t('pages.dashboard.dailyRegisterUser.newMale'),
            smooth: 0.6,
            symbol: 'none',
            symbolSize: 10,
            data: thatDayData.newMales,
            areaStyle: {}
          },
          {
            name: this.$t('pages.dashboard.dailyRegisterUser.newFemale'),
            type: 'line',
            stack: this.$t('pages.dashboard.dailyRegisterUser.newFemale'),
            smooth: 0.6,
            symbol: 'none',
            symbolSize: 10,
            data: thatDayData.newFamles,
            areaStyle: {}
          },
          {
            name: this.$t('pages.dashboard.dailyRegisterUser.logoutMale'),
            type: 'line',
            stack: this.$t('pages.dashboard.dailyRegisterUser.logoutMale'),
            smooth: 0.6,
            symbol: 'none',
            symbolSize: 10,
            data: thatDayData.maleLogouts,
            areaStyle: {}
          },
          {
            name: this.$t('pages.dashboard.dailyRegisterUser.logoutFemale'),
            type: 'line',
            stack: this.$t('pages.dashboard.dailyRegisterUser.newFemale'),
            smooth: 0.6,
            symbol: 'none',
            symbolSize: 10,
            data: thatDayData.femaleLogouts,
            areaStyle: {}
          }
        ]
      }, true)
    }
  }
}
</script>
