<template>
  <el-upload
      class="avatar-uploader"
      :action="state.action"
      :headers="state.headers"
      :show-file-list="false"
      :multiple="multiple"
      :on-success="handleAvatarSuccess"
      :on-error="handleAvatarError"
      :before-upload="beforeAvatarUpload"
  >
    <template v-if="multiple">
      <div v-for="(img,idx)  in showImageList" :key="idx" class="hp" style="display: block;">
        <SvgIcon name="ele-CircleCloseFilled" color="red" class="hc" style="position: absolute;top:2px;" @click.stop="handleRemove(idx)"></SvgIcon>
        <img :src="img" class="avatar"/>
      </div>

      <el-icon v-if="showImageList.length<max" class="avatar-uploader-icon">
        <Plus/>
      </el-icon>
    </template>
    <template v-else>
      <SvgIcon name="ele-Remove" class="hc"> </SvgIcon>
      <img v-if="showImageList[0]" :src="showImageList[0]" class="avatar"/>
      <el-icon v-else class="avatar-uploader-icon">
        <Plus/>
      </el-icon>
    </template>

  </el-upload>
</template>

<script lang="ts" setup>
import {computed, onMounted, reactive, ref, watch} from 'vue'
import type {UploadProps} from 'element-plus'
import {ElMessage} from 'element-plus'
import {Plus} from '@element-plus/icons-vue'
import {Session} from "/@/utils/storage";
import u from "/@/utils/u"
import {Msg} from "/@/utils/message";

const emit = defineEmits(['update:modelValue', 'on-change']);

const props = defineProps({
  modelValue: {
    type: String
  },
  multiple: {
    type: Boolean,
    default: false
  },
  max: {
    type: Number,
    default: 9
  },
  splitor: {
    type: String,
    default: ";"
  }
})


const typeOf = (obj: any) => {
  return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase();
}


const state = reactive({
  action: '',
  headers: {},
  imageList: [] as Array<string>
})

const imageUrl = ref('')

watch(() => props.modelValue, (nv, v) => {
  if (nv) {
    if (props.multiple) {
      if (Array.isArray(nv)) {
        state.imageList = nv;
      } else {
        state.imageList = nv.split(props.splitor);
      }
    } else {
      state.imageList = [nv]
    }
  } else {
    state.imageList = []
  }
  //console.log(state.imageList)
  // imageUrl.value = u.fmt.fmtUrl(o);
}, {immediate: true})


const handleRemove = (idx: number) => {
  state.imageList.splice(idx, 1);
  emitData()
}

const showImageList = computed(() => {
      return state.imageList.map((k: string) => u.fmt.fmtUrl(k))
    }
)

onMounted(() => {
  let url = import.meta.env.VITE_API_URL;
  if (!url) {
    url = `${location.origin}/admin/`;
  }
  state.action = `${url}/file/upload`
  state.headers = {"accessToken": Session.get("accessToken")}
})

const handleAvatarError = () => {
  Msg.hideLoading();
}

const handleAvatarSuccess: UploadProps['onSuccess'] = (
    response,
    uploadFile
) => {
  Msg.hideLoading();
  //console.log(uploadFile, response)
  let {url, uuid} = response.data;
  imageUrl.value = url;
  // imageUrl.value = URL.createObjectURL(uploadFile.raw!)
  if (props.multiple) {
    state.imageList.unshift(url)
  } else {
    state.imageList = [url]
  }
  emitData()
}

const emitData = () => {
  if (props.multiple) {
    emit("update:modelValue", state.imageList.join(props.splitor))
    emit("on-change", state.imageList.join(props.splitor))
  } else {
    if (state.imageList.length > 0) {
      emit("update:modelValue", state.imageList[0])
      emit("on-change", state.imageList[0])
    } else {
      emit("update:modelValue", null)
      emit("on-change", null)
    }
  }
}

const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
  if (rawFile.type !== 'image/jpeg' && rawFile.type !== 'image/png') {
    ElMessage.error('Avatar picture must be JPG format!')
    return false
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('Avatar picture size can not exceed 2MB!')
    return false
  }
  Msg.showLoading("上传中...")
  return true
}
</script>

<style scoped>
.avatar-uploader .avatar {
  width: 60px;
  height: 60px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 60px;
  height: 60px;
  text-align: center;
  margin-left: -5px;
}
</style>
