<template>
  <PageWrapper
    title="二维码组件"
    content="二维码组件使用示例，通常用于一些商品、促销活动、或者分享推广二维码展示"
  >
    <el-row class="mt-3" :gutter="10">
      <el-col :span="8">
        <el-card shadow="hover" size="small">
          <template #header>
            <div class="card-header">
              <span>基础实例</span>
            </div>
          </template>
          <div class="qrcode-space">
            <QrCode :value="qrCodeUrl" />
          </div>
        </el-card>
      </el-col>
      <el-col :span="8">
        <el-card shadow="hover" size="small">
          <template #header>
            <div class="card-header">
              <span>渲染成img标签示例</span>
            </div>
          </template>
          <div class="qrcode-space">
            <QrCode :value="qrCodeUrl" tag="img" />
          </div>
        </el-card>
      </el-col>
      <el-col :span="8">
        <el-card shadow="hover" size="small">
          <template #header>
            <div class="card-header">
              <span>配置样式示例</span>
            </div>
          </template>
          <div class="qrcode-space">
            <QrCode
              :value="qrCodeUrl"
              :options="{
                color: { dark: '#55D187' },
              }"
            />
          </div>
        </el-card>
      </el-col>
    </el-row>
    <el-row :gutter="10">
      <el-col :span="8">
        <el-card shadow="hover" size="small">
          <template #header>
            <div class="card-header">
              <span>本地logo示例</span>
            </div>
          </template>
          <div class="qrcode-space">
            <QrCode :value="qrCodeUrl" :logo="LogoImg" />
          </div>
        </el-card>
      </el-col>
      <el-col :span="8">
        <el-card shadow="hover" size="small">
          <template #header>
            <div class="card-header">
              <span>在线logo示例</span>
            </div>
          </template>
          <div class="qrcode-space">
            <QrCode
              :value="qrCodeUrl"
              logo="https://naive-ui-admin-docs.vercel.app/logo.png"
              :options="{
                color: { dark: '#55D187' },
              }"
            />
          </div>
        </el-card>
      </el-col>
      <el-col :span="8">
        <el-card shadow="hover" size="small">
          <template #header>
            <div class="card-header">
              <span>LOGO配置示例</span>
            </div>
          </template>
          <div class="qrcode-space">
            <QrCode
              :value="qrCodeUrl"
              :logo="{
                src: 'https://naive-ui-admin-docs.vercel.app/logo.png',
                logoSize: 0.2,
                borderSize: 0.05,
                borderRadius: 50,
                bgColor: 'blue',
              }"
            />
          </div>
        </el-card>
      </el-col>
    </el-row>
    <el-row :gutter="10">
      <el-col :span="8">
        <el-card shadow="hover" size="small">
          <template #header>
            <div class="card-header">
              <span>下载示例</span>
            </div>
          </template>
          <div class="qrcode-space">
            <QrCode :value="qrCodeUrl" ref="qrRef" :logo="LogoImg" />
          </div>
          <div class="mb-2 qrcode-space">
            <el-button type="primary" @click="download"> 下载 </el-button>
          </div>
          <div class="qrcode-space">
            <div class="msg">(在线logo会导致图片跨域，需要下载图片需要自行解决跨域问题)</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="8">
        <el-card shadow="hover" size="small">
          <template #header>
            <div class="card-header">
              <span>扩展绘制示例</span>
            </div>
          </template>
          <div class="qrcode-space">
            <QrCode
              :value="qrCodeUrl"
              :width="200"
              :options="{ margin: 5 }"
              ref="qrDiyRef"
              :logo="LogoImg"
              @done="onQrcodeDone"
            />
          </div>
          <div class="mb-2 qrcode-space">
            <el-button type="primary" @click="downloadDiy"> 下载 </el-button>
          </div>
          <div class="qrcode-space">
            <div class="msg">要进行扩展绘制则不能将tag设为img</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="8">
        <el-card shadow="hover" size="small">
          <template #header>
            <div class="card-header">
              <span>配置大小示例</span>
            </div>
          </template>
          <div class="qrcode-space">
            <QrCode :value="qrCodeUrl" :width="270" />
          </div>
        </el-card>
      </el-col>
    </el-row>
  </PageWrapper>
</template>

<script lang="ts" setup>
  import { ref, unref } from 'vue';
  import { QrCode, QrCodeActionType } from '@/components/Qrcode/index';
  import LogoImg from '@/assets/images/logo.png';
  const qrCodeUrl = 'https://www.naiveadmin.com';

  const qrRef = ref<Nullable<QrCodeActionType>>(null);
  const qrDiyRef = ref<Nullable<QrCodeActionType>>(null);

  function download() {
    const qrEl = unref(qrRef);
    if (!qrEl) return;
    qrEl.download('img');
  }

  function downloadDiy() {
    const qrEl = unref(qrDiyRef);
    if (!qrEl) return;
    qrEl.download('Qrcode');
  }

  function onQrcodeDone({ ctx }: any) {
    if (ctx instanceof CanvasRenderingContext2D) {
      // 额外绘制
      ctx.fillStyle = 'black';
      ctx.font = '16px "微软雅黑"';
      ctx.textBaseline = 'bottom';
      ctx.textAlign = 'center';
      ctx.fillText('扫描关注Star点一点', 100, 195, 200);
    }
  }
</script>

<style lang="scss" scoped>
  .el-row {
    margin-bottom: 10px;
  }
  .qrcode-space {
    display: flex;
    position: relative;
    bottom: 0;
    transition: bottom 0.4s;
    justify-content: center;
    &:hover {
      bottom: 6px;
    }
  }

  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
</style>
