<template>
  <div :class="[prefixCls, { fullscreen }]">
    <Upload
      name="file"
      multiple
      @change="handleChange"
      :action="uploadUrl"
      :showUploadList="false"
      :data="getBizData()"
      :headers="getheader()"
      accept=".jpg,.jpeg,.gif,.png,.webp"
    >
      <a-button type="primary" v-bind="{ ...getButtonProps }">
        <% if (enableTranslation) { %>{{ t('component.upload.imgUpload') }}<% } else { %>t$t('component.upload.imgUpload')$t$<% } %>
      </a-button>
    </Upload>
  </div>
</template>
<script lang="ts">
  import { defineComponent, computed } from 'vue';

  import { Upload } from 'ant-design-vue';
  import { useDesign } from '@/hooks/web/useDesign';
  import { useGlobSetting } from '@/hooks/setting';
  <%_ if (enableTranslation) { _%>
  import { useI18n } from '@/hooks/web/useI18n';
  <%_ } _%>
  import { getToken } from '@/utils/auth';
  import { getFileAccessHttpUrl } from '@/utils/common/compUtils';
  import uploadImageService from '@/api-service/files/upload-image.service';

  export default defineComponent({
    name: 'TinymceImageUpload',
    components: { Upload },
    props: {
      fullscreen: {
        type: Boolean,
      },
      disabled: {
        type: Boolean,
        default: false,
      },
    },
    emits: ['uploading', 'done', 'error'],
    setup(props, { emit }) {
      let uploading = false;

      function getheader() {
        return { Authorization: `Bearer ${getToken()}` };
      }

      function getBizData() {
        return {
          biz: 'jeditor',
          jeditor: '1',
        };
      }

      const { domainUrl } = useGlobSetting();
      const uploadUrl = domainUrl + uploadImageService.uploadImageUrl;

      <%_ if (enableTranslation) { _%>
      const { t } = useI18n();
      <%_ } _%>
      const { prefixCls } = useDesign('tinymce-img-upload');

      const getButtonProps = computed(() => {
        const { disabled } = props;
        return {
          disabled,
        };
      });

      function handleChange(info: Record<string, any>) {
        const file = info.file;
        const status = file?.status;
        // const url = file?.response?.url;
        const name = file?.name;

        if (status === 'uploading') {
          if (!uploading) {
            emit('uploading', name);
            uploading = true;
          }
        } else if (status === 'done') {
          let realUrl = getFileAccessHttpUrl(file.response.message);
          emit('done', name, realUrl);
          uploading = false;
        } else if (status === 'error') {
          emit('error');
          uploading = false;
        }
      }

      return {
        <%_ if (enableTranslation) { _%>
        t,
        <%_ } _%>
        prefixCls,
        handleChange,
        uploadUrl,
        getheader,
        getBizData,
        getButtonProps,
      };
    },
  });
</script>
<style lang="less" scoped>
  @prefix-cls: ~'@{namespace}-tinymce-img-upload';

  .@{prefix-cls} {
    position: absolute;
    top: 4px;
    right: 10px;
    z-index: 20;

    &.fullscreen {
      position: fixed;
      z-index: 10000;
    }
  }
</style>
