<template>
  <section class="lottery-instant">
    <div class="lottery-instant__main">
      <Banner class="lottery-instant__banner"></Banner>
      <CountdownBar class="lottery-instant__countdown"></CountdownBar>
      <div class="lottery-instant__layout-scratch">
        <div class="lottery-instant__content-scratch">
          <img class="content-title" src="@/assets/images/lottery/scratch/game-title.png" alt="" />
          <div class="content-main">
            <PrizeScratch ref="prizeScratchRef" :cover-img="coverImg" @endCallBack="endCallBack">
              <template #item>
                <div class="win__wrap">
                  <div class="win__content">
                    <div v-if="isWin" class="win__content-win">
                      <div class="win__content-win-title">恭喜获得</div>
                      <div class="win__content-win-img">
                        <img src="@/assets/images/lottery/gift.png" alt="" />
                      </div>
                      <div class="win__content-win-name">
                        {{ winInfo.name }}
                      </div>
                    </div>
                    <div v-else class="win__content-thanks">谢谢参与，再接再厉</div>
                  </div>
                </div>
              </template>
              <template #button>
                <div v-show="!isLottering" class="lottery-instant__button-wrap">
                  <div class="lottery-instant__button-mask">
                    <div v-show="firstTimeLottering" class="lottery-instant__button-img">
                      <img :src="buttonImg" @click="startLottery" />
                      <div class="times">
                        今日次数<span>{{ lotteryInfo.userTimes }}</span
                        >次
                      </div>
                    </div>
                    <div v-show="!firstTimeLottering" class="lottery-instant__button-continue">
                      <span @click="continueScratch"
                        >今日次数<span>{{ lotteryInfo.userTimes }}</span
                        >次，继续刮奖</span
                      >
                    </div>
                  </div>
                </div>
              </template>
            </PrizeScratch>
          </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-scratch.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>
    <Records></Records>
    <IntroductionPopup :value="isShowIntro" rule-position="bottom"></IntroductionPopup>
  </section>
</template>

<script>
import { mapGetters, mapActions } from 'vuex'
import { PrizeScratch } from 'lotteries'
import { LOTTERY_STATUS } from '@/utils/constant'
import coverImg from '@/assets/images/lottery/scratch/mask.png'
import onStartImg from '@/assets/images/lottery/scratch/on-start.png'
import onStartDidabledImg from '@/assets/images/lottery/scratch/on-start-disabled.png'
import Bus from '@/utils/Bus'

export default {
  components: {
    Banner: () => import('@/components/Lottery/Instant/Banner'),
    WinPopup: () => import('@/components/Lottery/Instant/WinPopup'),
    CountdownBar: () => import('@/components/Lottery/Instant/CountdownBar'),
    Records: () => import('@/components/Lottery/Instant/Records'),
    IntroductionPopup: () => import('@/components/Lottery/Instant/IntroductionPopup'),
    PrizeScratch
  },
  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 {
      coverImg,
      firstTimeLottering: true,
      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, // 引用类型
    }
  },
  computed: {
    ...mapGetters({
      uin: 'users/uin',
      isLogin: 'users/isLogin',
      lotteryInfo: 'lottery/lotteryInfo',
      isBindPhone: 'users/isBindPhone'
    }),
    isNotStart() {
      // 抽奖不在活动日期内
      const { status } = this.lotteryInfo
      return Number(status) !== LOTTERY_STATUS.start
    },
    // 按钮图片样式
    buttonImg() {
      if (!this.lotteryInfo.userTimes || this.isNotStart) {
        return onStartDidabledImg
      }
      return onStartImg
    },
    isWin() {
      return !!Number(this.winInfo.id)
    }
  },
  mounted() {
    Bus.$emit('initDeal', '#093c85')
  },
  methods: {
    ...mapActions({ updateInfo: 'lottery/updateInfo' }),
    // 开始抽奖
    startLottery() {
      Bus.$emit('updateMain', { func: 'startLottery' })
    },
    drawAnimation() {
      //
    },
    // 刮奖结束回调
    endCallBack() {
      this.$toast({
        message: '刮奖结束',
        duration: 1500
      })
      Bus.$emit('updateMain', { isLottering: false })
      setTimeout(() => {
        Bus.$emit('updateMain', { isShowWin: true })
      }, 400)
      if (this.lotteryInfo.userTimes === 0) {
        this.$refs.prizeScratchRef.tryAgain()
        this.firstTimeLottering = true
      } else {
        this.firstTimeLottering = false
      }
    },
    // 继续刮奖
    continueScratch() {
      this.$refs.prizeScratchRef.tryAgain()
      this.startLottery()
    },
    changeIsShowIntro() {
      Bus.$emit('updateMain', { isShowIntro: true })
    }
  }
}
</script>

<style lang="less" scoped>
.lottery-instant {
  position: relative;
  height: 100%;
  background-color: #83d4fd !important;
  background-image: none !important;
  background-size: cover;
  background-position: top center;
  background-repeat: no-repeat;
  padding: 0;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  &__countdown {
    padding-top: 18px;
  }
  &__main {
    padding: 0 0.13rem;
  }
  &__banner {
    margin-top: 10px;
    padding: 0 15px;
  }
  &__layout-scratch {
    position: relative;
    background-image: url('~@/assets/images/lottery/scratch/game-bg.png');
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    width: 100%;
    height: 0;
    padding-top: 363 / 355 * 100%;
    margin-top: 7px;
  }
  &__content-scratch {
    position: absolute;
    left: 0;
    top: 0;
    padding: 90px 20px 30px 20px;
    width: 100%;
    height: 100%;
    font-size: 20px;
    text-align: center;

    .content-title {
      width: 74%;
      object-fit: cover;
      margin: 10px 0;
    }
    .content-prize {
      font-size: 12px;
    }
  }

  &__button {
    &-wrap {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      z-index: 40;
    }
    &-mask {
      width: 100%;
      height: 100%;
    }
    &-img {
      width: 100%;
      height: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
      img {
        width: 40%;
        object-fit: cover;
      }
      .times {
        position: absolute;
        top: 70%;
        left: 50%;
        transform: translateX(-50%);
        font-size: 12px;
        color: #666;
      }
    }
    &-continue {
      position: absolute;
      bottom: 6%;
      left: 50%;
      transform: translateX(-50%);
      font-size: 12px;
      color: #666;
    }
  }
  .win {
    &__wrap {
      width: 100%;
      height: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    &__content {
      width: 90%;
      height: 90%;
      display: flex;
      flex-direction: column;
      justify-content: center;
      background-color: #fff;

      &-win {
        &-title {
          font-size: 16px;
          margin-bottom: 3px;
          color: #333;
        }
        &-img {
          margin-bottom: 3px;
          img {
            width: 60px;
          }
        }
        &-name {
          font-size: 12px;
          color: #666;
        }
      }

      &-thanks {
        font-size: 16px;
      }
    }
  }
  &__times {
    position: absolute;
    top: 0;
    left: 0;
    font-size: 13px;
    padding: 25px 15px;
  }
  &__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;
  }
}

// 样式穿透
.lottery-instant {
  // 介绍
  /deep/ .introduction-popup {
    background: linear-gradient(180deg, #60bdff 0%, #aaeefb 100%);
    &__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: #000;
    }
  }

  // 开奖
  /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-blue.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-blue.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-blue.png');
      background-size: 100% 100%;
    }
  }
  // countdown
  /deep/ .countdown-bar__status {
    min-width: 160px;
    padding: 0 9px;
    border: none;
    height: 22.5px !important;
    background-color: #2452bb99;
  }

  // records
  /deep/ .records {
    &__entry-wrap p {
      color: #093c85;
    }
    &-popup {
      width: 258px;
      height: 320px;
      background-color: transparent;
      &__tabs {
        padding-top: 0;
      }
      &__personal-record,
      &__winners-record {
        padding: 0 30px;
      }
      &__phone,
      &__prize-name,
      &__time {
        color: #333;
      }
    }
    .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-blue.png');
        background-size: 100% 100%;
      }
    }
    .van-tab {
      width: 60px;
      background: linear-gradient(#f6fbff, #a8d9fb);
      border-radius: 12px 12px 0 0;
      margin: 0 3px;
      span {
        color: #90b4dd;
        font-weight: 400;
      }
      &--active {
        background: linear-gradient(#feffff, #dff0ff);
        span {
          color: #4787d1 !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>
