<template>
  <div class="upload-image-container">
    <div v-for="(image, index) in photos" :key="index" class="image-item">
      <el-image
        :src="image.url || image"
        fit="cover"
        style="width: 100px; height: 100px"
        :preview-src-list="[image.url || image]"
      >
        <div slot="error" class="image-slot">
          <i class="el-icon-picture-outline"></i>
        </div>
      </el-image>
      <i
        class="el-icon-delete remove-icon"
        @click="removeImage(index)"
      ></i>
    </div>
    <div
      v-if="photos.length < imageCount"
      class="upload-button"
      @click="triggerUpload"
    >
      <i class="el-icon-plus"></i>
    </div>
    <input
      type="file"
      ref="fileInput"
      accept="image/*"
      hidden
      @change="handleFileChange"
    />
  </div>
</template>

<script>
import { uploadImage } from '@/api/community/addCommunityPublicityApi'

export default {
  name: 'UploadImageUrl',
  props: {
    imageCount: {
      type: Number,
      default: 99
    },
    callBackListener: {
      type: String,
      required: true
    },
    callBackFunction: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      photos: [],
      photosUrl: []
    }
  },
  watch: {
    photosUrl: {
      deep: true,
      handler(newVal) {
        this.$emit(this.callBackFunction, newVal)
      }
    }
  },
  methods: {
    triggerUpload() {
      this.$refs.fileInput.click()
    },
    async handleFileChange(event) {
      const files = event.target.files
      if (!files || files.length === 0) return

      const file = files[0]
      if (file.size > 2 * 1024 * 1024) {
        this.$message.error(this.$t('uploadImage.fileSizeError'))
        return
      }

      try {
        // Preview image
        const reader = new FileReader()
        reader.onload = (e) => {
          this.photos.push(e.target.result)
        }
        reader.readAsDataURL(file)

        // Upload image
        const formData = new FormData()
        formData.append('uploadFile', file)
        const response = await uploadImage(formData)
        this.photosUrl.push(response.data)
      } catch (error) {
        this.$message.error(this.$t('uploadImage.uploadError'))
      } finally {
        event.target.value = ''
      }
    },
    removeImage(index) {
      this.photos.splice(index, 1)
      this.photosUrl.splice(index, 1)
    },
    clearImages() {
      this.photos = []
      this.photosUrl = []
    },
    setImages(images) {
      this.clearImages()
      images.forEach(image => {
        if (typeof image === 'string') {
          this.photos.push(image)
          this.photosUrl.push({ fileId: image, url: image })
        } else {
          this.photos.push(image.url)
          this.photosUrl.push(image)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.upload-image-container {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;

  .image-item {
    position: relative;
    margin-right: 10px;
    margin-bottom: 10px;

    .remove-icon {
      position: absolute;
      top: -10px;
      right: -10px;
      color: #f56c6c;
      background: #fff;
      border-radius: 50%;
      cursor: pointer;
      font-size: 16px;
      z-index: 1;

      &:hover {
        color: #f78989;
      }
    }
  }

  .upload-button {
    width: 100px;
    height: 100px;
    border: 1px dashed #dcdfe6;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: #909399;
    font-size: 24px;

    &:hover {
      border-color: #409eff;
      color: #409eff;
    }
  }

  .image-slot {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
    background: #f5f7fa;
    color: #909399;
    font-size: 30px;
  }
}
</style>