<template>
  <div class="preview-container">
    <div class="text-center mb-2">
      <h2>{{ title || "文档预览" }}</h2>
      <p style="color: var(--text-muted); margin-top: 0.5rem;">当前展示前 2 页。</p>
    </div>

    <!-- The PDF Viewer for the first two pages -->
    <div class="pdf-render-wrapper">
      <VuePdfEmbed
        v-if="url"
        :source="url"
        class="pdf-viewer"
        @rendered="onRendered"
      />
      <div v-if="!rendered" class="loading-state text-center" style="padding: 3rem;">
        正在安全地解析文档...
      </div>
    </div>

    <!-- The Blurred Placeholder (Only shown after PDF renders) -->
    <div v-if="rendered" class="blur-placeholder pointer" @click="showModal = true">
      <div class="blur-content text-center">
        <h3 style="font-size: 1.8rem; margin-bottom: 0.5rem; text-shadow: 0 2px 10px rgba(0,0,0,0.8);">完整文档已被锁定</h3>
        <p style="text-shadow: 0 1px 5px rgba(0,0,0,0.8); margin-bottom: 1.5rem;">请联系我们获取完整文档。</p>
        <button class="btn btn-accent" style="font-size: 1.1rem; padding: 1rem 2rem;">立即联系我们</button>
      </div>
    </div>

    <!-- Modal with Iframe -->
    <div v-if="showModal" class="modal-overlay" @click.self="showModal = false">
      <div class="modal-content">
        <div class="modal-header">
          <span class="modal-title">获取完整文档</span>
          <button class="modal-close" @click="showModal = false">&times;</button>
        </div>
        <div class="modal-body">
          <iframe 
            src="https://www.wingsemitech.com/form-only/" 
            class="modal-iframe"
            title="联系表单"
          ></iframe>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import VuePdfEmbed from 'vue-pdf-embed';

const props = defineProps({
  url: String,
  title: String
});

const showModal = ref(false);
const rendered = ref(false);

const onRendered = () => {
  rendered.value = true;
};
</script>

<style scoped>
.preview-container {
  max-width: 900px;
  margin: 0 auto;
}

.pdf-render-wrapper {
  background: white; /* PDF viewer should have a white background so text is readable */
  border-radius: 8px 8px 0 0;
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0,0,0,0.3);
  margin-bottom: 0;
}

.pdf-viewer {
  width: 100%;
  display: block;
}

/* Forcing canvas elements to look nice */
:deep(.vue-pdf-embed canvas) {
  margin-bottom: 10px;
  box-shadow: 0 5px 15px rgba(0,0,0,0.1);
}

.blur-placeholder {
  background: linear-gradient(to bottom, rgba(255,255,255,1) 0%, rgba(15,23,42,0.8) 100%);
  position: relative;
  height: 400px;
  border-radius: 0 0 16px 16px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 10px 30px rgba(0,0,0,0.5);
  margin-top: -20px; /* pull up slightly to overlap with the pdf */
  z-index: 10;
  transition: transform 0.3s ease;
}

.blur-placeholder::before {
  content: "";
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  background-image: 
    repeating-linear-gradient(0deg, transparent, transparent 20px, rgba(0,0,0,0.05) 20px, rgba(0,0,0,0.05) 40px),
    repeating-linear-gradient(90deg, transparent, transparent 20px, rgba(0,0,0,0.05) 20px, rgba(0,0,0,0.05) 40px);
  backdrop-filter: blur(8px);
  z-index: -1;
}

.blur-placeholder:hover {
  transform: translateY(5px);
}

.blur-content {
  z-index: 1;
}
</style>
