<template>
  <div class="white">
    <div class="box">
      <div class="title">{{ watchWhite.tips || '欢迎观看直播' }}</div>
      <input class="pass" type="text" name="password" v-model="code" placeholder="请输入验证码" />
      <button type="button" class="sub" @click="_sub">提交</button>
    </div>
  </div>
</template>
<script>
import { checkWatchList } from '@/plugins/API/apiAll';
import { mapGetters, mapMutations } from 'vuex';
export default {
  name: 'white-box',
  data() {
    return {
      code: ''
    };
  },
  computed: {
    ...mapGetters({
      type: 'type',
      channelInfo: 'channelInfo',
      channelConfig: 'channelConfig'
    }),
    watchWhite() {
      const watchWhiteList = this.channelConfig.watchWhiteList || {};
      return watchWhiteList;
    },
    tokenKey() {
      return `white-${this.type}-${this.channelInfo.id}`;
    }
  },
  mounted() {
    this.get_watchWhite();
  },
  methods: {
    ...mapMutations({
      set_white: 'set_white'
    }),
    async get_watchWhite() {
      const watchObj = this.$cookie.get(this.tokenKey) || '{}';
      const { ticket, code } = JSON.parse(watchObj);
      if (ticket) {
        const res = await checkWatchList({
          uin: this.channelInfo.uin,
          channelId: this.channelInfo.id,
          ticket: ticket,
          code: code
        });
        if (res.code === 200 && res.errorCode === 0) {
          this.set_white(false);
        } else {
          this.set_white(true);
        }
      } else {
        this.set_white(true);
      }
    },
    async _sub() {
      const res = await checkWatchList({
        uin: this.channelInfo.uin,
        channelId: this.channelInfo.id,
        ticket: '',
        code: this.code
      });
      if (res.code === 200 && res.errorCode === 0) {
        this.$cookie.set(
          this.tokenKey,
          {
            ticket: res.data.ticket,
            code: this.code
          },
          { expires: 10 }
        );

        this.set_white(false);
      }
    }
  }
};
</script>

<style lang="less" scoped>
.white {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 2000;
  width: 100%;
  height: 100%;
  background: url('./img/white.png') no-repeat center center;
  background-size: cover;
  .box {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 100%;
    max-width: 3rem;
    transform: translate(-50%, -70%);
    .title,
    .pass,
    .sub {
      display: block;
      margin: auto;
    }
    .title {
      margin-bottom: 0.8rem;
      font-size: 0.3rem;
      color: #fff;
      text-align: center;
    }
    .pass {
      margin-bottom: 0.2rem;
      width: 100%;
      max-width: 3rem;
      height: 0.5rem;
      background-color: transparent;
      border: 0.01rem solid #c9edec;
      color: #ffffff;
      text-align: center;
      font-size: 0.16rem;
      border-radius: 0.5rem;
      outline: none;
      &::-webkit-input-placeholder {
        color: #ffffff;
      }
    }
    .sub {
      width: 100%;
      max-width: 3rem;
      height: 0.5rem;
      background-color: #ffffff;
      border: 0.01rem solid #ffffff;
      color: #4183ff;
      text-align: center;
      font-size: 0.16rem;
      border-radius: 0.5rem;
      outline: none;
    }
  }
}
</style>
