<template>
  <d-upload
    ref="uploadRef"
    :file-list="fileList"
    :multiple="false"
    accept="image/*"
    :upload-options="uploadOptions"
    :on-success="onSuccess"
  >
    <div class="container">
      <i class="icon-add_image"></i>
      <span>{{ $t("appendix") }}</span>
    </div>
  </d-upload>
</template>

<script setup lang="ts">
import { ref } from "vue";

import { eventBus } from "@/utils/event-bus";

const fileList = ref([]);
const uploadRef = ref();

const uploadOptions = ref({
  uri: "/api/upload_oss",
});

const onSuccess = (result) => {
  eventBus.emit("upload-success", {
    url: JSON.parse(result[0].response).data.url,
  });
  console.log("图片上传成功！URL：",JSON.parse(result[0].response).data.url);
};
</script>

<style scoped lang="scss">
@import "devui-theme/styles-var/devui-var.scss";

.container {
  display: flex;
  gap: 4px;
  align-items: center;
  height: 30px;
  color: $devui-text;
  border-radius: 4px;
  padding: 6px;
  cursor: pointer;

  span {
    font-size: $devui-font-size-sm;
  }

  &:hover {
    background-color: var(--devui-icon-hover-bg);
  }
}

@media screen and (max-width: 520px) {
  .container span {
    display: none;
  }
}
</style>
