<template style="background-color: red">
  <div
    style="
      height: 100vh;

      background: linear-gradient(216deg, #fe854c 0%, #ff9a5a 100%);
      box-sizing: border-box;
      overflow: scroll;
    "
  >
    <div
      class="first2"
      :style="{
        background: '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%',
        'padding-top': safeAreaTop,
        'box-sizing': 'border-box',
        overflow: 'scroll',
        'padding-bottom': '160rpx',
      }"
    >
      <div class="text-white bold text-center mt-2">FIRST赏详情</div>
      <div class="card relative">
        <div class="flex pt-3 px-3">
          <img
            src="https://fudaanfang.oss-cn-hangzhou.aliyuncs.com/festatic/bkm/card.png"
            style="width: 168rpx; height: 169rpx; border-radius: 8px"
          />
          <div class="flex-1 pl-2">
            <div class="" style="font-weight: 900">{{ detail.name }}</div>
            <div class="flex justify-between">
              <div>
                <span class="bold" style="color: #ff2c43; font-size: 24rpx">￥</span>
                <span class="bold" style="color: #ff2c43; font-size: 38rpx">
                  {{ ('' + detail.price).split('.')[0] }}
                </span>
                <span class="bold" style="color: #ff2c43; font-size: 28rpx">
                  .{{ ('' + detail.price).split('.')[1] }}
                </span>
                <span style="color: #4e504f; font-size: 14px">/张明信片</span>
              </div>
              <div class="pt-2">
                <img
                  @click="$navigateTo('/pages/rule/rule', { type: 5 })"
                  src="https://fudaanfang.oss-cn-hangzhou.aliyuncs.com/festatic/bkm/first2/rule.png"
                  style="width: 134rpx; height: 48rpx"
                />
              </div>
            </div>
            <div class="flex justify-between" style="margin-top: 5px">
              <div>
                <img
                  src="https://fudaanfang.oss-cn-hangzhou.aliyuncs.com/festatic/bkm/item.png"
                  style="width: 40rpx; height: 40rpx; border-radius: 50%; display: inline-block; border: 1px solid #fff"
                />
                <img
                  src="https://fudaanfang.oss-cn-hangzhou.aliyuncs.com/festatic/bkm/item.png"
                  class="relative right-2"
                  style="width: 40rpx; height: 40rpx; border-radius: 50%; display: inline-block; border: 1px solid #fff"
                />
                <img
                  src="https://fudaanfang.oss-cn-hangzhou.aliyuncs.com/festatic/bkm/item.png"
                  class="relative right-4"
                  style="width: 40rpx; height: 40rpx; border-radius: 50%; display: inline-block; border: 1px solid #fff"
                />
                <img
                  src="https://fudaanfang.oss-cn-hangzhou.aliyuncs.com/festatic/bkm/item.png"
                  class="relative right-6"
                  style="width: 40rpx; height: 40rpx; border-radius: 50%; display: inline-block; border: 1px solid #fff"
                />
                <img
                  src="https://fudaanfang.oss-cn-hangzhou.aliyuncs.com/festatic/bkm/item.png"
                  class="relative right-8"
                  style="width: 40rpx; height: 40rpx; border-radius: 50%; display: inline-block; border: 1px solid #fff"
                />
              </div>
              <div class="flex items-center">
                <img
                  src="https://fudaanfang.oss-cn-hangzhou.aliyuncs.com/festatic/bkm/first2/hot.png"
                  class="relative right-2"
                  style="width: 32rpx; height: 32rpx; display: inline-block"
                />
                <span style="color: #fc5b20" class="fs11">{{ detail.hotNum }}</span>
              </div>
            </div>
          </div>
        </div>
        <div
          class="absolute left-0 right-0 bottom-6 mx-auto"
          style="
            width: 670rpx;
            height: 128rpx;
            background: url(https://fudaanfang.oss-cn-hangzhou.aliyuncs.com/festatic/bkm/first2/notice_c.png) no-repeat
              center center;
            background-size: cover;
          "
        >
          <img
            src="https://fudaanfang.oss-cn-hangzhou.aliyuncs.com/festatic/bkm/first2/notice.png"
            class="absolute top-2 left-2"
            style="width: 106rpx; height: 28rpx"
          />
          <div class="text-white fs12 mt-6 px-2">
            购买须知购买须知购买须知购买须知购买须知购买须知购买须知购买须知购买须知购买须知购买须知购买须知。
          </div>
        </div>
        <div
          class="absolute left-0 right-0 bottom-1 mx-auto"
          style="
            width: 690rpx;
            height: 12px;
            background: #4f5150;
            box-shadow: inset 0px 4px 4px 0px rgba(0, 0, 0, 0.8);
            border-radius: 22px 22px 22px 22px;
            opacity: 1;
          "
        >
          <div
            class="absolute top-1 mx-auto"
            style="
              left: 10rpx;
              right: 10rpx;
              width: 670rpx;
              min-height: 890rpx;
              background: linear-gradient(180deg, #ccd3d9 0%, #ffffff 5%);
              border-radius: 0px 0px 0px 0px;
              opacity: 1;
            "
          >
            <div class="flex justify-around" style="margin: 10px 0">
              <img
                src="https://file.rongcyl.cn/festatic/bkm/first2/tab1.png"
                v-if="tabIndex !== 1"
                @click="tabIndex = 1"
                style="width: 126rpx; height: 32rpx"
              />
              <img
                v-if="tabIndex == 1"
                src="https://file.rongcyl.cn/festatic/bkm/first2/tab1a.png"
                @click="tabIndex = 1"
                style="width: 126rpx; height: 32rpx"
              />
              <img
                v-if="tabIndex !== 2"
                src="https://file.rongcyl.cn/festatic/bkm/first2/tab2.png"
                @click="tabIndex = 2"
                style="width: 126rpx; height: 32rpx"
              />
              <img
                v-if="tabIndex == 2"
                src="https://file.rongcyl.cn/festatic/bkm/first2/tab2a.png"
                @click="tabIndex = 2"
                style="width: 126rpx; height: 32rpx"
              />
              <img
                v-if="tabIndex != 3"
                src="https://file.rongcyl.cn/festatic/bkm/first2/tab3.png"
                @click="tabIndex = 3"
                style="width: 126rpx; height: 32rpx"
              />
              <img
                v-if="tabIndex == 3"
                src="https://file.rongcyl.cn/festatic/bkm/first2/tab3a.png"
                @click="tabIndex = 3"
                style="width: 126rpx; height: 32rpx"
              />
            </div>
            <div class="cardList list" style="padding: 10rpx 16rpx 20rpx 16rpx" v-if="tabIndex == 1">
              <div
                :class="['flex', 'items-center', 'justify-center', 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="1" />
                <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"
                />
                <!-- <img v-if="item.check" src="" alt=""> -->
              </div>
            </div>
            <div class="list pt-4" style="position: relative" v-if="tabIndex == 2">
              <div v-for="(item, index) in prizeList" :key="index">
                <div class="titleTip">概率：{{ item.probability }}%(此概率为当前赏级概率)</div>
                <Title :title="$selectDictLabel(rewardType, item.type).split('赏')[0]" />
                <div class="flex pl-3 pt-3 mb-3" style="background-color: #f6f0e9">
                  <div
                    v-for="(item2, index2) in item.prizeList"
                    @click="showImg(item2)"
                    class="mr-2"
                    style="width: 168rpx; background-color: #f9f6f2; border-radius: 4px; margin-bottom: 10px"
                  >
                    <image
                      :src="item2.icon"
                      mode="aspectFill"
                      style="width: 144rpx; height: 144rpx; border-radius: 5px; margin: auto"
                    />
                    <div class="fs13 pl-2 prizeName">{{ item2.name }}</div>
                    <div style="color: #ea6636" class="pl-2">
                      <span class="fs11">参考价</span>
                      <span class="fs14 bold">{{ detail.price }}</span>
                    </div>
                  </div>
                </div>
              </div>
              <!-- <Title title="FIRST" /> -->
              <!--         <div class="flex pl-3 pt-3 mb-3" style="background-color: #f6f0e9">
								<div
								  v-for="item in 3"
								  class="mr-2"
								  style="width: 168rpx; background-color: #f9f6f2; border-radius: 4px; margin-bottom: 10px"
								>
								  <img
									src="https://file.rongcyl.cn/festatic/bkm/card.png"
									style="width: 144rpx; height: 144rpx; border-radius: 5px; margin: auto"
								  />
								  <div class="fs13 pl-2">冲四层闯…</div>
								  <div style="color: #ea6636" class="pl-2">
									<span class="fs11">参考价</span>
									<span class="fs14 bold">990.5</span>
								  </div>
								</div>
							  </div> -->
            </div>

            <div class="list winningRecord" style="padding: 20rpx 16rpx" v-if="tabIndex == 3">
              <view class="winningRecordCard" v-for="(item, index) in winningRecordList" :key="index">
                <view class="row">
                  <view class="left">
                    <image class="photo" :src="item.userAvtar" mode="aspectFill"></image>
                    <span class="name">{{ item.userName }}</span>
                  </view>
                  <view class="right text1">
                    <span style="margin-right: 5px">{{ item.creatTime }}</span>
                    <span>No.{{ item.id }}</span>
                  </view>
                </view>
                <view class="row" style="margin-top: 10px">
                  <view class="left">
                    <image class="img" :src="item.prizeIcon" mode="aspectFill"></image>
                    <span class="name">{{ item.prizeName }}</span>
                  </view>
                  <view class="right" style="font-size: 14px">
                    {{ $selectDictLabel(rewardType, item.prizeType) }} × 1
                  </view>
                </view>
              </view>
            </div>
            <img
              src="https://file.rongcyl.cn/festatic/bkm/first2/dots.png"
              class="absolute mx-auto inline-block left-0 right-0"
              style="width: 612rpx; height: 24rpx; bottom: -12rpx"
            />
          </div>
        </div>
      </div>

      <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>

      <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>
    </div>
    <paymentPopup ref="paymentPopup" @callBack="paymentSuccess" />
    <imgPopup ref="imgPopup" />
  </div>
</template>

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

import numImg from '@/component/numImg.vue'
import paymentPopup from '@/component/paymentPopup.vue'
import Title from '@/component/title.vue'
export default {
  data() {
    return {
      rewardType: rewardType,
      safeAreaTop: '0px',
      tabIndex: 1,
      params: {
        raffleId: '',
        limit: 999,
        page: 1,
      },
      detail: {},
      cardList: [],
      selectIndex: [],
      prizeList: [],
      winningRecordList: [],
    }
  },
  components: {
    Title,
    imgPopup,
    paymentPopup,
    numImg,
  },
  onLoad(query) {
    console.log(query, 'query')
    if (query.id) {
      this.params.raffleId = query.id
      this.init()
    }
    this.paymentSuccess()
  },
  mounted() {
    this.getSafeAreaTop()
  },
  methods: {
    init() {
      // 获取卡牌商品列表
      this.getCardList()
      // 获取卡包详情
      this.getDetail()
      //获取奖池
      this.getPrizeList()
      // 中奖记录
      this.getPrizeUserList()
    },
    toKnapsack() {
      // 跳转背包界面
      wx.navigateTo({
        url: '/pages/knapsack/knapsack',
      })
    },
    getPrizeUserList() {
      let params = {
        limit: 9999,
        page: 1,
        raffleId: this.params.raffleId,
      }
      prizeUserListApi(params).then(res => {
        this.winningRecordList = res.data
      })
    },
    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()
      })
    },
    // 标记已经选中卡牌
    selectFormat() {
      for (var i = 0; i < this.cardList.length; i++) {
        if (this.selectIndex.includes(this.cardList[i].number)) {
          this.cardList[i].check = true
        }
      }
    },
    getDetail() {
      let params = {
        raffleId: this.params.raffleId,
      }
      raffleDetailApi(params).then(res => {
        console.log(res, '详情')
        this.detail = res.data
      })
    },
    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
      })
    },
    // 选中取消选中卡牌
    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)
      }
    },
    // 支付成功后回调
    paymentSuccess() {
      let _this = this
      console.log('刷新抽卡界面')
      // 刷新界面
      _this.refresh()
    },
    getSafeAreaTop() {
      wx.getSystemInfo({
        success: res => {
          this.safeAreaTop = res.safeArea.top + 'px'
        },
      })
    },
    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
      }
    },
    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)
    },
  },
}
</script>

<style scoped lang="scss">
.first2 {
  padding-top: v-bind(safeAreaBottom);
  color: v-bind(color);

  .card {
    width: 710rpx;
    margin: auto;
    background-color: #fff;
    border-radius: 10px;
    height: 400rpx;
    margin-top: 18px;
  }

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

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

  .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;
    }
  }
}

.cardList {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr 1fr 1fr;
}
.list {
  // height: calc(100% - 32rpx - 20px - 20px);
  // overflow: auto;
}
.prizeName {
  width: 100%;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.titleTip {
  position: absolute;
  z-index: 2;
  height: 32px;
  line-height: 42px;
  padding-left: 25vw;
  font-size: 12px;
  color: #fff;
}
.winningRecord {
  .winningRecordCard {
    margin: 10px 10px 0 10px;
    border-radius: 8px;
    background: #fff;
    padding: 10px;
    box-shadow: 1px 1px 3px #b7b7b7;

    .row {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .left {
        display: flex;
        align-items: center;

        .photo {
          margin-right: 5px;
          width: 30px;
          height: 30px;
          border-radius: 50%;
        }

        .name {
          font-size: 14px;
          font-weight: 600;
        }

        .img {
          width: 40px;
          height: 40px;
          border-radius: 2px;
          margin-right: 5px;
        }
      }

      .text1 {
        color: #666666;
        font-size: 12px;
      }
    }
  }
}
.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;
  }
}
</style>
