<template>
  <div class="image-gallery-container">
    <div
      v-if="images.length > 1"
      class="nav-arrow left-arrow"
      :class="{ disabled: currentIndex === 0 }"
      @click="previousImage"
    >
      <el-icon><ArrowLeftBold /></el-icon>
    </div>

    <div class="image-stage" @dblclick="handleOpenViewer">
      <img v-if="currentImage" :src="currentImage" alt="" />
      <div v-else>无图片</div>
    </div>

    <div
      v-if="images.length > 1"
      class="nav-arrow right-arrow"
      :class="{ disabled: currentIndex === images.length - 1 }"
      @click="nextImage"
    >
      <el-icon><ArrowRightBold /></el-icon>
    </div>

    <div v-if="images.length > 1" class="image-counter"> {{ currentIndex + 1 }} / {{ images.length }} </div>
  </div>
  <el-image-viewer
    v-if="showImageViewer"
    :url-list="images"
    :initial-index="currentIndex"
    @close="showImageViewer = false"
  />
</template>

<script lang="ts" setup>
import { ref, computed, watch } from "vue";
import { ElImageViewer, ElIcon } from "element-plus";
import { ArrowLeftBold, ArrowRightBold } from "@element-plus/icons-vue";

interface Props {
  images: string[];
  initialIndex?: number;
}

const props = withDefaults(defineProps<Props>(), {
  initialIndex: 0,
});

const currentIndex = ref(props.initialIndex);
const showImageViewer = ref(false);

const handleOpenViewer = () => {
  showImageViewer.value = true;
};

const currentImage = computed(() => {
  if (props.images.length === 0) return "";
  return props.images[currentIndex.value] || props.images[0];
});

const previousImage = () => {
  if (currentIndex.value > 0) {
    currentIndex.value--;
  }
};

const nextImage = () => {
  if (currentIndex.value < props.images.length - 1) {
    currentIndex.value++;
  }
};

watch(
  () => props.images,
  () => {
    currentIndex.value = 0;
  },
);

watch(
  () => props.initialIndex,
  (newIndex) => {
    if (newIndex >= 0 && newIndex < props.images.length) {
      currentIndex.value = newIndex;
    }
  },
);
</script>

<style lang="scss" scoped>
.image-gallery-container {
  width: 100%;
  height: 100%;
  background-color: #000;
  position: relative;
}

.image-stage {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;

  img {
    max-width: 100%;
    max-height: 100%;
  }
}

.nav-arrow {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 40px;
  height: 40px;
  background-color: rgba(255, 255, 255, 0.5);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s ease;
  color: #ffffff;
  z-index: 10;

  &:hover:not(.disabled) {
    background-color: rgba(24, 144, 255, 0.8);
    transform: translateY(-50%) scale(1.1);
  }

  &.disabled {
    opacity: 0.3;
    cursor: not-allowed;
    transform: translateY(-50%) scale(1);
  }

  &.left-arrow {
    left: 20px;
  }

  &.right-arrow {
    right: 20px;
  }

  .el-icon {
    font-size: 18px;
  }
}

.image-counter {
  position: absolute;
  left: 50%;
  bottom: 10px;
  transform: translateX(-50%);
  text-align: center;
  padding: 12px;
  background-color: #333;
  border-radius: 6px;
  font-size: 14px;
  color: #ccc;
}
</style>
