<template>
  <div class="uni-image-preview-mask" :class="{ 'uni-image-preview-show': show }">
    <div class="uni-image-preview-content">
      <div class="uni-image-preview-header">
        <span>{{ current + 1 }}/{{ urls.length }}</span>
        <i class="uni-icon-close" @click="close"></i>
      </div>
      <swiper class="uni-image-preview-swiper" :current="current" @change="onChange">
        <swiper-item v-for="(item, index) in urls" :key="index" class="uni-image-preview-swiper-item">
          <img :src="item" mode="aspectFit" class="uni-image-preview-img" @longpress="$emit('longpress')" @click="$emit('clickImage', urls[current])"/>
        </swiper-item>
      </swiper>
    </div>
  </div>
</template>

<script>
export default {
  name: 'uniImagePreview',
  props: {
    urls: {
      type: Array,
      default() {
        return [];
      },
    },
    current: {
      type: Number,
      default: 0,
    },
    show: {
      type: Boolean,
      default: false,
    },
  },
  methods: {
    close() {
      this.$emit('update:show', false);
      this.$emit('close');
    },
    onChange(event) {
      this.$emit('update:current', event.detail.current);
    },
  },
};
</script>

<style>
.uni-image-preview-mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.7);
  z-index: 9999;
  opacity: 0;
  visibility: hidden;
  transition: all 0.3s ease-in-out;
}

.uni-image-preview-show {
  opacity: 1;
  visibility: visible;
}

.uni-image-preview-content {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  border-radius: 2px;
  overflow: hidden;
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
}

.uni-image-preview-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px;
  color: #fff;
  background-color: rgba(0, 0, 0, 0.7);
  font-size: 14px;
}

.uni-icon-close {
  font-size: 20px;
}

.uni-image-preview-swiper {
  width: 100vw;
  height: 100vh;
}

.uni-image-preview-img {
  max-width: 100%;
  max-height: 100%;
}
</style>