<template>
  <div class="poster-container">
    <div class="poster-content" ref="posterRef">
      <div class="header-section">
        <h1 class="brand-name">BitWiseWorld</h1>
        <div class="main-title">邀请好友<br />赚取更多福利</div>
      </div>
      <div class="illustration-section">
        <img
          src="@/assets/icon/bitcoin/yaoqing.svg"
          class="main-img"
          alt="Illustration" />
      </div>
      <div class="footer-info">
        <div class="info-left">
          <div class="label">邀请码</div>
          <div class="code">{{ inviteCode }}</div>
        </div>
        <div class="info-right">
          <img src="@/assets/icon/bitcoin/erweima.svg" class="qr-img" alt="QR Code" />
        </div>
      </div>
      <div class="bg-pattern"></div>
    </div>
    <div class="action-area">
      <button class="save-btn" @click="handleSave">保存图片</button>
    </div>
  </div>
</template>

<script setup>
  import { ref, onMounted } from "vue";
  import { useRoute } from "vue-router";
  import html2canvas from "html2canvas";

  const route = useRoute();
  const posterRef = ref(null);
  const inviteCode = ref();

  const handleSave = async () => {
    if (!posterRef.value) return;
    try {
      const canvas = await html2canvas(posterRef.value, {
        useCORS: true,
        scale: 3, // 稍微提高一点清晰度
        backgroundColor: "#D91E2D",
      });
      const imgUrl = canvas.toDataURL("image/png");

      const link = document.createElement("a");
      link.href = imgUrl;
      link.download = "invite_poster.png";
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    } catch (error) {
      console.error(error);
    }
  };

  onMounted(() => {
    inviteCode.value = route.params.code;
  });
</script>

<style scoped>
  /* 页面容器：红色背景铺满全屏 */
  .poster-container {
    min-height: 100vh;
    background-color: #d91e2d;
    display: flex;
    flex-direction: column;
    /* 如果内容很少，可以加这个让内容整体垂直居中，或者去掉它让内容顶对齐 */
    /* justify-content: center; */
  }

  /* 核心海报内容区：这里决定了截图的高度 */
  .poster-content {
    position: relative;
    background-color: #d91e2d; /* 截图需要背景色 */
    padding: 30px 24px 10px; /* 减少底部 padding */
    overflow: hidden; /* 防止装饰溢出 */
  }

  .header-section {
    position: relative;
    z-index: 1;
    margin-bottom: 20px; /* 减小间距 */
  }
  .brand-name {
    font-size: 18px;
    font-weight: 800;
    color: #fff;
    opacity: 0.9;
    margin-bottom: 10px;
  }
  .main-title {
    font-size: 32px;
    font-weight: 600;
    color: #fff;
    line-height: 1.2;
  }

  /* 插画区：关键修改 */
  .illustration-section {
    position: relative;
    z-index: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 10px 0 20px; /* 上下留一点紧凑的间距 */
  }

  .main-img {
    width: 100%;
    max-width: 280px; /* 限制最大宽度 */
    height: auto;
    /* 如果图还是很长，可以强制限制高度，用 object-fit 保持比例 */
    max-height: 320px;
    object-fit: contain;
  }

  /* 底部信息区 */
  .footer-info {
    position: relative;
    z-index: 1;
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    padding-bottom: 20px;
  }
  .label {
    font-size: 14px;
    color: rgba(255, 255, 255, 0.8);
    margin-bottom: 4px;
  }
  .code {
    font-size: 20px;
    font-weight: 700;
    color: #fff;
    letter-spacing: 0.5px;
  }
  .qr-img {
    width: 72px;
    height: 72px;
    background: #fff;
    padding: 3px;
    border-radius: 4px;
  }

  /* 背景装饰 */
  .bg-pattern {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-image: radial-gradient(rgba(255, 255, 255, 0.1) 2px, transparent 2px);
    background-size: 18px 18px;
    pointer-events: none;
    z-index: 0;
  }

  /* 底部按钮区：脱离海报流，固定在底部或者紧跟内容 */
  .action-area {
    padding: 0px 24px 30px;
    margin-top: auto; /* 把按钮推到屏幕最底部，如果不想推到底部，去掉这行 */
  }

  .save-btn {
    width: 100%;
    height: 48px;
    background: #fff;
    color: #d91e2d;
    font-size: 16px;
    font-weight: 600;
    border: none;
    border-radius: 24px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  }
</style>
