<template>
  <Modal :visible="visible" :width="800" :closeButton="true" @update:visible="val => emit('update:visible', val)">
    <div class="shot-wrap">
      <img v-if="imageUrl" :src="imageUrl" alt="截图作业" class="shot-img" @click="openPreview" />
      <div v-else class="shot-empty">暂无图片</div>
    </div>
  </Modal>

  <!-- 预览放大（带缩放/拖拽/旋转/工具栏） -->
  <Teleport to="body">
    <div v-if="previewVisible" class="image-preview" @click.self="closePreview" @wheel.prevent="onWheel">
      <div class="image-preview__toolbar">
        <button @click.stop="zoomOut">－</button>
        <button @click.stop="zoomIn">＋</button>
        <button @click.stop="resetTransform">重置</button>
        <button @click.stop="rotateLeft">⟲</button>
        <button @click.stop="rotateRight">⟳</button>
        <button @click.stop="toggleFit">{{ fitMode ? '实际大小' : '适应屏幕' }}</button>
        <button @click.stop="closePreview">关闭</button>
      </div>
      <div class="image-preview__stage"
           @mousedown="onDragStart"
           @mousemove="onDragMove"
           @mouseup="onDragEnd"
           @mouseleave="onDragEnd"
           @dblclick.stop="toggleZoom">
        <img :src="imageUrl" alt="预览" class="image-preview__img"
             :style="imgStyle" draggable="false" />
      </div>
    </div>
  </Teleport>
</template>

<script lang="ts" setup>
import { computed, ref } from 'vue'
import Modal from '@/components/Modal.vue'

const props = defineProps<{
  visible: boolean
  work: any
}>()

const emit = defineEmits<{
  (e: 'update:visible', v: boolean): void
}>()

const visible = computed({
  get: () => props.visible,
  set: (v: boolean) => emit('update:visible', v)
})

const imageUrl = computed(() => {
  const raw = props.work?.content
  if (!raw) return ''
  try {
    const obj = typeof raw === 'string' ? JSON.parse(raw) : raw
    return obj?.url || obj?.image || obj?.src || ''
  }
  catch {
    return String(raw)
  }
})

const previewVisible = ref(false)
const scale = ref(1)
const rotate = ref(0)
const offsetX = ref(0)
const offsetY = ref(0)
const dragging = ref(false)
const lastX = ref(0)
const lastY = ref(0)
const fitMode = ref(true)

const imgStyle = computed(() => {
  const t = `translate(${offsetX.value}px, ${offsetY.value}px) rotate(${rotate.value}deg) scale(${scale.value})`
  return {
    transform: t
  }
})

const openPreview = () => {
  previewVisible.value = true
  nextTickFit()
}
const closePreview = () => {
  previewVisible.value = false
}

const zoomStep = 0.2
const minScale = 0.2
const maxScale = 6

const zoomIn = () => {
  fitMode.value = false; scale.value = Math.min(maxScale, +(scale.value + zoomStep).toFixed(2)) 
}
const zoomOut = () => {
  fitMode.value = false; scale.value = Math.max(minScale, +(scale.value - zoomStep).toFixed(2)) 
}
const resetTransform = () => {
  scale.value = 1; rotate.value = 0; offsetX.value = 0; offsetY.value = 0; fitMode.value = true; nextTickFit() 
}
const rotateLeft = () => {
  rotate.value = (rotate.value - 90) % 360 
}
const rotateRight = () => {
  rotate.value = (rotate.value + 90) % 360 
}

const toggleZoom = () => {
  fitMode.value = false
  scale.value = scale.value >= 1.8 ? 1 : 2
}

const toggleFit = () => {
  fitMode.value = !fitMode.value
  nextTickFit()
}

const nextTickFit = () => {
  // 适应屏幕时复位位置与缩放
  if (fitMode.value) {
    scale.value = 1
    offsetX.value = 0
    offsetY.value = 0
  }
}

const onWheel = (e: WheelEvent) => {
  if (e.deltaY > 0) zoomOut()
  else zoomIn()
}

const onDragStart = (e: MouseEvent) => {
  dragging.value = true
  lastX.value = e.clientX
  lastY.value = e.clientY
}
const onDragMove = (e: MouseEvent) => {
  if (!dragging.value) return
  const dx = e.clientX - lastX.value
  const dy = e.clientY - lastY.value
  lastX.value = e.clientX
  lastY.value = e.clientY
  offsetX.value += dx
  offsetY.value += dy
}
const onDragEnd = () => {
  dragging.value = false 
}
</script>

<style scoped>
.shot-wrap {
  max-height: 70vh;
  overflow: auto;
}
.shot-img {
  display: block;
  max-width: 100%;
  height: auto;
  border-radius: 6px;
  cursor: zoom-in;
}
.shot-empty {
  color: #999;
  font-size: 14px;
}

.image-preview {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.85);
  display: flex;
  flex-direction: column;
  z-index: 6000;
}
.image-preview__toolbar {
  display: flex;
  gap: 8px;
  padding: 10px;
  justify-content: center;
  z-index: 9999;
}
.image-preview__toolbar button {
  padding: 8px 12px;
  border: none;
  background: linear-gradient(180deg, #3a8bff 0%, #2f80ed 100%);
  color: #fff;
  border-radius: 10px;
  cursor: pointer;
  transition: transform .15s ease, box-shadow .2s ease, background .2s ease;
  box-shadow: 0 2px 8px rgba(47,128,237,.3);
}
.image-preview__toolbar button:hover {
  transform: translateY(-1px);
  box-shadow: 0 6px 16px rgba(47,128,237,.35);
}
.image-preview__toolbar button:active {
  transform: translateY(0);
  box-shadow: 0 2px 8px rgba(47,128,237,.28);
  background: linear-gradient(180deg, #2f80ed 0%, #1b6dde 100%);
}
.image-preview__toolbar button:focus-visible {
  outline: 2px solid rgba(47,128,237,.6);
  outline-offset: 2px;
}
.image-preview__stage {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: grab;
}
.image-preview__stage:active { cursor: grabbing; }
.image-preview__img {
  max-width: 92vw;
  max-height: 92vh;
  border-radius: 8px;
  box-shadow: 0 10px 30px rgba(0,0,0,.3);
  user-select: none;
  will-change: transform;
}
</style> 