<template>
  <div class="index">
    <div v-if="isMobile">
      <Home></Home>
    </div>
    <div v-if="!isMobile">
      <christmas v-if="isOpen" @listenopen="listenOpen" :hasNewYear="hasNewYear"></christmas>
      <carousel>
        <kind-category @loadchild="loadProductKinds"></kind-category>
      </carousel>
      <advert></advert>
      <floor-list></floor-list>
      <img class="banner-img" src="/images/all/banner-home2.jpg" style="margin: 44px auto 24px;" alt="">
      <news></news>
      <partner></partner>
    </div>
  </div>
</template>
<script>
  import { KindCategory, Carousel, Advert, FloorList, Partner, News } from '~components/home'
  import { Christmas } from '~components/default'
  import { Home } from '~components/mobile'

  export default {
    name: 'index',
    layout (context) {
      return context.store.state.option.isMobile ? 'mobile' : 'main'
    },
    data () {
      return {
        isOpen: false,
        hasNewYear: false
      }
    },
    components: {
      KindCategory,
      Carousel,
      Advert,
      FloorList,
      Partner,
      News,
      Christmas,
      Home
    },
    fetch ({store}) {
      return !store.state.option.isMobile ? Promise.all([
        store.dispatch('loadFloors'),
        store.dispatch('loadBanners', {type: 'home'}),
        store.dispatch('loadProductKinds', { id: 0 }),
        store.dispatch('loadNewsSnapshot', { page: 1, pageSize: 10 })
      ]) : []
    },
    mounted () {
      let user = this.user.logged
      let count = 1
      let self = this
      const nowDate = new Date()
      const activeStartDate = new Date('2017/12/20 00:00:00')
      const activeEndDate = new Date('2018/1/2 00:00:00')
      const EndDate = new Date('2017/12/26 00:00:00')
      this.hasNewYear = nowDate > EndDate
      if (nowDate > activeStartDate && nowDate < activeEndDate) {
        const endTime = window.localStorage.getItem('endTime')
        if (!user) {
          setInterval(function () {
            count++
            if (count >= 60 * 60 * 2) {
              count = 1
              self.isOpen = true
            }
          }, 1000)
        }
        if (endTime) {
          if (nowDate.getTime() - endTime >= 1000 * 60 * 60 * 2) {
            user ? this.isOpen = false : this.isOpen = true
            window.localStorage.setItem('endTime', nowDate.getTime())
          } else {
            this.isOpen = false
          }
        } else {
          user ? this.isOpen = false : this.isOpen = true
          window.localStorage.setItem('endTime', nowDate.getTime())
        }
      } else {
        this.isOpen = false
      }
    },
    computed: {
      user () {
        return this.$store.state.option.user
      },
      isMobile: function () {
        return this.$store.state.option.isMobile
      }
    },
    methods: {
      listenOpen () {
        this.isOpen = false
      },
      loadProductKinds (id) {
        this.$store.dispatch('loadAllProductKinds', {id})
      }
    }
  }
</script>
