<template>
  <view class="member-container">
    <view class="header dir-top-wrap cross-center">
      <view class="title">会员充值</view>
      <text class="tips">会员指定短剧无限观看、xxx 等特权</text>
    </view>
    <view class="content main-left">
      <view
        v-for="(item,index) in settings"
        :key="index"
        class="item dir-top-wrap cross-center main-center"
        :class="{active: activeIndex === index}"
        @click="handleSelect(index)"
      >
        <view class="border" />
        <view v-if="activeIndex === index" class="selected">已选择</view>
        <text class="day">{{ item.valid_day }}天</text>
        <text class="price">￥{{ item.price }}</text>
      </view>
    </view>
    <view class="free main-center cross-center" @click="$u.route('/pages/member/free')">
      查看 <text>免费片单</text>
    </view>
    <!--购买弹窗-->
    <u-modal
      :show="modal.show"
      content="确定购买?"
      show-cancel-button
      @confirm="handleBuy"
      @cancel="modal.show = false"
    />
  </view>
</template>

<script>

export default {
  data() {
    return {
      settings: [],
      activeIndex: 0,
      payId: null,
      interval: null,
      modal: {
        show: false
      }
    }
  },
  methods: {
    getSetting() {
      this.$loading()
      this.$api.user.vip.setting().then(res => {
        this.$hideLoading()
        this.settings = res.data
      })
    },
    handleSelect(index) {
      this.activeIndex = index
      this.modal.show = true
    },
    handleBuy() {
      const item = this.settings[this.activeIndex]
      this.$loading('购买中...')
      this.$api.user.vip.create({ id: item.id }).then(res => {
        this.$hideLoading()
        this.payId = res.pay_id
        this.modal.show = false
        /* ifdef MP-TOUTIAO */
        tt.pay({
          service: 5,
          orderInfo: {
            order_id: res.data.order_id,
            order_token: res.data.order_token
          },
          success: payRes => {
            if (payRes.code === 0) {
              this.$loading('支付结果查询中...')
              this.query()
            } else {
              this.$u.toast('支付失败')
            }
          },
          fail(res) {
            // 调起收银台失败处理逻辑
          }
        })
        /* endif */
      }).catch(() => {
        this.$hideLoading()
      })
    },
    query() {
      if (this.interval) return
      this.interval = setInterval(() => {
        this.$api.pay.query(this.payId).then(res => {
          this.$hideLoading()
          this.$u.toast('支付成功')
          clearInterval(this.interval)
          // 获取用户信息
          this.$api.user.info().then(res => {
            this.$store.dispatch('user/info', res.data)
          })
        }).catch(err => {

        })
      }, 1000)
    }
  },
  onLoad() {
    this.getSetting()
  }
}
</script>

<style lang="scss" scoped>
    .member-container{
      padding: 20rpx 0;
      font-size: 30rpx;
      .header{
        color: $primary-color;
        margin-top: 50rpx;
        .title{
          position: relative;
          font-size: 42rpx;
          font-weight: 600;
          width: 100%;
          text-align: center;
          &:before,&:after{
            content: "";
            background: url("/static/image/member-line-bg.png") no-repeat;
            background-size: 100%;
            position: absolute;
            top: 50%;
            left: 60rpx;
            width: 200rpx;
            height: 20rpx;
          }
          &:after{
            transform: rotate(180deg);
            right: 60rpx;
            left: unset;
            top: 20%;
          }
        }
        .tips{
          color: $dark-color;
          font-size: 26rpx;
          margin-top: 20rpx;
        }
      }
      .content{
        padding: 0 20rpx;
        margin-top: 80rpx;
        .item{
          color: $default-color;
          margin-left: 20rpx;
          flex: 1;
          height: 300rpx;
          position: relative;
          .border{
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            z-index: 0;
            border: 4rpx solid $info-color;
            border-radius: 10rpx;
            overflow: hidden;
          }
          &.active .border{
            border: unset;
            &:after{
              content: "";
              position: absolute;
              top: 0;
              left: 0;
              right: 0;
              bottom: 0;
              border: 4rpx solid;
              border-image: linear-gradient(222deg, #6EEBE8, #FF74B9) 1;
              z-index: 0;
            }
          }
          .selected{
            position: absolute;
            top: -20rpx;
            background: url("/static/image/member-selected-bg.png") no-repeat;
            background-size: 100%;
            text-align: center;
            width: 120rpx;
            height: 40rpx;
            font-size: 24rpx;
            line-height: 40rpx;
            left: 20rpx;
            z-index: 1;
          }
          &:first-child{
            margin-left: 0;
          }
          .day{
            font-size: 32rpx;
            margin-bottom: 80rpx;
          }
          .price{
            font-size: 38rpx;
            color: $primary-color;
          }
        }
      }
      .free{
        background: #1B1E32;
        width: 100%;
        padding: 20rpx 0;
        color: $info-color;
        text-align: center;
        font-size: 26rpx;
        margin-top: 60px;
        text{
          color: $dark-color;
        }
      }
    }
</style>
