<template>
  <div class="encrypt">
    <div class="box">
      <div class="title">{{ channelInfo.tips || '欢迎观看直播' }}</div>
      <input class="pass" type="text" name="password" v-model="password" placeholder="请输入密码" />
      <button type="button" class="sub" @click="_sub">立即登录</button>
    </div>
  </div>
</template>
<script>
import { checkPassword } from '@/plugins/API/apiAll';
import { mapGetters, mapMutations } from 'vuex';
export default {
  name: 'encrypt-box',
  data() {
    return {
      password: ''
    };
  },
  computed: {
    ...mapGetters({
      type: 'type',
      channelInfo: 'channelInfo'
    }),
    tokenKey() {
      return `encrypt-${this.type}-${this.channelInfo.id}`;
    }
  },
  mounted() {
    this.get_encrypt();
  },
  methods: {
    ...mapMutations({
      set_encrypt: 'set_encrypt'
    }),
    async get_encrypt() {
      const passToken = this.$cookie.get(this.tokenKey) || '';
      if (passToken) {
        const res = await checkPassword({
          id: this.channelInfo.id,
          passToken: passToken
        });
        if (res.code === 200 && res.errorCode === 0) {
          this.set_encrypt(false);
        } else {
          this.set_encrypt(true);
        }
      } else {
        this.set_encrypt(true);
      }
    },
    async _sub() {
      const res = await checkPassword({
        id: this.channelInfo.id,
        password: this.password
      });
      if (res.code === 200 && res.errorCode === 0) {
        this.$cookie.set(this.tokenKey, res.data, { expires: 10 });
        this.set_encrypt(false);
      }
    }
  }
};
</script>

<style lang="less" scoped>
.encrypt {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 2000;
  width: 100%;
  height: 100%;
  background: url('./img/encrypt.jpg') 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: #bae0e0;
      }
    }
    .sub {
      width: 100%;
      max-width: 3rem;
      height: 0.5rem;
      background-color: #ffffff;
      border: 0.01rem solid #ffffff;
      color: #1fb5ad;
      text-align: center;
      font-size: 0.16rem;
      border-radius: 0.5rem;
      outline: none;
    }
  }
}
</style>
