<template>
  <div
    v-if="visible"
    ref="imagePreview"
    v-customDialogResizeImg="resizeKey"
    class="preview-custom-dialog"
    :class="[resizeKey]"
  >
    <div class="preview-head">
      <span>试卷预览</span>
      <div class="head-btn-box flex justify-center items-center" @click="closeDialog">
        <el-icon><close /></el-icon>
      </div>
    </div>
    <div class="preview-body">
      <div class="control-box">
        <el-icon :size="30" color="#eee" class="zoom-icon" @click="add"><zoom-in /></el-icon>
        <el-icon :size="30" color="#eee" class="zoom-icon" @click="minus"><zoom-out /></el-icon>
      </div>
      <img v-show="!!url" class="small-img" :src="url" alt="" />
    </div>
    <!-- <div class="preview-foot"></div> -->
  </div>
</template>

<script setup lang="ts" name="ImagePreview">
// import BaseDialog from '../element/BaseDialog.vue'
import useVModel from '@/hooks/useVModel'
import { ref, watch } from 'vue'
import { Close, ZoomIn, ZoomOut } from '@element-plus/icons-vue'
import { ElIcon } from 'element-plus'
import { localKeyMap } from '@/directives/customDialogResizeImg'

const imagePreview = ref()
const add = () => {
  const w = imagePreview.value?.clientWidth
  if (w > window.innerWidth) return
  if (w) {
    imagePreview.value.style.width =
      (Math.floor(w * 1.1) > window.innerWidth ? window.innerWidth : Math.floor(w * 1.1)) + 'px'
    let l = parseFloat(imagePreview.value?.style?.left)
    l && (imagePreview.value.style.left = (l - Math.floor(w * 0.05) < 0 ? 0 : l - Math.floor(w * 0.05)) + 'px')
    localStorage.setItem(
      localKeyMap.positions[props.resizeKey],
      JSON.stringify({
        left: l + 'px',
        top: imagePreview.value?.style?.top,
      })
    )
    localStorage.setItem(
      localKeyMap.resize[props.resizeKey],
      JSON.stringify({
        width: imagePreview.value.style.width || 'auto',
        height: imagePreview.value?.style?.height || 'auto',
      })
    )
  }
}
const minus = () => {
  const w = imagePreview.value?.clientWidth
  if (w <= 290) return
  if (w) {
    imagePreview.value.style.width = (Math.floor(w * 0.9) < 290 ? 290 : Math.floor(w * 0.9)) + 'px'
    let l = parseFloat(imagePreview.value?.style?.left)
    l && (imagePreview.value.style.left = l + Math.floor(w * 0.05) + 'px')
    localStorage.setItem(
      localKeyMap.positions[props.resizeKey],
      JSON.stringify({
        left: l + 'px',
        top: imagePreview.value?.style?.top,
      })
    )
    localStorage.setItem(
      localKeyMap.resize[props.resizeKey],
      JSON.stringify({
        width: imagePreview.value.style.width || 'auto',
        height: imagePreview.value?.style?.height || 'auto',
      })
    )
  }
}
const props = withDefaults(
  defineProps<{
    modelValue: boolean
    url?: string
    resizeKey?: string
  }>(),
  {
    modelValue: false,
    url: '',
    resizeKey: 'can-resize2',
  }
)
const visible = useVModel(props)
const closeDialog = () => {
  visible.value = false
}
watch(
  () => props.url,
  () => {
    if (visible.value) {
      visible.value = false
      setTimeout(() => {
        visible.value = true
      }, 1)
    }
  }
)
</script>

<style scoped lang="scss">
.preview-custom-dialog {
  display: flex;
  flex-direction: column;
  position: fixed;
  z-index: 500;
  border-radius: 6px;
  box-shadow: 0px 12px 32px 4px rgba(0, 0, 0, 0.04), 0px 8px 20px rgba(0, 0, 0, 0.08);
  max-width: 100vw;
  max-height: 100vh;
  overflow: auto;

  .preview-head {
    background-color: #f8f8f8;
    border-radius: 6px 6px 0 0;
    color: #333;
    font-size: 14px;
    height: 44px;
    line-height: 44px;
    padding: 0 10px;
    position: relative;
    .head-btn-box {
      position: absolute;
      right: 0;
      top: 0;
      width: 44px;
      height: 44px;
      z-index: 1;
      cursor: pointer;
      &:hover {
        :deep(i) {
          color: $color--primary;
        }
      }
    }
  }
  .preview-body {
    flex: 1;
    padding: 2px;
    overflow: auto;
    &:hover {
      .control-box {
        width: 42px;
      }
    }
    .control-box {
      transition: all 0.2s;
      overflow: hidden;
      width: 0;
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      align-items: center;
      position: absolute;
      right: 10px;
      top: 56px;
      z-index: 1;
      border-radius: 6px;
      background: rgba(0, 0, 0, 0.7);
      padding: 8px 0;
      height: 100px;
      .zoom-icon {
        cursor: pointer;
        &:hover {
          color: #fff;
        }
      }
    }
    img {
      width: 100%;
      height: auto;
    }
  }
}
</style>
