<template>
  <view class="container">
    <view class="bk">
      <PageTitle color="#fff" title="全套赏详情" />
      <view class="head" style="margin-top: 36rpx">
        <image class="img" :src="detail.icon" mode="aspectFill"></image>
        <view class="info">
          <view class="title row">
            {{ detail.name }}
          </view>
          <view class="row">
            <view class="price">
              ￥{{ detail.price }}
              <span class="text">/张明信片</span>
            </view>
            <image
              @click="$navigateTo('/pages/rule/rule', { type: 3 })"
              class="button"
              src="https://fudaanfang.oss-cn-hangzhou.aliyuncs.com/festatic/bkm/first2/rule.png"
              mode="widthFix"
            ></image>
          </view>
          <view class="row">
            <view class="users">
              <image
                class="photo"
                :style="'left:' + index * 11 + 'px'"
                :src="'https://fudaanfang.oss-cn-hangzhou.aliyuncs.com/festatic/bkm/item.png'"
                v-for="(item, index) in userList"
                mode="widthFix"
              ></image>
            </view>
            <view class="hot">
              <image :src="'https://file.rongcyl.cn/festatic/bkm/imgv2' + '/first/hot.png'" mode="widthFix"></image>
              {{ detail.hotNum }}
            </view>
          </view>
        </view>
      </view>

      <view class="cardList">
        <image
          class="itemHead"
          :src="'https://file.rongcyl.cn/festatic/bkm/imgv2/drawCard_fullSet/headBk.png'"
          mode="widthFix"
        ></image>
        <view class="countdown textColor5">
          {{ countdown }}
        </view>
        <view class="title">
          <view class="left">
            <image
              class="text1"
              :src="'https://file.rongcyl.cn/festatic/bkm/imgv2/drawCard_fullSet/icon1.png'"
              mode="widthFix"
            ></image>
            <view class="row text2" style="font-size: 28rpx">已选择 {{ selectIndex.length }} 个</view>
          </view>
          <view class="right">
            <view class="text">
              分享福袋有几率提高
              <span style="color: #63fff6">S赏</span>
              概率
            </view>
            <!-- <image class="button" :src="'https://file.rongcyl.cn/festatic/bkm/imgv2/drawCard_fullSet/button1.png'" mode="widthFix"></image> -->
            <button open-type="share" class="share_btn">
              <image
                class="button"
                :src="'https://file.rongcyl.cn/festatic/bkm/imgv2/drawCard_fullSet/button1.png'"
                mode="widthFix"
              ></image>
            </button>
          </view>
        </view>
        <view class="list">
          <view
            :class="['item', item.status == 0 ? 'c1' : 'c2']"
            v-for="(item, index) in cardList"
            @click="clickCard(item)"
          >
            <numImg v-if="item.status == 0" class="num" :value="item" :type="3" />
            <img
              v-if="item.status == 2"
              src="https://file.rongcyl.cn/festatic/bkm/first2/gift_a.png"
              style="width: 60rpx; height: 72rpx"
            />
            <img
              v-if="item.status == 1"
              src="https://file.rongcyl.cn/festatic/bkm/first2/lock.png"
              style="width: 60rpx; height: 60rpx"
            />
            <image
              v-if="item.check"
              class="checkBk"
              src="https://file.rongcyl.cn/festatic/bkm/imgv2/first/cardCheck.png"
              mode="widthFix"
            ></image>
          </view>
        </view>
      </view>

      <!-- <view class="examples">
			  <view class="example">
				<image
				  class="exampleImg"
				  :src="'https://file.rongcyl.cn/festatic/bkm/imgv2/ka/kabk' + cardType(3, { status: 0 }) + '.png'"
				  mode="widthFix"
				></image>
				<image class="tip" :src="'https://file.rongcyl.cn/festatic/bkm/imgv2/ka/bkType1.png'" mode="widthFix"></image>
			  </view>
			  <view class="example">
				<image
				  class="exampleImg"
				  :src="'https://file.rongcyl.cn/festatic/bkm/imgv2/ka/kabk' + cardType(3, { status: 2 }) + '.png'"
				  mode="widthFix"
				></image>
				<image class="tip" :src="'https://file.rongcyl.cn/festatic/bkm/imgv2/ka/bkType5.png'" mode="widthFix"></image>
			  </view>
			  <view class="example">
				<image
				  class="exampleImg"
				  :src="'https://file.rongcyl.cn/festatic/bkm/imgv2/ka/kabk' + cardType(3, { status: 1 }) + '.png'"
				  mode="widthFix"
				></image>
				<image class="tip" :src="'https://file.rongcyl.cn/festatic/bkm/imgv2/ka/bkType4.png'" mode="widthFix"></image>
			  </view>
			</view> -->

      <view class="buttons">
        <!-- 	<image class="button1" @click="toKnapsack"
					:src="'https://file.rongcyl.cn/festatic/bkm/imgv2/drawCard_fullSet/button3.png'" mode="heightFix">
				</image> -->
        <!-- <image class="button1" :src="'https://file.rongcyl.cn/festatic/bkm/imgv2/drawCard_fullSet/button4.png'" mode="heightFix"></image> -->
        <!-- 	<image class="button1"
					@click="$navigateTo('/pages/drawCard_fullSet/goods', { raffleId: params.raffleId })"
					:src="'https://file.rongcyl.cn/festatic/bkm/imgv2/drawCard_fullSet/button5.png'" mode="heightFix">
				</image> -->
        <view class="button1" @click="$navigateTo('/pages/drawCard_fullSet/goods', { raffleId: params.raffleId })">
          奖品预览
        </view>
      </view>
      <view
        class="winningList"
        v-if="prizeUserList.length > 0"
        @click="$navigateTo('/pages/drawCard_fullSet/winningList', { raffleId: params.raffleId })"
      >
        <image
          class="winningListTitle"
          :src="'https://file.rongcyl.cn/festatic/bkm/imgv2/drawCard_fullSet/icon2.png'"
          mode="widthFix"
        ></image>
        <view class="list">
          <view class="item" v-for="(item, index) in prizeUserList.slice(0, 5)">
            <image class="photo" :src="item.userAvtar" mode="aspectFill"></image>
            <view class="name">
              {{ item.userName }}
            </view>
          </view>
          <view class="item" v-if="prizeUserList.length > 5">
            <image
              class="photo"
              :src="'https://file.rongcyl.cn/festatic/bkm/imgv2/drawCard_fullSet/icon3.png'"
              mode="aspectFill"
            ></image>
            <view class="name" style="color: #6478fd">查看更多</view>
          </view>
        </view>
      </view>
      <view class="prizes" style="display: none">
        <image
          class="title"
          :src="'https://file.rongcyl.cn/festatic/bkm/imgv2/first/title.png'"
          mode="widthFix"
        ></image>
        <view class="prizesList" v-for="(item, index) in prizeList">
          <image
            class="bk"
            :src="'https://file.rongcyl.cn/festatic/bkm/imgv2/first/bk2.png'"
            mode="scaleToFill"
          ></image>
          <view class="prizesTitle">
            {{ $selectDictLabel(rewardType, item.type) }}
          </view>
          <view class="prizesImgOne" v-if="item.type == 22">
            <view class="image" v-for="(item2, index2) in item.prizeList">
              <image :class="item2.leftNum == 0 ? 'grey' : ''" :src="item2.icon" mode="aspectFill"></image>
            </view>
          </view>
          <view class="prizesImg" v-else>
            <view class="image" v-for="(item2, index2) in item.prizeList">
              <image :class="item2.leftNum == 0 ? 'grey' : ''" :src="item2.icon" mode="aspectFill"></image>
            </view>
          </view>
        </view>
      </view>
      <div class="buy items-center">
        <div class="pica ml-2" style="margin-top: -38rpx">
          <img src="https://file.rongcyl.cn/festatic/bkm/first2/pica.png" class="w-full h-full" />
        </div>
        <div class="flex-1 text-white fs12 ml12 relative bottom-3 pl-3">
          <span class="inline-block">已选{{ selectIndex.length }}个 合计:</span>
          <span class="inline-block" style="color: #ea6636">
            <span class="bold">¥</span>
            <span class="fs18 bold">{{ (selectIndex.length * detail.price).toFixed(2) }}</span>
          </span>
        </div>
        <img
          @click="submit"
          src="https://file.rongcyl.cn/festatic/bkm/first2/bug.png"
          class="b_btn relative bottom-3 right-2"
        />
      </div>
    </view>
    <div class="fixed right-2 bottom-20" style="height: 460rpx">
      <!-- 		  <img
		    src="https://file.rongcyl.cn/festatic/bkm/first2/fu.png"
		    style="width: 88rpx; height: 88rpx; margin-bottom: 24rpx"
		  /> -->
      <img
        @click="toKnapsack"
        src="https://file.rongcyl.cn/festatic/bkm/first2/bag.png"
        style="width: 88rpx; height: 88rpx; margin-bottom: 24rpx"
      />
      <img
        @click="refresh"
        src="https://file.rongcyl.cn/festatic/bkm/first2/refresh.png"
        style="width: 88rpx; height: 88rpx; margin-bottom: 24rpx"
      />
      <button open-type="share" class="share_btn">
        <img
          src="https://file.rongcyl.cn/festatic/bkm/first2/share.png"
          style="width: 88rpx; height: 88rpx; margin-bottom: 24rpx"
        />
      </button>
    </div>
    <!-- <image class="refresh" @click="refresh"
			:src="'https://file.rongcyl.cn/festatic/bkm/imgv2/first/refresh.png'" mode="widthFix"></image> -->
    <paymentPopup ref="paymentPopup" @callBack="paymentSuccess" />
  </view>
</template>

<script>
import { rewardType } from '@/utils/commonConfig.js'
import { prizePoolStatusApi, raffleDetailApi, prizeListApi, prizeUserListApi } from '@/api/drawCard.js'

import { cardType, formatSeconds } from '@/utils/utils.js'

import numImg from '@/component/numImg.vue'
import paymentPopup from '@/component/paymentPopup.vue'
import PageTitle from '@/component/pageTitle.vue'

export default {
  components: {
    PageTitle,
    numImg,
    paymentPopup,
  },
  data() {
    return {
      safeAreaTop: '0rpx',
      countdown: '',
      countdownInfinity: null,
      cardType: cardType,
      rewardType: rewardType,
      params: {
        raffleId: '',
        limit: 20,
        page: 1,
      },
      detail: {},
      total: 0,
      userList: 8,
      cardList: [],
      selectIndex: [],
      prizeList: [],
      prizeUserList: [],
    }
  },
  onLoad(query) {
    console.log(query, 'query')
    if (query.id) {
      this.params.raffleId = query.id
      this.init()
    }
    this.paymentSuccess()
  },
  mounted() {
    this.getSafeAreaTop()
  },
  onHide() {
    if (!this.countdownInfinity) {
      clearInterval(this.countdownInfinity)
    }
  },
  methods: {
    init() {
      // 获取卡牌商品列表
      this.getCardList()
      // 获取卡包详情
      this.getDetail()
      //获取奖池
      this.getPrizeList()
      // 获取中奖记录
      this.getPrizeUserList()
    },
    getSafeAreaTop() {
      wx.getSystemInfo({
        success: res => {
          this.safeAreaTop = res.safeArea.top + 'px'
        },
      })
    },
    toKnapsack() {
      // 跳转背包界面
      wx.navigateTo({
        url: '/pages/knapsack/knapsack',
      })
    },
    refresh() {
      wx.showLoading()
      setTimeout(function () {
        wx.hideLoading()
      }, 1000)
      this.init()
      this.selectIndex = []
      for (var i = 0; i < this.cardList.length; i++) {
        this.cardList[i].check = false
      }
    },
    getPrizeUserList() {
      let params = {
        limit: 999,
        page: 1,
        raffleId: this.params.raffleId,
      }
      prizeUserListApi(params).then(res => {
        console.log(res, '中奖记录')
        let userIds = []
        let list = []
        for (var i = 0; i < res.data.length; i++) {
          let item = res.data[i]
          if (userIds.indexOf(item.userId) == -1) {
            userIds.push(item.userId)
            console.log(userIds.indexOf(item.userId))
            let index = userIds.indexOf(item.userId)
            list[index] = {
              userId: item.userId,
              userAvtar: item.userAvtar,
              userName: item.userName,
              prizes: [],
            }
            list[index].prizes.push(item)
          } else {
            let index = userIds.indexOf(item.userId)
            list[index].prizes.push(item)
          }
        }
        this.userIds = userIds
        this.prizeUserList = list
      })
    },
    getPrizeList() {
      let params = {
        raffleId: this.params.raffleId,
      }
      prizeListApi(params).then(res => {
        console.log(res, '奖池预览')
        let newArray = res.data
        for (var i = 0; i < res.data.length; i++) {
          if (res.data[i].type == 21) {
            newArray = []
            newArray.push(res.data[i])
            res.data.splice(i, 1)
            newArray.push(...res.data)
            break
          }
        }
        this.prizeList = newArray
      })
    },
    toWinningRecord() {
      let params = {
        raffleId: this.params.raffleId,
      }
      this.$navigateTo('/pages/winningRecord/winningRecord', params)
    },
    getDetail() {
      let params = {
        raffleId: this.params.raffleId,
      }
      raffleDetailApi(params).then(res => {
        console.log(res, '详情')
        this.detail = res.data
        this.endTime = new Date(this.detail.endTime).getTime()
        // this.endTime = new Date('2023-11-10 14:48:00').getTime()
        // 初始化倒计时
        this.initCountdown()
        if (this.countdown != '已结束') {
          this.countdownInfinity = setInterval(() => {
            if (this.countdown == '已结束') {
              clearInterval(this.countdownInfinity)
              this.countdownInfinity = null
              return
            }
            this.initCountdown()
          }, 1000)
        }
      })
    },
    initCountdown() {
      let nowTime = new Date().getTime()
      let endTime = this.endTime
      let countdownText = ''
      let countdown = endTime - nowTime
      if (countdown <= 0) {
        countdownText = '已结束'
      } else {
        countdownText = formatSeconds(countdown)
      }
      // console.log(countdownText, 'countdownText')
      this.countdown = countdownText
    },
    submit() {
      if (this.selectIndex.length == 0) {
        wx.showToast({
          title: '请选则要购买的卡牌。',
          icon: 'none',
        })
        return
      }
      let params = {
        raffleId: this.params.raffleId,
      }
      this.$refs.paymentPopup.show(params, this.selectIndex, this.detail)
    },
    // 支付成功后回调
    paymentSuccess() {
      let _this = this
      console.log('刷新抽卡界面')
      // 刷新界面
      _this.refresh()
    },
    // 选中取消选中卡牌
    clickCard(item) {
      if (item.status == 0) {
        item.check = !item.check
      }
      // 如果是选中
      let index = this.selectIndex.indexOf(item.number)
      if (item.check && index == -1) {
        this.selectIndex.push(item.number)
      } else if (!item.check && index != -1) {
        this.selectIndex.splice(index, 1)
      }
    },
    // 标记已经选中卡牌
    selectFormat() {
      for (var i = 0; i < this.cardList.length; i++) {
        if (this.selectIndex.includes(this.cardList[i].number)) {
          this.cardList[i].check = true
        }
      }
    },
    getCardList() {
      prizePoolStatusApi(this.params).then(res => {
        console.log(res, '卡牌分页')
        for (var i = 0; i < res.data.list.length; i++) {
          res.data.list[i].check = false
        }
        this.cardList = res.data.list
        this.selectFormat()
        this.total = res.data.total
      })
    },
    left() {
      console.log('上一页')
      if (this.params.page > 1) {
        this.params.page = this.params.page - 1
      }
      this.getCardList()
    },
    right() {
      console.log('下一页')
      let pages = Math.ceil(this.total / this.params.limit)
      if (this.params.page < pages) {
        this.params.page = this.params.page + 1
      }
      this.getCardList()
    },
    back() {
      wx.navigateBack({ delta: 1 })
    },
  },
}
</script>

<style scoped lang="scss">
.share_btn {
  background-color: transparent;
  border-width: 0 !important;
  border-radius: 0;
  overflow: initial;
  text-align: start;
  padding: 0;
  box-shadow: none !important;
  -webkit-tap-highlight-color: transparent;

  &::after {
    border: none;
  }
}

.container {
  // background: #9ec8f9;
  background: linear-gradient(216deg, #fe854c 0%, #ff9a5a 100%);

  .bk {
    overflow: auto;
    padding-top: 30rpx;
    background-image: url(https://file.rongcyl.cn/festatic/bkm/first2/bg.png);
    height: calc(100vh - 160rpx);
    background-repeat: no-repeat;
    background-position: center center;
    background-size: 100% 100%;
    box-sizing: border-box;
    overflow: scroll;
    padding-bottom: 160rpx;

    .buy {
      position: fixed;
      left: 0;
      right: 0;
      bottom: 0;
      width: 750rpx;
      height: 160rpx;
      background: #000000;
      opacity: 1;
      display: flex;

      .pica {
        width: 88rpx;
        height: 88rpx;
      }

      .b_btn {
        width: 232rpx;
        height: 76rpx;
      }
    }
  }

  .refresh {
    position: fixed;
    right: 8rpx;
    top: 75%;
    width: 80rpx;
    z-index: 5;
  }

  .head {
    background: #fff;
    border: 20rpx;
    padding: 30rpx;
    border-radius: 20rpx 20rpx 0 0;
    display: flex;
    justify-content: space-between;
    margin-bottom: 40rpx;

    .img {
      width: 212rpx;
      height: 212rpx;
      border-radius: 10rpx;
      overflow: hidden;
      margin-right: 20rpx;
    }

    .info {
      display: flex;
      flex-direction: column;
      justify-content: start;
      flex: 1;

      .row {
        margin: 10rpx 0;
        display: flex;
        justify-content: space-between;
        color: #999999;

        .button {
          width: 134rpx;
        }

        .hot {
          display: flex;
          font-size: 24rpx;
          color: #999999;

          image {
            width: 32rpx;
          }
        }

        .users {
          position: relative;

          .photo {
            position: absolute;
            width: 40rpx;
            height: 40rpx;
            border-radius: 50%;
          }
        }
      }

      .title {
        font-weight: 600;
        color: #000;
      }

      .price {
        font-size: 32rpx;
        font-weight: 600;
        color: #ff2c43;

        .text {
          font-size: 28rpx;
          color: #999999;
          font-weight: 500;
        }
      }
    }
  }

  .cardList {
    margin: 20rpx;
    border-radius: 24rpx;
    position: relative;
    // background: linear-gradient(180deg, #31b7fd 10%, #6478fd 90%);
    background: #ff6f01;
    // overflow: scroll;
    padding-bottom: 2rpx;

    .countdown {
      position: absolute;
      top: 2rpx;
      padding-left: 140rpx;
      font-size: 30rpx;
    }

    .itemHead {
      position: relative;
      left: -20rpx;
      top: -20rpx;
      width: calc(100% + 20rpx);
    }

    .title {
      display: flex;
      justify-content: space-between;
      color: #fff;
      padding: 0 30rpx 12rpx 30rpx;

      .left {
        .text1 {
          width: 6.5rem;
          // font-size: 48rpx;
          // font-weight: 600;
          // text-shadow: 2rpx 2rpx 0rpx #0A57BA;
          // font-family: YouSheBiaoTiHei-Regular, YouSheBiaoTiHei;
        }

        .text2 {
          font-family:
            Alimama FangYuanTi VF-Medium-Round,
            Alimama FangYuanTi VF-Medium;
        }
      }

      .right {
        display: flex;

        .text {
          width: 200rpx;
          font-size: 24rpx;
          text-align: right;
        }

        .button {
          width: 144rpx;
          height: 30rpx;
        }
      }
    }

    .bk {
      width: 100%;
    }

    .list {
      background: #fff;
      padding: 30rpx;
      border-radius: 24rpx;
      // width: calc(100% - 160rpx);
      // height: calc(100% - 140rpx);
      // position: absolute;
      // top: 0;
      display: grid;
      grid-template-columns: 1fr 1fr 1fr 1fr 1fr;
      grid-template-rows: 1fr 1fr 1fr;
      grid-gap: 30rpx;

      .c1 {
        background-image: url(https://file.rongcyl.cn/festatic/bkm/imgv2/ka/kabk3.png);
        // width: 96rpx;
        height: 136rpx;
        background-repeat: no-repeat;
        background-position: center center;
        background-size: contain;
        margin-bottom: 14rpx;
        position: relative;

        .checkBk {
          position: absolute;
          width: 105%;
        }
      }

      .c2 {
        background-image: url(https://file.rongcyl.cn/festatic/bkm/imgv2/ka/kabk4.png);
        // width: 96rpx;
        height: 136rpx;
        background-repeat: no-repeat;
        background-position: center center;
        background-size: contain;
        margin-bottom: 14rpx;
      }

      .item {
        display: inline-flex;
        justify-content: center;
        align-items: center;
        position: relative;

        .kaBkActive {
          height: 142%;
          width: 100%;
          position: absolute;
          z-index: 1;
        }

        .kaBk {
          width: 100%;
          border-radius: 8rpx;
          z-index: 2;
        }

        .num {
          // position: absolute;
          width: 100%;
          height: 100%;
          // top: 0;
          // z-index: 2;
        }
      }
    }

    .buyButton {
      width: 40%;
      margin: 20rpx auto;
    }
  }

  .winningList {
    margin: 40rpx 20rpx;
    padding: 30rpx;
    background: linear-gradient(180deg, #ff6f01 10%, #ff5a01 90%);
    border-radius: 24rpx;

    .winningListTitle {
      width: 184rpx;
      margin-bottom: 24rpx;
    }

    .list {
      position: relative;
      padding: 24rpx 14rpx 24rpx 24rpx;
      border-radius: 24rpx;
      background: #fff;
      display: flex;

      .item {
        width: calc(100% / 6 - 10rpx);
        // margin: 0 auto;
        position: relative;
        display: inline-flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        margin-right: 10rpx;

        .photo {
          width: 80rpx;
          height: 80rpx;
          border-radius: 50%;
          background: #b6b6b6;
        }

        .name {
          margin-top: 10rpx;
          display: inline-block;
          font-size: 22rpx;
          max-width: 100%;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
        }
      }
    }
  }

  .buttons {
    display: flex;
    justify-content: center;
    align-items: flex-start;
    padding: 0 20rpx;

    .button1 {
      height: 70rpx;
      width: 140rpx;
      line-height: 70rpx;
      color: #fff;
      background: linear-gradient(180deg, #fef104 10%, #ff7a33 90%);
      text-align: center;
      border-radius: 35rpx;
      padding: 0 20rpx;
      font-weight: 600;
      font-size: 32rpx;
    }
  }

  .prizes {
    .title {
      width: 206rpx;
      margin: 0 auto;
    }

    .prizesList {
      margin: 40rpx;
      position: relative;
      overflow: hidden;
      // z-index: 5;

      .bk {
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
      }

      .prizesTitle {
        position: relative;
        height: 60rpx;
        line-height: 60rpx;
        width: 100%;
        margin-left: 2rpx;
        margin-top: 30rpx;
        padding-left: 30rpx;
        color: #fff;
        font-weight: 800;
        background: linear-gradient(90deg, rgba(89, 166, 255, 0.8) 0%, rgba(158, 200, 249, 0) 100%);
      }

      .prizesImg {
        position: relative;
        display: grid;
        grid-template-columns: 1fr 1fr;
        margin: 30rpx;
        white-space: wrap;
        text-align: center;
        // overflow: auto;

        .image {
          text-align: center;

          image {
            display: inline-block;
            width: 36vw;
            height: 36vw;
            border-radius: 20rpx;
            margin: 0 10rpx;
          }
        }
      }

      .prizesImgOne {
        position: relative;
        display: flex;
        justify-content: center;
        margin: 30rpx;

        .image {
          text-align: center;

          image {
            display: inline-block;
            width: 45vw;
            height: 45vw;
            border-radius: 20rpx;
            margin: 0 10rpx;
          }
        }
      }
    }
  }
}

.examples {
  width: 100%;
  margin-bottom: 40rpx;
  padding: 0 20rpx;
  display: flex;

  .example {
    width: 14vw;
    margin-right: 20rpx;
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
    position: relative;

    .exampleImg {
      width: 13vw;
    }

    .tip {
      width: 100%;
      position: absolute;
      bottom: 0;
      transform: translateY(50%);
    }
  }
}
</style>
