<template>
  <div>
    <img :src="src" :alt="alt" @click="open()" :width="width" :height="height" title="点击查看图片" :id="'vc-imgself-img-'+attach">
    <div class="full-img" v-if="show"  >
      <div :id="'fullimage_'+imgid" style="position: absolute">
        <img :src="src" alt="" class="img-state" :alt="alt || ''">
      </div>
      <div class="btns_new">
        <!--        <button type="button" name="button" class="btn btn-primary" @click="enlarge()" :id="'vc-imgself-enlarge-'+attach">放大</button>
                <button type="button" name="button" class="btn btn-primary" @click="reduce()" :id="'vc-imgself-reduce-'+attach">缩小</button>-->
        <button type="button" name="button" class="btn btn-primary"  @touchstart="leftRevolve()" @click="leftRevolve()" :id="'vc-imgself-leftRevolve-'+attach">左旋</button>
        <button type="button" name="button" class="btn btn-primary"  @touchstart="rightRevolve()" @click="rightRevolve()" :id="'vc-imgself-rightRevolve-'+attach">右旋</button>
        <button type="button" name="button" class="btn btn-primary"  @touchstart="close()" @click="close()" :id="'vc-imgself-close-'+attach">关闭</button>
      </div>
    </div>
  </div>
</template>

<script>
import $ from 'jquery'
import Panzoom from 'panzoom';
export default {
  // props: ['src', 'width', 'height', 'alt'],
  props: {
    src: {
      type: String
    },
    width: {
      default: 60
    },
    height: {
      default: 60
    },
    alt: {
      default: '图片加载失败'
    },
    attach: {
      type: String,
      default: 'name'
    },
    imgid: {
      type: String,
      default: null
    }
  },
  data () {
    return {
      show: false,
      deg: 0,
      power: 1
    }
  },
  methods: {
    open () {
      this.deg = 0
      this.power = 0.5
      this.show = true
      // 获取容器元素和图片元素
      console.log(this.$refs)
      this.$nextTick(()=>{
        const container = document.getElementById('fullimage_'+this.imgid)
        // 初始化 Panzoom
        this.panzoom = Panzoom(container, {
          contain: 'outside',
          minScale: 1,
          maxScale: 3
        })
        // 添加事件监听器，使图片可以缩放和平移
        container.addEventListener('wheel', this.panzoom.zoomWithWheel);
        container.addEventListener('mousedown', this.panzoom.startPan);
        container.addEventListener('touchstart', this.panzoom.startPan);
      })
    },
    close () {
      this.show = false
    },
    enlarge () {
      console.log('放大')
      this.power = 2*this.power
      this.imgState()
    },
    reduce () {
      console.log('缩小')
      this.power = 0.5*this.power
      this.imgState()
    },
    leftRevolve () {
      console.log('左旋转')
      this.deg -= 90
      this.imgState()
    },
    rightRevolve () {
      console.log('右旋转')
      this.deg += 90
      this.imgState()
    },
    imgState () {
      console.log('调取总函数')
      $('.img-state').css({
        'transform': 'rotate(' + this.deg + 'deg) scale(' + this.power + ' ,' + this.power + ')'
      })
    }
  }
}
</script>
<style media="screen" scoped>
.full-img {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 20;
  background-color: white;
  display: flex;
  overflow: auto;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  color: #fff;
}
.btns_new {
  position: fixed;
  right: 10px;
  height: 100%;
  z-index: 10;
  display: flex;
  height: 100%;
  flex-direction: column;
  justify-content: space-evenly;
}
.img-state {

}
</style>
