<template>
  <div class="auth-box">
    <div class="watch-pay" v-if="isWatchPay" @click="goAuth">
      <img src="./img/pay.png" alt="" />
      <span>免费试看 {{ expire | formatTime('mm:ss') }}</span>
    </div>
  </div>
</template>

<script>
import { getPayWatchInfo } from '@/plugins/API/apiAuth';
import { mapGetters } from 'vuex';

export default {
  name: 'auth-box',
  data() {
    return {
      timet: null,
      isWatchPay: false,
      expire: 0
    };
  },
  computed: {
    ...mapGetters({
      channelInfo: 'channelInfo',
      jumpUrls: 'jumpUrls'
    })
  },
  mounted() {
    if (this.channelInfo.needPay === 1) {
      this.get_Info();
    }
  },
  methods: {
    async get_Info() {
      const res = await getPayWatchInfo({
        channelId: this.channelInfo.id,
        uin: this.channelInfo.uin
      });
      if (res.code === 200 && res.errorCode === 0) {
        const info = res.data || {};
        if (info.needPayInvalid === 1) {
          this.isWatchPay = false;
        } else if (info.isCanPlay === 0) {
          this.goAuth();
        } else {
          if (info.type !== 2) {
            this.isWatchPay = true;
            this.expire = info.expire;
            if (this.expire > 0) {
              this.setTimer();
            }
          }
        }
      } else {
        if (res.errorMessage) {
          this.$layer.msg(res.errorMessage);
        }
      }
    },
    goAuth() {
      window.location.replace(this.jumpUrls.auth_pay);
    },
    setTimer() {
      this.clearTimer();
      this.timer = setInterval(() => {
        this.expire--;
        if (this.expire <= 0) {
          this.clearTimer();
          this.goAuth();
        }
      }, 1000);
    },
    clearTimer() {
      clearInterval(this.timer);
      this.timer = null;
    }
  },
  beforeDestroy() {
    this.clearTimer();
  }
};
</script>

<style lang="less" scoped>
.watch-pay {
  display: inline-block;
  height: 0.35rem;
  line-height: 0.35rem;
  padding: 0 0.19rem 0 0.07rem;
  position: absolute;
  bottom: 0.6rem;
  left: 0.09rem;
  z-index: 110;
  background-color: rgba(0, 141, 246, 0.5);
  border-radius: 0.1rem;
  font-size: 0;
  cursor: pointer;
  img {
    vertical-align: middle;
    display: inline-block;
    margin-right: 0.05rem;
    width: 0.16rem;
    height: 0.16rem;
  }
  span {
    vertical-align: middle;
    display: inline-block;
    font-size: 0.14rem;
    color: #fff;
  }
}
</style>
