<template>
  <div class="comp-content">
    <div class="content">
      <div v-if="imgUrl" class="preview">
        <el-image class="img" :src="imgUrl" fit="cover" />
        <div v-if="!disabled" class="cover">
          <div class="icon-btns">
            <el-icon :size="20" class="cover-icon"><View /></el-icon>
            <el-icon :size="20" class="cover-icon" @click="delFun"><Delete /></el-icon>
          </div>
        </div>
      </div>
      <div v-else class="upload">
        <el-upload
          :show-file-list="false"
          :before-upload="beforeUploadFun"
        >
          <div v-if="!uploading" class="add">
            <el-icon :size="20" class="add-icon"><Plus /></el-icon>
          </div>
          <div v-else class="progress">
            <el-progress type="circle" :percentage="percentage" :width="120" />
          </div>
        </el-upload>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue';
import { dataUploadAvatarApi } from '@/api/data/data';
import { Plus, View, Delete } from '@element-plus/icons-vue';
import { ElMessage } from 'element-plus';

interface Props {
  modelValue: any;
  disabled?: boolean;
}

const props = withDefaults(defineProps<Props>(), {
  modelValue: '',
  disabled: false,
});

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

const env = import.meta.env;
const uploading = ref(false);
const percentage = ref(0);
const imgUrl = ref('');

watch(() => props.modelValue, (val: any) => {
  if (val) {
    imgUrl.value = `${env.VITE_API_IMAGE_PREVIEW_URL}/data/previewImage?fileId=${val}`;
  } else {
    imgUrl.value = '';
  }
}, { deep: true, immediate: true });

const beforeUploadFun = (file: any) => {
  uploading.value = true;
  const params = {
    avatar: file,
  };
  dataUploadAvatarApi(params, (event: any) => {
    const { loaded, total } = event;
    const percent = (loaded / total) * 100;
    percentage.value = Number(percent.toFixed(2));
  }).then((res: any) => {
    if (res.code === 200) {
      const avatarId = res.data.avatarId;
      updateFun(avatarId);
      imgUrl.value = `${env.VITE_API_IMAGE_PREVIEW_URL}/data/previewImage?fileId=${avatarId}`;
      ElMessage.success('上传成功');
    }
  }).finally(() => {
    uploading.value = false;
    percentage.value = 0;
  });
  return false;
};

const delFun = () => {
  imgUrl.value = '';
  updateFun('');
};

const updateFun = (data: any) => {
  emit('change', data);
  emit('update:modelValue', data);
};
</script>

<style lang="scss" scoped>
.comp-content {
  width: 122px !important;
  height: 122px !important;
  .content {
    width: 100%;
    height: 100%;
    border: dashed 1px var(--el-border-color-light);
    border-radius: var(--border-radius-normal);
    overflow: hidden;
    .preview {
      width: 120px;
      height: 120px;
      position: relative;
      &:hover {
        .cover {
          display: flex;
        }
      }
      .img {
        width: 100%;
        height: 100%;
      }
      .cover {
        display: none;
        position: absolute;
        align-items: center;
        justify-content: center;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        width: 100%;
        height: 100%;
        background-color: rgba(0, 0, 0, 0.6);
        .icon-btns {
          display: flex;
          align-items: center;
          justify-content: center;
          width: 100%;
          .cover-icon {
            color: #fff;
            margin: 0 var(--margin-small);
            cursor: pointer;
          }
        }
      }
    }
    .upload {
      .add {
        width: 120px;
        height: 120px;
        display: flex;
        align-items: center;
        justify-content: center;
        .add-icon {
          color: var(--el-text-color-secondary);
        }
      }
      .progress {
        width: 100%;
        height: 100%;
      }
    }
  }
}
</style>
