<template>
  <div>
    <x-header>
    </x-header>
    <div class="share">
      <img class="real_pic" :src="dataURL" />
      <div class="imageWrapper" ref="imageWrapper" v-show="isshow">
        <slot>
          <div class="share-box" :style="'background:'+background">
            <div class="share-book_info">
              <div class="share-book_intro">
                {{book.intro}}
              </div>
              <img :src="book.cover_url" class="share-book_cover">
            </div>
            <div class="share-info_box">
              <div class="share-info_user">
                <img class="share-info_user-head" :src="head_img||'../assets/个人中心-默认头像.png'">
                <div class="share-info_user-userinfo">
                  <div class="username">{{username}}</div>
                  <div class="bookname">向您推荐
                    <span>《{{book.book_name}}》</span>
                  </div>
                </div>
              </div>
              <!-- <div class="share-info_box-good">
              <span>超好看的!</span>
            </div> -->
              <div class="share-code">
                <div class="qr-img"></div>
                <div class="qr-info">
                  <div class="qr-prompt">长按识别二维码</div>
                  <div class="qr-free">免费看书</div>
                </div>
              </div>
            </div>
          </div>
        </slot>
      </div>
    </div>
  </div>
</template>
<script>
import QRCode from "qrcodejs2";
import html2canvas from "html2canvas";
import { BookShare } from "./namespace.js";
import { getDetail } from "../api";

export default {
  name: BookShare.name,
  data() {
    return {
      book: "",
      head_img: "",
      share_url: "",
      username: "",
      dataURL: "",
      isshow: true,
      background: ""
    };
  },
  async mounted() {
    new QRCode(document.querySelector(".share-code .qr-img"), {
      text: this.share_url,
      width: 120,
      height: 120,
      colorDark: "#000000",
      colorLight: "#ffffff",
      correctLevel: 2
    });
    await getDetail(this.$route.query.bid).then(r => {
      r.intro = r.intro.slice(0, 40) + "...";
      this.book = r;
    });
    this.$nextTick(() => {
      html2canvas(this.$refs.imageWrapper, {
        backgroundColor: null
      }).then(canvas => {
        let dataURL = canvas.toDataURL("image/png");
        this.dataURL = dataURL;
        this.isshow = false;
      });
    });
  },
  created() {
    if (process.env.NODE_ENV === "development") {
      this.background = "url(" + require("../assets/beij.jpg") + ") no-repeat center / cover;";
    } else {
      this.background = "url(" +("/static/img/wap_share_bg.jpg") + ") no-repeat center / cover;";
    }
    this.share_url = this.$route.query.share_url;
    this.username = this.$route.query.username;
    this.head_img = this.$route.query.head_img;
  }
};
</script>
<style scoped lang="scss">
.share {
  // padding: 0.3rem 0.2rem;
}
.imageWrapper {
  position: absolute;
  padding: 0.3rem 0.2rem;
  top: 1rem;
  z-index: -1;
}
.share-box {
  background-size: 100%;
  // background: url("/static/img/wap_share_bg.jpg") no-repeat center / cover;
}
.real_pic {
  width: 100%;
  height: 10.9rem;
  // padding: 0 0.3rem;
}
.share-book_info {
  // background-color: white;
  position: relative;
  top: 0.8rem;
  display: flex;
  padding: 0.3rem 0.3rem 0 0.3rem;
  .share-book_intro {
    font-size: 0.26rem;
    margin-right: 0.2rem;
    color: white;
  }
  .share-book_cover {
    width: 2rem;
    height: 2.6rem;
  }
}
.share-info_box {
  // background-color: #f4f4f4;
  margin-top: 0.3rem;
  padding: 0.5rem 0.25rem;
  .share-info_user {
    margin-top: 0.2rem;
    display: flex;
    padding: 0.3rem 0;
    position: relative;
    top: 0.2rem;
    .share-info_user-head {
      width: 0.8rem;
      height: 0.8rem;
      border-radius: 1rem;
    }
    .share-info_user-userinfo {
      margin-left: 0.2rem;
      .username {
        font-size: 0.25rem;
      }
      .bookname {
        font-size: 0.25rem;
        span {
          font-weight: 900;
        }
      }
    }
  }
  .share-info_box-good {
    border-bottom: #eaeaea solid 0.01rem;
    color: #d37f7d;
    text-align: center;
    padding: 0.3rem 0;
    span {
      font-size: 0.3rem;
      border: solid #d37f7d 0.01rem;
      border-radius: 0.5rem;
      padding: 0.02rem 0.3rem;
    }
  }
}
.share-code {
  display: flex;
  justify-content: center;
  margin-top: 1.9rem;
  .qr-info {
    margin-left: 0.5rem;
    .qr-prompt {
      margin-top: 30%;
      font-size: 0.3rem;
    }
    .qr-free {
      font-size: 0.3rem;
      font-weight: 900;
      color: #eb3935;
    }
  }
}
</style>