<template>
  <a-modal
    :visible="true"
    :footer="null"
    :title="null"
    :centere="true"
    :closable="false"
    style="margin-top: 400px"
    :zIndex="9999"
    @cancel="close"
  >
    <div id="qrcode" class="QR">
      <div class="qr_header"></div>
      <div class="qr_main">
        <div class="qrm_header">
          <span>{{ title }}</span>
          <div class="close_exit" @click="close"></div>
        </div>
        <div class="downloadCode" style="">
          <div class="qrm_body">
            <div class="codename">{{ name }}</div>
            <QrcodeVue :value="url" :size="size" style="width: 200px; height: 200px"></QrcodeVue>
          </div>
        </div>
        <div v-if="copyAble" class="codeUrl" :style="{ display: 'flex'}">
          <div class="codeUrlLink">链接</div>
          <a-input :value="url" disabled class="codeUrlInp"/>
          <a-input
            :value="url"
            id="courseUrl"
            class="codeUrlInp"
            style="position: absolute; opacity: 0; z-index: -1"
          />
          <div @click="copyUrl" class="codeUrlCopy">复制链接</div>
        </div>
        <div class="qrm_footer">
            <span style="color: #387df7; cursor: pointer" @click="download(200)">下载二维码</span>
        </div>
      </div>
    </div>
  </a-modal>
</template>
<script setup>
import {defineProps, onMounted} from "vue";
import html2canvas from "html2canvas";
import QrcodeVue from "qrcode.vue";
import {message} from "ant-design-vue";

const props = defineProps({
  close: {
    type: Function,
    default: () => ({})
  },
  ok: {
    type: Function,
    default: () => ({})
  },
  url: String,
  copyAble: {
    type: Boolean,
    default: false
  },
  size: {
    type: Number,
    default: 800
  },
  name: String,
  title: {
    type: String,
    default: "二维码"
  },
  cancel: {
    type: Boolean,
    default: true
  },
  duration: {
    type: Number,
    default: 0
  },
});

onMounted(() => props.duration && setTimeout(() => props.close(), props.duration));

function download() {
  html2canvas(
    document.querySelector(".downloadCode"),
    { useCORS: true }
  ).then((canvas) => {
    let a = document.createElement("a");
    a.style.display = "none";
    a.download = `${new Date().getTime()}.png`;
    a.href = canvas.toDataURL("image/png");
    document.body.appendChild(a);
    a.click();
  });
}

function copyUrl() {
  const input = document.createElement("input"); // 创建input对象
  input.value = props.url;
  document.body.appendChild(input); // 添加临时实例
  input.select(); // 选择实例内容
  document.execCommand("Copy"); // 执行复制
  document.body.removeChild(input); // 删除临时实例
  message.success("复制成功！");
}
</script>
<style lang="scss" scoped>
.QR {
  z-index: 9999;
  width: 520px;
  background: #ffffff;
  box-shadow: 0px 1px 35px 0px rgba(118, 136, 166, 0.21);
  position: absolute;
  left: 50%;
  top: 10%;
  transform: translate(-50%, -50%);

  .qr_header {
    position: absolute;
    width: calc(100%);
    height: 40px;
    background: linear-gradient(
        rgba(78, 166, 255, 0.2) 0%,
        rgba(78, 166, 255, 0) 100%
    );
  }

  .qr_main {
    width: 100%;
    position: relative;

    .qrm_header {
      display: flex;
      align-items: center;
      padding-top: 20px;
      padding-left: 26px;
      font-size: 16px;

      .title {
        font-size: 16px;
        font-weight: 600;
        color: #333333;
        line-height: 22px;
      }

      .close_exit {
        position: absolute;
        right: 42px;
        cursor: pointer;
        width: 20px;
        height: 20px;
        background-image: url(@/assets/images/coursewareManage/close.png);
        background-size: 100% 100%;
      }
    }

    .qrm_body {
      width: 100%;
      padding-top: 22px;
      padding-bottom: 32px;
      display: flex;
      flex-direction: column;
      align-items: center;

      .codename {
        font-size: 18px;
        font-weight: 400;
        color: #333333;
        line-height: 25px;
        margin-bottom: 20px;
        margin-left: 20px;
        margin-right: 20px;
      }
    }

    .codeUrl {
      display: flex;
      align-items: center;
      justify-content: center;
      margin-bottom: 24px;

      .codeUrlLink {
        width: 72px;
        height: 40px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 14px;
        font-weight: 400;
        color: #c7cbd2;
        line-height: 20px;
        border: 1px solid #c7cbd2;
        border-right: 0px solid #c7cbd2;
      }

      .codeUrlInp {
        width: 305px;
        height: 40px;
        border: 1px solid #c7cbd2;
      }

      .ant-input-disabled {
        background-color: rgba(0, 0, 0, 0) !important;
      }

      .ant-input[disabled] {
        background-color: rgba(0, 0, 0, 0) !important;
      }

      .codeUrlCopy {
        width: 96px;
        height: 40px;
        background-color: #4ea6ff;
        border-radius: 8px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 14px;
        font-weight: 400;
        color: #ffffff;
        line-height: 20px;
        cursor: pointer;
        margin-left: 8px;
      }
    }

    .qrm_footer {
      display: flex;
      align-items: center;
      justify-content: center;
      margin-bottom: 44px;

      .qrmbtn {
        width: 80px;
        height: 32px;
        display: flex;
        line-height: 32px;
        justify-content: center;
        border-radius: 4px;
        border: 1px solid #387df7;
        cursor: pointer;

        .btntext {
          color: #387df7;
        }
      }
    }
  }
}

.codeModal {
  .ant-modal {
    .ant-modal-content {
      width: 479px !important;
    }
  }
}
</style>
<style lang="scss">
.ant-modal-body {
  padding: 0 !important;
}
</style>