<template>
  <section class="lottery-instant">
    <div class="lottery-instant__main">
      <img class="lottery-instant__title" src="@/assets/images/lottery/guess/game-title.png" />
      <CountdownBar class="lottery-instant__countdown"></CountdownBar>
      <div class="lottery-instant__layout-guess">
        <div class="content-img">
          <div v-for="(item, index) in starConfig" :key="index" class="content-img--star" :style="item">
            <img src="@/assets/images/lottery/guess/star.png" />
          </div>
          <img src="@/assets/images/lottery/guess/gift-main.png" />
        </div>
        <div class="content-main">
          <div class="content-main-times">
            抽奖次数：<span>{{ lotteryInfo.userTimes }}</span
            >次
          </div>
          <div class="content-main-box">
            <PrizeGuess :blind-box-list="blindBoxList" @startLottery="startLottery">
              <template #choose="{ index }">
                <transition name="choose-fade">
                  <div v-show="chooseIndex === index" class="choose">选我</div>
                </transition>
              </template>
            </PrizeGuess>
          </div>
        </div>
      </div>
    </div>
    <div class="lottery-instant__intro-entry" @click="changeIsShowIntro">活动介绍</div>
    <WinPopup :value="isShowWin" :info="winInfo">
      <template #title-bg>
        <div class="slot-title-bg">
          <img src="@/assets/images/lottery/win-new/title-guess.png" alt="" />
        </div>
      </template>
      <template #thanks>
        <div class="slot-thanks">
          <p class="slot-thanks-p1">很遗憾</p>
          <p class="slot-thanks-p2">你没有猜中</p>
        </div>
      </template>
    </WinPopup>
    <BindPhoneDialog :value="isShowBindPhone"></BindPhoneDialog>
    <Records></Records>
    <IntroductionPopup :value="isShowIntro" rule-position="bottom"></IntroductionPopup>
  </section>
</template>

<script>
import { PrizeGuess } from 'lotteries'
import { mapGetters } from 'vuex'
import Bus from '@/utils/Bus'
import { blindBoxListGuess, starConfig } from './config'

export default {
  components: {
    CountdownBar: () => import('@/components/Lottery/Instant/CountdownBar'),
    PrizeGuess,
    WinPopup: () => import('@/components/Lottery/Instant/WinPopup'),
    BindPhoneDialog: () => import('@/components/Common/BindPhoneDialog'),
    Records: () => import('@/components/Lottery/Instant/Records'),
    IntroductionPopup: () => import('@/components/Lottery/Instant/IntroductionPopup')
  },
  props: {
    winInfo: {
      type: Object,
      default: () => ({})
    },
    isShowWin: {
      type: Boolean,
      default: false
    },
    isShowBindPhone: {
      type: Boolean,
      default: false
    },
    isLottering: {
      type: Boolean,
      default: false
    },
    isLoading: {
      type: Boolean,
      default: false
    },
    isShowIntro: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      id: this.$route.query.id || null, // 抽奖id
      playId: this.$route.query.sessionId || null, // 场次id
      sid: this.$route.query.sid || null, // 引用id
      stype: this.$route.query.stype || null, // 引用类型
      chooseIndex: 0,
      timer: null,
      blindBoxList: blindBoxListGuess,
      starConfig
    }
  },
  computed: {
    ...mapGetters({
      uin: 'users/uin',
      isLogin: 'users/isLogin',
      lotteryInfo: 'lottery/lotteryInfo',
      isBindPhone: 'users/isBindPhone'
    })
  },
  mounted() {
    this.startChooseScroll()
    Bus.$emit('initDeal', '#6f19d1')
  },
  beforeDestroy() {
    this.endChooseScroll()
  },
  methods: {
    startChooseScroll() {
      this.chooseIndex = 0
      this.timer = setInterval(() => {
        const indexTemp = Math.floor(Math.random() * (3 - 0 + 1)) + 0
        if (indexTemp === this.chooseIndex && this.chooseIndex === 3) {
          this.chooseIndex = 0
        } else if (indexTemp === this.chooseIndex && this.chooseIndex !== 3) {
          this.chooseIndex = indexTemp + 1
        } else {
          this.chooseIndex = indexTemp
        }
      }, 1000)
    },
    endChooseScroll() {
      clearInterval(this.timer)
    },
    startLottery() {
      Bus.$emit('updateMain', { func: 'startLottery' })
    },
    drawAnimation() {
      setTimeout(() => {
        Bus.$emit('updateMain', { func: 'runEnd' })
      }, 0)
    },
    changeIsShowIntro() {
      Bus.$emit('updateMain', { isShowIntro: true })
    }
  }
}
</script>

<style lang="less" scoped>
.lottery-instant {
  position: relative;
  height: 100%;
  background-color: #631e88 !important;
  background-image: url('@/assets/images/lottery/guess/page-bg-mini.png') !important;
  background-size: cover;
  background-position: top center;
  background-repeat: no-repeat;
  padding: 0 15px 22px;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  &__main {
    text-align: center;
  }
  &__title {
    width: 66.7%;
    object-fit: cover;
    margin-top: 28px;
  }
  &__countdown {
    padding-top: 6px;
  }
  &__layout-guess {
    .content-img {
      position: relative;
      width: 152px;
      height: 154px;
      margin: 10px auto 0 auto;
      img {
        width: 100%;
      }
      &--star {
        position: absolute;
      }
    }
    .content-main {
      position: relative;
      background-image: url('@/assets/images/lottery/guess/game-bg.png');
      background-size: 100% 100%;
      background-position: center;
      width: 100%;
      height: 0;
      padding-top: 156 / 367 * 100%;
      margin-top: 16px;
      &-box {
        position: absolute;
        bottom: 20%;
        left: 50%;
        transform: translateX(-50%);
        width: 90%;
        height: 38%;
        z-index: 20;
        .choose {
          position: absolute;
          top: -40%;
          left: 50%;
          transform: translateX(-50%);
          font-size: 12px;
          background-color: #ffffffcc;
          padding: 2px 6px;
          white-space: nowrap;
          border-radius: 9px;
          z-index: -1;
          &::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 50%;
            transform: translate(-50%, 100%);
            width: 0;
            height: 0;
            border-top: 4px solid #ffffffcc;
            border-bottom: 4px solid transparent;
            border-left: 4px solid transparent;
            border-right: 4px solid transparent;
          }
        }
      }
      &-times {
        position: absolute;
        top: 20%;
        left: 50%;
        transform: translateX(-50%);
        font-size: 12px;
        color: #fff;
        z-index: 10;
      }
    }
  }
  &__intro-entry {
    position: fixed;
    top: 56px;
    right: 0;
    background: linear-gradient(180deg, #ffd63e 0%, #fca60d 100%);
    box-shadow: 0px 1px 2px 0px rgba(202, 146, 46, 1);
    color: #9c5c00;
    font-size: 12px;
    writing-mode: vertical-lr;
    padding: 6px 3px;
    border-radius: 10px 0 0 10px;
  }
}

.slot-thanks {
  font-size: 14px;
}

.choose-fade-enter-active {
  transition: all 0.1s ease-in;
}
.choose-fade-leave-active {
  transition: all 0.1s ease-out;
}
.choose-fade-enter,
.choose-fade-leave-to {
  opacity: 0;
  top: 0 !important;
}

// 样式穿透
.lottery-instant {
  // 介绍
  /deep/ .introduction-popup {
    background: url('@/assets/images/lottery/guess/intro-bg-mini.png');
    &__wrap {
      background-color: transparent;
      padding: 2px;
    }
    &__content {
      background-color: transparent;
      color: #fff;
      padding: 0;
    }
    &__box {
      background-color: #00000030;
      padding: 1px 15px 15px 15px;
      border-radius: 22px;
    }
    &__back {
      top: 10px;
      right: 0;
      background: linear-gradient(180deg, #ffd63e 0%, #fca60d 100%);
      box-shadow: 0 1px 2px 0 rgba(202, 146, 46, 1);
      border-radius: 10px 0 0 10px;
      padding: 6px 3px;
      border: 0;
    }
    &__intro {
      color: #fff;
    }
  }

  /deep/ .win-popup {
    width: 263px;
    height: 309px;
    &__container {
      width: 100%;
    }
    &__wrap {
      position: relative;
      width: 100%;
      height: 100%;
      background-image: url('@/assets/images/lottery/records-new/bg-purple.png');
    }
    &__sure {
      width: 157px;
      height: 49px;
      line-height: 49px;
      border-radius: 20px;
      background: linear-gradient(#fff0cd, #fdbf2c) !important;
      .van-button__text {
        font-size: 16px;
        color: #860300;
        font-weight: 600;
        text-shadow: 1px 0 0 #fff, 0 1px 0 #fff, -1px 0 0 #fff, 0 -1px 0 #fff;
      }
    }
    &__title {
      font-size: 14px;
    }
    &__win-info {
      width: 80%;
      margin: 0 auto;
      background-color: #0000001a;
      border-radius: 20px;
      margin-top: 15px;
    }
    &__footer {
      width: 100%;
      min-height: 80px;
      justify-content: flex-start;
    }
    &__prize-img {
      width: 65px;
      height: 65px;
      background-image: url('@/assets/images/lottery/win-new/prize-bg-purple.png');
      background-size: 100% 100%;
      display: flex;
      justify-content: center;
      align-items: center;
      img {
        width: 60%;
      }
    }
    &__prize-name {
      margin-bottom: 11px;
      font-size: 12px;
    }

    .slot-title-bg {
      text-align: center;
      margin-top: 36px;
      height: 27px;
      img {
        height: 100%;
      }
    }
    .slot-thanks {
      text-align: center;
      &-p1 {
        font-size: 18px;
        margin-bottom: 10px;
      }
    }
    .van-popup__close-icon {
      right: -20px;
      top: -10px;
      width: 25px;
      height: 25px;
      font-size: 0;
      background-image: url('@/assets/images/lottery/win-new/close-purple.png');
      background-size: 100% 100%;
    }
  }

  // countdown
  /deep/ .countdown-bar__status {
    min-width: 160px;
    padding: 0 9px;
    border: none;
    height: 22.5px !important;
    background-color: #12121233;
  }

  // records
  /deep/ .records {
    &__entry-wrap p {
      color: #6f19d1;
    }
    &-popup {
      width: 258px;
      height: 320px;
      background-color: transparent;
      &__tabs {
        padding-top: 0;
      }
      &__personal-record,
      &__winners-record {
        padding: 0 30px;
      }
      &__phone,
      &__prize-name,
      &__time,
      &__session,
      &__item {
        color: #fff;
      }
    }
    .van-tabs {
      &__wrap {
        height: 29px;
      }
      &__line {
        display: none;
      }
      &__nav--line {
        width: 180px;
        padding-bottom: 0;
      }
      &__content {
        background-image: url('@/assets/images/lottery/records-new/bg-purple.png');
        background-size: 100% 100%;
      }
    }
    .van-tab {
      width: 60px;
      background: linear-gradient(#fcf9fc, #efd6ed);
      border-radius: 12px 12px 0 0;
      margin: 0 3px;
      span {
        color: #b46ed0;
        font-weight: 400;
      }
      &--active {
        background: linear-gradient(#fffdfe, #ffd5fd);
        span {
          color: #752ec8 !important;
        }
      }
    }
    .van-popup__close-icon {
      display: none;
    }
    .van-tabs__content .van-tab__pane {
      padding: 20px 0;
    }
    .records-popup__winners-record .records-popup__prize-name {
      color: #d92e32;
    }
  }
}
</style>
