<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 icon_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.qrCode)" 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";

export default {
  props: ["show", "item"],
  components: {
    Popup,
  },
  data() {
    return {
      key: "",
    };
  },
  mounted() {},
  methods: {
    downloadImg(url) {
      var a = document.createElement("a");
      var event = new MouseEvent("click");
      a.download = "qrcode";
      a.href = 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.$tips({ content: "复制成功", icon: "ok" })
          : this.$tips({ content: "复制失败", icon: "ok" });
      } catch (err) {
        this.$tips({ content: "复制失败", icon: "ok" });
      }

      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 #ccc;
        }
        .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>
