<template>
    <div class="invite">
      <van-nav-bar
        title="邀请好友"
        left-arrow
        @click-left="$router.go(-1)"
      />
      <div class="invite-main">
        <p class="invite-title">我的邀请码</p>
        <div class="invite-code">
          <span v-for="(item, index) in user.refcode.length" :key="index">{{ user.refcode[index] }}</span>
        </div>
        <p class="text1-2">邀请的好友也可在注册时直接填写邀请码</p>
        <img :src="inviteQR" alt="" width="150">
        <input class="invite-url" type="text" :value="inviteUrl" disabled>
        <van-button type="info" class="btn" :data-clipboard-text="inviteUrl">复制邀请链接</van-button>
      </div>
      <div class="invite-tip">
        <div class="invite-tip__item">
          <span>1</span>
          <span>用手机扫一扫，直接注册。</span>
        </div>
        <div class="invite-tip__item">
          <span>2</span>
          <span>点击【复制邀请链接】按钮，用浏览器打开进行注册。</span>
        </div>
        <div class="invite-tip__item">
          <span>3</span>
          <span>注册时，手动输入【我的邀请码】。</span>
        </div>
      </div>
    </div>
</template>

<script>
import { mapState } from 'vuex'
import ClipboardJS from 'clipboard'

export default {
  name: 'invite',
  computed: {
    ...mapState(['user']),
    inviteQR () {
      return `http://qr.liantu.com/api.php?bg=f3f3f3&fg=ff0000&gc=222222&el=l&w=200&m=10&text=${window.origin}/register?code=${this.user.refcode}`
    },
    inviteUrl () {
      return `${window.origin}/register?code=${this.user.refcode}`
    }
  },
  created () {
    const clipboard = new ClipboardJS('.btn')

    clipboard.on('success', (e) => {
      this.$toast.success('复制成功！')
      e.clearSelection()
    })

    clipboard.on('error', (e) => {
      this.$toast.fail('复制失败！')
    })
  }
}
</script>

<style lang="scss" scoped>
.invite {
  &-url {
    margin-bottom: 15px;
    display: block;
    width: 100%;
  }
  &-main {
    background-color: #ffffff;
    padding: 20px 15px 40px;
    border-radius: 6px;
    margin: 10px;
    text-align: center;
    img {
      margin: 25px 0;
    }
  }
  &-title {
    font-size: 16px;
  }
  &-code {
    margin: 15px 0;
    span {
      display: inline-block;
      border: 1px solid #E6E7EB;
      margin: 0 6px;
      width: 28px;
      height: 28px;
      text-align: center;
      line-height: 28px;
      border-radius: 3px;
      font-size: 16px;
      color: #FF5722;
    }
  }
  &-tip {
    margin: 15px 10px;
    line-height: 28px;
    &__item {
      font-size: 13px;
      color: #999999;
      span {
        &:first-child {
          display: inline-block;
          width: 18px;
          height: 18px;
          line-height: 18px;
          border-radius: 50%;
          text-align: center;
          background-color: rgba(0, 0, 0, 0.2);
          margin-right: 5px;
          color: #ffffff;
        }
      }
    }
  }
}
</style>
