<template>
  <div>
    <div class="revive-card-container">
      <img class="revive-card-btn" src="@/images/SignUp/revive_card.png" @click="changeRebornShow()">
      <div class="revive-card-tip">参加并邀请好友可获得复活卡一张</div>
      <div class="revive-card-num">x{{rebornNum}}</div>
    </div>
    <div class="Reborn-component" v-if="showRebornModal">
      <div class="content">
        <img class="close-btn" src="@/images/common/pop_ic_close.png" @click="changeRebornShow()">
        <div class="title1">邀请一位好友报名成功</div>
        <div class="title2">可获得一次复活机会</div>
        <img class="center-img" src="@/images/components/Reborn/center_img.png">
        <div class="desc">每期比赛只能获得1次复活机会</div>
        <div class="desc">未达标次日11:50后复活机会自动生效</div>
        <div class="invite-btn" @click="inviteFriend">立即邀请好友参赛</div>
        <div class="desc">注：每位好友仅可被邀请1次</div>
      </div>
      <ShareModal v-on:close="closeShareModal" v-if="showShareModal"></ShareModal>
    </div>
  </div>
</template>

<script>
import ShareModal from "@/components/ShareModal.vue";
import {shareContent, track, userCards} from "@/components/axios/api";

export default {
  name: "Reborn",
  components: { ShareModal },
  data() {
    return {
      showShareModal: false,
      userData: null,
      showRebornModal: false,
      rebornNum: 0
    };
  },
  props: [""],
  computed: {
    // invite_num() {
    //   return 456;
    // }
  },
  mounted() {
    if (window["userData"]) {
      this.userData = window["userData"];
    } else {
      let localUserData = localStorage.getItem("walk_userData");
      if (localUserData) {
        window["userData"] = JSON.parse(localUserData);
        this.userData = window["userData"];
      }
    }
    this.getRebornNum();
  },
  methods: {
    // 获取复活卡数量
    getRebornNum() {
      userCards().then(res => {
        if (res.code === 0) {
          this.rebornNum = res.data.rebirth_card;
        }
      });
    },
    changeRebornShow() {
      this.showRebornModal = !this.showRebornModal;
    },
    //  @rebirth="rebirthAction"
    // rebirthAction() {
    //   daka(1)
    //     .then(res => {
    //       if (res.code == 1) {
    //         this.showModal("温馨提示", res.msg);
    //         return;
    //       }
    //       this.changeRebornShow();
    //       this.dakaSuccessHandle();
    //     })
    //     .catch(() => {});
    // },

    // 邀请好友
    inviteFriend() {
      this.showShareModal = true;
      if (!(this.userData && this.userData.id)) {
        return alert('为获取到用户id，此次分享不能获得复活卡');
      }
      track("rebitrh_share");
      console.log("处理前连接", location.href);
      let links = `${location.href.split("#")[0].split("?")[0]}?inviter=${this.userData.id}&from_type=rebirth&`;
      console.log("处理后的连接", links);
      shareContent("rebirth").then(res => {
        let result = res.data;
        wx.updateAppMessageShareData({
          title: result.title, // 分享标题
          desc: result.desc, // 分享描述
          link: links, // 分享链接
          imgUrl: result.img_url, // 分享图标
          success: function() {}
        });
        wx.onMenuShareAppMessage({
          title: result.title, // 分享标题
          desc: result.desc, // 分享描述
          link: links, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
          imgUrl: result.img_url, // 分享图标
          success: function() {
            // 用户点击了分享后执行的回调函数
            track("rebitrh_share_act");
          }
        });
      });
    },
    // 关闭邀请弹窗
    closeShareModal() {
      shareContent("").then(res => {
        let result = res.data;
        let links = `${location.href.split("#")[0].split("?")[0]}`;
        wx.updateAppMessageShareData({
          title: result.title, // 分享标题
          desc: result.desc, // 分享描述
          link: links, // 分享链接
          imgUrl: result.img_url, // 分享图标
          success: function() {}
        });
        wx.onMenuShareAppMessage({
          title: result.title, // 分享标题
          desc: result.desc, // 分享描述
          link: links, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
          imgUrl: result.img_url, // 分享图标
          success: function() {}
        });
      });
      this.showShareModal = false;
    },
  }
}
</script>

<style lang="less" scoped>
.revive-card-container {
  position: relative;
  .revive-card-btn {
    width: 1.06rem;
    height: 0.9rem;
    display: block;
  }
  .revive-card-tip {
    white-space: nowrap;
    font-size: 0.26rem;
    line-height: 0.56rem;
    color: #fff;
    padding: 0 0.18rem;
    border-radius: 0.02rem;
    background: rgba(0, 0, 0, 0.3);
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    right: 1.18rem;
    &:after {
      content: "";
      width: 0;
      height: 0;
      border-left: 0.12rem solid rgba(0, 0, 0, 0.3);
      border-right: 0.12rem solid transparent;
      border-top: 0.09rem solid transparent;
      border-bottom: 0.09rem solid transparent;
      position: absolute;
      top: 50%;
      transform: translateY(-50%);
      right: -0.24rem;
    }
  }
  .revive-card-num {
    font-size: 0.2rem;
    line-height: 0.26rem;
    color: #fff;
    padding: 0 0.1rem;
    background: #f55954;
    border-radius: 0.26rem;
    position: absolute;
    top: -0.13rem;
    right: 0.04rem;
  }
}
.Reborn-component {
  background: rgba(0, 0, 0, 0.5);
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 999;
  display: flex;
  align-items: center;
  justify-content: center;
  .content {
    width: 6rem;
    height: 7.3rem;
    background: #fff;
    border-radius: .2rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    position: relative;
    .title1 {
      font-size: .4rem;
      line-height: 1;
      color: #000;
      font-weight: bold;
      margin-top: .6rem;
    }
    .title2 {
      font-size: .34rem;
      line-height: 1;
      color: #000;
      margin-top: .2rem;
    }
    .center-img {
      width: 2.75rem;
      height: 1.85rem;
      margin-top: .52rem;
      margin-bottom: .62rem;
    }
    .desc {
      font-size: .26rem;
      line-height: .4rem;
      color: #888;
      span {
        font-size: .36rem;
        color: #000;
        font-weight: bold;
        margin: 0 .07rem;
      }
    }
    .invite-btn {
      width: 4.6rem;
      height: .88rem;
      background: #FFBB33;
      border-radius: .88rem;
      font-size: .34rem;
      line-height: .88rem;
      color: #fff;
      text-align: center;
      margin-top: .42rem;
      margin-bottom: .1rem
    }
    .close-btn {
      width: .58rem;
      height: .58rem;
      position: absolute;
      top: -.88rem;
      right: 0;
    }
  }
}
</style>