<template>
  <div ref="dragDiv" class="FloatingFrame">
    <div>
      <div v-show="isQrcode" class="qrcode_div" @mouseleave="handQrcodeHide">
        <ul>
          <li @mouseover="handQrcodeShow">
            <Icon type="icon-cur-qrcode" :size="20"></Icon>
          </li>
        </ul>
        <div v-show="isDownload" class="large_qrCode" @click="goRouter">
          <div id="qrCode" ref="qrCodeDiv"></div>
          <span
            >{{ $t("SCAN_CODE_TO_DOWNLOAD") }}
            <span class="blue_download">{{ $t("CLICK_DOWNLOAD") }}</span></span
          >
          <span class="icon_right"></span>
        </div>
      </div>
      <div
        class="qrcode"
        @mouseover="handIconHover"
        @mouseleave="handIconLeave"
      >
        <div
          class="icon_div"
          :class="isHideIcon ? 'icon_div_hide' : 'icon_div_show'"
          @click="handIconDiv"
        >
          <img
            v-show="!isQrcode"
            src="@/assets/images/qrCodeFold.png"
            width="20"
            height="20"
          />
          <img
            v-show="isQrcode"
            src="@/assets/images/qrCodeOpen.png"
            width="20"
            height="20"
          />
        </div>
        <div v-if="isMinus" class="minus_sign" @click="hideIcon">
          <Icon type="icon-ins-minus" :size="14"></Icon>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { Icon } from "@mega-design/ui-vue";
import QRCode from "qrcodejs2";

export default {
  components: { Icon },
  data() {
    return {
      isMinus: false,
      isHideIcon: false,
      isHideIconClick: false,
      isDownload: false,
      isQrcode: true,
    };
  },
  mounted() {
    let url = `${window.location.host}/white-route/app-download`;
    if (url.includes("http") || url.includes("https")) {
      // console.log("有协议");
    } else {
      url = `${window.location.protocol}//${url}`;
    }
    this.$nextTick(() => {
      // eslint-disable-next-line no-new
      new QRCode(this.$refs.qrCodeDiv, {
        text: url,
        width: 150,
        height: 150,
        colorDark: "#333", // 二维码颜色
        colorLight: "#fff", // 二维码背景色
        correctLevel: QRCode.CorrectLevel.L, // 容错率，L/M/H
      });
      // // 获取DOM元素
      // const { dragDiv } = this.$refs;
      // // 缓存 clientX clientY 的对象: 用于判断是点击事件还是移动事件
      // const clientOffset = {};
      // // 绑定鼠标按下事件
      // dragDiv.addEventListener("mousedown", (event) => {
      //   const offsetY = dragDiv.getBoundingClientRect().top; // 获取当前的y轴距离
      //   const innerY = event.clientY - offsetY; // 获取鼠标在方块内的y轴距
      //   // 缓存 clientX clientY
      //   clientOffset.clientY = event.clientY;
      //   // 鼠标移动的时候不停的修改div的left和top值
      //   // eslint-disable-next-line no-shadow
      //   document.onmousemove = function (event) {
      //     const { clientHeight } = document.documentElement;
      //     dragDiv.style.bottom = `${clientHeight - (event.clientY - innerY)}px`;
      //     // dragDiv 距离顶部的距离
      //     if (clientHeight - dragDiv.style.bottom.replace("px", "") < 350) {
      //       dragDiv.style.bottom = `${clientHeight - 350}px`;
      //     }
      //     // 3、超出底部处理
      //     if (dragDiv.style.bottom.replace("px", "") < 85) {
      //       dragDiv.style.bottom = `85px`;
      //     }
      //   };
      //   // 鼠标抬起时，清除绑定在文档上的mousemove和mouseup事件；否则鼠标抬起后还可以继续拖拽方块
      //   document.onmouseup = function () {
      //     document.onmousemove = null;
      //     document.onmouseup = null;
      //   };
      // });
    });
  },
  methods: {
    // 蓝色图标放上去
    handIconHover() {
      this.isMinus = true;
      if (this.isHideIconClick) {
        this.isHideIcon = false;
        this.isMinus = false;
      }
      // if (this.isQrcode) {
      //   this.isMinus = false;
      // }
    },
    // 蓝色图标离开
    handIconLeave() {
      this.isMinus = false;
      if (this.isHideIconClick) {
        this.isHideIcon = true;
        this.isMinus = false;
      }
    },
    // 蓝色图标的点击
    handIconDiv() {
      this.isHideIconClick = false;
      this.isQrcode = !this.isQrcode;
      const { dragDiv } = this.$refs;
    },
    // 减号隐藏点击
    hideIcon() {
      this.isHideIconClick = true;
      this.isHideIcon = true;
      this.isQrcode = false;
    },
    // 二维码放上去显示大二维码
    handQrcodeShow() {
      this.isDownload = true;
    },
    // 二维码离开隐藏大二维码
    handQrcodeHide() {
      this.isDownload = false;
    },
    handMousemove() {
      this.isDownload = true;
    },
    handMouseleave() {
      this.isDownload = false;
    },
    goRouter() {
      window.open("/white-route/app-download");
    },
  },
};
</script>

<style lang="less" scoped>
.FloatingFrame {
  position: fixed;
  right: 0;
  bottom: 85px;
  z-index: 40;
  .qrcode {
    position: relative;
    .icon_div {
      width: 42px;
      height: 42px;
      display: flex;
      justify-content: center;
      align-items: center;
      border-radius: 50%;
      color: #fff;
      background: #0064c8;
      text-align: center;
      clear: both;
      cursor: pointer;
      position: relative;
      box-shadow: 0 0 4px 1px rgba(0, 0, 0, 0.07);
    }
    .icon_div_hide {
      right: -20px;
    }
    .icon_div_show {
      right: 0;
    }
    .minus_sign {
      display: inline-block;
      width: 20px;
      line-height: 20px;
      text-align: center;
      border-radius: 50%;
      background: #fff;
      color: #000;
      position: absolute;
      right: 0;
      top: -15px;
      cursor: pointer;
    }
  }
  .qrcode_div {
    width: 42px;
    height: 42px;
    line-height: 42px;
    border-radius: 50%;
    color: #000;
    background: #fff;
    text-align: center;
    position: relative;
    cursor: pointer;
    box-shadow: 0 0 4px 1px rgba(0, 0, 0, 0.07);
    margin-bottom: 10px;
    ul {
      margin: 0;
      padding: 0;
      li {
        list-style: none;
      }
      li::marker {
        content: " ";
        display: none;
      }
    }
  }
  .large_qrCode {
    width: 185px;
    background: #fff;
    box-shadow: 0 3px 20px 1px rgba(0, 0, 0, 0.2);
    border-radius: 6px 6px 6px 6px;
    text-align: center;
    position: absolute;
    top: -106px;
    right: 70px;
    font-weight: 400;
    font-size: 16px;
    color: #545d60;
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    padding-top: 10px;
    .blue_download {
      color: #006896;
      cursor: pointer;
    }
    .icon_right {
      position: absolute;
      right: -40px;
      bottom: 55px;
      width: 0;
      height: 0;
      border-width: 20px;
      border-color: transparent transparent transparent #fff;
      border-style: dashed dashed dashed solid;
    }
  }
}
</style>
