<template>
  <div class="records">
    <div ref="recordsLayouts" class="records__layouts">
      <div class="records__entry" @click="openRecord">
        <div class="records__entry-wrap">
          <p>抽奖</p>
          <p>记录</p>
        </div>
      </div>
      <div v-if="isShowWinnersList" class="records__entry records__entry--winners" @click="openRecordPopup('winners')">
        <div class="records__entry-wrap">
          <p>中奖</p>
          <p>名单</p>
        </div>
      </div>
    </div>
    <RecordsPupup
      v-model="pupupVisible"
      :active-tab="activeTab"
      :is-show-winners-list="isShowWinnersList"
      @setIsShowRecordsPopup="setIsShowRecordsPopup"
    ></RecordsPupup>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import RecordsPupup from '@/components/Lottery/Instant/RecordsPupup'
import Bus from '@/utils/Bus'

export default {
  name: 'Records',
  components: {
    RecordsPupup
  },
  data() {
    return {
      isShowRecordsPopup: false,
      activeTab: 'personal',
      recordsLayouts: null,
      body: null
    }
  },
  computed: {
    ...mapGetters({ isLogin: 'users/isLogin', lotteryInfo: 'lottery/lotteryInfo' }),
    isShowWinnersList() {
      const { lotteryInfo } = this
      return !!lotteryInfo.showResult
    },
    pupupVisible() {
      return this.isShowRecordsPopup
    },
  },
  mounted() {
    this.recordsLayouts = this.$refs.recordsLayouts
    this.body = document.body
    this.recordsAnimation()
  },
  methods: {
    openRecord() {
      if (this.recordsLayouts && this.recordsLayouts.classList.contains('sidebar-hidden-scroll')) {
        this.recordsLayouts.classList.remove('sidebar-hidden-scroll')
        return false
      }
      if (!this.isLogin) {
        this.$toast({
          message: '请先登录，正在为您转跳',
          duration: 1500,
          onClose: () => {
            Bus.$emit('showLogin')
          }
        })
      } else {
        this.openRecordPopup('personal')
      }
    },
    openRecordPopup(type) {
      if (this.recordsLayouts && this.recordsLayouts.classList.contains('sidebar-hidden-scroll')) {
        this.recordsLayouts.classList.remove('sidebar-hidden-scroll')
        return false
      }
      this.activeTab = type
      this.isShowRecordsPopup = true
    },
    setIsShowRecordsPopup(val) {
      this.isShowRecordsPopup = val
    },
    recordsAnimation() {
      setTimeout(() => {
        this.recordsLayouts.classList.add('sidebar-hidden-scroll')
        this.body.addEventListener('touchstart', () => {
          this.body.addEventListener('touchmove', this.recordsScrollOn)
          // this.recordsScrollOff()
        })
        this.body.addEventListener('touchend', () => {
          this.body.removeEventListener('touchmove', this.recordsScrollOn)
        })
      }, 500)
    },
    recordsScrollOn() {
      if (this.recordsLayouts && !this.recordsLayouts.classList.contains('sidebar-hidden-scroll')) {
        this.recordsLayouts.classList.add('sidebar-hidden-scroll')
      }
    },
    recordsScrollOff() {
      if (this.recordsLayouts && this.recordsLayouts.classList.contains('sidebar-hidden-scroll')) {
        setTimeout(() => {
          this.recordsLayouts.classList.remove('sidebar-hidden-scroll')
        }, 500)
      }
    }
  }
}
</script>

<style lang="less" scoped>
.records {
  &__layouts {
    position: fixed;
    z-index: 51;
    right: 10px;
    bottom: 90px;
    transition: all 0.25s cubic-bezier(0.55, 0, 0.1, 1);
  }
  &__entry {
    width: 47px;
    height: 47px;
    background-color: #fff;
    box-shadow: 0px 1px 3px 0px rgba(170, 80, 40, 0.35);
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    &--winners {
      margin-top: 18px;
    }
  }
  &__entry-wrap {
    > p {
      font-size: 13px;
      font-weight: 500;
      color: #804e26;
    }
  }
}
</style>
