<template>
  <div class="upload-file">
    <el-upload
      class="upload-demo"
      action="#"
      v-loading="loading"
      element-loading-text="上传中"
      :http-request="handlRequest"
      :before-remove="beforeRemove"
      :on-remove="handleRemove"
      :on-preview="handleItem"
      :multiple="multiple"
      :before-upload="beforeUpload"
      :file-list="fileList"
      :show-file-list="!showLib"
      :disabled="disabled"
    >
      <!-- <slot>
        <el-button :disabled="disabled" type="primary" icon="el-icon-plus" size="mini">点击上传</el-button>
      </slot> -->
      <el-button
        slot="trigger"
        :disabled="disabled"
        type="primary"
        icon="el-icon-plus"
        size="mini"
        >点击上传</el-button
      >
      <slot name="templateBtn"> </slot>
    </el-upload>
  </div>
</template>

<script>
import {
  getFileList,
  getFile,
  removeFile
} from '@/api/system/file/index.js';
import {
  uploadFileNew,
} from './api';
import { getImageUrl, getImagePath } from '@/utils/file';
export default {
  props: {
    value: {
      type: [Array, String],
      default: () => []
    },
    multiple: {
      type: Boolean,
      default: false
    },
    // 所属模块
    module: {
      type: String,
      required: true
    },
    // 文档库
    showLib: {
      type: Boolean,
      default: false
    },
    // 限制数量
    limit: {
      type: Number,
      default: -1
    },
    // 限制大小 mb
    size: {
      type: Number,
      default: 1000
    },
    disabled: {
      default: false,
      type: Boolean
    }
  },
  data() {
    return {
      documentVisible: false,
      selectItem: null,
      loading: false,
      documentForm: {
        name: ''
      },
      columns: [
        {
          label: '序号',
          type: 'index',
          width: 55,
          align: 'center'
        },
        {
          label: '文档名称',
          prop: 'name',
          minWidth: '180',
          showOverflowTooltip: true
        },
        {
          label: '文档类型',
          prop: 'type'
        },
        {
          label: '系统'
        },
        {
          label: '储存路径',
          prop: 'storePath',
          minWidth: '180',
          showOverflowTooltip: true
        },
        {
          label: '模块名',
          prop: 'module'
        },
        {
          label: '上传时间',
          prop: 'createTime'
        }
      ]
    };
  },
  computed: {
    fileList: {
      set(val) {
        // console.log(val);
        this.$emit(
          'input',
          val.map((item) => ({
            ...item,
            url: getImagePath(item.url)
          }))
        );
      },
      get() {
        // console.log(this.value, 2);
        if (!Array.isArray(this.value)) return [];
        const arr =
          (this.value &&
            this.value.map((item) => ({
              ...item,
              url: getImageUrl(item.url)
            }))) ||
          [];
        return arr;
      }
    }
  },

  methods: {
    //点击查看图片
    handleItem(file) {
      getFile({ objectName: file.storePath }, file.name);
    },
    delFileList() {
      this.$emit('input', []);
    },
    handleOpenLib() {
      this.documentVisible = true;
      this.$nextTick(() => {
        this.reload();
      });
    },
    //图文档勾选
    submitDocument() {
      this.$emit('input', [
        { url: this.selectItem.storePath, ...this.selectItem }
      ]);
      this.documentVisible = false;
    },
    datasource({ page, limit }) {
      return getFileList({
        ...this.documentForm,
        pageNum: page,
        size: limit
      });
    },
    reload() {
      this.$refs.table.reload();
    },
    beforeRemove(file) {
      if (file.id) {
        return removeFile({
          fileId: file.id
        }).then(() => {
          return true;
        });
        return true;
      } else {
        return true;
      }
    },
    handleRemove(file, fileList) {
      this.fileList = fileList;
    },
    beforeUpload(file) {
      // if (file.size / 1024 / 1024 > this.size) {
      //   this.$message.error(`大小不能超过 ${this.size}MB`);
      //   return false;
      // }

      if (this.limit > 0 && this.fileList.length === this.limit) {
        this.$message.error(`最多上传 ${this.limit}个文件`);
        return false;
      }
      this.loading = true;
      return uploadFileNew({
        module: this.module,
        multiPartFile: file
      })
        .then((res) => {
          if (res.data) {
            this.$emit('input', [
              ...(this.value || []),
              { ...file, url: res.data.storePath, ...res.data }
            ]);
          }
          this.$emit('fileChange', res.data);
          return res.data;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    handlRequest() {
      return Promise.resolve();
    }
    // onSuccess(response, file, fileList){
    //   alert(1)
    //    this.$emit('fileChange',file)
    // },
  }
};
</script>

<style lang="scss" scoped>
.upload-file {
  display: flex;
  justify-content: flex-start;
  align-items: center;

  .lib {
    margin-left: 12px;
  }

  .imgs-box {
    margin-left: 10px;
    flex: 1;
  }
  .imgs-box .imgs-p {
    height: 30px;
    background: #f0f3f3;
    line-height: 30px;
    min-width: 480px;
    margin-bottom: 5px;
    padding: 0 10px;
    display: flex;
    justify-content: space-between;
  }
}
</style>
