<!--
 * @Author       : xiaolin
 * @Date         : 2022-09-21 18:43:25
 * @LastEditors  : xiaolin
 * @LastEditTime : 2022-09-23 14:05:05
 * @Description  : 图片组件
 * @FilePath     : \icpx-studio\src\views\platform\user\component\CompanyAuthImage.vue
-->
<template>
  <div class="company-info__image">
    <div class="company-info__image__name" v-if="name">{{ name }}</div>
    <div>
      <h-upload
        v-model:file-list="fileList"
        list-type="picture-card"
        :headers="headers"
        :show-upload-list="false"
        accept=".png, .jpg, .jpeg"
        action="/api/studio-file/pdproject/upload"
        :before-upload="beforeUpload"
        @change="handleChange"
      >
        <img v-if="url" :src="getImage(url)" />
        <div v-else>
          <h-icon v-if="loading" name="hoteam-refresh-left"></h-icon>
          <h-icon v-else name="hoteam-plus"></h-icon>
          <div class="ant-upload-text">上传</div>
        </div>
      </h-upload>
    </div>
  </div>
</template>

<script lang="ts">
import { message as HMessage, Icon as HIcon } from '@crami/ui';
import { defineComponent, ref } from 'vue';
import type { IUploadChangeParam, IUploadProps } from '@crami/ui-types';
import { useStore } from 'vuex';

export default defineComponent({
  name: 'CompanyAuthImage',
  components: {
    HIcon,
  },
  props: {
    name: {
      type: String,
      default: '',
    },
    url: {
      type: String,
      default: '',
    },
  },
  emits: ['update:url'],
  setup(props, { emit }) {
    // headers
    const store = useStore();
    const token = store.state?.user?.token;
    const headers = {
      Authorization: `${token.tokenType} ${token.token}`,
    };
    // 图片地址
    const getImage = fileName => {
      return `/api/studio-file/pdproject/download/${fileName}`;
    };

    const fileList = ref([]);
    const loading = ref<boolean>(false);

    const handleChange = (info: IUploadChangeParam) => {
      if (info.file.status === 'uploading') {
        loading.value = true;
        return;
      }
      if (info.file.status === 'done') {
        // Get this url from response in real world.
        const _list = info.fileList.map(item => item.dirPath || item.response.data.dirPath);
        emit('update:url', _list[0]);
      }
      if (info.file.status === 'error') {
        loading.value = false;
        HMessage.error('upload error');
      }
    };

    //不能超过2MB
    const beforeUpload = (file: IUploadProps['fileList'][number]) => {
      const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
      if (!isJpgOrPng) {
        HMessage.error('只能上传jpg或png文件');
      }
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        HMessage.error('图片不能超过 2MB!');
      }
      return isJpgOrPng && isLt2M;
    };

    return {
      fileList,
      headers,
      loading,
      handleChange,
      beforeUpload,
      getImage,
    };
  },
});
</script>

<style scoped lang="less">
@import './CompanyAuthImage.less';
</style>
