<template>
  <popup v-if="show">
    <div class="ui-message ui-message-confirm message-material" style="width: 500px">
      <div class="ui-message-header header-material">
        <span>分享</span>
        <span @click="$emit('close')">
          <i class="iconfont iconedit_close"></i>
        </span>
      </div>
      <div class="ui-message-main">
        <ul>
          <li>
            <span>作品链接</span>
            <input :title="item.share" class="ui-input" disabled type="text" maxlength="15" placeholder="输入名字" v-model="item.share" />
          </li>
          <li>
            <span>作品二维码</span>
            <img :src="item.qrCode + `?${Math.random()}` || $thumb" alt="" />
          </li>
        </ul>
      </div>

      <div class="ui-message-footer footer-material">
        <button @click="downloadImg(item)" class="ui-button">
          下载二维码
        </button>
        <button @click="copy(item.share)" class="ui-button submit">
          复制链接
        </button>
      </div>
    </div>
  </popup>
</template>

<script>
import Popup from "@/components/shared/popup";

let dataUrlToBold = function(url) {
  let arr = url.split(","),
    mime = arr[0].match(/:(.*?);/)[1],
    bStr = atob(arr[1]),
    n = bStr.length,
    u8arr = new Uint8Array(n);
  while (n--) {
    u8arr[n] = bStr.charCodeAt(n);
  }
  return new Blob([u8arr], { type: mime });
};

export default {
  props: ["show", "item"],
  components: {
    Popup,
  },
  data() {
    return {
      key: "",
    };
  },
  mounted() {},
  methods: {
    downloadImg(workItem) {
      let val = workItem.qrCode
      let _type_index = val.lastIndexOf(".");
      let _type = val.substr(_type_index + 1); //原始图片类型
      let image = new Image();
      image.setAttribute("crossOrigin", "anonymous"); //消除跨域
      image.src = val;
      image.onload = function() {
        //借助canvas实现 消除 图片地址会先直接窗口打开图片地址
        let canvas = document.createElement("canvas");
        canvas.width = image.width;
        canvas.height = image.height;
        let context = canvas.getContext("2d");
        context.drawImage(image, 0, 0, image.width, image.height);
        let url = canvas.toDataURL("image/" + _type);
        let blob = dataUrlToBold(url);
        let obj_url = URL.createObjectURL(blob); // 消除Chrome下文件太大 导致下载失败（网络失败）的问题
        let a = document.createElement("a");
        let event = new MouseEvent("click");
        a.download = workItem.name || "qrcode";
        a.href = obj_url;
        a.dispatchEvent(event);
      };
    },
    copy(text) {
      var textArea = document.createElement("textarea");

      textArea.style.position = "fixed";
      textArea.style.top = 0;
      textArea.style.left = 0;
      textArea.style.width = "2em";
      textArea.style.height = "2em";
      textArea.style.padding = 0;
      textArea.style.border = "none";
      textArea.style.outline = "none";
      textArea.style.boxShadow = "none";
      textArea.style.background = "transparent";
      textArea.value = text;

      document.body.appendChild(textArea);

      textArea.select();

      try {
        document.execCommand("copy") ? this.$msg.success("复制成功") : this.$msg.error("复制失败");
      } catch (err) {
        this.$msg.error("复制失败");
      }

      document.body.removeChild(textArea);
    },
  },
};
</script>

<style lang="less" scoped>
.ui-message-confirm {
  border-radius: 0;
  .icon_close {
    color: #909090;
  }

  .ui-message-main {
    margin: 20px auto;
    padding-bottom: 20px;
    background: #fff;
    > ul {
      color: #646566;
      > li {
        display: flex;
        align-items: flex-start;
        margin-bottom: 20px;
        > span {
          font-size: 14px;
          display: inline-block;
          width: 70px;
          margin-top: 4px;
          text-align: right;
          margin-right: 16px;
        }
        > img {
          width: 120px;
          border: 1px solid #ebedf0;
          border-radius: 4px;
        }
        .ui-input {
          width: 362px;
          height: 36px;
          background: #f7f7f7;
          border-radius: 4px;
          border: 1px solid #ebedf0;
        }
        .ui-button {
          min-width: 104px;
          margin-left: 10px;
        }
      }
    }
  }
  .ui-message-footer {
    width: 100%;
  }
  .footer-material {
    margin-top: 0 !important;
  }
}
</style>

<style lang="less" scoped>
@import "../style.less";
</style>
