<!--
 * @Description: 
 * @Author: Format-qi 283810417@qq.com
 * @Date: 2022-06-29 17:23:15
 * @LastEditors: Format-qi jsq15517589670@163.com
 * @LastEditTime: 2022-12-16 13:41:56
-->
<template>
  <div :class="[prefixCls, { fullscreen }]">
    <Upload
      name="file"
      multiple
      @change="handleChange"
      :action="uploadUrl"
      :showUploadList="false"
      :headers="headers"
      accept=".jpg,.jpeg,.gif,.png,.webp"
    >
      <BzButton type="primary" v-bind="{ ...getButtonProps }"> 图片上传 </BzButton>
    </Upload>
  </div>
</template>
<script lang="ts">
  import { defineComponent, computed } from 'vue';

  import { Upload } from 'ant-design-vue';
  import { useDesign } from '/@/hooks/web/useDesign';
  import { BzButton } from '/@/components/BzButton';
  export default defineComponent({
    name: 'TinymceImageUpload',
    components: { Upload, BzButton },
    props: {
      uploadUrl: {
        type: String,
        default: '',
      },
      headers: {
        type: Object,
        default: () => {},
      },
      fullscreen: {
        type: Boolean,
      },
      disabled: {
        type: Boolean,
        default: false,
      },
      responseKey: {
        type: String,
        default: 'url',
      },
    },
    emits: ['uploading', 'done', 'error'],
    setup(props, { emit }) {
      let uploading = false;
      const { prefixCls } = useDesign('tinymce-img-upload');

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

      function handleChange(info: Recordable) {
        const file = info.file;
        const status = file?.status;
        const url = file?.response?.[props.responseKey];
        console.log(file?.response);
        const name = file?.name;

        if (status === 'uploading') {
          if (!uploading) {
            emit('uploading', name);
            uploading = true;
          }
        } else if (status === 'done') {
          emit('done', name, url);
          uploading = false;
        } else if (status === 'error') {
          emit('error');
          uploading = false;
        }
      }

      return {
        prefixCls,
        handleChange,
        getButtonProps,
      };
    },
  });
</script>
